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 | /** |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 68 | * 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 | /** |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 92 | * Returns an array of table names |
| 93 | * |
| 94 | * @access public |
| 95 | * @return array |
| 96 | */ |
| 97 | function tables() |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 98 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 99 | if (FALSE === ($sql = $this->_show_tables())) |
| 100 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 101 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 102 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 103 | return $this->db->display_error('db_unsupported_function'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 104 | } |
| 105 | return FALSE; |
| 106 | } |
| 107 | |
| 108 | $retval = array(); |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 109 | $query = $this->db->query($sql); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 110 | |
| 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) |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 137 | { |
| 138 | return ( ! in_array($this->db->dbprefix.$table_name, $this->tables())) ? FALSE : TRUE; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 139 | } |
| 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 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 154 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 155 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 156 | return $this->db->display_error('db_field_param_missing'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 157 | } |
| 158 | return FALSE; |
| 159 | } |
| 160 | |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 161 | if (FALSE === ($sql = $this->_show_columns($this->db->dbprefix.$table))) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 162 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 163 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 164 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 165 | return $this->db->display_error('db_unsupported_function'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 166 | } |
| 167 | return FALSE; |
| 168 | } |
| 169 | |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 170 | $query = $this->db->query($sql); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 171 | |
| 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 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 201 | if ($this->db->db_debug) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 202 | { |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 203 | return $this->db->display_error('db_field_param_missing'); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 204 | } |
| 205 | return FALSE; |
| 206 | } |
| 207 | |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 208 | $query = $this->db->query($this->_field_data($this->db->dbprefix.$table)); |
| 209 | return $query->field_data(); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // -------------------------------------------------------------------- |
| 213 | |
| 214 | /** |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 215 | * Create database |
| 216 | * |
| 217 | * @access public |
| 218 | * @param string the database name |
| 219 | * @return bool |
| 220 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 221 | function create_database($db_name) |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 222 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 223 | $sql = $this->_create_database($db_name); |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 224 | |
| 225 | if (is_bool($sql)) |
| 226 | { |
| 227 | return $sql; |
| 228 | } |
| 229 | |
| 230 | return $this->db->query($sql); |
| 231 | } |
| 232 | |
| 233 | // -------------------------------------------------------------------- |
| 234 | |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 235 | /** |
| 236 | * Drop database |
| 237 | * |
| 238 | * @access public |
| 239 | * @param string the database name |
| 240 | * @return bool |
| 241 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 242 | function drop_database($db_name) |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 243 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 244 | $sql = $this->_drop_database($db_name); |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 245 | |
| 246 | if (is_bool($sql)) |
| 247 | { |
| 248 | return $sql; |
| 249 | } |
| 250 | |
| 251 | return $this->db->query($sql); |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 252 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 253 | |
| 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 | } |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 276 | |
| 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 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 321 | |
| 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
| 325 | * Drop Table |
| 326 | * |
| 327 | * @access public |
| 328 | * @param string the table name |
| 329 | * @return bool |
| 330 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 331 | function drop_table($table_name) |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 332 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame^] | 333 | $sql = $this->_drop_table($table_name); |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 334 | |
| 335 | if (is_bool($sql)) |
| 336 | { |
| 337 | return $sql; |
| 338 | } |
| 339 | |
| 340 | return $this->db->query($sql); |
| 341 | } |
| 342 | |
| 343 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 344 | |
| 345 | |
| 346 | } |
| 347 | |
| 348 | ?> |