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