Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame^] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame^] | 22 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 23 | * @link http://www.codeigniter.com/user_guide/database/
|
| 24 | */
|
| 25 | class CI_DB_mysqli_utility extends CI_DB_utility {
|
| 26 |
|
| 27 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 28 | * List databases
|
| 29 | *
|
| 30 | * @access private
|
| 31 | * @return bool
|
| 32 | */
|
| 33 | function _list_databases()
|
| 34 | {
|
| 35 | return "SHOW DATABASES";
|
| 36 | }
|
| 37 |
|
| 38 | // --------------------------------------------------------------------
|
| 39 |
|
| 40 | /**
|
| 41 | * Optimize table query
|
| 42 | *
|
| 43 | * Generates a platform-specific query so that a table can be optimized
|
| 44 | *
|
| 45 | * @access private
|
| 46 | * @param string the table name
|
| 47 | * @return object
|
| 48 | */
|
| 49 | function _optimize_table($table)
|
| 50 | {
|
| 51 | return "OPTIMIZE TABLE ".$this->db->_escape_table($table);
|
| 52 | }
|
| 53 |
|
| 54 | // --------------------------------------------------------------------
|
| 55 |
|
| 56 | /**
|
| 57 | * Repair table query
|
| 58 | *
|
| 59 | * Generates a platform-specific query so that a table can be repaired
|
| 60 | *
|
| 61 | * @access private
|
| 62 | * @param string the table name
|
| 63 | * @return object
|
| 64 | */
|
| 65 | function _repair_table($table)
|
| 66 | {
|
| 67 | return "REPAIR TABLE ".$this->db->_escape_table($table);
|
| 68 | }
|
| 69 |
|
| 70 | // --------------------------------------------------------------------
|
| 71 |
|
| 72 | /**
|
| 73 | * MySQLi Export
|
| 74 | *
|
| 75 | * @access private
|
| 76 | * @param array Preferences
|
| 77 | * @return mixed
|
| 78 | */
|
| 79 | function _backup($params = array())
|
| 80 | {
|
| 81 | if (count($params) == 0)
|
| 82 | {
|
| 83 | return FALSE;
|
| 84 | }
|
| 85 |
|
| 86 | // Extract the prefs for simplicity
|
| 87 | extract($params);
|
| 88 |
|
| 89 | // Build the output
|
| 90 | $output = '';
|
| 91 | foreach ((array)$tables as $table)
|
| 92 | {
|
| 93 | // Is the table in the "ignore" list?
|
| 94 | if (in_array($table, (array)$ignore, TRUE))
|
| 95 | {
|
| 96 | continue;
|
| 97 | }
|
| 98 |
|
| 99 | // Get the table schema
|
| 100 | $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
|
| 101 |
|
| 102 | // No result means the table name was invalid
|
| 103 | if ($query === FALSE)
|
| 104 | {
|
| 105 | continue;
|
| 106 | }
|
| 107 |
|
| 108 | // Write out the table schema
|
| 109 | $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
|
| 110 |
|
| 111 | if ($add_drop == TRUE)
|
| 112 | {
|
| 113 | $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
|
| 114 | }
|
| 115 |
|
| 116 | $i = 0;
|
| 117 | $result = $query->result_array();
|
| 118 | foreach ($result[0] as $val)
|
| 119 | {
|
| 120 | if ($i++ % 2)
|
| 121 | {
|
| 122 | $output .= $val.';'.$newline.$newline;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | // If inserts are not needed we're done...
|
| 127 | if ($add_insert == FALSE)
|
| 128 | {
|
| 129 | continue;
|
| 130 | }
|
| 131 |
|
| 132 | // Grab all the data from the current table
|
| 133 | $query = $this->db->query("SELECT * FROM $table");
|
| 134 |
|
| 135 | if ($query->num_rows() == 0)
|
| 136 | {
|
| 137 | continue;
|
| 138 | }
|
| 139 |
|
| 140 | // Fetch the field names and determine if the field is an
|
| 141 | // integer type. We use this info to decide whether to
|
| 142 | // surround the data with quotes or not
|
| 143 |
|
| 144 | $i = 0;
|
| 145 | $field_str = '';
|
| 146 | $is_int = array();
|
| 147 | while ($field = mysqli_fetch_field($query->result_id))
|
| 148 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 149 | // Most versions of MySQL store timestamp as a string
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 150 | $is_int[$i] = (in_array(
|
| 151 | strtolower(mysql_field_type($query->result_id, $i)),
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 152 | array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), // 'timestamp'),
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 153 | TRUE)
|
| 154 | ) ? TRUE : FALSE;
|
| 155 |
|
| 156 | // Create a string of field names
|
| 157 | $field_str .= $field->name.', ';
|
| 158 | $i++;
|
| 159 | }
|
| 160 |
|
| 161 | // Trim off the end comma
|
| 162 | $field_str = preg_replace( "/, $/" , "" , $field_str);
|
| 163 |
|
| 164 |
|
| 165 | // Build the insert string
|
| 166 | foreach ($query->result_array() as $row)
|
| 167 | {
|
| 168 | $val_str = '';
|
| 169 |
|
| 170 | $i = 0;
|
| 171 | foreach ($row as $v)
|
| 172 | {
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 173 | // Is the value NULL?
|
Derek Jones | 8435771 | 2008-01-18 15:52:48 +0000 | [diff] [blame] | 174 | if ($v === NULL)
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 175 | {
|
| 176 | $val_str .= 'NULL';
|
| 177 | }
|
| 178 | else
|
| 179 | {
|
Derek Jones | 4a9cb96 | 2008-01-18 18:13:13 +0000 | [diff] [blame] | 180 | // Do a little formatting...
|
| 181 | $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v);
|
| 182 | $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
|
| 183 | $v = str_replace('\\', '\\\\', $v);
|
| 184 | $v = str_replace('\'', '\\\'', $v);
|
| 185 | $v = str_replace('\\\n', '\n', $v);
|
| 186 | $v = str_replace('\\\r', '\r', $v);
|
| 187 | $v = str_replace('\\\t', '\t', $v);
|
| 188 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 189 | // Escape the data if it's not an integer
|
| 190 | if ($is_int[$i] == FALSE)
|
| 191 | {
|
| 192 | $val_str .= $this->db->escape($v);
|
| 193 | }
|
| 194 | else
|
| 195 | {
|
| 196 | $val_str .= $v;
|
| 197 | }
|
Derek Jones | 4a9cb96 | 2008-01-18 18:13:13 +0000 | [diff] [blame] | 198 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 199 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 200 | // Append a comma
|
| 201 | $val_str .= ', ';
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 202 | $i++;
|
| 203 | }
|
| 204 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 205 | // Remove the comma at the end of the string
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 206 | $val_str = preg_replace( "/, $/" , "" , $val_str);
|
| 207 |
|
| 208 | // Build the INSERT string
|
| 209 | $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
|
| 210 | }
|
| 211 |
|
| 212 | $output .= $newline.$newline;
|
| 213 | }
|
| 214 |
|
| 215 | return $output;
|
| 216 | }
|
| 217 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 218 | /**
|
| 219 | *
|
| 220 | * The functions below have been deprecated as of 1.6, and are only here for backwards
|
| 221 | * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
|
| 222 | * is STRONGLY discouraged in favour if using dbforge.
|
| 223 | *
|
| 224 | */
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 225 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 226 | /**
|
| 227 | * Create database
|
| 228 | *
|
| 229 | * @access private
|
| 230 | * @param string the database name
|
| 231 | * @return bool
|
| 232 | */
|
| 233 | function _create_database($name)
|
| 234 | {
|
| 235 | return "CREATE DATABASE ".$name;
|
| 236 | }
|
| 237 |
|
| 238 | // --------------------------------------------------------------------
|
| 239 |
|
| 240 | /**
|
| 241 | * Drop database
|
| 242 | *
|
| 243 | * @access private
|
| 244 | * @param string the database name
|
| 245 | * @return bool
|
| 246 | */
|
| 247 | function _drop_database($name)
|
| 248 | {
|
| 249 | return "DROP DATABASE ".$name;
|
| 250 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 251 |
|
| 252 | }
|
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 253 | ?> |