PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/database/drivers/mysql/mysql_utility.php

http://github.com/prashants/webzash
PHP | 245 lines | 110 code | 36 blank | 99 comment | 16 complexity | 280cbeb5acc424988a1520eb7c687ab3 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * MySQL Utility Class
  18. *
  19. * @category Database
  20. * @author ExpressionEngine Dev Team
  21. * @link http://codeigniter.com/user_guide/database/
  22. */
  23. class CI_DB_mysql_utility extends CI_DB_utility {
  24. /**
  25. * List databases
  26. *
  27. * @access private
  28. * @return bool
  29. */
  30. function _list_databases()
  31. {
  32. return "SHOW DATABASES";
  33. }
  34. // --------------------------------------------------------------------
  35. /**
  36. * Optimize table query
  37. *
  38. * Generates a platform-specific query so that a table can be optimized
  39. *
  40. * @access private
  41. * @param string the table name
  42. * @return object
  43. */
  44. function _optimize_table($table)
  45. {
  46. return "OPTIMIZE TABLE ".$this->db->_escape_identifiers($table);
  47. }
  48. // --------------------------------------------------------------------
  49. /**
  50. * Repair table query
  51. *
  52. * Generates a platform-specific query so that a table can be repaired
  53. *
  54. * @access private
  55. * @param string the table name
  56. * @return object
  57. */
  58. function _repair_table($table)
  59. {
  60. return "REPAIR TABLE ".$this->db->_escape_identifiers($table);
  61. }
  62. // --------------------------------------------------------------------
  63. /**
  64. * MySQL Export
  65. *
  66. * @access private
  67. * @param array Preferences
  68. * @return mixed
  69. */
  70. function _backup($params = array())
  71. {
  72. if (count($params) == 0)
  73. {
  74. return FALSE;
  75. }
  76. // Extract the prefs for simplicity
  77. extract($params);
  78. // Build the output
  79. $output = '';
  80. foreach ((array)$tables as $table)
  81. {
  82. // Is the table in the "ignore" list?
  83. if (in_array($table, (array)$ignore, TRUE))
  84. {
  85. continue;
  86. }
  87. // Get the table schema
  88. $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
  89. // No result means the table name was invalid
  90. if ($query === FALSE)
  91. {
  92. continue;
  93. }
  94. // Write out the table schema
  95. $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
  96. if ($add_drop == TRUE)
  97. {
  98. $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
  99. }
  100. $i = 0;
  101. $result = $query->result_array();
  102. foreach ($result[0] as $val)
  103. {
  104. if ($i++ % 2)
  105. {
  106. $output .= $val.';'.$newline.$newline;
  107. }
  108. }
  109. // If inserts are not needed we're done...
  110. if ($add_insert == FALSE)
  111. {
  112. continue;
  113. }
  114. // Grab all the data from the current table
  115. $query = $this->db->query("SELECT * FROM $table");
  116. if ($query->num_rows() == 0)
  117. {
  118. continue;
  119. }
  120. // Fetch the field names and determine if the field is an
  121. // integer type. We use this info to decide whether to
  122. // surround the data with quotes or not
  123. $i = 0;
  124. $field_str = '';
  125. $is_int = array();
  126. while ($field = mysql_fetch_field($query->result_id))
  127. {
  128. // Most versions of MySQL store timestamp as a string
  129. $is_int[$i] = (in_array(
  130. strtolower(mysql_field_type($query->result_id, $i)),
  131. array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
  132. TRUE)
  133. ) ? TRUE : FALSE;
  134. // Create a string of field names
  135. $field_str .= '`'.$field->name.'`, ';
  136. $i++;
  137. }
  138. // Trim off the end comma
  139. $field_str = preg_replace( "/, $/" , "" , $field_str);
  140. // Build the insert string
  141. foreach ($query->result_array() as $row)
  142. {
  143. $val_str = '';
  144. $i = 0;
  145. foreach ($row as $v)
  146. {
  147. // Is the value NULL?
  148. if ($v === NULL)
  149. {
  150. $val_str .= 'NULL';
  151. }
  152. else
  153. {
  154. // Escape the data if it's not an integer
  155. if ($is_int[$i] == FALSE)
  156. {
  157. $val_str .= $this->db->escape($v);
  158. }
  159. else
  160. {
  161. $val_str .= $v;
  162. }
  163. }
  164. // Append a comma
  165. $val_str .= ', ';
  166. $i++;
  167. }
  168. // Remove the comma at the end of the string
  169. $val_str = preg_replace( "/, $/" , "" , $val_str);
  170. // Build the INSERT string
  171. $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
  172. }
  173. $output .= $newline.$newline;
  174. }
  175. return $output;
  176. }
  177. /**
  178. *
  179. * The functions below have been deprecated as of 1.6, and are only here for backwards
  180. * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
  181. * is STRONGLY discouraged in favour if using dbforge.
  182. *
  183. */
  184. /**
  185. * Create database
  186. *
  187. * @access private
  188. * @param string the database name
  189. * @return bool
  190. */
  191. function _create_database($name)
  192. {
  193. return "CREATE DATABASE ".$name;
  194. }
  195. // --------------------------------------------------------------------
  196. /**
  197. * Drop database
  198. *
  199. * @access private
  200. * @param string the database name
  201. * @return bool
  202. */
  203. function _drop_database($name)
  204. {
  205. return "DROP DATABASE ".$name;
  206. }
  207. }
  208. /* End of file mysql_utility.php */
  209. /* Location: ./system/database/drivers/mysql/mysql_utility.php */