blob: c43b3995056083257b22c1d5089284d88af471f2 [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
admina5e812c2006-09-25 02:17:30 +0000184 return $this->_field_data($this->db->dbprefix.$table);
admin7b613c72006-09-24 18:05:17 +0000185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Primary
191 *
192 * Retrieves the primary key. It assumes that the row in the first
193 * position is the primary key
194 *
195 * @access public
196 * @param string the table name
197 * @return string
198 */
199 function primary($table = '')
200 {
201 $fields = $this->field_names($table);
202
203 if ( ! is_array($fields))
204 {
205 return FALSE;
206 }
207
208 return current($fields);
209 }
210
admin7b613c72006-09-24 18:05:17 +0000211
adminbb1d4392006-09-24 20:14:38 +0000212
213
214
215 function create_database()
admin7b613c72006-09-24 18:05:17 +0000216 {
admin7b613c72006-09-24 18:05:17 +0000217 }
218
adminbb1d4392006-09-24 20:14:38 +0000219 function drop_database()
admin7b613c72006-09-24 18:05:17 +0000220 {
adminbb1d4392006-09-24 20:14:38 +0000221 }
admin7b613c72006-09-24 18:05:17 +0000222
adminbb1d4392006-09-24 20:14:38 +0000223 function show_databases()
224 {
225 }
226
227 function create_table()
228 {
229 }
230
231 function alter_table()
232 {
233 }
234
235 function create_index()
236 {
237 }
238
239 function drop_index()
240 {
241 }
242
243 function optimize()
244 {
245 }
admin7b613c72006-09-24 18:05:17 +0000246
admin7b613c72006-09-24 18:05:17 +0000247
248
249}
250
251?>