Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * MySQL Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_mysql_driver extends CI_DB { |
| 42 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 43 | public $dbdriver = 'mysql'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | |
| 45 | // The character used for escaping |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 46 | protected $_escape_char = '`'; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 47 | |
| 48 | // clause and character used for LIKE escape sequences - not used in MySQL |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = ''; |
Andrey Andreev | 21cb2d3 | 2012-05-25 01:01:06 +0300 | [diff] [blame] | 50 | protected $_like_escape_chr = '\\'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 52 | protected $_random_keyword = ' RAND()'; // database specific random keyword |
RH Becker | cfdb232 | 2011-10-03 17:28:32 -0700 | [diff] [blame] | 53 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | /** |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 55 | * Whether to use the MySQL "delete hack" which allows the number |
| 56 | * of affected rows to be shown. Uses a preg_replace when enabled, |
| 57 | * adding a bit more processing to all queries. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 58 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 59 | public $delete_hack = TRUE; |
| 60 | |
Andrey Andreev | 473130a | 2012-06-24 02:51:18 +0300 | [diff] [blame] | 61 | /** |
| 62 | * Constructor |
| 63 | * |
| 64 | * @param array |
| 65 | * @return void |
| 66 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 67 | public function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 69 | parent::__construct($params); |
| 70 | |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 71 | if ( ! empty($this->port)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | { |
| 73 | $this->hostname .= ':'.$this->port; |
| 74 | } |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 75 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 76 | |
Andrey Andreev | 473130a | 2012-06-24 02:51:18 +0300 | [diff] [blame] | 77 | // -------------------------------------------------------------------- |
| 78 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 79 | /** |
| 80 | * Non-persistent database connection |
| 81 | * |
| 82 | * @return resource |
| 83 | */ |
| 84 | public function db_connect() |
| 85 | { |
Michiel Vugteveen | 49f7b72 | 2012-08-20 18:52:21 +0200 | [diff] [blame^] | 86 | if ($this->compress === TRUE) |
| 87 | { |
| 88 | return @mysql_connect($this->hostname, $this->username, $this->password, TRUE, MYSQL_CLIENT_COMPRESS); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | return @mysql_connect($this->hostname, $this->username, $this->password, TRUE); |
| 93 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | // -------------------------------------------------------------------- |
| 97 | |
| 98 | /** |
| 99 | * Persistent database connection |
| 100 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 103 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | { |
Michiel Vugteveen | 49f7b72 | 2012-08-20 18:52:21 +0200 | [diff] [blame^] | 105 | if ($this->compress === TRUE) |
| 106 | { |
| 107 | return @mysql_pconnect($this->hostname, $this->username, $this->password, MYSQL_CLIENT_COMPRESS); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | return @mysql_pconnect($this->hostname, $this->username, $this->password); |
| 112 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | // -------------------------------------------------------------------- |
| 116 | |
| 117 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 118 | * Reconnect |
| 119 | * |
| 120 | * Keep / reestablish the db connection if no queries have been |
| 121 | * sent for a length of time exceeding the server's idle timeout |
| 122 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 123 | * @return void |
| 124 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 125 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 126 | { |
| 127 | if (mysql_ping($this->conn_id) === FALSE) |
| 128 | { |
| 129 | $this->conn_id = FALSE; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 135 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | * Select the database |
| 137 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 138 | * @param string database name |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 139 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 141 | public function db_select($database = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 143 | if ($database === '') |
| 144 | { |
| 145 | $database = $this->database; |
| 146 | } |
| 147 | |
| 148 | if (@mysql_select_db($database, $this->conn_id)) |
| 149 | { |
| 150 | $this->database = $database; |
| 151 | return TRUE; |
| 152 | } |
| 153 | |
| 154 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // -------------------------------------------------------------------- |
| 158 | |
| 159 | /** |
| 160 | * Set client character set |
| 161 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | * @param string |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 163 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | */ |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 165 | protected function _db_set_charset($charset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
Andrey Andreev | b3442a1 | 2012-03-12 15:35:40 +0200 | [diff] [blame] | 167 | return @mysql_set_charset($charset, $this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 173 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | * @return string |
| 176 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 177 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 179 | return isset($this->data_cache['version']) |
| 180 | ? $this->data_cache['version'] |
| 181 | : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
| 187 | * Execute the query |
| 188 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | * @param string an SQL query |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 190 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 192 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 194 | return @mysql_query($this->_prep_query($sql), $this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | // -------------------------------------------------------------------- |
| 198 | |
| 199 | /** |
| 200 | * Prep the query |
| 201 | * |
| 202 | * If needed, each database adapter can prep the query string |
| 203 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | * @param string an SQL query |
| 205 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 207 | protected function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 209 | // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack |
| 210 | // modifies the query so that it a proper number of affected rows is returned. |
| 211 | if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 213 | return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 215 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | return $sql; |
| 217 | } |
| 218 | |
| 219 | // -------------------------------------------------------------------- |
| 220 | |
| 221 | /** |
| 222 | * Begin Transaction |
| 223 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | * @return bool |
| 225 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 226 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 229 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | { |
| 231 | return TRUE; |
| 232 | } |
| 233 | |
| 234 | // Reset the transaction failure flag. |
| 235 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 236 | // even if the queries produce a successful result. |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 237 | $this->_trans_failure = ($test_mode === TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | $this->simple_query('SET AUTOCOMMIT=0'); |
| 240 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
| 241 | return TRUE; |
| 242 | } |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Commit Transaction |
| 248 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 249 | * @return bool |
| 250 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 251 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 254 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | { |
| 256 | return TRUE; |
| 257 | } |
| 258 | |
| 259 | $this->simple_query('COMMIT'); |
| 260 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 261 | return TRUE; |
| 262 | } |
| 263 | |
| 264 | // -------------------------------------------------------------------- |
| 265 | |
| 266 | /** |
| 267 | * Rollback Transaction |
| 268 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | * @return bool |
| 270 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 271 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 274 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | { |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
| 279 | $this->simple_query('ROLLBACK'); |
| 280 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 281 | return TRUE; |
| 282 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 283 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | // -------------------------------------------------------------------- |
| 285 | |
| 286 | /** |
| 287 | * Escape String |
| 288 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 290 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | * @return string |
| 292 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 293 | public function escape_str($str, $like = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | if (is_array($str)) |
| 296 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 297 | foreach ($str as $key => $val) |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 298 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 299 | $str[$key] = $this->escape_str($val, $like); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 300 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 302 | return $str; |
| 303 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | |
Andrey Andreev | f6a4114 | 2012-03-12 15:34:10 +0200 | [diff] [blame] | 305 | $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 306 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 307 | // escape LIKE condition wildcards |
| 308 | if ($like === TRUE) |
| 309 | { |
Andrey Andreev | 21cb2d3 | 2012-05-25 01:01:06 +0300 | [diff] [blame] | 310 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 311 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 312 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 313 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 314 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 315 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | // -------------------------------------------------------------------- |
| 319 | |
| 320 | /** |
| 321 | * Affected Rows |
| 322 | * |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 323 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 325 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | { |
| 327 | return @mysql_affected_rows($this->conn_id); |
| 328 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | // -------------------------------------------------------------------- |
| 331 | |
| 332 | /** |
| 333 | * Insert ID |
| 334 | * |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 335 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 337 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | { |
| 339 | return @mysql_insert_id($this->conn_id); |
| 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | * List table query |
| 346 | * |
| 347 | * Generates a platform-specific query string so that the table names can be fetched |
| 348 | * |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 349 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | * @return string |
| 351 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 352 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
Andrey Andreev | 473130a | 2012-06-24 02:51:18 +0300 | [diff] [blame] | 354 | $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 356 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 358 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | return $sql; |
| 362 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 363 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | // -------------------------------------------------------------------- |
| 365 | |
| 366 | /** |
| 367 | * Show column query |
| 368 | * |
| 369 | * Generates a platform-specific query string so that the column names can be fetched |
| 370 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | * @param string the table name |
| 372 | * @return string |
| 373 | */ |
Andrey Andreev | 473130a | 2012-06-24 02:51:18 +0300 | [diff] [blame] | 374 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 376 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 382 | * Returns an object with field data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | * @param string the table name |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 385 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | */ |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 387 | public function field_data($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 389 | if ($table === '') |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 390 | { |
| 391 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
| 392 | } |
| 393 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 394 | $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 395 | $query = $query->result_object(); |
| 396 | |
| 397 | $retval = array(); |
| 398 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 399 | { |
| 400 | preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches); |
| 401 | |
| 402 | $retval[$i] = new stdClass(); |
| 403 | $retval[$i]->name = $query[$i]->Field; |
| 404 | $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1]; |
| 405 | $retval[$i]->default = $query[$i]->Default; |
| 406 | $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]); |
| 407 | $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); |
| 408 | } |
| 409 | |
| 410 | return $retval; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // -------------------------------------------------------------------- |
| 414 | |
| 415 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 416 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 418 | * Returns an array containing code and message of the last |
| 419 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 421 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 423 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 425 | return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | // -------------------------------------------------------------------- |
| 429 | |
| 430 | /** |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 431 | * Update_Batch statement |
| 432 | * |
| 433 | * Generates a platform-specific batch update string from the supplied data |
| 434 | * |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 435 | * @param string the table name |
| 436 | * @param array the update data |
| 437 | * @param array the where clause |
| 438 | * @return string |
| 439 | */ |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 440 | protected function _update_batch($table, $values, $index, $where = NULL) |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 441 | { |
| 442 | $ids = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 443 | foreach ($values as $key => $val) |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 444 | { |
| 445 | $ids[] = $val[$index]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 446 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 447 | foreach (array_keys($val) as $field) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 449 | if ($field !== $index) |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 450 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 451 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 455 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 456 | $cases = ''; |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 457 | foreach ($final as $k => $v) |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 458 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 459 | $cases .= $k." = CASE \n" |
| 460 | .implode("\n", $v)."\n" |
| 461 | .'ELSE '.$k.' END, '; |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 462 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 464 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 465 | .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 466 | .$index.' IN('.implode(',', $ids).')'; |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // -------------------------------------------------------------------- |
| 470 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | * Close DB Connection |
| 473 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | * @return void |
| 475 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 476 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 478 | @mysql_close($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | /* End of file mysql_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 484 | /* Location: ./system/database/drivers/mysql/mysql_driver.php */ |