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 | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 28 | var $data_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 | /** |
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 90 | * List databases |
| 91 | * |
| 92 | * @access public |
| 93 | * @return bool |
| 94 | */ |
| 95 | function list_databases() |
| 96 | { |
| 97 | // Is there a cached result? |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 98 | if (isset($this->data_cache['db_names'])) |
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 99 | { |
admin | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 100 | return $this->data_cache['db_names']; |
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 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 | e8f6eb6 | 2006-10-02 06:46:16 +0000 | [diff] [blame] | 113 | return $this->data_cache['db_names'] =& $dbs; |
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 119 | * Optimize Table |
| 120 | * |
| 121 | * @access public |
| 122 | * @param string the table name |
| 123 | * @return bool |
| 124 | */ |
| 125 | function optimize_table($table_name) |
| 126 | { |
| 127 | $sql = $this->_optimize_table($table_name); |
| 128 | |
| 129 | if (is_bool($sql)) |
| 130 | { |
| 131 | return $sql; |
| 132 | } |
| 133 | |
| 134 | $query = $this->db->query($sql); |
| 135 | return current($query->result_array()); |
| 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
| 139 | |
| 140 | /** |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 141 | * Optimize Database |
| 142 | * |
| 143 | * @access public |
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 144 | * @return array |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 145 | */ |
| 146 | function optimize_database() |
| 147 | { |
| 148 | $result = array(); |
| 149 | foreach ($this->list_tables() as $table_name) |
| 150 | { |
| 151 | $sql = $this->_optimize_table($table_name); |
| 152 | |
| 153 | if (is_bool($sql)) |
| 154 | { |
| 155 | return $sql; |
| 156 | } |
| 157 | |
| 158 | $query = $this->db->query($sql); |
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 159 | |
| 160 | // Build the result array... |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 161 | $res = current($query->result_array()); |
| 162 | $key = str_replace($this->db->database.'.', '', current($res)); |
| 163 | $keys = array_keys($res); |
| 164 | unset($res[$keys[0]]); |
| 165 | |
| 166 | $result[$key] = $res; |
| 167 | } |
| 168 | |
| 169 | return $result; |
| 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 175 | * Optimize Table |
| 176 | * |
| 177 | * @access public |
| 178 | * @param string the table name |
| 179 | * @return bool |
| 180 | */ |
| 181 | |
| 182 | function repair_table($table_name) |
| 183 | { |
| 184 | $sql = $this->_repair_table($table_name); |
| 185 | |
| 186 | if (is_bool($sql)) |
| 187 | { |
| 188 | return $sql; |
| 189 | } |
| 190 | |
| 191 | $query = $this->db->query($sql); |
| 192 | return current($query->result_array()); |
| 193 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 194 | |
| 195 | // -------------------------------------------------------------------- |
| 196 | |
| 197 | /** |
| 198 | * Drop Table |
| 199 | * |
| 200 | * @access public |
| 201 | * @param string the table name |
| 202 | * @return bool |
| 203 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 204 | function drop_table($table_name) |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 205 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 206 | $sql = $this->_drop_table($table_name); |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 207 | |
| 208 | if (is_bool($sql)) |
| 209 | { |
| 210 | return $sql; |
| 211 | } |
| 212 | |
| 213 | return $this->db->query($sql); |
| 214 | } |
| 215 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 216 | // -------------------------------------------------------------------- |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 217 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 218 | /** |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 219 | * Generate CSV from a query result object |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 220 | * |
| 221 | * @access public |
| 222 | * @param object The query result object |
| 223 | * @param string The delimiter - tab by default |
| 224 | * @param string The newline character - \n by default |
| 225 | * @return string |
| 226 | */ |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 227 | function csv_from_result($query, $delim = "\t", $newline = "\n") |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 228 | { |
| 229 | if ( ! is_object($query) OR ! method_exists($query, 'field_names')) |
| 230 | { |
| 231 | show_error('You must submit a valid result object'); |
| 232 | } |
| 233 | |
| 234 | $out = ''; |
| 235 | |
| 236 | // First generate the headings from the table column names |
| 237 | foreach ($query->field_names() as $name) |
| 238 | { |
| 239 | $out .= $name.$delim; |
| 240 | } |
| 241 | |
| 242 | $out = rtrim($out); |
| 243 | $out .= $newline; |
| 244 | |
| 245 | // Next blast through the result array and build out the rows |
| 246 | foreach ($query->result_array() as $row) |
| 247 | { |
| 248 | foreach ($row as $item) |
| 249 | { |
| 250 | $out .= $item.$delim; |
| 251 | } |
| 252 | $out = rtrim($out); |
| 253 | $out .= $newline; |
| 254 | } |
| 255 | |
| 256 | return $out; |
| 257 | } |
| 258 | |
| 259 | // -------------------------------------------------------------------- |
| 260 | |
| 261 | /** |
| 262 | * Generate XML data from a query result object |
| 263 | * |
| 264 | * @access public |
| 265 | * @param object The query result object |
| 266 | * @param array Any preferences |
| 267 | * @return string |
| 268 | */ |
| 269 | function xml_from_result($query, $params = array()) |
| 270 | { |
| 271 | if ( ! is_object($query) OR ! method_exists($query, 'field_names')) |
| 272 | { |
| 273 | show_error('You must submit a valid result object'); |
| 274 | } |
| 275 | |
| 276 | // Set our default values |
| 277 | foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val) |
| 278 | { |
| 279 | if ( ! isset($params[$key])) |
| 280 | { |
| 281 | $params[$key] = $val; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Create variables for convenience |
| 286 | extract($params); |
| 287 | |
| 288 | // Load the xml helper |
| 289 | $obj =& get_instance(); |
| 290 | $obj->load->helper('xml'); |
| 291 | |
| 292 | // Generate the result |
| 293 | $xml = "<{$root}/>".$newline; |
| 294 | foreach ($query->result_array() as $row) |
| 295 | { |
| 296 | $xml .= $tab."<{$element}/>".$newline; |
| 297 | |
| 298 | foreach ($row as $key => $val) |
| 299 | { |
| 300 | $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline; |
| 301 | } |
| 302 | $xml .= $tab."</{$element}>".$newline; |
| 303 | } |
| 304 | $xml .= "</$root>".$newline; |
| 305 | |
| 306 | return $xml; |
| 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------- |
| 310 | |
| 311 | /** |
| 312 | * Database Backup |
| 313 | * |
| 314 | * @access public |
| 315 | * @return void |
| 316 | */ |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 317 | function backup($params = array()) |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 318 | { |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 319 | // If the parameters have not been submitted as an |
| 320 | // array then we know that it is simply the table |
| 321 | // name, which is a valid short cut. |
| 322 | if (is_string($params)) |
| 323 | { |
| 324 | $params = array('tables' => $params); |
| 325 | } |
| 326 | |
| 327 | // ------------------------------------------------------ |
| 328 | |
| 329 | // Set up our default preferences |
| 330 | $prefs = array( |
| 331 | 'tables' => array(), |
| 332 | 'ignore' => array(), |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 333 | 'filename' => '', |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 334 | 'format' => 'gzip', // gzip, zip, txt |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 335 | 'add_drop' => TRUE, |
| 336 | 'add_insert' => TRUE, |
| 337 | 'newline' => "\n" |
| 338 | ); |
| 339 | |
| 340 | // Did the user submit any preferences? If so set them.... |
| 341 | if (count($params) > 0) |
| 342 | { |
| 343 | foreach ($prefs as $key => $val) |
| 344 | { |
| 345 | if (isset($params[$key])) |
| 346 | { |
| 347 | $prefs[$key] = $params[$key]; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // ------------------------------------------------------ |
| 353 | |
| 354 | // Are we backing up a complete database or individual tables? |
| 355 | // If no table names were submitted we'll fetch the entire table list |
| 356 | if (count($prefs['tables']) == 0) |
| 357 | { |
| 358 | $prefs['tables'] = $this->list_tables(); |
| 359 | } |
| 360 | |
| 361 | // ------------------------------------------------------ |
| 362 | |
| 363 | // Validate the format |
| 364 | if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE)) |
| 365 | { |
| 366 | $prefs['format'] = 'txt'; |
| 367 | } |
| 368 | |
| 369 | // ------------------------------------------------------ |
| 370 | |
| 371 | // Is the encoder supported? If not, we'll either issue an |
| 372 | // error or use plain text depending on the debug settings |
| 373 | if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode')) |
| 374 | OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress'))) |
| 375 | { |
| 376 | if ($this->db->db_debug) |
| 377 | { |
| 378 | return $this->db->display_error('db_unsuported_compression'); |
| 379 | } |
| 380 | |
| 381 | $prefs['format'] = 'txt'; |
| 382 | } |
| 383 | |
| 384 | // ------------------------------------------------------ |
| 385 | |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 386 | // Set the filename if not provided - Only needed with Zip files |
| 387 | if ($prefs['filename'] == '' AND $prefs['format'] == 'zip') |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 388 | { |
| 389 | $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database; |
| 390 | $prefs['filename'] .= '_'.date('Y-m-d_H-i', time()); |
| 391 | } |
| 392 | |
| 393 | // ------------------------------------------------------ |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 394 | |
| 395 | // Grab the super object |
| 396 | $obj =& get_instance(); |
| 397 | |
| 398 | // Was a Gzip file requested? |
| 399 | if ($prefs['format'] == 'gzip') |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 400 | { |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 401 | return gzencode($this->_backup($prefs)); |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // ------------------------------------------------------ |
| 405 | |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 406 | // Was a text file requested? |
| 407 | if ($prefs['format'] == 'txt') |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 408 | { |
| 409 | return $this->_backup($prefs); |
| 410 | } |
| 411 | |
| 412 | // ------------------------------------------------------ |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 413 | |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 414 | // Was a Zip file requested? |
admin | 0f8015f | 2006-10-01 22:08:34 +0000 | [diff] [blame] | 415 | if ($prefs['format'] == 'zip') |
| 416 | { |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 417 | // If they included the .zip file extension we'll remove it |
| 418 | if (preg_match("|.+?\.zip$|", $prefs['filename'])) |
| 419 | { |
| 420 | $prefs['filename'] = str_replace('.zip', '', $prefs['filename']); |
| 421 | } |
| 422 | |
| 423 | // Tack on the ".sql" file extension if needed |
| 424 | if ( ! preg_match("|.+?\.sql$|", $prefs['filename'])) |
| 425 | { |
| 426 | $prefs['filename'] .= '.sql'; |
| 427 | } |
| 428 | |
| 429 | // Load the Zip class and output it |
admin | 0f8015f | 2006-10-01 22:08:34 +0000 | [diff] [blame] | 430 | $obj->load->library('zip'); |
admin | 78bc039 | 2006-10-02 02:38:50 +0000 | [diff] [blame] | 431 | $obj->zip->add_data($prefs['filename'], $this->_backup($prefs)); |
| 432 | return $obj->zip->get_zip(); |
admin | 0f8015f | 2006-10-01 22:08:34 +0000 | [diff] [blame] | 433 | } |
| 434 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 435 | } |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 436 | |
| 437 | |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 438 | |
| 439 | |
| 440 | |
| 441 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | ?> |