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 | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 15 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * MySQL Utility Class |
| 20 | * |
| 21 | * @category Database |
| 22 | * @author Rick Ellis |
| 23 | * @link http://www.codeigniter.com/user_guide/database/ |
| 24 | */ |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 25 | class CI_DB_mysql_utility extends CI_DB_utility { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 26 | |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Create database |
| 29 | * |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 30 | * @access private |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 31 | * @param string the database name |
| 32 | * @return bool |
| 33 | */ |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 34 | function _create_database($name) |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 35 | { |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 36 | return "CREATE DATABASE ".$name; |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // -------------------------------------------------------------------- |
| 40 | |
| 41 | /** |
| 42 | * Drop database |
| 43 | * |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 44 | * @access private |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 45 | * @param string the database name |
| 46 | * @return bool |
| 47 | */ |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 48 | function _drop_database($name) |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 49 | { |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 50 | return "DROP DATABASE ".$name; |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 54 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 55 | /** |
admin | 4ceac2d | 2006-09-25 06:40:16 +0000 | [diff] [blame] | 56 | * Drop Table |
| 57 | * |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 58 | * @access private |
admin | 4ceac2d | 2006-09-25 06:40:16 +0000 | [diff] [blame] | 59 | * @return bool |
| 60 | */ |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 61 | function _drop_table($table) |
admin | 4ceac2d | 2006-09-25 06:40:16 +0000 | [diff] [blame] | 62 | { |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 63 | return "DROP TABLE IF EXISTS ".$this->db->_escape_table($name); |
admin | 4ceac2d | 2006-09-25 06:40:16 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
| 68 | /** |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 69 | * Optimize table query |
| 70 | * |
| 71 | * Generates a platform-specific query so that a table can be optimized |
| 72 | * |
| 73 | * @access private |
| 74 | * @param string the table name |
| 75 | * @return object |
| 76 | */ |
| 77 | function _optimize_table($table) |
| 78 | { |
| 79 | return "OPTIMIZE TABLE ".$this->db->_escape_table($table); |
| 80 | } |
| 81 | |
| 82 | // -------------------------------------------------------------------- |
| 83 | |
| 84 | /** |
| 85 | * Repair table query |
| 86 | * |
| 87 | * Generates a platform-specific query so that a table can be repaired |
| 88 | * |
| 89 | * @access private |
| 90 | * @param string the table name |
| 91 | * @return object |
| 92 | */ |
| 93 | function _repair_table($table) |
| 94 | { |
| 95 | return "REPAIR TABLE ".$this->db->_escape_table($table); |
| 96 | } |
| 97 | |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 98 | // -------------------------------------------------------------------- |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 99 | |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 100 | /** |
| 101 | * MySQL Export |
| 102 | * |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 103 | * @access private |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 104 | * @param array Any preferences |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 105 | * @return mixed |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 106 | */ |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 107 | function _backup($params = array()) |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 108 | { |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 109 | if (count($params) == 0) |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 110 | { |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 111 | return FALSE; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Extract the prefs for simplicity |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 115 | extract($params); |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 116 | |
| 117 | // Build the output |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 118 | $output = ''; |
| 119 | foreach ((array)$tables as $table) |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 120 | { |
| 121 | // Is the table in the "ignore" list? |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 122 | if (in_array($table, (array)$ignore, TRUE)) |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 123 | { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | // Get the table schema |
| 128 | $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table); |
| 129 | |
| 130 | // No result means the table name was invalid |
| 131 | if ($query === FALSE) |
| 132 | { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | // Write out the table schema |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 137 | $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 138 | |
| 139 | if ($add_drop == TRUE) |
| 140 | { |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 141 | $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | $i = 0; |
| 145 | $result = $query->result_array(); |
| 146 | foreach ($result[0] as $val) |
| 147 | { |
| 148 | if ($i++ % 2) |
| 149 | { |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 150 | $output .= $val.';'.$newline.$newline; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 154 | // If inserts are not needed we're done... |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 155 | if ($add_insert == FALSE) |
| 156 | { |
| 157 | continue; |
| 158 | } |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 159 | |
| 160 | // Grab all the data from the current table |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 161 | $query = $this->db->query("SELECT * FROM $table"); |
| 162 | |
| 163 | if ($query->num_rows() == 0) |
| 164 | { |
| 165 | continue; |
| 166 | } |
| 167 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 168 | // Fetch the field names and determine if the field is an |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 169 | // integer type. We use this info to decide whether to |
| 170 | // surround the data with quotes or not |
| 171 | |
| 172 | $i = 0; |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 173 | $field_str = ''; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 174 | $is_int = array(); |
| 175 | while ($field = mysql_fetch_field($query->result_id)) |
| 176 | { |
| 177 | $is_int[$i] = (in_array( |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 178 | strtolower(mysql_field_type($query->result_id, $i)), |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 179 | array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'), |
| 180 | TRUE) |
| 181 | ) ? TRUE : FALSE; |
| 182 | |
| 183 | // Create a string of field names |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 184 | $field_str .= $field->name.', '; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 185 | $i++; |
| 186 | } |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 187 | |
| 188 | // Trim off the end comma |
| 189 | $field_str = preg_replace( "/, $/" , "" , $field_str); |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 190 | |
| 191 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 192 | // Build the insert string |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 193 | foreach ($query->result_array() as $row) |
| 194 | { |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 195 | $val_str = ''; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 196 | |
| 197 | $i = 0; |
| 198 | foreach ($row as $v) |
| 199 | { |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 200 | // Do a little formatting... |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 201 | $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v); |
| 202 | $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v); |
| 203 | $v = str_replace('\\', '\\\\', $v); |
| 204 | $v = str_replace('\'', '\\\'', $v); |
| 205 | $v = str_replace('\\\n', '\n', $v); |
| 206 | $v = str_replace('\\\r', '\r', $v); |
| 207 | $v = str_replace('\\\t', '\t', $v); |
| 208 | |
| 209 | // Escape the data if it's not an integer type |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 210 | $val_str .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v; |
| 211 | $val_str .= ', '; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 212 | |
| 213 | $i++; |
| 214 | } |
| 215 | |
admin | 3ed8c51 | 2006-09-29 23:26:28 +0000 | [diff] [blame] | 216 | $val_str = preg_replace( "/, $/" , "" , $val_str); |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 217 | |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 218 | // Build the INSERT string |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 219 | $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 220 | |
| 221 | } |
| 222 | |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 223 | $output .= $newline.$newline; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 224 | } |
| 225 | |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 226 | return $output; |
admin | 051402b | 2006-09-29 23:11:40 +0000 | [diff] [blame] | 227 | } |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 228 | |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 229 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | ?> |