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 Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * MySQL Utility Class
|
| 20 | *
|
| 21 | * @category Database
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 22 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 24 | */
|
| 25 | class CI_DB_mysql_utility extends CI_DB_utility {
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 26 |
|
| 27 | /**
|
| 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 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 | // --------------------------------------------------------------------
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 71 | /**
|
| 72 | * MySQL Export
|
| 73 | *
|
| 74 | * @access private
|
| 75 | * @param array Preferences
|
| 76 | * @return mixed
|
| 77 | */
|
| 78 | function _backup($params = array())
|
| 79 | {
|
| 80 | if (count($params) == 0)
|
| 81 | {
|
| 82 | return FALSE;
|
| 83 | }
|
| 84 |
|
| 85 | // Extract the prefs for simplicity
|
| 86 | extract($params);
|
| 87 |
|
| 88 | // Build the output
|
| 89 | $output = '';
|
| 90 | foreach ((array)$tables as $table)
|
| 91 | {
|
| 92 | // Is the table in the "ignore" list?
|
| 93 | if (in_array($table, (array)$ignore, TRUE))
|
| 94 | {
|
| 95 | continue;
|
| 96 | }
|
| 97 |
|
| 98 | // Get the table schema
|
| 99 | $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table);
|
| 100 |
|
| 101 | // No result means the table name was invalid
|
| 102 | if ($query === FALSE)
|
| 103 | {
|
| 104 | continue;
|
| 105 | }
|
| 106 |
|
| 107 | // Write out the table schema
|
| 108 | $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
|
| 109 |
|
| 110 | if ($add_drop == TRUE)
|
| 111 | {
|
| 112 | $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
|
| 113 | }
|
| 114 |
|
| 115 | $i = 0;
|
| 116 | $result = $query->result_array();
|
| 117 | foreach ($result[0] as $val)
|
| 118 | {
|
| 119 | if ($i++ % 2)
|
| 120 | {
|
| 121 | $output .= $val.';'.$newline.$newline;
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | // If inserts are not needed we're done...
|
| 126 | if ($add_insert == FALSE)
|
| 127 | {
|
| 128 | continue;
|
| 129 | }
|
| 130 |
|
| 131 | // Grab all the data from the current table
|
| 132 | $query = $this->db->query("SELECT * FROM $table");
|
| 133 |
|
| 134 | if ($query->num_rows() == 0)
|
| 135 | {
|
| 136 | continue;
|
| 137 | }
|
| 138 |
|
| 139 | // Fetch the field names and determine if the field is an
|
| 140 | // integer type. We use this info to decide whether to
|
| 141 | // surround the data with quotes or not
|
| 142 |
|
| 143 | $i = 0;
|
| 144 | $field_str = '';
|
| 145 | $is_int = array();
|
| 146 | while ($field = mysql_fetch_field($query->result_id))
|
| 147 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 148 | // Most versions of MySQL store timestamp as a string
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 149 | $is_int[$i] = (in_array(
|
| 150 | strtolower(mysql_field_type($query->result_id, $i)),
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 151 | array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 152 | TRUE)
|
| 153 | ) ? TRUE : FALSE;
|
| 154 |
|
| 155 | // Create a string of field names
|
| 156 | $field_str .= $field->name.', ';
|
| 157 | $i++;
|
| 158 | }
|
| 159 |
|
| 160 | // Trim off the end comma
|
| 161 | $field_str = preg_replace( "/, $/" , "" , $field_str);
|
| 162 |
|
| 163 |
|
| 164 | // Build the insert string
|
| 165 | foreach ($query->result_array() as $row)
|
| 166 | {
|
| 167 | $val_str = '';
|
| 168 |
|
| 169 | $i = 0;
|
| 170 | foreach ($row as $v)
|
| 171 | {
|
Derek Allard | db43858 | 2007-10-13 17:00:16 +0000 | [diff] [blame] | 172 | // Is the value NULL?
|
Derek Jones | 8435771 | 2008-01-18 15:52:48 +0000 | [diff] [blame] | 173 | if ($v === NULL)
|
Derek Allard | db43858 | 2007-10-13 17:00:16 +0000 | [diff] [blame] | 174 | {
|
| 175 | $val_str .= 'NULL';
|
| 176 | }
|
| 177 | else
|
| 178 | {
|
Derek Jones | 4a9cb96 | 2008-01-18 18:13:13 +0000 | [diff] [blame] | 179 | // Do a little formatting...
|
| 180 | $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $v);
|
| 181 | $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
|
| 182 | $v = str_replace('\\', '\\\\', $v);
|
| 183 | $v = str_replace('\'', '\\\'', $v);
|
| 184 | $v = str_replace('\\\n', '\n', $v);
|
| 185 | $v = str_replace('\\\r', '\r', $v);
|
| 186 | $v = str_replace('\\\t', '\t', $v);
|
| 187 |
|
Derek Allard | db43858 | 2007-10-13 17:00:16 +0000 | [diff] [blame] | 188 | // Escape the data if it's not an integer
|
| 189 | if ($is_int[$i] == FALSE)
|
| 190 | {
|
| 191 | $val_str .= $this->db->escape($v);
|
| 192 | }
|
| 193 | else
|
| 194 | {
|
| 195 | $val_str .= $v;
|
| 196 | }
|
| 197 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 198 |
|
Derek Allard | db43858 | 2007-10-13 17:00:16 +0000 | [diff] [blame] | 199 | // Append a comma
|
| 200 | $val_str .= ', ';
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 201 | $i++;
|
| 202 | }
|
| 203 |
|
Derek Allard | db43858 | 2007-10-13 17:00:16 +0000 | [diff] [blame] | 204 | // Remove the comma at the end of the string
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 205 | $val_str = preg_replace( "/, $/" , "" , $val_str);
|
| 206 |
|
| 207 | // Build the INSERT string
|
| 208 | $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
|
| 209 | }
|
| 210 |
|
| 211 | $output .= $newline.$newline;
|
| 212 | }
|
| 213 |
|
| 214 | return $output;
|
| 215 | }
|
| 216 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 217 | /**
|
| 218 | *
|
| 219 | * The functions below have been deprecated as of 1.6, and are only here for backwards
|
| 220 | * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
|
| 221 | * is STRONGLY discouraged in favour if using dbforge.
|
| 222 | *
|
| 223 | */
|
| 224 |
|
| 225 | /**
|
| 226 | * Create database
|
| 227 | *
|
| 228 | * @access private
|
| 229 | * @param string the database name
|
| 230 | * @return bool
|
| 231 | */
|
| 232 | function _create_database($name)
|
| 233 | {
|
| 234 | return "CREATE DATABASE ".$name;
|
| 235 | }
|
| 236 |
|
| 237 | // --------------------------------------------------------------------
|
| 238 |
|
| 239 | /**
|
| 240 | * Drop database
|
| 241 | *
|
| 242 | * @access private
|
| 243 | * @param string the database name
|
| 244 | * @return bool
|
| 245 | */
|
| 246 | function _drop_database($name)
|
| 247 | {
|
| 248 | return "DROP DATABASE ".$name;
|
| 249 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 250 |
|
| 251 | }
|
Derek Jones | c7deac9 | 2008-05-11 16:27:41 +0000 | [diff] [blame^] | 252 | ?> |