blob: 513664be81ae74daf5f604d4d5e3f42a3bb839a1 [file] [log] [blame]
Derek Allard6838f002007-10-04 19:29:59 +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
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allard39b622d2008-01-16 21:10:09 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.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
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Allard6838f002007-10-04 19:29:59 +000023 * @link http://www.codeigniter.com/user_guide/database/
24 */
Derek Allard39b622d2008-01-16 21:10:09 +000025class CI_DB_utility extends CI_DB_forge {
Derek Allard6838f002007-10-04 19:29:59 +000026
27 var $db;
Derek Allard39b622d2008-01-16 21:10:09 +000028 var $data_cache = array();
Derek Allard6838f002007-10-04 19:29:59 +000029
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 /**
Derek Allard6838f002007-10-04 19:29:59 +000048 * List databases
49 *
50 * @access public
51 * @return bool
52 */
53 function list_databases()
54 {
55 // Is there a cached result?
56 if (isset($this->data_cache['db_names']))
57 {
58 return $this->data_cache['db_names'];
59 }
60
61 $query = $this->db->query($this->_list_databases());
62 $dbs = array();
63 if ($query->num_rows() > 0)
64 {
65 foreach ($query->result_array() as $row)
66 {
67 $dbs[] = current($row);
68 }
69 }
70
71 $this->data_cache['db_names'] = $dbs;
72 return $this->data_cache['db_names'];
73 }
74
75 // --------------------------------------------------------------------
76
77 /**
78 * Optimize Table
79 *
80 * @access public
81 * @param string the table name
82 * @return bool
83 */
84 function optimize_table($table_name)
85 {
86 $sql = $this->_optimize_table($table_name);
87
88 if (is_bool($sql))
89 {
Derek Allard39b622d2008-01-16 21:10:09 +000090 show_error('db_must_use_set');
Derek Allard6838f002007-10-04 19:29:59 +000091 }
92
93 $query = $this->db->query($sql);
94 $res = $query->result_array();
95
96 // Note: Due to a bug in current() that affects some versions
97 // of PHP we can not pass function call directly into it
98 return current($res);
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Optimize Database
105 *
106 * @access public
107 * @return array
108 */
109 function optimize_database()
110 {
111 $result = array();
112 foreach ($this->db->list_tables() as $table_name)
113 {
114 $sql = $this->_optimize_table($table_name);
115
116 if (is_bool($sql))
117 {
118 return $sql;
119 }
120
121 $query = $this->db->query($sql);
122
123 // Build the result array...
124 // Note: Due to a bug in current() that affects some versions
125 // of PHP we can not pass function call directly into it
126 $res = $query->result_array();
127 $res = current($res);
128 $key = str_replace($this->db->database.'.', '', current($res));
129 $keys = array_keys($res);
130 unset($res[$keys[0]]);
131
132 $result[$key] = $res;
133 }
134
135 return $result;
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000141 * Repair Table
Derek Allard6838f002007-10-04 19:29:59 +0000142 *
143 * @access public
144 * @param string the table name
145 * @return bool
146 */
Derek Allard6838f002007-10-04 19:29:59 +0000147 function repair_table($table_name)
148 {
149 $sql = $this->_repair_table($table_name);
150
151 if (is_bool($sql))
152 {
153 return $sql;
154 }
155
156 $query = $this->db->query($sql);
157
158 // Note: Due to a bug in current() that affects some versions
159 // of PHP we can not pass function call directly into it
160 $res = $query->result_array();
161 return current($res);
162 }
Derek Allard6838f002007-10-04 19:29:59 +0000163
Derek Allard6838f002007-10-04 19:29:59 +0000164 // --------------------------------------------------------------------
165
166 /**
167 * Generate CSV from a query result object
168 *
169 * @access public
170 * @param object The query result object
171 * @param string The delimiter - tab by default
172 * @param string The newline character - \n by default
173 * @return string
174 */
175 function csv_from_result($query, $delim = "\t", $newline = "\n")
176 {
177 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
178 {
179 show_error('You must submit a valid result object');
180 }
181
182 $out = '';
183
184 // First generate the headings from the table column names
185 foreach ($query->list_fields() as $name)
186 {
187 $out .= $name.$delim;
188 }
189
190 $out = rtrim($out);
191 $out .= $newline;
192
193 // Next blast through the result array and build out the rows
194 foreach ($query->result_array() as $row)
195 {
196 foreach ($row as $item)
197 {
198 $out .= $item.$delim;
199 }
200 $out = rtrim($out);
201 $out .= $newline;
202 }
203
204 return $out;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Generate XML data from a query result object
211 *
212 * @access public
213 * @param object The query result object
214 * @param array Any preferences
215 * @return string
216 */
217 function xml_from_result($query, $params = array())
218 {
219 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
220 {
221 show_error('You must submit a valid result object');
222 }
223
224 // Set our default values
225 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
226 {
227 if ( ! isset($params[$key]))
228 {
229 $params[$key] = $val;
230 }
231 }
232
233 // Create variables for convenience
234 extract($params);
235
236 // Load the xml helper
237 $CI =& get_instance();
238 $CI->load->helper('xml');
239
240 // Generate the result
241 $xml = "<{$root}>".$newline;
242 foreach ($query->result_array() as $row)
243 {
244 $xml .= $tab."<{$element}>".$newline;
245
246 foreach ($row as $key => $val)
247 {
248 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
249 }
250 $xml .= $tab."</{$element}>".$newline;
251 }
252 $xml .= "</$root>".$newline;
253
254 return $xml;
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Database Backup
261 *
262 * @access public
263 * @return void
264 */
265 function backup($params = array())
266 {
267 // If the parameters have not been submitted as an
268 // array then we know that it is simply the table
269 // name, which is a valid short cut.
270 if (is_string($params))
271 {
272 $params = array('tables' => $params);
273 }
274
275 // ------------------------------------------------------
276
277 // Set up our default preferences
278 $prefs = array(
279 'tables' => array(),
280 'ignore' => array(),
281 'filename' => '',
282 'format' => 'gzip', // gzip, zip, txt
283 'add_drop' => TRUE,
284 'add_insert' => TRUE,
285 'newline' => "\n"
286 );
287
288 // Did the user submit any preferences? If so set them....
289 if (count($params) > 0)
290 {
291 foreach ($prefs as $key => $val)
292 {
293 if (isset($params[$key]))
294 {
295 $prefs[$key] = $params[$key];
296 }
297 }
298 }
299
300 // ------------------------------------------------------
301
302 // Are we backing up a complete database or individual tables?
303 // If no table names were submitted we'll fetch the entire table list
304 if (count($prefs['tables']) == 0)
305 {
306 $prefs['tables'] = $this->db->list_tables();
307 }
308
309 // ------------------------------------------------------
310
311 // Validate the format
312 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
313 {
314 $prefs['format'] = 'txt';
315 }
316
317 // ------------------------------------------------------
318
319 // Is the encoder supported? If not, we'll either issue an
320 // error or use plain text depending on the debug settings
321 if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
322 OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
323 {
324 if ($this->db->db_debug)
325 {
326 return $this->db->display_error('db_unsuported_compression');
327 }
328
329 $prefs['format'] = 'txt';
330 }
331
332 // ------------------------------------------------------
333
334 // Set the filename if not provided - Only needed with Zip files
335 if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
336 {
337 $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
338 $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
339 }
340
341 // ------------------------------------------------------
342
343 // Was a Gzip file requested?
344 if ($prefs['format'] == 'gzip')
345 {
346 return gzencode($this->_backup($prefs));
347 }
348
349 // ------------------------------------------------------
350
351 // Was a text file requested?
352 if ($prefs['format'] == 'txt')
353 {
354 return $this->_backup($prefs);
355 }
356
357 // ------------------------------------------------------
358
359 // Was a Zip file requested?
360 if ($prefs['format'] == 'zip')
361 {
362 // If they included the .zip file extension we'll remove it
363 if (preg_match("|.+?\.zip$|", $prefs['filename']))
364 {
365 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
366 }
367
368 // Tack on the ".sql" file extension if needed
369 if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
370 {
371 $prefs['filename'] .= '.sql';
372 }
373
374 // Load the Zip class and output it
375
376 $CI =& get_instance();
377 $CI->load->library('zip');
378 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
379 return $CI->zip->get_zip();
380 }
381
382 }
383
Derek Allard6838f002007-10-04 19:29:59 +0000384}
385
admin7b613c72006-09-24 18:05:17 +0000386?>