blob: 9533ec607c86f91cf5f70ab1bbfd4dd5b0b9691c [file] [log] [blame]
Derek Allard476b8712007-07-12 18:22:05 +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 */
15
16// ------------------------------------------------------------------------
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
27 var $db;
28 var $data_cache = array();
29
30 /**
31 * Constructor
32 *
33 * Grabs the CI super object instance so we can access it.
34 *
35 */
36 function CI_DB_utility()
37 {
38 // Assign the main database object to $this->db
39 $CI =& get_instance();
40 $this->db =& $CI->db;
41
42 log_message('debug', "Database Utility Class Initialized");
43 }
44
45 // --------------------------------------------------------------------
46
47 /**
48 * Create database
49 *
50 * @access public
51 * @param string the database name
52 * @return bool
53 */
54 function create_database($db_name)
55 {
56 $sql = $this->_create_database($db_name);
57
58 if (is_bool($sql))
59 {
60 return $sql;
61 }
62
63 return $this->db->query($sql);
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
69 * Drop database
70 *
71 * @access public
72 * @param string the database name
73 * @return bool
74 */
75 function drop_database($db_name)
76 {
77 $sql = $this->_drop_database($db_name);
78
79 if (is_bool($sql))
80 {
81 return $sql;
82 }
83
84 return $this->db->query($sql);
85 }
86
87 // --------------------------------------------------------------------
88
89 /**
90 * List databases
91 *
92 * @access public
93 * @return bool
94 */
95 function list_databases()
96 {
97 // Is there a cached result?
98 if (isset($this->data_cache['db_names']))
99 {
100 return $this->data_cache['db_names'];
101 }
102
103 $query = $this->db->query($this->_list_databases());
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
113 $this->data_cache['db_names'] = $dbs;
114 return $this->data_cache['db_names'];
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * 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 $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);
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Optimize Database
147 *
148 * @access public
149 * @return array
150 */
151 function optimize_database()
152 {
153 $result = array();
154 foreach ($this->db->list_tables() as $table_name)
155 {
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);
164
165 // Build the result array...
166 // 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);
170 $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 /**
183 * 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);
200
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);
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Drop Table
211 *
212 * @access public
213 * @param string the table name
214 * @return bool
215 */
216 function drop_table($table_name)
217 {
218 $sql = $this->_drop_table($table_name);
219
220 if (is_bool($sql))
221 {
222 return $sql;
223 }
224
225 return $this->db->query($sql);
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Generate CSV from a query result object
232 *
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 */
239 function csv_from_result($query, $delim = "\t", $newline = "\n")
240 {
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
249 foreach ($query->list_fields() as $name)
250 {
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
301 $CI =& get_instance();
302 $CI->load->helper('xml');
303
304 // Generate the result
305 $xml = "<{$root}>".$newline;
306 foreach ($query->result_array() as $row)
307 {
308 $xml .= $tab."<{$element}>".$newline;
309
310 foreach ($row as $key => $val)
311 {
312 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
313 }
314 $xml .= $tab."</{$element}>".$newline;
315 }
316 $xml .= "</$root>".$newline;
317
318 return $xml;
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Database Backup
325 *
326 * @access public
327 * @return void
328 */
329 function backup($params = array())
330 {
331 // 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(),
345 'filename' => '',
346 'format' => 'gzip', // gzip, zip, txt
347 '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 {
370 $prefs['tables'] = $this->db->list_tables();
371 }
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
385 if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
386 OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
387 {
388 if ($this->db->db_debug)
389 {
390 return $this->db->display_error('db_unsuported_compression');
391 }
392
393 $prefs['format'] = 'txt';
394 }
395
396 // ------------------------------------------------------
397
398 // Set the filename if not provided - Only needed with Zip files
399 if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
400 {
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 // ------------------------------------------------------
406
407 // Was a Gzip file requested?
408 if ($prefs['format'] == 'gzip')
409 {
410 return gzencode($this->_backup($prefs));
411 }
412
413 // ------------------------------------------------------
414
415 // Was a text file requested?
416 if ($prefs['format'] == 'txt')
417 {
418 return $this->_backup($prefs);
419 }
420
421 // ------------------------------------------------------
422
423 // Was a Zip file requested?
424 if ($prefs['format'] == 'zip')
425 {
426 // 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
439
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();
444 }
445
446 }
447
448
449
450
451
452
453}
454
admin7b613c72006-09-24 18:05:17 +0000455?>