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