blob: 9533ec607c86f91cf5f70ab1bbfd4dd5b0b9691c [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
admin7b613c72006-09-24 18:05:17 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine79dc712006-09-26 03:52:45 +000015
admin7b613c72006-09-24 18:05:17 +000016// ------------------------------------------------------------------------
17
18/**
19 * Database Utility Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
25class CI_DB_utility {
26
admina5e812c2006-09-25 02:17:30 +000027 var $db;
admine8f6eb62006-10-02 06:46:16 +000028 var $data_cache = array();
admin7b613c72006-09-24 18:05:17 +000029
admin41a16852006-09-26 18:17:19 +000030 /**
31 * Constructor
32 *
33 * Grabs the CI super object instance so we can access it.
34 *
35 */
admina5e812c2006-09-25 02:17:30 +000036 function CI_DB_utility()
37 {
38 // Assign the main database object to $this->db
admin88a8ad12006-10-07 03:16:32 +000039 $CI =& get_instance();
admin1b0ab462006-10-10 23:30:21 +000040 $this->db =& $CI->db;
admin910d8622006-09-26 02:09:05 +000041
42 log_message('debug', "Database Utility Class Initialized");
admina5e812c2006-09-25 02:17:30 +000043 }
44
admin7b613c72006-09-24 18:05:17 +000045 // --------------------------------------------------------------------
46
47 /**
admin6cec6a52006-09-25 06:56:49 +000048 * Create database
49 *
50 * @access public
51 * @param string the database name
52 * @return bool
53 */
adminab4f61b2006-09-25 22:12:32 +000054 function create_database($db_name)
admin6cec6a52006-09-25 06:56:49 +000055 {
adminab4f61b2006-09-25 22:12:32 +000056 $sql = $this->_create_database($db_name);
admin6cec6a52006-09-25 06:56:49 +000057
58 if (is_bool($sql))
59 {
60 return $sql;
61 }
62
63 return $this->db->query($sql);
64 }
65
66 // --------------------------------------------------------------------
67
admin83b05a82006-09-25 21:06:46 +000068 /**
69 * Drop database
70 *
71 * @access public
72 * @param string the database name
73 * @return bool
74 */
adminab4f61b2006-09-25 22:12:32 +000075 function drop_database($db_name)
adminbb1d4392006-09-24 20:14:38 +000076 {
adminab4f61b2006-09-25 22:12:32 +000077 $sql = $this->_drop_database($db_name);
admin83b05a82006-09-25 21:06:46 +000078
79 if (is_bool($sql))
80 {
81 return $sql;
82 }
83
84 return $this->db->query($sql);
adminbb1d4392006-09-24 20:14:38 +000085 }
admin83b05a82006-09-25 21:06:46 +000086
87 // --------------------------------------------------------------------
88
89 /**
adminb2a9cec2006-10-01 03:38:04 +000090 * List databases
91 *
92 * @access public
93 * @return bool
94 */
95 function list_databases()
96 {
97 // Is there a cached result?
admine8f6eb62006-10-02 06:46:16 +000098 if (isset($this->data_cache['db_names']))
adminb2a9cec2006-10-01 03:38:04 +000099 {
admine8f6eb62006-10-02 06:46:16 +0000100 return $this->data_cache['db_names'];
adminb2a9cec2006-10-01 03:38:04 +0000101 }
102
admin1b0ab462006-10-10 23:30:21 +0000103 $query = $this->db->query($this->_list_databases());
adminb2a9cec2006-10-01 03:38:04 +0000104 $dbs = array();
105 if ($query->num_rows() > 0)
106 {
107 foreach ($query->result_array() as $row)
108 {
109 $dbs[] = current($row);
110 }
111 }
112
admin7099a582006-10-10 17:47:59 +0000113 $this->data_cache['db_names'] = $dbs;
114 return $this->data_cache['db_names'];
adminb2a9cec2006-10-01 03:38:04 +0000115 }
116
117 // --------------------------------------------------------------------
118
119 /**
adminab4f61b2006-09-25 22:12:32 +0000120 * Optimize Table
121 *
122 * @access public
123 * @param string the table name
124 * @return bool
125 */
126 function optimize_table($table_name)
127 {
128 $sql = $this->_optimize_table($table_name);
129
130 if (is_bool($sql))
131 {
132 return $sql;
133 }
134
135 $query = $this->db->query($sql);
admin7acd5812006-10-23 21:37:22 +0000136 $res = $query->result_array();
137
138 // Note: Due to a bug in current() that affects some versions
139 // of PHP we can not pass function call directly into it
140 return current($res);
adminab4f61b2006-09-25 22:12:32 +0000141 }
142
143 // --------------------------------------------------------------------
144
145 /**
admin31075cf2006-09-26 18:50:02 +0000146 * Optimize Database
147 *
148 * @access public
admine5bb9362006-09-27 00:31:22 +0000149 * @return array
admin31075cf2006-09-26 18:50:02 +0000150 */
151 function optimize_database()
152 {
153 $result = array();
admin1b0ab462006-10-10 23:30:21 +0000154 foreach ($this->db->list_tables() as $table_name)
admin31075cf2006-09-26 18:50:02 +0000155 {
156 $sql = $this->_optimize_table($table_name);
157
158 if (is_bool($sql))
159 {
160 return $sql;
161 }
162
163 $query = $this->db->query($sql);
admine5bb9362006-09-27 00:31:22 +0000164
165 // Build the result array...
admin7acd5812006-10-23 21:37:22 +0000166 // Note: Due to a bug in current() that affects some versions
167 // of PHP we can not pass function call directly into it
168 $res = $query->result_array();
169 $res = current($res);
admin31075cf2006-09-26 18:50:02 +0000170 $key = str_replace($this->db->database.'.', '', current($res));
171 $keys = array_keys($res);
172 unset($res[$keys[0]]);
173
174 $result[$key] = $res;
175 }
176
177 return $result;
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
adminab4f61b2006-09-25 22:12:32 +0000183 * Optimize Table
184 *
185 * @access public
186 * @param string the table name
187 * @return bool
188 */
189
190 function repair_table($table_name)
191 {
192 $sql = $this->_repair_table($table_name);
193
194 if (is_bool($sql))
195 {
196 return $sql;
197 }
198
199 $query = $this->db->query($sql);
admin7acd5812006-10-23 21:37:22 +0000200
201 // Note: Due to a bug in current() that affects some versions
202 // of PHP we can not pass function call directly into it
203 $res = $query->result_array();
204 return current($res);
adminab4f61b2006-09-25 22:12:32 +0000205 }
admin83b05a82006-09-25 21:06:46 +0000206
207 // --------------------------------------------------------------------
208
209 /**
210 * Drop Table
211 *
212 * @access public
213 * @param string the table name
214 * @return bool
215 */
adminab4f61b2006-09-25 22:12:32 +0000216 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000217 {
adminab4f61b2006-09-25 22:12:32 +0000218 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000219
220 if (is_bool($sql))
221 {
222 return $sql;
223 }
224
225 return $this->db->query($sql);
226 }
227
admin3ed8c512006-09-29 23:26:28 +0000228 // --------------------------------------------------------------------
admin83b05a82006-09-25 21:06:46 +0000229
admin3ed8c512006-09-29 23:26:28 +0000230 /**
admin24dd7e72006-10-01 19:02:29 +0000231 * Generate CSV from a query result object
admin3ed8c512006-09-29 23:26:28 +0000232 *
233 * @access public
234 * @param object The query result object
235 * @param string The delimiter - tab by default
236 * @param string The newline character - \n by default
237 * @return string
238 */
admin24dd7e72006-10-01 19:02:29 +0000239 function csv_from_result($query, $delim = "\t", $newline = "\n")
admin3ed8c512006-09-29 23:26:28 +0000240 {
241 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
242 {
243 show_error('You must submit a valid result object');
244 }
245
246 $out = '';
247
248 // First generate the headings from the table column names
adminb6224a12006-10-23 01:25:46 +0000249 foreach ($query->list_fields() as $name)
admin3ed8c512006-09-29 23:26:28 +0000250 {
251 $out .= $name.$delim;
252 }
253
254 $out = rtrim($out);
255 $out .= $newline;
256
257 // Next blast through the result array and build out the rows
258 foreach ($query->result_array() as $row)
259 {
260 foreach ($row as $item)
261 {
262 $out .= $item.$delim;
263 }
264 $out = rtrim($out);
265 $out .= $newline;
266 }
267
268 return $out;
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Generate XML data from a query result object
275 *
276 * @access public
277 * @param object The query result object
278 * @param array Any preferences
279 * @return string
280 */
281 function xml_from_result($query, $params = array())
282 {
283 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
284 {
285 show_error('You must submit a valid result object');
286 }
287
288 // Set our default values
289 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
290 {
291 if ( ! isset($params[$key]))
292 {
293 $params[$key] = $val;
294 }
295 }
296
297 // Create variables for convenience
298 extract($params);
299
300 // Load the xml helper
admin88a8ad12006-10-07 03:16:32 +0000301 $CI =& get_instance();
302 $CI->load->helper('xml');
admin3ed8c512006-09-29 23:26:28 +0000303
304 // Generate the result
admin69af5bd2006-10-07 03:22:46 +0000305 $xml = "<{$root}>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000306 foreach ($query->result_array() as $row)
307 {
admin69af5bd2006-10-07 03:22:46 +0000308 $xml .= $tab."<{$element}>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000309
310 foreach ($row as $key => $val)
311 {
312 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
313 }
314 $xml .= $tab."</{$element}>".$newline;
315 }
admine334c472006-10-21 19:44:22 +0000316 $xml .= "</$root>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000317
318 return $xml;
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Database Backup
325 *
326 * @access public
327 * @return void
328 */
admin3dd978f2006-09-30 19:24:45 +0000329 function backup($params = array())
admin3ed8c512006-09-29 23:26:28 +0000330 {
admin3dd978f2006-09-30 19:24:45 +0000331 // If the parameters have not been submitted as an
332 // array then we know that it is simply the table
333 // name, which is a valid short cut.
334 if (is_string($params))
335 {
336 $params = array('tables' => $params);
337 }
338
339 // ------------------------------------------------------
340
341 // Set up our default preferences
342 $prefs = array(
343 'tables' => array(),
344 'ignore' => array(),
admin3dd978f2006-09-30 19:24:45 +0000345 'filename' => '',
admin78bc0392006-10-02 02:38:50 +0000346 'format' => 'gzip', // gzip, zip, txt
admin3dd978f2006-09-30 19:24:45 +0000347 'add_drop' => TRUE,
348 'add_insert' => TRUE,
349 'newline' => "\n"
350 );
351
352 // Did the user submit any preferences? If so set them....
353 if (count($params) > 0)
354 {
355 foreach ($prefs as $key => $val)
356 {
357 if (isset($params[$key]))
358 {
359 $prefs[$key] = $params[$key];
360 }
361 }
362 }
363
364 // ------------------------------------------------------
365
366 // Are we backing up a complete database or individual tables?
367 // If no table names were submitted we'll fetch the entire table list
368 if (count($prefs['tables']) == 0)
369 {
admin1b0ab462006-10-10 23:30:21 +0000370 $prefs['tables'] = $this->db->list_tables();
admin3dd978f2006-09-30 19:24:45 +0000371 }
372
373 // ------------------------------------------------------
374
375 // Validate the format
376 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
377 {
378 $prefs['format'] = 'txt';
379 }
380
381 // ------------------------------------------------------
382
383 // Is the encoder supported? If not, we'll either issue an
384 // error or use plain text depending on the debug settings
admine334c472006-10-21 19:44:22 +0000385 if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
386 OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
admin3dd978f2006-09-30 19:24:45 +0000387 {
admine334c472006-10-21 19:44:22 +0000388 if ($this->db->db_debug)
389 {
admin3dd978f2006-09-30 19:24:45 +0000390 return $this->db->display_error('db_unsuported_compression');
admine334c472006-10-21 19:44:22 +0000391 }
admin3dd978f2006-09-30 19:24:45 +0000392
393 $prefs['format'] = 'txt';
394 }
395
396 // ------------------------------------------------------
397
admin78bc0392006-10-02 02:38:50 +0000398 // Set the filename if not provided - Only needed with Zip files
399 if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
admin3dd978f2006-09-30 19:24:45 +0000400 {
401 $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
402 $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
403 }
404
405 // ------------------------------------------------------
admin88a8ad12006-10-07 03:16:32 +0000406
admin78bc0392006-10-02 02:38:50 +0000407 // Was a Gzip file requested?
408 if ($prefs['format'] == 'gzip')
admin3dd978f2006-09-30 19:24:45 +0000409 {
admin78bc0392006-10-02 02:38:50 +0000410 return gzencode($this->_backup($prefs));
admin3dd978f2006-09-30 19:24:45 +0000411 }
412
413 // ------------------------------------------------------
414
admin78bc0392006-10-02 02:38:50 +0000415 // Was a text file requested?
416 if ($prefs['format'] == 'txt')
admin3dd978f2006-09-30 19:24:45 +0000417 {
418 return $this->_backup($prefs);
419 }
420
421 // ------------------------------------------------------
admin3dd978f2006-09-30 19:24:45 +0000422
admin78bc0392006-10-02 02:38:50 +0000423 // Was a Zip file requested?
admin0f8015f2006-10-01 22:08:34 +0000424 if ($prefs['format'] == 'zip')
425 {
admin78bc0392006-10-02 02:38:50 +0000426 // If they included the .zip file extension we'll remove it
427 if (preg_match("|.+?\.zip$|", $prefs['filename']))
428 {
429 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
430 }
431
432 // Tack on the ".sql" file extension if needed
433 if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
434 {
435 $prefs['filename'] .= '.sql';
436 }
437
438 // Load the Zip class and output it
admin88a8ad12006-10-07 03:16:32 +0000439
440 $CI =& get_instance();
441 $CI->load->library('zip');
442 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
443 return $CI->zip->get_zip();
admin0f8015f2006-10-01 22:08:34 +0000444 }
445
admin3ed8c512006-09-29 23:26:28 +0000446 }
admin7b613c72006-09-24 18:05:17 +0000447
448
admin3dd978f2006-09-30 19:24:45 +0000449
450
451
452
admin7b613c72006-09-24 18:05:17 +0000453}
454
455?>