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 | * MySQLi 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_mysqli_utility extends CI_DB_utility { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
admin | 6ca6f94 | 2006-09-25 02:51:08 +0000 | [diff] [blame] | 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 | // -------------------------------------------------------------------- |
| 54 | |
| 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 | } |
admin | b2a9cec | 2006-10-01 03:38:04 +0000 | [diff] [blame] | 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
| 68 | /** |
| 69 | * List databases |
| 70 | * |
| 71 | * @access private |
| 72 | * @return bool |
| 73 | */ |
| 74 | function _list_databases() |
| 75 | { |
| 76 | return "SHOW DATABASES"; |
| 77 | } |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 78 | |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Optimize table query |
| 83 | * |
| 84 | * Generates a platform-specific query so that a table can be optimized |
| 85 | * |
| 86 | * @access private |
| 87 | * @param string the table name |
| 88 | * @return object |
| 89 | */ |
| 90 | function _optimize_table($table) |
| 91 | { |
| 92 | return "OPTIMIZE TABLE ".$this->db->_escape_table($table); |
| 93 | } |
| 94 | |
| 95 | // -------------------------------------------------------------------- |
| 96 | |
| 97 | /** |
| 98 | * Repair table query |
| 99 | * |
| 100 | * Generates a platform-specific query so that a table can be repaired |
| 101 | * |
| 102 | * @access private |
| 103 | * @param string the table name |
| 104 | * @return object |
| 105 | */ |
| 106 | function _repair_table($table) |
| 107 | { |
| 108 | return "REPAIR TABLE ".$this->db->_escape_table($table); |
| 109 | } |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 110 | |
admin | 3cad41e | 2006-10-02 03:21:46 +0000 | [diff] [blame^] | 111 | // -------------------------------------------------------------------- |
| 112 | |
| 113 | /** |
| 114 | * MySQLi Export |
| 115 | * |
| 116 | * @access private |
| 117 | * @param array Preferences |
| 118 | * @return mixed |
| 119 | */ |
| 120 | function _backup($params = array()) |
| 121 | { |
| 122 | if (count($params) == 0) |
| 123 | { |
| 124 | return FALSE; |
| 125 | } |
| 126 | |
| 127 | // Extract the prefs for simplicity |
| 128 | extract($params); |
| 129 | |
| 130 | // Build the output |
| 131 | $output = ''; |
| 132 | foreach ((array)$tables as $table) |
| 133 | { |
| 134 | // Is the table in the "ignore" list? |
| 135 | if (in_array($table, (array)$ignore, TRUE)) |
| 136 | { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // Get the table schema |
| 141 | $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table); |
| 142 | |
| 143 | // No result means the table name was invalid |
| 144 | if ($query === FALSE) |
| 145 | { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | // Write out the table schema |
| 150 | $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; |
| 151 | |
| 152 | if ($add_drop == TRUE) |
| 153 | { |
| 154 | $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline; |
| 155 | } |
| 156 | |
| 157 | $i = 0; |
| 158 | $result = $query->result_array(); |
| 159 | foreach ($result[0] as $val) |
| 160 | { |
| 161 | if ($i++ % 2) |
| 162 | { |
| 163 | $output .= $val.';'.$newline.$newline; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // If inserts are not needed we're done... |
| 168 | if ($add_insert == FALSE) |
| 169 | { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | // Grab all the data from the current table |
| 174 | $query = $this->db->query("SELECT * FROM $table"); |
| 175 | |
| 176 | if ($query->num_rows() == 0) |
| 177 | { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | // Fetch the field names and determine if the field is an |
| 182 | // integer type. We use this info to decide whether to |
| 183 | // surround the data with quotes or not |
| 184 | |
| 185 | $i = 0; |
| 186 | $field_str = ''; |
| 187 | $is_int = array(); |
| 188 | while ($field = mysqli_fetch_field($query->result_id)) |
| 189 | { |
| 190 | $is_int[$i] = (in_array( |
| 191 | strtolower(mysql_field_type($query->result_id, $i)), |
| 192 | array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp'), |
| 193 | TRUE) |
| 194 | ) ? TRUE : FALSE; |
| 195 | |
| 196 | // Create a string of field names |
| 197 | $field_str .= $field->name.', '; |
| 198 | $i++; |
| 199 | } |
| 200 | |
| 201 | // Trim off the end comma |
| 202 | $field_str = preg_replace( "/, $/" , "" , $field_str); |
| 203 | |
| 204 | |
| 205 | // Build the insert string |
| 206 | foreach ($query->result_array() as $row) |
| 207 | { |
| 208 | $val_str = ''; |
| 209 | |
| 210 | $i = 0; |
| 211 | foreach ($row as $v) |
| 212 | { |
| 213 | // Do a little formatting... |
| 214 | $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v); |
| 215 | $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v); |
| 216 | $v = str_replace('\\', '\\\\', $v); |
| 217 | $v = str_replace('\'', '\\\'', $v); |
| 218 | $v = str_replace('\\\n', '\n', $v); |
| 219 | $v = str_replace('\\\r', '\r', $v); |
| 220 | $v = str_replace('\\\t', '\t', $v); |
| 221 | |
| 222 | // Escape the data if it's not an integer type |
| 223 | $val_str .= ($is_int[$i] == FALSE) ? $this->db->escape($v) : $v; |
| 224 | $val_str .= ', '; |
| 225 | |
| 226 | $i++; |
| 227 | } |
| 228 | |
| 229 | $val_str = preg_replace( "/, $/" , "" , $val_str); |
| 230 | |
| 231 | // Build the INSERT string |
| 232 | $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline; |
| 233 | } |
| 234 | |
| 235 | $output .= $newline.$newline; |
| 236 | } |
| 237 | |
| 238 | return $output; |
| 239 | } |
| 240 | |
| 241 | |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 242 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | ?> |