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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.3.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 41 | * MySQLi Database Adapter Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * |
| 43 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 44 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | * class is being used or not. |
| 46 | * |
| 47 | * @package CodeIgniter |
| 48 | * @subpackage Drivers |
| 49 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 50 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 51 | * @link https://codeigniter.com/user_guide/database/ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | */ |
| 53 | class CI_DB_mysqli_driver extends CI_DB { |
| 54 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 55 | /** |
| 56 | * Database driver |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 60 | public $dbdriver = 'mysqli'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 63 | * Compression flag |
| 64 | * |
| 65 | * @var bool |
| 66 | */ |
| 67 | public $compress = FALSE; |
| 68 | |
| 69 | /** |
| 70 | * DELETE hack flag |
| 71 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | * Whether to use the MySQL "delete hack" which allows the number |
| 73 | * of affected rows to be shown. Uses a preg_replace when enabled, |
| 74 | * adding a bit more processing to all queries. |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 75 | * |
| 76 | * @var bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 78 | public $delete_hack = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | |
Andrey Andreev | f8f14f3 | 2013-12-10 11:32:32 +0200 | [diff] [blame] | 80 | /** |
| 81 | * Strict ON flag |
| 82 | * |
| 83 | * Whether we're running in strict SQL mode. |
| 84 | * |
| 85 | * @var bool |
| 86 | */ |
Andrey Andreev | c83e894 | 2016-01-07 17:27:39 +0200 | [diff] [blame] | 87 | public $stricton; |
Andrey Andreev | f8f14f3 | 2013-12-10 11:32:32 +0200 | [diff] [blame] | 88 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 89 | // -------------------------------------------------------------------- |
| 90 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 92 | * Identifier escape character |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 94 | * @var string |
| 95 | */ |
| 96 | protected $_escape_char = '`'; |
| 97 | |
| 98 | // -------------------------------------------------------------------- |
| 99 | |
| 100 | /** |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 101 | * MySQLi object |
| 102 | * |
| 103 | * Has to be preserved without being assigned to $conn_id. |
| 104 | * |
| 105 | * @var MySQLi |
| 106 | */ |
| 107 | protected $_mysqli; |
| 108 | |
| 109 | // -------------------------------------------------------------------- |
| 110 | |
| 111 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 112 | * Database connection |
| 113 | * |
| 114 | * @param bool $persistent |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 115 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | */ |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 117 | public function db_connect($persistent = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | { |
Andrey Andreev | 1fb0cc7 | 2014-12-17 19:08:35 +0200 | [diff] [blame] | 119 | // Do we have a socket path? |
| 120 | if ($this->hostname[0] === '/') |
| 121 | { |
| 122 | $hostname = NULL; |
| 123 | $port = NULL; |
| 124 | $socket = $this->hostname; |
| 125 | } |
| 126 | else |
| 127 | { |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 128 | $hostname = ($persistent === TRUE) |
Andrey Andreev | 1fb0cc7 | 2014-12-17 19:08:35 +0200 | [diff] [blame] | 129 | ? 'p:'.$this->hostname : $this->hostname; |
| 130 | $port = empty($this->port) ? NULL : $this->port; |
| 131 | $socket = NULL; |
| 132 | } |
| 133 | |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 134 | $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0; |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 135 | $this->_mysqli = mysqli_init(); |
Michiel Vugteveen | c27721f | 2012-08-20 18:34:24 +0200 | [diff] [blame] | 136 | |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 137 | $this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10); |
Andrey Andreev | 9305a8b | 2015-01-26 12:09:58 +0200 | [diff] [blame] | 138 | |
Andrey Andreev | c83e894 | 2016-01-07 17:27:39 +0200 | [diff] [blame] | 139 | if (isset($this->stricton)) |
Andrey Andreev | f8f14f3 | 2013-12-10 11:32:32 +0200 | [diff] [blame] | 140 | { |
Andrey Andreev | c83e894 | 2016-01-07 17:27:39 +0200 | [diff] [blame] | 141 | if ($this->stricton) |
| 142 | { |
| 143 | $this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode = CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")'); |
| 144 | } |
Andrey Andreev | 0a9cc83 | 2016-01-07 17:52:20 +0200 | [diff] [blame] | 145 | else |
Andrey Andreev | c83e894 | 2016-01-07 17:27:39 +0200 | [diff] [blame] | 146 | { |
| 147 | $this->_mysqli->options(MYSQLI_INIT_COMMAND, |
| 148 | 'SET SESSION sql_mode = |
| 149 | REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( |
| 150 | @@sql_mode, |
| 151 | "STRICT_ALL_TABLES,", ""), |
| 152 | ",STRICT_ALL_TABLES", ""), |
| 153 | "STRICT_ALL_TABLES", ""), |
| 154 | "STRICT_TRANS_TABLES,", ""), |
| 155 | ",STRICT_TRANS_TABLES", ""), |
| 156 | "STRICT_TRANS_TABLES", "")' |
| 157 | ); |
| 158 | } |
Andrey Andreev | f8f14f3 | 2013-12-10 11:32:32 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Andrey Andreev | 76e643e | 2015-07-16 13:14:49 +0300 | [diff] [blame] | 161 | if (is_array($this->encrypt)) |
Tim Nolte | a0f1872 | 2015-06-05 13:40:18 -0400 | [diff] [blame] | 162 | { |
Andrey Andreev | 76e643e | 2015-07-16 13:14:49 +0300 | [diff] [blame] | 163 | $ssl = array(); |
| 164 | empty($this->encrypt['ssl_key']) OR $ssl['key'] = $this->encrypt['ssl_key']; |
| 165 | empty($this->encrypt['ssl_cert']) OR $ssl['cert'] = $this->encrypt['ssl_cert']; |
| 166 | empty($this->encrypt['ssl_ca']) OR $ssl['ca'] = $this->encrypt['ssl_ca']; |
| 167 | empty($this->encrypt['ssl_capath']) OR $ssl['capath'] = $this->encrypt['ssl_capath']; |
| 168 | empty($this->encrypt['ssl_cipher']) OR $ssl['cipher'] = $this->encrypt['ssl_cipher']; |
Tim Nolte | ced557b | 2015-06-18 15:28:43 -0400 | [diff] [blame] | 169 | |
Andrey Andreev | 76e643e | 2015-07-16 13:14:49 +0300 | [diff] [blame] | 170 | if ( ! empty($ssl)) |
| 171 | { |
Andrey Andreev | 89576a8 | 2016-01-07 14:39:04 +0200 | [diff] [blame] | 172 | if (isset($this->encrypt['ssl_verify'])) |
Andrey Andreev | cfc9e77 | 2015-07-16 16:17:27 +0300 | [diff] [blame] | 173 | { |
Andrey Andreev | 89576a8 | 2016-01-07 14:39:04 +0200 | [diff] [blame] | 174 | if ($this->encrypt['ssl_verify']) |
| 175 | { |
| 176 | defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT') && $this->_mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, TRUE); |
| 177 | } |
| 178 | // Apparently (when it exists), setting MYSQLI_OPT_SSL_VERIFY_SERVER_CERT |
| 179 | // to FALSE didn't do anything, so PHP 5.6.16 introduced yet another |
| 180 | // constant ... |
| 181 | // |
| 182 | // https://secure.php.net/ChangeLog-5.php#5.6.16 |
| 183 | // https://bugs.php.net/bug.php?id=68344 |
| 184 | elseif (defined('MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT')) |
| 185 | { |
Andrey Andreev | 676072e | 2016-09-09 15:33:09 +0300 | [diff] [blame] | 186 | $client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT; |
Andrey Andreev | 89576a8 | 2016-01-07 14:39:04 +0200 | [diff] [blame] | 187 | } |
Andrey Andreev | cfc9e77 | 2015-07-16 16:17:27 +0300 | [diff] [blame] | 188 | } |
| 189 | |
Andrey Andreev | 76e643e | 2015-07-16 13:14:49 +0300 | [diff] [blame] | 190 | $client_flags |= MYSQLI_CLIENT_SSL; |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 191 | $this->_mysqli->ssl_set( |
Andrey Andreev | 76e643e | 2015-07-16 13:14:49 +0300 | [diff] [blame] | 192 | isset($ssl['key']) ? $ssl['key'] : NULL, |
| 193 | isset($ssl['cert']) ? $ssl['cert'] : NULL, |
| 194 | isset($ssl['ca']) ? $ssl['ca'] : NULL, |
| 195 | isset($ssl['capath']) ? $ssl['capath'] : NULL, |
| 196 | isset($ssl['cipher']) ? $ssl['cipher'] : NULL |
| 197 | ); |
| 198 | } |
Tim Nolte | a0f1872 | 2015-06-05 13:40:18 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 201 | if ($this->_mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags)) |
Tim Nolte | a0f1872 | 2015-06-05 13:40:18 -0400 | [diff] [blame] | 202 | { |
Andrey Andreev | 76e643e | 2015-07-16 13:14:49 +0300 | [diff] [blame] | 203 | // Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails |
Andrey Andreev | 9194b49 | 2015-07-16 14:23:51 +0300 | [diff] [blame] | 204 | if ( |
| 205 | ($client_flags & MYSQLI_CLIENT_SSL) |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 206 | && version_compare($this->_mysqli->client_info, '5.7.3', '<=') |
| 207 | && empty($this->_mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value) |
Andrey Andreev | 9194b49 | 2015-07-16 14:23:51 +0300 | [diff] [blame] | 208 | ) |
Tim Nolte | a0f1872 | 2015-06-05 13:40:18 -0400 | [diff] [blame] | 209 | { |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 210 | $this->_mysqli->close(); |
Andrey Andreev | 9194b49 | 2015-07-16 14:23:51 +0300 | [diff] [blame] | 211 | $message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!'; |
| 212 | log_message('error', $message); |
Andrey Andreev | ea07352 | 2017-03-14 18:42:12 +0200 | [diff] [blame] | 213 | return ($this->db_debug) ? $this->display_error($message, '', TRUE) : FALSE; |
Tim Nolte | a0f1872 | 2015-06-05 13:40:18 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 216 | return $this->_mysqli; |
Tim Nolte | a0f1872 | 2015-06-05 13:40:18 -0400 | [diff] [blame] | 217 | } |
Tim Nolte | a0f1872 | 2015-06-05 13:40:18 -0400 | [diff] [blame] | 218 | |
| 219 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 225 | * Reconnect |
| 226 | * |
| 227 | * Keep / reestablish the db connection if no queries have been |
| 228 | * sent for a length of time exceeding the server's idle timeout |
| 229 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 230 | * @return void |
| 231 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 232 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 233 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 234 | if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE) |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 235 | { |
| 236 | $this->conn_id = FALSE; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // -------------------------------------------------------------------- |
| 241 | |
| 242 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | * Select the database |
| 244 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 245 | * @param string $database |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 246 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 248 | public function db_select($database = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 250 | if ($database === '') |
| 251 | { |
| 252 | $database = $this->database; |
| 253 | } |
| 254 | |
mdunisch | d3ddf97 | 2014-05-07 15:00:40 +0200 | [diff] [blame] | 255 | if ($this->conn_id->select_db($database)) |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 256 | { |
| 257 | $this->database = $database; |
Andrey Andreev | ed6f010 | 2016-06-08 11:06:59 +0300 | [diff] [blame] | 258 | $this->data_cache = array(); |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 259 | return TRUE; |
| 260 | } |
| 261 | |
| 262 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // -------------------------------------------------------------------- |
| 266 | |
| 267 | /** |
| 268 | * Set client character set |
| 269 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 270 | * @param string $charset |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 271 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | */ |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 273 | protected function _db_set_charset($charset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 275 | return $this->conn_id->set_charset($charset); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 281 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | * @return string |
| 284 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 285 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | { |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame] | 287 | if (isset($this->data_cache['version'])) |
| 288 | { |
| 289 | return $this->data_cache['version']; |
| 290 | } |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame] | 291 | |
| 292 | return $this->data_cache['version'] = $this->conn_id->server_info; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // -------------------------------------------------------------------- |
| 296 | |
| 297 | /** |
| 298 | * Execute the query |
| 299 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 300 | * @param string $sql an SQL query |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 301 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 303 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | { |
mdunisch | d3ddf97 | 2014-05-07 15:00:40 +0200 | [diff] [blame] | 305 | return $this->conn_id->query($this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | // -------------------------------------------------------------------- |
| 309 | |
| 310 | /** |
| 311 | * Prep the query |
| 312 | * |
| 313 | * If needed, each database adapter can prep the query string |
| 314 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 315 | * @param string $sql an SQL query |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 318 | protected function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 320 | // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack |
| 321 | // modifies the query so that it a proper number of affected rows is returned. |
| 322 | 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] | 323 | { |
Andrey Andreev | be99966 | 2013-01-10 11:40:09 +0200 | [diff] [blame] | 324 | return trim($sql).' WHERE 1=1'; |
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 | return $sql; |
| 328 | } |
| 329 | |
| 330 | // -------------------------------------------------------------------- |
| 331 | |
| 332 | /** |
| 333 | * Begin Transaction |
| 334 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | * @return bool |
| 336 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 337 | protected function _trans_begin() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | { |
Andrey Andreev | 3c5ec85 | 2013-09-13 15:01:08 +0300 | [diff] [blame] | 339 | $this->conn_id->autocommit(FALSE); |
| 340 | return is_php('5.5') |
| 341 | ? $this->conn_id->begin_transaction() |
| 342 | : $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // -------------------------------------------------------------------- |
| 346 | |
| 347 | /** |
| 348 | * Commit Transaction |
| 349 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | * @return bool |
| 351 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 352 | protected function _trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
Andrey Andreev | 3c5ec85 | 2013-09-13 15:01:08 +0300 | [diff] [blame] | 354 | if ($this->conn_id->commit()) |
| 355 | { |
| 356 | $this->conn_id->autocommit(TRUE); |
| 357 | return TRUE; |
| 358 | } |
| 359 | |
| 360 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
| 366 | * Rollback Transaction |
| 367 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 368 | * @return bool |
| 369 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 370 | protected function _trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | { |
Andrey Andreev | 3c5ec85 | 2013-09-13 15:01:08 +0300 | [diff] [blame] | 372 | if ($this->conn_id->rollback()) |
| 373 | { |
| 374 | $this->conn_id->autocommit(TRUE); |
| 375 | return TRUE; |
| 376 | } |
| 377 | |
| 378 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // -------------------------------------------------------------------- |
| 382 | |
| 383 | /** |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 384 | * Platform-dependent string escape |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | * |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 386 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | * @return string |
| 388 | */ |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 389 | protected function _escape_str($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | { |
Andrey Andreev | 62fad28 | 2014-06-19 15:25:40 +0300 | [diff] [blame] | 391 | return $this->conn_id->real_escape_string($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | // -------------------------------------------------------------------- |
| 395 | |
| 396 | /** |
| 397 | * Affected Rows |
| 398 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 399 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 401 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 403 | return $this->conn_id->affected_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | // -------------------------------------------------------------------- |
| 407 | |
| 408 | /** |
| 409 | * Insert ID |
| 410 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 411 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 413 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 415 | return $this->conn_id->insert_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // -------------------------------------------------------------------- |
| 419 | |
| 420 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | * List table query |
| 422 | * |
| 423 | * Generates a platform-specific query string so that the table names can be fetched |
| 424 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 425 | * @param bool $prefix_limit |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | * @return string |
| 427 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 428 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | { |
Andrey Andreev | 473130a | 2012-06-24 02:51:18 +0300 | [diff] [blame] | 430 | $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 432 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 434 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | return $sql; |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------- |
| 441 | |
| 442 | /** |
| 443 | * Show column query |
| 444 | * |
| 445 | * Generates a platform-specific query string so that the column names can be fetched |
| 446 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 447 | * @param string $table |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | * @return string |
| 449 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 450 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 452 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // -------------------------------------------------------------------- |
| 456 | |
| 457 | /** |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 458 | * Returns an object with field data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 460 | * @param string $table |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 461 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | */ |
Andrey Andreev | 5350f05 | 2015-01-12 12:33:37 +0200 | [diff] [blame] | 463 | public function field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | { |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 465 | if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE) |
| 466 | { |
| 467 | return FALSE; |
| 468 | } |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 469 | $query = $query->result_object(); |
| 470 | |
| 471 | $retval = array(); |
| 472 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 473 | { |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 474 | $retval[$i] = new stdClass(); |
| 475 | $retval[$i]->name = $query[$i]->Field; |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 476 | |
| 477 | sscanf($query[$i]->Type, '%[a-z](%d)', |
| 478 | $retval[$i]->type, |
| 479 | $retval[$i]->max_length |
| 480 | ); |
| 481 | |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 482 | $retval[$i]->default = $query[$i]->Default; |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 483 | $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); |
| 484 | } |
| 485 | |
| 486 | return $retval; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // -------------------------------------------------------------------- |
| 490 | |
| 491 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 492 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 494 | * Returns an array containing code and message of the last |
Claudio Galdiolo | b993b4b | 2015-01-28 12:22:38 -0500 | [diff] [blame] | 495 | * database error that has occurred. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 497 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 499 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | { |
Andrey Andreev | f3ddda7 | 2015-12-30 21:38:54 +0200 | [diff] [blame] | 501 | if ( ! empty($this->_mysqli->connect_errno)) |
Andrey Andreev | dbad54e | 2012-10-05 21:53:32 +0300 | [diff] [blame] | 502 | { |
| 503 | return array( |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 504 | 'code' => $this->_mysqli->connect_errno, |
| 505 | 'message' => $this->_mysqli->connect_error |
Andrey Andreev | dbad54e | 2012-10-05 21:53:32 +0300 | [diff] [blame] | 506 | ); |
| 507 | } |
| 508 | |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 509 | return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | // -------------------------------------------------------------------- |
| 513 | |
| 514 | /** |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 515 | * FROM tables |
| 516 | * |
| 517 | * Groups tables in FROM clauses if needed, so there is no confusion |
| 518 | * about operator precedence. |
| 519 | * |
| 520 | * @return string |
| 521 | */ |
| 522 | protected function _from_tables() |
| 523 | { |
Andrey Andreev | fce9abe | 2012-10-09 11:37:00 +0300 | [diff] [blame] | 524 | if ( ! empty($this->qb_join) && count($this->qb_from) > 1) |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 525 | { |
| 526 | return '('.implode(', ', $this->qb_from).')'; |
| 527 | } |
| 528 | |
| 529 | return implode(', ', $this->qb_from); |
| 530 | } |
| 531 | |
| 532 | // -------------------------------------------------------------------- |
| 533 | |
| 534 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | * Close DB Connection |
| 536 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | * @return void |
| 538 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 539 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 541 | $this->conn_id->close(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | } |