mysql_utility.php 4.6 KB

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