blob: d7018bf2b50745c555248ed3653f7c3f1dd722d0 [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);
136 return current($query->result_array());
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
admin31075cf2006-09-26 18:50:02 +0000142 * Optimize Database
143 *
144 * @access public
admine5bb9362006-09-27 00:31:22 +0000145 * @return array
admin31075cf2006-09-26 18:50:02 +0000146 */
147 function optimize_database()
148 {
149 $result = array();
admin1b0ab462006-10-10 23:30:21 +0000150 foreach ($this->db->list_tables() as $table_name)
admin31075cf2006-09-26 18:50:02 +0000151 {
152 $sql = $this->_optimize_table($table_name);
153
154 if (is_bool($sql))
155 {
156 return $sql;
157 }
158
159 $query = $this->db->query($sql);
admine5bb9362006-09-27 00:31:22 +0000160
161 // Build the result array...
admin31075cf2006-09-26 18:50:02 +0000162 $res = current($query->result_array());
163 $key = str_replace($this->db->database.'.', '', current($res));
164 $keys = array_keys($res);
165 unset($res[$keys[0]]);
166
167 $result[$key] = $res;
168 }
169
170 return $result;
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
adminab4f61b2006-09-25 22:12:32 +0000176 * Optimize Table
177 *
178 * @access public
179 * @param string the table name
180 * @return bool
181 */
182
183 function repair_table($table_name)
184 {
185 $sql = $this->_repair_table($table_name);
186
187 if (is_bool($sql))
188 {
189 return $sql;
190 }
191
192 $query = $this->db->query($sql);
193 return current($query->result_array());
194 }
admin83b05a82006-09-25 21:06:46 +0000195
196 // --------------------------------------------------------------------
197
198 /**
199 * Drop Table
200 *
201 * @access public
202 * @param string the table name
203 * @return bool
204 */
adminab4f61b2006-09-25 22:12:32 +0000205 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000206 {
adminab4f61b2006-09-25 22:12:32 +0000207 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000208
209 if (is_bool($sql))
210 {
211 return $sql;
212 }
213
214 return $this->db->query($sql);
215 }
216
admin3ed8c512006-09-29 23:26:28 +0000217 // --------------------------------------------------------------------
admin83b05a82006-09-25 21:06:46 +0000218
admin3ed8c512006-09-29 23:26:28 +0000219 /**
admin24dd7e72006-10-01 19:02:29 +0000220 * Generate CSV from a query result object
admin3ed8c512006-09-29 23:26:28 +0000221 *
222 * @access public
223 * @param object The query result object
224 * @param string The delimiter - tab by default
225 * @param string The newline character - \n by default
226 * @return string
227 */
admin24dd7e72006-10-01 19:02:29 +0000228 function csv_from_result($query, $delim = "\t", $newline = "\n")
admin3ed8c512006-09-29 23:26:28 +0000229 {
230 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
231 {
232 show_error('You must submit a valid result object');
233 }
234
235 $out = '';
236
237 // First generate the headings from the table column names
adminb6224a12006-10-23 01:25:46 +0000238 foreach ($query->list_fields() as $name)
admin3ed8c512006-09-29 23:26:28 +0000239 {
240 $out .= $name.$delim;
241 }
242
243 $out = rtrim($out);
244 $out .= $newline;
245
246 // Next blast through the result array and build out the rows
247 foreach ($query->result_array() as $row)
248 {
249 foreach ($row as $item)
250 {
251 $out .= $item.$delim;
252 }
253 $out = rtrim($out);
254 $out .= $newline;
255 }
256
257 return $out;
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Generate XML data from a query result object
264 *
265 * @access public
266 * @param object The query result object
267 * @param array Any preferences
268 * @return string
269 */
270 function xml_from_result($query, $params = array())
271 {
272 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
273 {
274 show_error('You must submit a valid result object');
275 }
276
277 // Set our default values
278 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
279 {
280 if ( ! isset($params[$key]))
281 {
282 $params[$key] = $val;
283 }
284 }
285
286 // Create variables for convenience
287 extract($params);
288
289 // Load the xml helper
admin88a8ad12006-10-07 03:16:32 +0000290 $CI =& get_instance();
291 $CI->load->helper('xml');
admin3ed8c512006-09-29 23:26:28 +0000292
293 // Generate the result
admin69af5bd2006-10-07 03:22:46 +0000294 $xml = "<{$root}>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000295 foreach ($query->result_array() as $row)
296 {
admin69af5bd2006-10-07 03:22:46 +0000297 $xml .= $tab."<{$element}>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000298
299 foreach ($row as $key => $val)
300 {
301 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
302 }
303 $xml .= $tab."</{$element}>".$newline;
304 }
admine334c472006-10-21 19:44:22 +0000305 $xml .= "</$root>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000306
307 return $xml;
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Database Backup
314 *
315 * @access public
316 * @return void
317 */
admin3dd978f2006-09-30 19:24:45 +0000318 function backup($params = array())
admin3ed8c512006-09-29 23:26:28 +0000319 {
admin3dd978f2006-09-30 19:24:45 +0000320 // If the parameters have not been submitted as an
321 // array then we know that it is simply the table
322 // name, which is a valid short cut.
323 if (is_string($params))
324 {
325 $params = array('tables' => $params);
326 }
327
328 // ------------------------------------------------------
329
330 // Set up our default preferences
331 $prefs = array(
332 'tables' => array(),
333 'ignore' => array(),
admin3dd978f2006-09-30 19:24:45 +0000334 'filename' => '',
admin78bc0392006-10-02 02:38:50 +0000335 'format' => 'gzip', // gzip, zip, txt
admin3dd978f2006-09-30 19:24:45 +0000336 'add_drop' => TRUE,
337 'add_insert' => TRUE,
338 'newline' => "\n"
339 );
340
341 // Did the user submit any preferences? If so set them....
342 if (count($params) > 0)
343 {
344 foreach ($prefs as $key => $val)
345 {
346 if (isset($params[$key]))
347 {
348 $prefs[$key] = $params[$key];
349 }
350 }
351 }
352
353 // ------------------------------------------------------
354
355 // Are we backing up a complete database or individual tables?
356 // If no table names were submitted we'll fetch the entire table list
357 if (count($prefs['tables']) == 0)
358 {
admin1b0ab462006-10-10 23:30:21 +0000359 $prefs['tables'] = $this->db->list_tables();
admin3dd978f2006-09-30 19:24:45 +0000360 }
361
362 // ------------------------------------------------------
363
364 // Validate the format
365 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
366 {
367 $prefs['format'] = 'txt';
368 }
369
370 // ------------------------------------------------------
371
372 // Is the encoder supported? If not, we'll either issue an
373 // error or use plain text depending on the debug settings
admine334c472006-10-21 19:44:22 +0000374 if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
375 OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
admin3dd978f2006-09-30 19:24:45 +0000376 {
admine334c472006-10-21 19:44:22 +0000377 if ($this->db->db_debug)
378 {
admin3dd978f2006-09-30 19:24:45 +0000379 return $this->db->display_error('db_unsuported_compression');
admine334c472006-10-21 19:44:22 +0000380 }
admin3dd978f2006-09-30 19:24:45 +0000381
382 $prefs['format'] = 'txt';
383 }
384
385 // ------------------------------------------------------
386
admin78bc0392006-10-02 02:38:50 +0000387 // Set the filename if not provided - Only needed with Zip files
388 if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
admin3dd978f2006-09-30 19:24:45 +0000389 {
390 $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
391 $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
392 }
393
394 // ------------------------------------------------------
admin88a8ad12006-10-07 03:16:32 +0000395
admin78bc0392006-10-02 02:38:50 +0000396 // Was a Gzip file requested?
397 if ($prefs['format'] == 'gzip')
admin3dd978f2006-09-30 19:24:45 +0000398 {
admin78bc0392006-10-02 02:38:50 +0000399 return gzencode($this->_backup($prefs));
admin3dd978f2006-09-30 19:24:45 +0000400 }
401
402 // ------------------------------------------------------
403
admin78bc0392006-10-02 02:38:50 +0000404 // Was a text file requested?
405 if ($prefs['format'] == 'txt')
admin3dd978f2006-09-30 19:24:45 +0000406 {
407 return $this->_backup($prefs);
408 }
409
410 // ------------------------------------------------------
admin3dd978f2006-09-30 19:24:45 +0000411
admin78bc0392006-10-02 02:38:50 +0000412 // Was a Zip file requested?
admin0f8015f2006-10-01 22:08:34 +0000413 if ($prefs['format'] == 'zip')
414 {
admin78bc0392006-10-02 02:38:50 +0000415 // If they included the .zip file extension we'll remove it
416 if (preg_match("|.+?\.zip$|", $prefs['filename']))
417 {
418 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
419 }
420
421 // Tack on the ".sql" file extension if needed
422 if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
423 {
424 $prefs['filename'] .= '.sql';
425 }
426
427 // Load the Zip class and output it
admin88a8ad12006-10-07 03:16:32 +0000428
429 $CI =& get_instance();
430 $CI->load->library('zip');
431 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
432 return $CI->zip->get_zip();
admin0f8015f2006-10-01 22:08:34 +0000433 }
434
admin3ed8c512006-09-29 23:26:28 +0000435 }
admin7b613c72006-09-24 18:05:17 +0000436
437
admin3dd978f2006-09-30 19:24:45 +0000438
439
440
441
admin7b613c72006-09-24 18:05:17 +0000442}
443
444?>