blob: ff9407ebdf7360565603c1efdbd4ace7fea39fca [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;
admin3ed8c512006-09-29 23:26:28 +000028 var $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
39 $obj =& get_instance();
40 $this->db =& $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 /**
90 * List databases
91 *
92 * @access public
93 * @return bool
94 */
95 function list_databases()
96 {
admin3ed8c512006-09-29 23:26:28 +000097 // Is there a cached result?
98 if (isset($this->cache['db_names']))
99 {
100 return $this->cache['db_names'];
101 }
102
admin83b05a82006-09-25 21:06:46 +0000103 $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
admin3ed8c512006-09-29 23:26:28 +0000113 return $this->cache['db_names'] =& $dbs;
admin83b05a82006-09-25 21:06:46 +0000114 }
admin9cd4e8e2006-09-25 23:26:25 +0000115
116 // --------------------------------------------------------------------
117
118 /**
119 * Returns an array of table names
120 *
121 * @access public
122 * @return array
123 */
124 function list_tables()
125 {
admin3ed8c512006-09-29 23:26:28 +0000126 // Is there a cached result?
127 if (isset($this->cache['table_names']))
128 {
129 return $this->cache['table_names'];
130 }
131
admin9cd4e8e2006-09-25 23:26:25 +0000132 if (FALSE === ($sql = $this->_list_tables()))
133 {
134 if ($this->db->db_debug)
135 {
136 return $this->db->display_error('db_unsupported_function');
137 }
138 return FALSE;
139 }
140
141 $retval = array();
142 $query = $this->db->query($sql);
143
144 if ($query->num_rows() > 0)
145 {
146 foreach($query->result_array() as $row)
147 {
148 if (isset($row['TABLE_NAME']))
149 {
150 $retval[] = $row['TABLE_NAME'];
151 }
152 else
153 {
154 $retval[] = array_shift($row);
155 }
156 }
157 }
158
admin3ed8c512006-09-29 23:26:28 +0000159 return $this->cache['table_names'] =& $retval;
admin9cd4e8e2006-09-25 23:26:25 +0000160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Determine if a particular table exists
166 * @access public
167 * @return boolean
168 */
169 function table_exists($table_name)
170 {
171 return ( ! in_array($this->db->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE;
172 }
adminab4f61b2006-09-25 22:12:32 +0000173
174 // --------------------------------------------------------------------
175
176 /**
177 * Optimize Table
178 *
179 * @access public
180 * @param string the table name
181 * @return bool
182 */
183 function optimize_table($table_name)
184 {
185 $sql = $this->_optimize_table($table_name);
186
187 if (is_bool($sql))
188 {
189 return $sql;
190 }
191
192 $query = $this->db->query($sql);
193 return current($query->result_array());
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
admin31075cf2006-09-26 18:50:02 +0000199 * Optimize Database
200 *
201 * @access public
admine5bb9362006-09-27 00:31:22 +0000202 * @return array
admin31075cf2006-09-26 18:50:02 +0000203 */
204 function optimize_database()
205 {
206 $result = array();
207 foreach ($this->list_tables() as $table_name)
208 {
209 $sql = $this->_optimize_table($table_name);
210
211 if (is_bool($sql))
212 {
213 return $sql;
214 }
215
216 $query = $this->db->query($sql);
admine5bb9362006-09-27 00:31:22 +0000217
218 // Build the result array...
admin31075cf2006-09-26 18:50:02 +0000219 $res = current($query->result_array());
220 $key = str_replace($this->db->database.'.', '', current($res));
221 $keys = array_keys($res);
222 unset($res[$keys[0]]);
223
224 $result[$key] = $res;
225 }
226
227 return $result;
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
adminab4f61b2006-09-25 22:12:32 +0000233 * Optimize Table
234 *
235 * @access public
236 * @param string the table name
237 * @return bool
238 */
239
240 function repair_table($table_name)
241 {
242 $sql = $this->_repair_table($table_name);
243
244 if (is_bool($sql))
245 {
246 return $sql;
247 }
248
249 $query = $this->db->query($sql);
250 return current($query->result_array());
251 }
admin83b05a82006-09-25 21:06:46 +0000252
253 // --------------------------------------------------------------------
254
255 /**
256 * Drop Table
257 *
258 * @access public
259 * @param string the table name
260 * @return bool
261 */
adminab4f61b2006-09-25 22:12:32 +0000262 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000263 {
adminab4f61b2006-09-25 22:12:32 +0000264 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000265
266 if (is_bool($sql))
267 {
268 return $sql;
269 }
270
271 return $this->db->query($sql);
272 }
273
admin3ed8c512006-09-29 23:26:28 +0000274 // --------------------------------------------------------------------
admin83b05a82006-09-25 21:06:46 +0000275
admin3ed8c512006-09-29 23:26:28 +0000276 /**
277 * Generate CVS from a query result object
278 *
279 * @access public
280 * @param object The query result object
281 * @param string The delimiter - tab by default
282 * @param string The newline character - \n by default
283 * @return string
284 */
285 function cvs_from_result($query, $delim = "\t", $newline = "\n")
286 {
287 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
288 {
289 show_error('You must submit a valid result object');
290 }
291
292 $out = '';
293
294 // First generate the headings from the table column names
295 foreach ($query->field_names() as $name)
296 {
297 $out .= $name.$delim;
298 }
299
300 $out = rtrim($out);
301 $out .= $newline;
302
303 // Next blast through the result array and build out the rows
304 foreach ($query->result_array() as $row)
305 {
306 foreach ($row as $item)
307 {
308 $out .= $item.$delim;
309 }
310 $out = rtrim($out);
311 $out .= $newline;
312 }
313
314 return $out;
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Generate XML data from a query result object
321 *
322 * @access public
323 * @param object The query result object
324 * @param array Any preferences
325 * @return string
326 */
327 function xml_from_result($query, $params = array())
328 {
329 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
330 {
331 show_error('You must submit a valid result object');
332 }
333
334 // Set our default values
335 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
336 {
337 if ( ! isset($params[$key]))
338 {
339 $params[$key] = $val;
340 }
341 }
342
343 // Create variables for convenience
344 extract($params);
345
346 // Load the xml helper
347 $obj =& get_instance();
348 $obj->load->helper('xml');
349
350 // Generate the result
351 $xml = "<{$root}/>".$newline;
352 foreach ($query->result_array() as $row)
353 {
354 $xml .= $tab."<{$element}/>".$newline;
355
356 foreach ($row as $key => $val)
357 {
358 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
359 }
360 $xml .= $tab."</{$element}>".$newline;
361 }
362 $xml .= "</$root>".$newline;
363
364 return $xml;
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Database Backup
371 *
372 * @access public
373 * @return void
374 */
375 function export()
376 {
377 // The individual driver overloads this method
378 }
admin7b613c72006-09-24 18:05:17 +0000379
380
381}
382
383?>