blob: 128984d4f8c43203b890ab6738d5fd2ebb5f0d85 [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 /**
68 * Returns an array of table names
69 *
70 * @access public
71 * @return array
72 */
73 function tables()
admina5e812c2006-09-25 02:17:30 +000074 {
admin7b613c72006-09-24 18:05:17 +000075 if (FALSE === ($sql = $this->_show_tables()))
76 {
admina5e812c2006-09-25 02:17:30 +000077 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +000078 {
admina5e812c2006-09-25 02:17:30 +000079 return $this->db->display_error('db_unsupported_function');
admin7b613c72006-09-24 18:05:17 +000080 }
81 return FALSE;
82 }
83
84 $retval = array();
admina5e812c2006-09-25 02:17:30 +000085 $query = $this->db->query($sql);
admin7b613c72006-09-24 18:05:17 +000086
87 if ($query->num_rows() > 0)
88 {
89 foreach($query->result_array() as $row)
90 {
91 if (isset($row['TABLE_NAME']))
92 {
93 $retval[] = $row['TABLE_NAME'];
94 }
95 else
96 {
97 $retval[] = array_shift($row);
98 }
99 }
100 }
101
102 return $retval;
103 }
104
105 // --------------------------------------------------------------------
106
107 /**
108 * Determine if a particular table exists
109 * @access public
110 * @return boolean
111 */
112 function table_exists($table_name)
admina5e812c2006-09-25 02:17:30 +0000113 {
114 return ( ! in_array($this->db->dbprefix.$table_name, $this->tables())) ? FALSE : TRUE;
admin7b613c72006-09-24 18:05:17 +0000115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Fetch MySQL Field Names
121 *
122 * @access public
123 * @param string the table name
124 * @return array
125 */
126 function field_names($table = '')
127 {
128 if ($table == '')
129 {
admina5e812c2006-09-25 02:17:30 +0000130 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +0000131 {
admina5e812c2006-09-25 02:17:30 +0000132 return $this->db->display_error('db_field_param_missing');
admin7b613c72006-09-24 18:05:17 +0000133 }
134 return FALSE;
135 }
136
admina5e812c2006-09-25 02:17:30 +0000137 if (FALSE === ($sql = $this->_show_columns($this->db->dbprefix.$table)))
admin7b613c72006-09-24 18:05:17 +0000138 {
admina5e812c2006-09-25 02:17:30 +0000139 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +0000140 {
admina5e812c2006-09-25 02:17:30 +0000141 return $this->db->display_error('db_unsupported_function');
admin7b613c72006-09-24 18:05:17 +0000142 }
143 return FALSE;
144 }
145
admina5e812c2006-09-25 02:17:30 +0000146 $query = $this->db->query($sql);
admin7b613c72006-09-24 18:05:17 +0000147
148 $retval = array();
149 foreach($query->result_array() as $row)
150 {
151 if (isset($row['COLUMN_NAME']))
152 {
153 $retval[] = $row['COLUMN_NAME'];
154 }
155 else
156 {
157 $retval[] = current($row);
158 }
159 }
160
161 return $retval;
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Returns an object with field data
168 *
169 * @access public
170 * @param string the table name
171 * @return object
172 */
173 function field_data($table = '')
174 {
175 if ($table == '')
176 {
admina5e812c2006-09-25 02:17:30 +0000177 if ($this->db->db_debug)
admin7b613c72006-09-24 18:05:17 +0000178 {
admina5e812c2006-09-25 02:17:30 +0000179 return $this->db->display_error('db_field_param_missing');
admin7b613c72006-09-24 18:05:17 +0000180 }
181 return FALSE;
182 }
183
admin6cec6a52006-09-25 06:56:49 +0000184 $query = $this->db->query($this->_field_data($this->db->dbprefix.$table));
185 return $query->field_data();
admin7b613c72006-09-24 18:05:17 +0000186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Primary
192 *
193 * Retrieves the primary key. It assumes that the row in the first
194 * position is the primary key
195 *
196 * @access public
197 * @param string the table name
198 * @return string
199 */
200 function primary($table = '')
201 {
202 $fields = $this->field_names($table);
203
204 if ( ! is_array($fields))
205 {
206 return FALSE;
207 }
208
209 return current($fields);
210 }
211
admin6cec6a52006-09-25 06:56:49 +0000212 // --------------------------------------------------------------------
213
214 /**
215 * Create database
216 *
217 * @access public
218 * @param string the database name
219 * @return bool
220 */
221 function create_database($name)
222 {
223 $sql = $this->_create_database($name);
224
225 if (is_bool($sql))
226 {
227 return $sql;
228 }
229
230 return $this->db->query($sql);
231 }
232
233 // --------------------------------------------------------------------
234
235
236
237
admin7b613c72006-09-24 18:05:17 +0000238
adminbb1d4392006-09-24 20:14:38 +0000239
adminbb1d4392006-09-24 20:14:38 +0000240 function create_table()
241 {
242 }
243
244 function alter_table()
245 {
246 }
247
248 function create_index()
249 {
250 }
251
252 function drop_index()
253 {
254 }
255
256 function optimize()
257 {
258 }
admin7b613c72006-09-24 18:05:17 +0000259
admin7b613c72006-09-24 18:05:17 +0000260
261
262}
263
264?>