1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
16
// ------------------------------------------------------------------------
19
* Database Utility Class
22
* @author ExpressionEngine Dev Team
23
* @link http://codeigniter.com/user_guide/database/
25
class CI_DB_utility extends CI_DB_forge {
28
var $data_cache = array();
33
* Grabs the CI super object instance so we can access it.
36
function __construct()
38
// Assign the main database object to $this->db
39
$CI =& get_instance();
42
log_message('debug', "Database Utility Class Initialized");
45
// --------------------------------------------------------------------
53
function list_databases()
55
// Is there a cached result?
56
if (isset($this->data_cache['db_names']))
58
return $this->data_cache['db_names'];
61
$query = $this->db->query($this->_list_databases());
63
if ($query->num_rows() > 0)
65
foreach ($query->result_array() as $row)
67
$dbs[] = current($row);
71
$this->data_cache['db_names'] = $dbs;
72
return $this->data_cache['db_names'];
75
// --------------------------------------------------------------------
78
* Determine if a particular database exists
84
function database_exists($database_name)
86
// Some databases won't have access to the list_databases() function, so
87
// this is intended to allow them to override with their own functions as
88
// defined in $driver_utility.php
89
if (method_exists($this, '_database_exists'))
91
return $this->_database_exists($database_name);
95
return ( ! in_array($database_name, $this->list_databases())) ? FALSE : TRUE;
100
// --------------------------------------------------------------------
106
* @param string the table name
109
function optimize_table($table_name)
111
$sql = $this->_optimize_table($table_name);
115
show_error('db_must_use_set');
118
$query = $this->db->query($sql);
119
$res = $query->result_array();
121
// Note: Due to a bug in current() that affects some versions
122
// of PHP we can not pass function call directly into it
123
return current($res);
126
// --------------------------------------------------------------------
134
function optimize_database()
137
foreach ($this->db->list_tables() as $table_name)
139
$sql = $this->_optimize_table($table_name);
146
$query = $this->db->query($sql);
148
// Build the result array...
149
// Note: Due to a bug in current() that affects some versions
150
// of PHP we can not pass function call directly into it
151
$res = $query->result_array();
152
$res = current($res);
153
$key = str_replace($this->db->database.'.', '', current($res));
154
$keys = array_keys($res);
155
unset($res[$keys[0]]);
157
$result[$key] = $res;
163
// --------------------------------------------------------------------
169
* @param string the table name
172
function repair_table($table_name)
174
$sql = $this->_repair_table($table_name);
181
$query = $this->db->query($sql);
183
// Note: Due to a bug in current() that affects some versions
184
// of PHP we can not pass function call directly into it
185
$res = $query->result_array();
186
return current($res);
189
// --------------------------------------------------------------------
192
* Generate CSV from a query result object
195
* @param object The query result object
196
* @param string The delimiter - comma by default
197
* @param string The newline character - \n by default
198
* @param string The enclosure - double quote by default
201
function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
203
if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
205
show_error('You must submit a valid result object');
210
// First generate the headings from the table column names
211
foreach ($query->list_fields() as $name)
213
$out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
219
// Next blast through the result array and build out the rows
220
foreach ($query->result_array() as $row)
222
foreach ($row as $item)
224
$out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
233
// --------------------------------------------------------------------
236
* Generate XML data from a query result object
239
* @param object The query result object
240
* @param array Any preferences
243
function xml_from_result($query, $params = array())
245
if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
247
show_error('You must submit a valid result object');
250
// Set our default values
251
foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
253
if ( ! isset($params[$key]))
255
$params[$key] = $val;
259
// Create variables for convenience
262
// Load the xml helper
263
$CI =& get_instance();
264
$CI->load->helper('xml');
266
// Generate the result
267
$xml = "<{$root}>".$newline;
268
foreach ($query->result_array() as $row)
270
$xml .= $tab."<{$element}>".$newline;
272
foreach ($row as $key => $val)
274
$xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
276
$xml .= $tab."</{$element}>".$newline;
278
$xml .= "</$root>".$newline;
283
// --------------------------------------------------------------------
291
function backup($params = array())
293
// If the parameters have not been submitted as an
294
// array then we know that it is simply the table
295
// name, which is a valid short cut.
296
if (is_string($params))
298
$params = array('tables' => $params);
301
// ------------------------------------------------------
303
// Set up our default preferences
308
'format' => 'gzip', // gzip, zip, txt
310
'add_insert' => TRUE,
314
// Did the user submit any preferences? If so set them....
315
if (count($params) > 0)
317
foreach ($prefs as $key => $val)
319
if (isset($params[$key]))
321
$prefs[$key] = $params[$key];
326
// ------------------------------------------------------
328
// Are we backing up a complete database or individual tables?
329
// If no table names were submitted we'll fetch the entire table list
330
if (count($prefs['tables']) == 0)
332
$prefs['tables'] = $this->db->list_tables();
335
// ------------------------------------------------------
337
// Validate the format
338
if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
340
$prefs['format'] = 'txt';
343
// ------------------------------------------------------
345
// Is the encoder supported? If not, we'll either issue an
346
// error or use plain text depending on the debug settings
347
if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
348
OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
350
if ($this->db->db_debug)
352
return $this->db->display_error('db_unsuported_compression');
355
$prefs['format'] = 'txt';
358
// ------------------------------------------------------
360
// Set the filename if not provided - Only needed with Zip files
361
if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
363
$prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
364
$prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
367
// ------------------------------------------------------
369
// Was a Gzip file requested?
370
if ($prefs['format'] == 'gzip')
372
return gzencode($this->_backup($prefs));
375
// ------------------------------------------------------
377
// Was a text file requested?
378
if ($prefs['format'] == 'txt')
380
return $this->_backup($prefs);
383
// ------------------------------------------------------
385
// Was a Zip file requested?
386
if ($prefs['format'] == 'zip')
388
// If they included the .zip file extension we'll remove it
389
if (preg_match("|.+?\.zip$|", $prefs['filename']))
391
$prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
394
// Tack on the ".sql" file extension if needed
395
if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
397
$prefs['filename'] .= '.sql';
400
// Load the Zip class and output it
402
$CI =& get_instance();
403
$CI->load->library('zip');
404
$CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
405
return $CI->zip->get_zip();
413
/* End of file DB_utility.php */
414
/* Location: ./system/database/DB_utility.php */
b'\\ No newline at end of file'