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