1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * Database Utility Class
\r
21 * @category Database
\r
22 * @author ExpressionEngine Dev Team
\r
23 * @link http://codeigniter.com/user_guide/database/
\r
25 class CI_DB_utility extends CI_DB_forge {
\r
28 var $data_cache = array();
\r
33 * Grabs the CI super object instance so we can access it.
\r
36 function CI_DB_utility()
\r
38 // Assign the main database object to $this->db
\r
39 $CI =& get_instance();
\r
40 $this->db =& $CI->db;
\r
42 log_message('debug', "Database Utility Class Initialized");
\r
45 // --------------------------------------------------------------------
\r
53 function list_databases()
\r
55 // Is there a cached result?
\r
56 if (isset($this->data_cache['db_names']))
\r
58 return $this->data_cache['db_names'];
\r
61 $query = $this->db->query($this->_list_databases());
\r
63 if ($query->num_rows() > 0)
\r
65 foreach ($query->result_array() as $row)
\r
67 $dbs[] = current($row);
\r
71 $this->data_cache['db_names'] = $dbs;
\r
72 return $this->data_cache['db_names'];
\r
75 // --------------------------------------------------------------------
\r
81 * @param string the table name
\r
84 function optimize_table($table_name)
\r
86 $sql = $this->_optimize_table($table_name);
\r
90 show_error('db_must_use_set');
\r
93 $query = $this->db->query($sql);
\r
94 $res = $query->result_array();
\r
96 // Note: Due to a bug in current() that affects some versions
\r
97 // of PHP we can not pass function call directly into it
\r
98 return current($res);
\r
101 // --------------------------------------------------------------------
\r
104 * Optimize Database
\r
109 function optimize_database()
\r
112 foreach ($this->db->list_tables() as $table_name)
\r
114 $sql = $this->_optimize_table($table_name);
\r
121 $query = $this->db->query($sql);
\r
123 // Build the result array...
\r
124 // Note: Due to a bug in current() that affects some versions
\r
125 // of PHP we can not pass function call directly into it
\r
126 $res = $query->result_array();
\r
127 $res = current($res);
\r
128 $key = str_replace($this->db->database.'.', '', current($res));
\r
129 $keys = array_keys($res);
\r
130 unset($res[$keys[0]]);
\r
132 $result[$key] = $res;
\r
138 // --------------------------------------------------------------------
\r
144 * @param string the table name
\r
147 function repair_table($table_name)
\r
149 $sql = $this->_repair_table($table_name);
\r
156 $query = $this->db->query($sql);
\r
158 // Note: Due to a bug in current() that affects some versions
\r
159 // of PHP we can not pass function call directly into it
\r
160 $res = $query->result_array();
\r
161 return current($res);
\r
164 // --------------------------------------------------------------------
\r
167 * Generate CSV from a query result object
\r
170 * @param object The query result object
\r
171 * @param string The delimiter - comma by default
\r
172 * @param string The newline character - \n by default
\r
173 * @param string The enclosure - double quote by default
\r
176 function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
\r
178 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
\r
180 show_error('You must submit a valid result object');
\r
185 // First generate the headings from the table column names
\r
186 foreach ($query->list_fields() as $name)
\r
188 $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
\r
191 $out = rtrim($out);
\r
194 // Next blast through the result array and build out the rows
\r
195 foreach ($query->result_array() as $row)
\r
197 foreach ($row as $item)
\r
199 $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
\r
201 $out = rtrim($out);
\r
208 // --------------------------------------------------------------------
\r
211 * Generate XML data from a query result object
\r
214 * @param object The query result object
\r
215 * @param array Any preferences
\r
218 function xml_from_result($query, $params = array())
\r
220 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
\r
222 show_error('You must submit a valid result object');
\r
225 // Set our default values
\r
226 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
\r
228 if ( ! isset($params[$key]))
\r
230 $params[$key] = $val;
\r
234 // Create variables for convenience
\r
237 // Load the xml helper
\r
238 $CI =& get_instance();
\r
239 $CI->load->helper('xml');
\r
241 // Generate the result
\r
242 $xml = "<{$root}>".$newline;
\r
243 foreach ($query->result_array() as $row)
\r
245 $xml .= $tab."<{$element}>".$newline;
\r
247 foreach ($row as $key => $val)
\r
249 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
\r
251 $xml .= $tab."</{$element}>".$newline;
\r
253 $xml .= "</$root>".$newline;
\r
258 // --------------------------------------------------------------------
\r
266 function backup($params = array())
\r
268 // If the parameters have not been submitted as an
\r
269 // array then we know that it is simply the table
\r
270 // name, which is a valid short cut.
\r
271 if (is_string($params))
\r
273 $params = array('tables' => $params);
\r
276 // ------------------------------------------------------
\r
278 // Set up our default preferences
\r
280 'tables' => array(),
\r
281 'ignore' => array(),
\r
283 'format' => 'gzip', // gzip, zip, txt
\r
284 'add_drop' => TRUE,
\r
285 'add_insert' => TRUE,
\r
289 // Did the user submit any preferences? If so set them....
\r
290 if (count($params) > 0)
\r
292 foreach ($prefs as $key => $val)
\r
294 if (isset($params[$key]))
\r
296 $prefs[$key] = $params[$key];
\r
301 // ------------------------------------------------------
\r
303 // Are we backing up a complete database or individual tables?
\r
304 // If no table names were submitted we'll fetch the entire table list
\r
305 if (count($prefs['tables']) == 0)
\r
307 $prefs['tables'] = $this->db->list_tables();
\r
310 // ------------------------------------------------------
\r
312 // Validate the format
\r
313 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
\r
315 $prefs['format'] = 'txt';
\r
318 // ------------------------------------------------------
\r
320 // Is the encoder supported? If not, we'll either issue an
\r
321 // error or use plain text depending on the debug settings
\r
322 if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
\r
323 OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
\r
325 if ($this->db->db_debug)
\r
327 return $this->db->display_error('db_unsuported_compression');
\r
330 $prefs['format'] = 'txt';
\r
333 // ------------------------------------------------------
\r
335 // Set the filename if not provided - Only needed with Zip files
\r
336 if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
\r
338 $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
\r
339 $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
\r
342 // ------------------------------------------------------
\r
344 // Was a Gzip file requested?
\r
345 if ($prefs['format'] == 'gzip')
\r
347 return gzencode($this->_backup($prefs));
\r
350 // ------------------------------------------------------
\r
352 // Was a text file requested?
\r
353 if ($prefs['format'] == 'txt')
\r
355 return $this->_backup($prefs);
\r
358 // ------------------------------------------------------
\r
360 // Was a Zip file requested?
\r
361 if ($prefs['format'] == 'zip')
\r
363 // If they included the .zip file extension we'll remove it
\r
364 if (preg_match("|.+?\.zip$|", $prefs['filename']))
\r
366 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
\r
369 // Tack on the ".sql" file extension if needed
\r
370 if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
\r
372 $prefs['filename'] .= '.sql';
\r
375 // Load the Zip class and output it
\r
377 $CI =& get_instance();
\r
378 $CI->load->library('zip');
\r
379 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
\r
380 return $CI->zip->get_zip();
\r
388 /* End of file DB_utility.php */
\r
389 /* Location: ./system/database/DB_utility.php */