blob: 950db7dfd91a548ff0f2603b481e0cdc1d2bb88f [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 */
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
admina5e812c2006-09-25 02:17:30 +000027 var $db;
admin7b613c72006-09-24 18:05:17 +000028
admina5e812c2006-09-25 02:17:30 +000029 function CI_DB_utility()
30 {
31 // Assign the main database object to $this->db
32 $obj =& get_instance();
33 $this->db =& $obj->db;
34 }
35
admin7b613c72006-09-24 18:05:17 +000036
37 /**
38 * Database Version Number. Returns a string containing the
39 * version of the database being used
40 *
41 * @access public
42 * @return string
43 */
44 function version()
45 {
46 if (FALSE === ($sql = $this->_version()))
47 {
admina5e812c2006-09-25 02:17:30 +000048 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +000049 {
admina5e812c2006-09-25 02:17:30 +000050 return $this->db->display_error('db_unsupported_function');
admin7b613c72006-09-24 18:05:17 +000051 }
52 return FALSE;
53 }
54
admina5e812c2006-09-25 02:17:30 +000055 if ($this->db->dbdriver == 'oci8')
admin7b613c72006-09-24 18:05:17 +000056 {
57 return $sql;
admina5e812c2006-09-25 02:17:30 +000058 }
admin7b613c72006-09-24 18:05:17 +000059
admina5e812c2006-09-25 02:17:30 +000060 $query = $this->db->query($sql);
admin7b613c72006-09-24 18:05:17 +000061 $row = $query->row();
62 return $row->ver;
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
adminab4f61b2006-09-25 22:12:32 +000068 * Primary
69 *
70 * Retrieves the primary key. It assumes that the row in the first
71 * position is the primary key
72 *
73 * @access public
74 * @param string the table name
75 * @return string
76 */
77 function primary($table = '')
78 {
79 $fields = $this->field_names($table);
80
81 if ( ! is_array($fields))
82 {
83 return FALSE;
84 }
85
86 return current($fields);
87 }
88
89 // --------------------------------------------------------------------
90
91 /**
admin7b613c72006-09-24 18:05:17 +000092 * Returns an array of table names
93 *
94 * @access public
95 * @return array
96 */
97 function tables()
admina5e812c2006-09-25 02:17:30 +000098 {
admin7b613c72006-09-24 18:05:17 +000099 if (FALSE === ($sql = $this->_show_tables()))
100 {
admina5e812c2006-09-25 02:17:30 +0000101 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +0000102 {
admina5e812c2006-09-25 02:17:30 +0000103 return $this->db->display_error('db_unsupported_function');
admin7b613c72006-09-24 18:05:17 +0000104 }
105 return FALSE;
106 }
107
108 $retval = array();
admina5e812c2006-09-25 02:17:30 +0000109 $query = $this->db->query($sql);
admin7b613c72006-09-24 18:05:17 +0000110
111 if ($query->num_rows() > 0)
112 {
113 foreach($query->result_array() as $row)
114 {
115 if (isset($row['TABLE_NAME']))
116 {
117 $retval[] = $row['TABLE_NAME'];
118 }
119 else
120 {
121 $retval[] = array_shift($row);
122 }
123 }
124 }
125
126 return $retval;
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Determine if a particular table exists
133 * @access public
134 * @return boolean
135 */
136 function table_exists($table_name)
admina5e812c2006-09-25 02:17:30 +0000137 {
138 return ( ! in_array($this->db->dbprefix.$table_name, $this->tables())) ? FALSE : TRUE;
admin7b613c72006-09-24 18:05:17 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Fetch MySQL Field Names
145 *
146 * @access public
147 * @param string the table name
148 * @return array
149 */
150 function field_names($table = '')
151 {
152 if ($table == '')
153 {
admina5e812c2006-09-25 02:17:30 +0000154 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +0000155 {
admina5e812c2006-09-25 02:17:30 +0000156 return $this->db->display_error('db_field_param_missing');
admin7b613c72006-09-24 18:05:17 +0000157 }
158 return FALSE;
159 }
160
admina5e812c2006-09-25 02:17:30 +0000161 if (FALSE === ($sql = $this->_show_columns($this->db->dbprefix.$table)))
admin7b613c72006-09-24 18:05:17 +0000162 {
admina5e812c2006-09-25 02:17:30 +0000163 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +0000164 {
admina5e812c2006-09-25 02:17:30 +0000165 return $this->db->display_error('db_unsupported_function');
admin7b613c72006-09-24 18:05:17 +0000166 }
167 return FALSE;
168 }
169
admina5e812c2006-09-25 02:17:30 +0000170 $query = $this->db->query($sql);
admin7b613c72006-09-24 18:05:17 +0000171
172 $retval = array();
173 foreach($query->result_array() as $row)
174 {
175 if (isset($row['COLUMN_NAME']))
176 {
177 $retval[] = $row['COLUMN_NAME'];
178 }
179 else
180 {
181 $retval[] = current($row);
182 }
183 }
184
185 return $retval;
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Returns an object with field data
192 *
193 * @access public
194 * @param string the table name
195 * @return object
196 */
197 function field_data($table = '')
198 {
199 if ($table == '')
200 {
admina5e812c2006-09-25 02:17:30 +0000201 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +0000202 {
admina5e812c2006-09-25 02:17:30 +0000203 return $this->db->display_error('db_field_param_missing');
admin7b613c72006-09-24 18:05:17 +0000204 }
205 return FALSE;
206 }
207
admin6cec6a52006-09-25 06:56:49 +0000208 $query = $this->db->query($this->_field_data($this->db->dbprefix.$table));
209 return $query->field_data();
admin7b613c72006-09-24 18:05:17 +0000210 }
211
212 // --------------------------------------------------------------------
213
214 /**
admin6cec6a52006-09-25 06:56:49 +0000215 * Create database
216 *
217 * @access public
218 * @param string the database name
219 * @return bool
220 */
adminab4f61b2006-09-25 22:12:32 +0000221 function create_database($db_name)
admin6cec6a52006-09-25 06:56:49 +0000222 {
adminab4f61b2006-09-25 22:12:32 +0000223 $sql = $this->_create_database($db_name);
admin6cec6a52006-09-25 06:56:49 +0000224
225 if (is_bool($sql))
226 {
227 return $sql;
228 }
229
230 return $this->db->query($sql);
231 }
232
233 // --------------------------------------------------------------------
234
admin83b05a82006-09-25 21:06:46 +0000235 /**
236 * Drop database
237 *
238 * @access public
239 * @param string the database name
240 * @return bool
241 */
adminab4f61b2006-09-25 22:12:32 +0000242 function drop_database($db_name)
adminbb1d4392006-09-24 20:14:38 +0000243 {
adminab4f61b2006-09-25 22:12:32 +0000244 $sql = $this->_drop_database($db_name);
admin83b05a82006-09-25 21:06:46 +0000245
246 if (is_bool($sql))
247 {
248 return $sql;
249 }
250
251 return $this->db->query($sql);
adminbb1d4392006-09-24 20:14:38 +0000252 }
admin83b05a82006-09-25 21:06:46 +0000253
254 // --------------------------------------------------------------------
255
256 /**
257 * List databases
258 *
259 * @access public
260 * @return bool
261 */
262 function list_databases()
263 {
264 $query = $this->db->query($this->_list_database());
265 $dbs = array();
266 if ($query->num_rows() > 0)
267 {
268 foreach ($query->result_array() as $row)
269 {
270 $dbs[] = current($row);
271 }
272 }
273
274 return $dbs;
275 }
adminab4f61b2006-09-25 22:12:32 +0000276
277 // --------------------------------------------------------------------
278
279 /**
280 * Optimize Table
281 *
282 * @access public
283 * @param string the table name
284 * @return bool
285 */
286 function optimize_table($table_name)
287 {
288 $sql = $this->_optimize_table($table_name);
289
290 if (is_bool($sql))
291 {
292 return $sql;
293 }
294
295 $query = $this->db->query($sql);
296 return current($query->result_array());
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Optimize Table
303 *
304 * @access public
305 * @param string the table name
306 * @return bool
307 */
308
309 function repair_table($table_name)
310 {
311 $sql = $this->_repair_table($table_name);
312
313 if (is_bool($sql))
314 {
315 return $sql;
316 }
317
318 $query = $this->db->query($sql);
319 return current($query->result_array());
320 }
admin83b05a82006-09-25 21:06:46 +0000321
322 // --------------------------------------------------------------------
323
324 /**
325 * Drop Table
326 *
327 * @access public
328 * @param string the table name
329 * @return bool
330 */
adminab4f61b2006-09-25 22:12:32 +0000331 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000332 {
adminab4f61b2006-09-25 22:12:32 +0000333 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000334
335 if (is_bool($sql))
336 {
337 return $sql;
338 }
339
340 return $this->db->query($sql);
341 }
342
343
admin7b613c72006-09-24 18:05:17 +0000344
345
346}
347
348?>