blob: f43299382e8f340615019c8cd1fc7682baa454f5 [file] [log] [blame]
admin7b613c72006-09-24 18:05:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine5bb9362006-09-27 00:31:22 +000015
admin7b613c72006-09-24 18:05:17 +000016// ------------------------------------------------------------------------
17
18/**
19 * MySQL Utility Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
admina5e812c2006-09-25 02:17:30 +000025class CI_DB_mysql_utility extends CI_DB_utility {
admin7b613c72006-09-24 18:05:17 +000026
admin6ca6f942006-09-25 02:51:08 +000027 /**
28 * Create database
29 *
admin6cec6a52006-09-25 06:56:49 +000030 * @access private
admin6ca6f942006-09-25 02:51:08 +000031 * @param string the database name
32 * @return bool
33 */
admin6cec6a52006-09-25 06:56:49 +000034 function _create_database($name)
admin6ca6f942006-09-25 02:51:08 +000035 {
admin6cec6a52006-09-25 06:56:49 +000036 return "CREATE DATABASE ".$name;
admin6ca6f942006-09-25 02:51:08 +000037 }
38
39 // --------------------------------------------------------------------
40
41 /**
42 * Drop database
43 *
admin83b05a82006-09-25 21:06:46 +000044 * @access private
admin6ca6f942006-09-25 02:51:08 +000045 * @param string the database name
46 * @return bool
47 */
admin83b05a82006-09-25 21:06:46 +000048 function _drop_database($name)
admin6ca6f942006-09-25 02:51:08 +000049 {
admin83b05a82006-09-25 21:06:46 +000050 return "DROP DATABASE ".$name;
admin6ca6f942006-09-25 02:51:08 +000051 }
52
53 // --------------------------------------------------------------------
admina5e812c2006-09-25 02:17:30 +000054
admin7b613c72006-09-24 18:05:17 +000055 /**
admin72496372006-09-25 03:44:04 +000056 * List databases
57 *
admin83b05a82006-09-25 21:06:46 +000058 * @access private
admin72496372006-09-25 03:44:04 +000059 * @return bool
60 */
admin83b05a82006-09-25 21:06:46 +000061 function _list_databases()
admin72496372006-09-25 03:44:04 +000062 {
admin83b05a82006-09-25 21:06:46 +000063 return "SHOW DATABASES";
admin72496372006-09-25 03:44:04 +000064 }
65
66 // --------------------------------------------------------------------
67
68 /**
admin9cd4e8e2006-09-25 23:26:25 +000069 * Show table query
70 *
71 * Generates a platform-specific query string so that the table names can be fetched
72 *
73 * @access private
74 * @return string
75 */
76 function _list_tables()
77 {
78 return "SHOW TABLES FROM `".$this->db->database."`";
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
admin4ceac2d2006-09-25 06:40:16 +000084 * Drop Table
85 *
admin83b05a82006-09-25 21:06:46 +000086 * @access private
admin4ceac2d2006-09-25 06:40:16 +000087 * @return bool
88 */
admin83b05a82006-09-25 21:06:46 +000089 function _drop_table($table)
admin4ceac2d2006-09-25 06:40:16 +000090 {
admin83b05a82006-09-25 21:06:46 +000091 return "DROP TABLE IF EXISTS ".$this->db->_escape_table($name);
admin4ceac2d2006-09-25 06:40:16 +000092 }
93
94 // --------------------------------------------------------------------
95
96 /**
adminab4f61b2006-09-25 22:12:32 +000097 * Optimize table query
98 *
99 * Generates a platform-specific query so that a table can be optimized
100 *
101 * @access private
102 * @param string the table name
103 * @return object
104 */
105 function _optimize_table($table)
106 {
107 return "OPTIMIZE TABLE ".$this->db->_escape_table($table);
108 }
109
110 // --------------------------------------------------------------------
111
112 /**
113 * Repair table query
114 *
115 * Generates a platform-specific query so that a table can be repaired
116 *
117 * @access private
118 * @param string the table name
119 * @return object
120 */
121 function _repair_table($table)
122 {
123 return "REPAIR TABLE ".$this->db->_escape_table($table);
124 }
125
admin051402b2006-09-29 23:11:40 +0000126 // --------------------------------------------------------------------
adminab4f61b2006-09-25 22:12:32 +0000127
admin051402b2006-09-29 23:11:40 +0000128 /**
129 * MySQL Export
130 *
admin3ed8c512006-09-29 23:26:28 +0000131 * @access private
admin051402b2006-09-29 23:11:40 +0000132 * @param array Any preferences
admin3ed8c512006-09-29 23:26:28 +0000133 * @return mixed
admin051402b2006-09-29 23:11:40 +0000134 */
admin3ed8c512006-09-29 23:26:28 +0000135 function _export($params = array())
admin051402b2006-09-29 23:11:40 +0000136 {
137 // Set up our default preferences
138 $prefs = array(
139 'tables' => array(),
140 'ignore' => array(),
141 'format' => 'gzip',
admin3ed8c512006-09-29 23:26:28 +0000142 'action' => 'download', // download, archive, echo, return
admin051402b2006-09-29 23:11:40 +0000143 'filename' => date('Y-m-d-H:i', time()),
144 'filepath' => '',
145 'add_drop' => TRUE,
146 'add_insert' => TRUE,
147 'newline' => "\n"
148 );
149
150 // Did the user submit any preference overrides? If so set them....
151 if (count($params) > 0)
152 {
153 foreach ($prefs as $key => $val)
154 {
155 if (isset($params[$key]))
156 {
157 $prefs[$key] = $params[$key];
158 }
159 }
160 }
161
162 // Extract the prefs for simplicity
163 extract($prefs);
164
165 // Are we backing up a complete database or individual tables?
166 if (count($tables) == 0)
167 {
168 $tables = $this->list_tables();
169 }
170
admin051402b2006-09-29 23:11:40 +0000171 // Start buffering the output
172 ob_start();
173
174 // Build the output
175 foreach ($tables as $table)
176 {
177 // Is the table in the "ignore" list?
178 if (in_array($table, $ignore))
179 {
180 continue;
181 }
182
183 // Get the table schema
184 $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
185
186 // No result means the table name was invalid
187 if ($query === FALSE)
188 {
189 continue;
190 }
191
192 // Write out the table schema
admin051402b2006-09-29 23:11:40 +0000193 echo $newline.$newline.'#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
194
195 if ($add_drop == TRUE)
196 {
197 echo 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
198 }
199
200 $i = 0;
201 $result = $query->result_array();
202 foreach ($result[0] as $val)
203 {
204 if ($i++ % 2)
205 {
206 echo $val.';'.$newline.$newline;
207 }
208 }
209
admin3ed8c512006-09-29 23:26:28 +0000210 // If inserts are not needed we're done...
admin051402b2006-09-29 23:11:40 +0000211 if ($add_insert == FALSE)
212 {
213 continue;
214 }
admin3ed8c512006-09-29 23:26:28 +0000215
216 // Grab all the data from the current table
admin051402b2006-09-29 23:11:40 +0000217 $query = $this->db->query("SELECT * FROM $table");
218
219 if ($query->num_rows() == 0)
220 {
221 continue;
222 }
223
admin3ed8c512006-09-29 23:26:28 +0000224 // Fetch the field names and determine if the field is an
admin051402b2006-09-29 23:11:40 +0000225 // integer type. We use this info to decide whether to
226 // surround the data with quotes or not
227
228 $i = 0;
admin3ed8c512006-09-29 23:26:28 +0000229 $field_str = '';
admin051402b2006-09-29 23:11:40 +0000230 $is_int = array();
231 while ($field = mysql_fetch_field($query->result_id))
232 {
233 $is_int[$i] = (in_array(
admin3ed8c512006-09-29 23:26:28 +0000234 strtolower(mysql_field_type($query->result_id, $i)),
admin051402b2006-09-29 23:11:40 +0000235 array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'),
236 TRUE)
237 ) ? TRUE : FALSE;
238
239 // Create a string of field names
admin3ed8c512006-09-29 23:26:28 +0000240 $field_str .= $field->name.', ';
admin051402b2006-09-29 23:11:40 +0000241 $i++;
242 }
admin3ed8c512006-09-29 23:26:28 +0000243
244 // Trim off the end comma
245 $field_str = preg_replace( "/, $/" , "" , $field_str);
admin051402b2006-09-29 23:11:40 +0000246
247
admin3ed8c512006-09-29 23:26:28 +0000248 // Build the insert string
admin051402b2006-09-29 23:11:40 +0000249 foreach ($query->result_array() as $row)
250 {
admin3ed8c512006-09-29 23:26:28 +0000251 $val_str = '';
admin051402b2006-09-29 23:11:40 +0000252
253 $i = 0;
254 foreach ($row as $v)
255 {
admin3ed8c512006-09-29 23:26:28 +0000256 // Do a little formatting...
admin051402b2006-09-29 23:11:40 +0000257 $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v);
258 $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
259 $v = str_replace('\\', '\\\\', $v);
260 $v = str_replace('\'', '\\\'', $v);
261 $v = str_replace('\\\n', '\n', $v);
262 $v = str_replace('\\\r', '\r', $v);
263 $v = str_replace('\\\t', '\t', $v);
264
265 // Escape the data if it's not an integer type
admin3ed8c512006-09-29 23:26:28 +0000266 $val_str .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v;
267 $val_str .= ', ';
admin051402b2006-09-29 23:11:40 +0000268
269 $i++;
270 }
271
admin3ed8c512006-09-29 23:26:28 +0000272 $val_str = preg_replace( "/, $/" , "" , $val_str);
admin051402b2006-09-29 23:11:40 +0000273
admin3ed8c512006-09-29 23:26:28 +0000274 if ($action == 'echo')
admin051402b2006-09-29 23:11:40 +0000275 {
admin3ed8c512006-09-29 23:26:28 +0000276 $val_str = htmlspecialchars($val_str);
admin051402b2006-09-29 23:11:40 +0000277 }
278
279 // Build the INSERT string
admin3ed8c512006-09-29 23:26:28 +0000280 echo 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
admin051402b2006-09-29 23:11:40 +0000281
282 }
283
284
285
286 $buffer = ob_get_contents();
287 @ob_end_clean();
288
289 echo $buffer;
290
291 }
292
293
294 }
admina5e812c2006-09-25 02:17:30 +0000295
admin6ca6f942006-09-25 02:51:08 +0000296
admin7b613c72006-09-24 18:05:17 +0000297}
298
299?>