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 | */ |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 15 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame^] | 28 | var $cache = array(); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 29 | |
admin | 41a1685 | 2006-09-26 18:17:19 +0000 | [diff] [blame] | 30 | /** |
| 31 | * Constructor |
| 32 | * |
| 33 | * Grabs the CI super object instance so we can access it. |
| 34 | * |
| 35 | */ |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 36 | function CI_DB_utility() |
| 37 | { |
| 38 | // Assign the main database object to $this->db |
| 39 | $obj =& get_instance(); |
| 40 | $this->db =& $obj->db; |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 41 | |
| 42 | log_message('debug', "Database Utility Class Initialized"); |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 43 | } |
| 44 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 45 | // -------------------------------------------------------------------- |
| 46 | |
| 47 | /** |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 48 | * Create database |
| 49 | * |
| 50 | * @access public |
| 51 | * @param string the database name |
| 52 | * @return bool |
| 53 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 54 | function create_database($db_name) |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 55 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 56 | $sql = $this->_create_database($db_name); |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 57 | |
| 58 | if (is_bool($sql)) |
| 59 | { |
| 60 | return $sql; |
| 61 | } |
| 62 | |
| 63 | return $this->db->query($sql); |
| 64 | } |
| 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Drop database |
| 70 | * |
| 71 | * @access public |
| 72 | * @param string the database name |
| 73 | * @return bool |
| 74 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 75 | function drop_database($db_name) |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 76 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 77 | $sql = $this->_drop_database($db_name); |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 78 | |
| 79 | if (is_bool($sql)) |
| 80 | { |
| 81 | return $sql; |
| 82 | } |
| 83 | |
| 84 | return $this->db->query($sql); |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 85 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 86 | |
| 87 | // -------------------------------------------------------------------- |
| 88 | |
| 89 | /** |
| 90 | * List databases |
| 91 | * |
| 92 | * @access public |
| 93 | * @return bool |
| 94 | */ |
| 95 | function list_databases() |
| 96 | { |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame^] | 97 | // Is there a cached result? |
| 98 | if (isset($this->cache['db_names'])) |
| 99 | { |
| 100 | return $this->cache['db_names']; |
| 101 | } |
| 102 | |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 103 | $query = $this->db->query($this->_list_database()); |
| 104 | $dbs = array(); |
| 105 | if ($query->num_rows() > 0) |
| 106 | { |
| 107 | foreach ($query->result_array() as $row) |
| 108 | { |
| 109 | $dbs[] = current($row); |
| 110 | } |
| 111 | } |
| 112 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame^] | 113 | return $this->cache['db_names'] =& $dbs; |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 114 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 115 | |
| 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
| 119 | * Returns an array of table names |
| 120 | * |
| 121 | * @access public |
| 122 | * @return array |
| 123 | */ |
| 124 | function list_tables() |
| 125 | { |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame^] | 126 | // Is there a cached result? |
| 127 | if (isset($this->cache['table_names'])) |
| 128 | { |
| 129 | return $this->cache['table_names']; |
| 130 | } |
| 131 | |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 132 | if (FALSE === ($sql = $this->_list_tables())) |
| 133 | { |
| 134 | if ($this->db->db_debug) |
| 135 | { |
| 136 | return $this->db->display_error('db_unsupported_function'); |
| 137 | } |
| 138 | return FALSE; |
| 139 | } |
| 140 | |
| 141 | $retval = array(); |
| 142 | $query = $this->db->query($sql); |
| 143 | |
| 144 | if ($query->num_rows() > 0) |
| 145 | { |
| 146 | foreach($query->result_array() as $row) |
| 147 | { |
| 148 | if (isset($row['TABLE_NAME'])) |
| 149 | { |
| 150 | $retval[] = $row['TABLE_NAME']; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | $retval[] = array_shift($row); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame^] | 159 | return $this->cache['table_names'] =& $retval; |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Determine if a particular table exists |
| 166 | * @access public |
| 167 | * @return boolean |
| 168 | */ |
| 169 | function table_exists($table_name) |
| 170 | { |
| 171 | return ( ! in_array($this->db->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; |
| 172 | } |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
| 177 | * Optimize Table |
| 178 | * |
| 179 | * @access public |
| 180 | * @param string the table name |
| 181 | * @return bool |
| 182 | */ |
| 183 | function optimize_table($table_name) |
| 184 | { |
| 185 | $sql = $this->_optimize_table($table_name); |
| 186 | |
| 187 | if (is_bool($sql)) |
| 188 | { |
| 189 | return $sql; |
| 190 | } |
| 191 | |
| 192 | $query = $this->db->query($sql); |
| 193 | return current($query->result_array()); |
| 194 | } |
| 195 | |
| 196 | // -------------------------------------------------------------------- |
| 197 | |
| 198 | /** |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 199 | * Optimize Database |
| 200 | * |
| 201 | * @access public |
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 202 | * @return array |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 203 | */ |
| 204 | function optimize_database() |
| 205 | { |
| 206 | $result = array(); |
| 207 | foreach ($this->list_tables() as $table_name) |
| 208 | { |
| 209 | $sql = $this->_optimize_table($table_name); |
| 210 | |
| 211 | if (is_bool($sql)) |
| 212 | { |
| 213 | return $sql; |
| 214 | } |
| 215 | |
| 216 | $query = $this->db->query($sql); |
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 217 | |
| 218 | // Build the result array... |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 219 | $res = current($query->result_array()); |
| 220 | $key = str_replace($this->db->database.'.', '', current($res)); |
| 221 | $keys = array_keys($res); |
| 222 | unset($res[$keys[0]]); |
| 223 | |
| 224 | $result[$key] = $res; |
| 225 | } |
| 226 | |
| 227 | return $result; |
| 228 | } |
| 229 | |
| 230 | // -------------------------------------------------------------------- |
| 231 | |
| 232 | /** |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 233 | * Optimize Table |
| 234 | * |
| 235 | * @access public |
| 236 | * @param string the table name |
| 237 | * @return bool |
| 238 | */ |
| 239 | |
| 240 | function repair_table($table_name) |
| 241 | { |
| 242 | $sql = $this->_repair_table($table_name); |
| 243 | |
| 244 | if (is_bool($sql)) |
| 245 | { |
| 246 | return $sql; |
| 247 | } |
| 248 | |
| 249 | $query = $this->db->query($sql); |
| 250 | return current($query->result_array()); |
| 251 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 252 | |
| 253 | // -------------------------------------------------------------------- |
| 254 | |
| 255 | /** |
| 256 | * Drop Table |
| 257 | * |
| 258 | * @access public |
| 259 | * @param string the table name |
| 260 | * @return bool |
| 261 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 262 | function drop_table($table_name) |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 263 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 264 | $sql = $this->_drop_table($table_name); |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 265 | |
| 266 | if (is_bool($sql)) |
| 267 | { |
| 268 | return $sql; |
| 269 | } |
| 270 | |
| 271 | return $this->db->query($sql); |
| 272 | } |
| 273 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame^] | 274 | // -------------------------------------------------------------------- |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 275 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame^] | 276 | /** |
| 277 | * Generate CVS from a query result object |
| 278 | * |
| 279 | * @access public |
| 280 | * @param object The query result object |
| 281 | * @param string The delimiter - tab by default |
| 282 | * @param string The newline character - \n by default |
| 283 | * @return string |
| 284 | */ |
| 285 | function cvs_from_result($query, $delim = "\t", $newline = "\n") |
| 286 | { |
| 287 | if ( ! is_object($query) OR ! method_exists($query, 'field_names')) |
| 288 | { |
| 289 | show_error('You must submit a valid result object'); |
| 290 | } |
| 291 | |
| 292 | $out = ''; |
| 293 | |
| 294 | // First generate the headings from the table column names |
| 295 | foreach ($query->field_names() as $name) |
| 296 | { |
| 297 | $out .= $name.$delim; |
| 298 | } |
| 299 | |
| 300 | $out = rtrim($out); |
| 301 | $out .= $newline; |
| 302 | |
| 303 | // Next blast through the result array and build out the rows |
| 304 | foreach ($query->result_array() as $row) |
| 305 | { |
| 306 | foreach ($row as $item) |
| 307 | { |
| 308 | $out .= $item.$delim; |
| 309 | } |
| 310 | $out = rtrim($out); |
| 311 | $out .= $newline; |
| 312 | } |
| 313 | |
| 314 | return $out; |
| 315 | } |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
| 320 | * Generate XML data from a query result object |
| 321 | * |
| 322 | * @access public |
| 323 | * @param object The query result object |
| 324 | * @param array Any preferences |
| 325 | * @return string |
| 326 | */ |
| 327 | function xml_from_result($query, $params = array()) |
| 328 | { |
| 329 | if ( ! is_object($query) OR ! method_exists($query, 'field_names')) |
| 330 | { |
| 331 | show_error('You must submit a valid result object'); |
| 332 | } |
| 333 | |
| 334 | // Set our default values |
| 335 | foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val) |
| 336 | { |
| 337 | if ( ! isset($params[$key])) |
| 338 | { |
| 339 | $params[$key] = $val; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Create variables for convenience |
| 344 | extract($params); |
| 345 | |
| 346 | // Load the xml helper |
| 347 | $obj =& get_instance(); |
| 348 | $obj->load->helper('xml'); |
| 349 | |
| 350 | // Generate the result |
| 351 | $xml = "<{$root}/>".$newline; |
| 352 | foreach ($query->result_array() as $row) |
| 353 | { |
| 354 | $xml .= $tab."<{$element}/>".$newline; |
| 355 | |
| 356 | foreach ($row as $key => $val) |
| 357 | { |
| 358 | $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline; |
| 359 | } |
| 360 | $xml .= $tab."</{$element}>".$newline; |
| 361 | } |
| 362 | $xml .= "</$root>".$newline; |
| 363 | |
| 364 | return $xml; |
| 365 | } |
| 366 | |
| 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
| 370 | * Database Backup |
| 371 | * |
| 372 | * @access public |
| 373 | * @return void |
| 374 | */ |
| 375 | function export() |
| 376 | { |
| 377 | // The individual driver overloads this method |
| 378 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | } |
| 382 | |
| 383 | ?> |