admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1 | <?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 | */ | ||||
25 | class CI_DB_utility { | ||||
26 | |||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 27 | var $db; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 28 | |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 29 | 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 | |||||
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 36 | |
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 | { | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 48 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 49 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 50 | return $this->db->display_error('db_unsupported_function'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 51 | } |
52 | return FALSE; | ||||
53 | } | ||||
54 | |||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 55 | if ($this->db->dbdriver == 'oci8') |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 56 | { |
57 | return $sql; | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 58 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 59 | |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 60 | $query = $this->db->query($sql); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 61 | $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() | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 74 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 75 | if (FALSE === ($sql = $this->_show_tables())) |
76 | { | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 77 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 78 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 79 | return $this->db->display_error('db_unsupported_function'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 80 | } |
81 | return FALSE; | ||||
82 | } | ||||
83 | |||||
84 | $retval = array(); | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 85 | $query = $this->db->query($sql); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 86 | |
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) | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 113 | { |
114 | return ( ! in_array($this->db->dbprefix.$table_name, $this->tables())) ? FALSE : TRUE; | ||||
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 115 | } |
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 | { | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 130 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 131 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 132 | return $this->db->display_error('db_field_param_missing'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 133 | } |
134 | return FALSE; | ||||
135 | } | ||||
136 | |||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 137 | if (FALSE === ($sql = $this->_show_columns($this->db->dbprefix.$table))) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 138 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 139 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 140 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 141 | return $this->db->display_error('db_unsupported_function'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 142 | } |
143 | return FALSE; | ||||
144 | } | ||||
145 | |||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 146 | $query = $this->db->query($sql); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 147 | |
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 | { | ||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 177 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 178 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 179 | return $this->db->display_error('db_field_param_missing'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 180 | } |
181 | return FALSE; | ||||
182 | } | ||||
183 | |||||
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 184 | return $this->_field_data($this->db->dbprefix.$table); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 185 | } |
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 | |||||
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 211 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 212 | |
213 | |||||
214 | |||||
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 215 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 216 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 217 | function show_databases() |
218 | { | ||||
219 | } | ||||
220 | |||||
221 | function create_table() | ||||
222 | { | ||||
223 | } | ||||
224 | |||||
225 | function alter_table() | ||||
226 | { | ||||
227 | } | ||||
228 | |||||
229 | function create_index() | ||||
230 | { | ||||
231 | } | ||||
232 | |||||
233 | function drop_index() | ||||
234 | { | ||||
235 | } | ||||
236 | |||||
237 | function optimize() | ||||
238 | { | ||||
239 | } | ||||
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 240 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 241 | |
242 | |||||
243 | } | ||||
244 | |||||
245 | ?> |