blob: c2c5f1fe089dbcdebe33b8afdd2ff61b19fc2598 [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 */
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();
40 $this->CI =& $obj->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
103 $query = $this->db->query($this->_list_database());
104 $dbs = array();
105 if ($query->num_rows() > 0)
106 {
107 foreach ($query->result_array() as $row)
108 {
109 $dbs[] = current($row);
110 }
111 }
112
admine8f6eb62006-10-02 06:46:16 +0000113 return $this->data_cache['db_names'] =& $dbs;
adminb2a9cec2006-10-01 03:38:04 +0000114 }
115
116 // --------------------------------------------------------------------
117
118 /**
adminab4f61b2006-09-25 22:12:32 +0000119 * Optimize Table
120 *
121 * @access public
122 * @param string the table name
123 * @return bool
124 */
125 function optimize_table($table_name)
126 {
127 $sql = $this->_optimize_table($table_name);
128
129 if (is_bool($sql))
130 {
131 return $sql;
132 }
133
134 $query = $this->db->query($sql);
135 return current($query->result_array());
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
admin31075cf2006-09-26 18:50:02 +0000141 * Optimize Database
142 *
143 * @access public
admine5bb9362006-09-27 00:31:22 +0000144 * @return array
admin31075cf2006-09-26 18:50:02 +0000145 */
146 function optimize_database()
147 {
148 $result = array();
149 foreach ($this->list_tables() as $table_name)
150 {
151 $sql = $this->_optimize_table($table_name);
152
153 if (is_bool($sql))
154 {
155 return $sql;
156 }
157
158 $query = $this->db->query($sql);
admine5bb9362006-09-27 00:31:22 +0000159
160 // Build the result array...
admin31075cf2006-09-26 18:50:02 +0000161 $res = current($query->result_array());
162 $key = str_replace($this->db->database.'.', '', current($res));
163 $keys = array_keys($res);
164 unset($res[$keys[0]]);
165
166 $result[$key] = $res;
167 }
168
169 return $result;
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
adminab4f61b2006-09-25 22:12:32 +0000175 * Optimize Table
176 *
177 * @access public
178 * @param string the table name
179 * @return bool
180 */
181
182 function repair_table($table_name)
183 {
184 $sql = $this->_repair_table($table_name);
185
186 if (is_bool($sql))
187 {
188 return $sql;
189 }
190
191 $query = $this->db->query($sql);
192 return current($query->result_array());
193 }
admin83b05a82006-09-25 21:06:46 +0000194
195 // --------------------------------------------------------------------
196
197 /**
198 * Drop Table
199 *
200 * @access public
201 * @param string the table name
202 * @return bool
203 */
adminab4f61b2006-09-25 22:12:32 +0000204 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000205 {
adminab4f61b2006-09-25 22:12:32 +0000206 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000207
208 if (is_bool($sql))
209 {
210 return $sql;
211 }
212
213 return $this->db->query($sql);
214 }
215
admin3ed8c512006-09-29 23:26:28 +0000216 // --------------------------------------------------------------------
admin83b05a82006-09-25 21:06:46 +0000217
admin3ed8c512006-09-29 23:26:28 +0000218 /**
admin24dd7e72006-10-01 19:02:29 +0000219 * Generate CSV from a query result object
admin3ed8c512006-09-29 23:26:28 +0000220 *
221 * @access public
222 * @param object The query result object
223 * @param string The delimiter - tab by default
224 * @param string The newline character - \n by default
225 * @return string
226 */
admin24dd7e72006-10-01 19:02:29 +0000227 function csv_from_result($query, $delim = "\t", $newline = "\n")
admin3ed8c512006-09-29 23:26:28 +0000228 {
229 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
230 {
231 show_error('You must submit a valid result object');
232 }
233
234 $out = '';
235
236 // First generate the headings from the table column names
237 foreach ($query->field_names() as $name)
238 {
239 $out .= $name.$delim;
240 }
241
242 $out = rtrim($out);
243 $out .= $newline;
244
245 // Next blast through the result array and build out the rows
246 foreach ($query->result_array() as $row)
247 {
248 foreach ($row as $item)
249 {
250 $out .= $item.$delim;
251 }
252 $out = rtrim($out);
253 $out .= $newline;
254 }
255
256 return $out;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Generate XML data from a query result object
263 *
264 * @access public
265 * @param object The query result object
266 * @param array Any preferences
267 * @return string
268 */
269 function xml_from_result($query, $params = array())
270 {
271 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
272 {
273 show_error('You must submit a valid result object');
274 }
275
276 // Set our default values
277 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
278 {
279 if ( ! isset($params[$key]))
280 {
281 $params[$key] = $val;
282 }
283 }
284
285 // Create variables for convenience
286 extract($params);
287
288 // Load the xml helper
admin88a8ad12006-10-07 03:16:32 +0000289 $CI =& get_instance();
290 $CI->load->helper('xml');
admin3ed8c512006-09-29 23:26:28 +0000291
292 // Generate the result
admin69af5bd2006-10-07 03:22:46 +0000293 $xml = "<{$root}>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000294 foreach ($query->result_array() as $row)
295 {
admin69af5bd2006-10-07 03:22:46 +0000296 $xml .= $tab."<{$element}>".$newline;
admin3ed8c512006-09-29 23:26:28 +0000297
298 foreach ($row as $key => $val)
299 {
300 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
301 }
302 $xml .= $tab."</{$element}>".$newline;
303 }
304 $xml .= "</$root>".$newline;
305
306 return $xml;
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Database Backup
313 *
314 * @access public
315 * @return void
316 */
admin3dd978f2006-09-30 19:24:45 +0000317 function backup($params = array())
admin3ed8c512006-09-29 23:26:28 +0000318 {
admin3dd978f2006-09-30 19:24:45 +0000319 // If the parameters have not been submitted as an
320 // array then we know that it is simply the table
321 // name, which is a valid short cut.
322 if (is_string($params))
323 {
324 $params = array('tables' => $params);
325 }
326
327 // ------------------------------------------------------
328
329 // Set up our default preferences
330 $prefs = array(
331 'tables' => array(),
332 'ignore' => array(),
admin3dd978f2006-09-30 19:24:45 +0000333 'filename' => '',
admin78bc0392006-10-02 02:38:50 +0000334 'format' => 'gzip', // gzip, zip, txt
admin3dd978f2006-09-30 19:24:45 +0000335 'add_drop' => TRUE,
336 'add_insert' => TRUE,
337 'newline' => "\n"
338 );
339
340 // Did the user submit any preferences? If so set them....
341 if (count($params) > 0)
342 {
343 foreach ($prefs as $key => $val)
344 {
345 if (isset($params[$key]))
346 {
347 $prefs[$key] = $params[$key];
348 }
349 }
350 }
351
352 // ------------------------------------------------------
353
354 // Are we backing up a complete database or individual tables?
355 // If no table names were submitted we'll fetch the entire table list
356 if (count($prefs['tables']) == 0)
357 {
358 $prefs['tables'] = $this->list_tables();
359 }
360
361 // ------------------------------------------------------
362
363 // Validate the format
364 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
365 {
366 $prefs['format'] = 'txt';
367 }
368
369 // ------------------------------------------------------
370
371 // Is the encoder supported? If not, we'll either issue an
372 // error or use plain text depending on the debug settings
373 if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
374 OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
375 {
376 if ($this->db->db_debug)
377 {
378 return $this->db->display_error('db_unsuported_compression');
379 }
380
381 $prefs['format'] = 'txt';
382 }
383
384 // ------------------------------------------------------
385
admin78bc0392006-10-02 02:38:50 +0000386 // Set the filename if not provided - Only needed with Zip files
387 if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
admin3dd978f2006-09-30 19:24:45 +0000388 {
389 $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
390 $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
391 }
392
393 // ------------------------------------------------------
admin88a8ad12006-10-07 03:16:32 +0000394
admin78bc0392006-10-02 02:38:50 +0000395 // Was a Gzip file requested?
396 if ($prefs['format'] == 'gzip')
admin3dd978f2006-09-30 19:24:45 +0000397 {
admin78bc0392006-10-02 02:38:50 +0000398 return gzencode($this->_backup($prefs));
admin3dd978f2006-09-30 19:24:45 +0000399 }
400
401 // ------------------------------------------------------
402
admin78bc0392006-10-02 02:38:50 +0000403 // Was a text file requested?
404 if ($prefs['format'] == 'txt')
admin3dd978f2006-09-30 19:24:45 +0000405 {
406 return $this->_backup($prefs);
407 }
408
409 // ------------------------------------------------------
admin3dd978f2006-09-30 19:24:45 +0000410
admin78bc0392006-10-02 02:38:50 +0000411 // Was a Zip file requested?
admin0f8015f2006-10-01 22:08:34 +0000412 if ($prefs['format'] == 'zip')
413 {
admin78bc0392006-10-02 02:38:50 +0000414 // If they included the .zip file extension we'll remove it
415 if (preg_match("|.+?\.zip$|", $prefs['filename']))
416 {
417 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
418 }
419
420 // Tack on the ".sql" file extension if needed
421 if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
422 {
423 $prefs['filename'] .= '.sql';
424 }
425
426 // Load the Zip class and output it
admin88a8ad12006-10-07 03:16:32 +0000427
428 $CI =& get_instance();
429 $CI->load->library('zip');
430 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
431 return $CI->zip->get_zip();
admin0f8015f2006-10-01 22:08:34 +0000432 }
433
admin3ed8c512006-09-29 23:26:28 +0000434 }
admin7b613c72006-09-24 18:05:17 +0000435
436
admin3dd978f2006-09-30 19:24:45 +0000437
438
439
440
admin7b613c72006-09-24 18:05:17 +0000441}
442
443?>