diff options
Diffstat (limited to 'system')
-rw-r--r-- | system/database/drivers/mysql/mysql_utility.php | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/system/database/drivers/mysql/mysql_utility.php b/system/database/drivers/mysql/mysql_utility.php index a81936c56..65fb87f92 100644 --- a/system/database/drivers/mysql/mysql_utility.php +++ b/system/database/drivers/mysql/mysql_utility.php @@ -123,7 +123,177 @@ class CI_DB_mysql_utility extends CI_DB_utility { return "REPAIR TABLE ".$this->db->_escape_table($table); } + // -------------------------------------------------------------------- + + /** + * MySQL Export + * + * @access public + * @param object The query result object + * @param array Any preferences + * @return string + */ + function export($params = array()) + { + // Set up our default preferences + $prefs = array( + 'tables' => array(), + 'ignore' => array(), + 'format' => 'gzip', + 'download' => TRUE, + 'filename' => date('Y-m-d-H:i', time()), + 'filepath' => '', + 'add_drop' => TRUE, + 'add_insert' => TRUE, + 'newline' => "\n" + ); + + // Did the user submit any preference overrides? If so set them.... + if (count($params) > 0) + { + foreach ($prefs as $key => $val) + { + if (isset($params[$key])) + { + $prefs[$key] = $params[$key]; + } + } + } + + // Extract the prefs for simplicity + extract($prefs); + + // Are we backing up a complete database or individual tables? + if (count($tables) == 0) + { + $tables = $this->list_tables(); + } + + + + // Start buffering the output + ob_start(); + + // Build the output + foreach ($tables as $table) + { + // Is the table in the "ignore" list? + if (in_array($table, $ignore)) + { + continue; + } + // Get the table schema + $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table); + + // No result means the table name was invalid + if ($query === FALSE) + { + continue; + } + + // Write out the table schema + + echo $newline.$newline.'#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; + + if ($add_drop == TRUE) + { + echo 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline; + } + + $i = 0; + $result = $query->result_array(); + foreach ($result[0] as $val) + { + if ($i++ % 2) + { + echo $val.';'.$newline.$newline; + } + } + + // Build the insert statements + + if ($add_insert == FALSE) + { + continue; + } + + $query = $this->db->query("SELECT * FROM $table"); + + if ($query->num_rows() == 0) + { + continue; + } + + // Grab the field names and determine if the field is an + // integer type. We use this info to decide whether to + // surround the data with quotes or not + + $i = 0; + $fields = ''; + $is_int = array(); + while ($field = mysql_fetch_field($query->result_id)) + { + $is_int[$i] = (in_array( + mysql_field_type($query->result_id, $i), + array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'), + TRUE) + ) ? TRUE : FALSE; + + // Create a string of field names + $fields .= $field->name.', '; + $i++; + } + + $fields = preg_replace( "/, $/" , "" , $fields); + + + // Build the inserts + foreach ($query->result_array() as $row) + { + $values = ''; + + $i = 0; + foreach ($row as $v) + { + $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v); + $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v); + $v = str_replace('\\', '\\\\', $v); + $v = str_replace('\'', '\\\'', $v); + $v = str_replace('\\\n', '\n', $v); + $v = str_replace('\\\r', '\r', $v); + $v = str_replace('\\\t', '\t', $v); + + // Escape the data if it's not an integer type + $values .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v; + $values .= ', '; + + $i++; + } + + $values = preg_replace( "/, $/" , "" , $values); + + if ($download == FALSE) + { + $values = htmlspecialchars($values); + } + + // Build the INSERT string + echo 'INSERT INTO '.$table.' ('.$fields.') VALUES ('.$values.');'.$newline; + + } + + + + $buffer = ob_get_contents(); + @ob_end_clean(); + + echo $buffer; + + } + + + } } |