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

https://github.com/dchill42/CodeIgniter · PHP · 190 lines · 85 code · 29 blank · 76 comment · 9 complexity · df4771d9730bdfc93ac8d91de619abcc MD5 · raw file

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