blob: d374e01740b45bd599cd42652f9bb337bbb21666 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev1ff49e02012-01-27 11:30:41 +02008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreev1ff49e02012-01-27 11:30:41 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
Andrey Andreev1ff49e02012-01-27 11:30:41 +020041 * MySQLi Database Adapter Class
Derek Allard2067d1a2008-11-13 22:59:24 +000042 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
53class CI_DB_mysqli_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020060 public $dbdriver = 'mysqli';
Derek Allard2067d1a2008-11-13 22:59:24 +000061
62 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020063 * Compression flag
64 *
65 * @var bool
66 */
67 public $compress = FALSE;
68
69 /**
70 * DELETE hack flag
71 *
Derek Allard2067d1a2008-11-13 22:59:24 +000072 * 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 Andreeva24e52e2012-11-02 03:54:12 +020075 *
76 * @var bool
Barry Mienydd671972010-10-04 16:33:58 +020077 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020078 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000079
Andrey Andreevf8f14f32013-12-10 11:32:32 +020080 /**
81 * Strict ON flag
82 *
83 * Whether we're running in strict SQL mode.
84 *
85 * @var bool
86 */
Andrey Andreevc83e8942016-01-07 17:27:39 +020087 public $stricton;
Andrey Andreevf8f14f32013-12-10 11:32:32 +020088
Andrey Andreeva24e52e2012-11-02 03:54:12 +020089 // --------------------------------------------------------------------
90
Derek Allard2067d1a2008-11-13 22:59:24 +000091 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020092 * Identifier escape character
Derek Allard2067d1a2008-11-13 22:59:24 +000093 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020094 * @var string
95 */
96 protected $_escape_char = '`';
97
98 // --------------------------------------------------------------------
99
100 /**
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200101 * 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 Andreeva24e52e2012-11-02 03:54:12 +0200112 * Database connection
113 *
114 * @param bool $persistent
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200115 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200116 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300117 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Andrey Andreev1fb0cc72014-12-17 19:08:35 +0200119 // 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 Andreeva8382792016-07-28 16:40:12 +0300128 $hostname = ($persistent === TRUE)
Andrey Andreev1fb0cc72014-12-17 19:08:35 +0200129 ? 'p:'.$this->hostname : $this->hostname;
130 $port = empty($this->port) ? NULL : $this->port;
131 $socket = NULL;
132 }
133
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300134 $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200135 $this->_mysqli = mysqli_init();
Michiel Vugteveenc27721f2012-08-20 18:34:24 +0200136
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200137 $this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
Andrey Andreev9305a8b2015-01-26 12:09:58 +0200138
Andrey Andreevc83e8942016-01-07 17:27:39 +0200139 if (isset($this->stricton))
Andrey Andreevf8f14f32013-12-10 11:32:32 +0200140 {
Andrey Andreevc83e8942016-01-07 17:27:39 +0200141 if ($this->stricton)
142 {
143 $this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode = CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")');
144 }
Andrey Andreev0a9cc832016-01-07 17:52:20 +0200145 else
Andrey Andreevc83e8942016-01-07 17:27:39 +0200146 {
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 Andreevf8f14f32013-12-10 11:32:32 +0200159 }
160
Andrey Andreev76e643e2015-07-16 13:14:49 +0300161 if (is_array($this->encrypt))
Tim Noltea0f18722015-06-05 13:40:18 -0400162 {
Andrey Andreev76e643e2015-07-16 13:14:49 +0300163 $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 Nolteced557b2015-06-18 15:28:43 -0400169
Andrey Andreev4ea5cf12018-08-22 14:23:17 +0300170 if (isset($this->encrypt['ssl_verify']))
171 {
172 $client_flags |= MYSQLI_CLIENT_SSL;
173
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 {
186 $client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
187 }
188 }
189
Andrey Andreev76e643e2015-07-16 13:14:49 +0300190 if ( ! empty($ssl))
191 {
192 $client_flags |= MYSQLI_CLIENT_SSL;
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200193 $this->_mysqli->ssl_set(
Andrey Andreev76e643e2015-07-16 13:14:49 +0300194 isset($ssl['key']) ? $ssl['key'] : NULL,
195 isset($ssl['cert']) ? $ssl['cert'] : NULL,
196 isset($ssl['ca']) ? $ssl['ca'] : NULL,
197 isset($ssl['capath']) ? $ssl['capath'] : NULL,
198 isset($ssl['cipher']) ? $ssl['cipher'] : NULL
199 );
200 }
Tim Noltea0f18722015-06-05 13:40:18 -0400201 }
202
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200203 if ($this->_mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags))
Tim Noltea0f18722015-06-05 13:40:18 -0400204 {
Andrey Andreev76e643e2015-07-16 13:14:49 +0300205 // Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
Andrey Andreev9194b492015-07-16 14:23:51 +0300206 if (
207 ($client_flags & MYSQLI_CLIENT_SSL)
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200208 && version_compare($this->_mysqli->client_info, '5.7.3', '<=')
209 && empty($this->_mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)
Andrey Andreev9194b492015-07-16 14:23:51 +0300210 )
Tim Noltea0f18722015-06-05 13:40:18 -0400211 {
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200212 $this->_mysqli->close();
Andrey Andreev9194b492015-07-16 14:23:51 +0300213 $message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!';
214 log_message('error', $message);
Andrey Andreevea073522017-03-14 18:42:12 +0200215 return ($this->db_debug) ? $this->display_error($message, '', TRUE) : FALSE;
Tim Noltea0f18722015-06-05 13:40:18 -0400216 }
217
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200218 return $this->_mysqli;
Tim Noltea0f18722015-06-05 13:40:18 -0400219 }
Tim Noltea0f18722015-06-05 13:40:18 -0400220
221 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223
224 // --------------------------------------------------------------------
225
226 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000227 * Reconnect
228 *
229 * Keep / reestablish the db connection if no queries have been
230 * sent for a length of time exceeding the server's idle timeout
231 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000232 * @return void
233 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200234 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000235 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200236 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000237 {
238 $this->conn_id = FALSE;
239 }
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * Select the database
246 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200247 * @param string $database
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200248 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200249 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200250 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200252 if ($database === '')
253 {
254 $database = $this->database;
255 }
256
mdunischd3ddf972014-05-07 15:00:40 +0200257 if ($this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200258 {
259 $this->database = $database;
Andrey Andreeved6f0102016-06-08 11:06:59 +0300260 $this->data_cache = array();
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200261 return TRUE;
262 }
263
264 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Set client character set
271 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200272 * @param string $charset
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200273 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200275 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200277 return $this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279
280 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200283 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @return string
286 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200287 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200289 if (isset($this->data_cache['version']))
290 {
291 return $this->data_cache['version'];
292 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200293
294 return $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * Execute the query
301 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200302 * @param string $sql an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200303 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200304 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200305 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
mdunischd3ddf972014-05-07 15:00:40 +0200307 return $this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // --------------------------------------------------------------------
311
312 /**
313 * Prep the query
314 *
315 * If needed, each database adapter can prep the query string
316 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200317 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200320 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200322 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
323 // modifies the query so that it a proper number of affected rows is returned.
324 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreevbe999662013-01-10 11:40:09 +0200326 return trim($sql).' WHERE 1=1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 return $sql;
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Begin Transaction
336 *
Barry Mienydd671972010-10-04 16:33:58 +0200337 * @return bool
338 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300339 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300341 $this->conn_id->autocommit(FALSE);
342 return is_php('5.5')
343 ? $this->conn_id->begin_transaction()
344 : $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * Commit Transaction
351 *
Barry Mienydd671972010-10-04 16:33:58 +0200352 * @return bool
353 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300354 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300356 if ($this->conn_id->commit())
357 {
358 $this->conn_id->autocommit(TRUE);
359 return TRUE;
360 }
361
362 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Rollback Transaction
369 *
Barry Mienydd671972010-10-04 16:33:58 +0200370 * @return bool
371 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300372 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300374 if ($this->conn_id->rollback())
375 {
376 $this->conn_id->autocommit(TRUE);
377 return TRUE;
378 }
379
380 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382
383 // --------------------------------------------------------------------
384
385 /**
Andrey Andreev71d8f722017-01-17 12:01:00 +0200386 * Platform-dependent string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200388 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @return string
390 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200391 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300393 return $this->conn_id->real_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // --------------------------------------------------------------------
397
398 /**
399 * Affected Rows
400 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200401 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200403 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200405 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // --------------------------------------------------------------------
409
410 /**
411 * Insert ID
412 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200413 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200415 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200417 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * List table query
424 *
425 * Generates a platform-specific query string so that the table names can be fetched
426 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200427 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @return string
429 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200430 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300432 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200433
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100434 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200436 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 return $sql;
440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Show column query
446 *
447 * Generates a platform-specific query string so that the column names can be fetched
448 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200449 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 * @return string
451 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200452 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200454 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
457 // --------------------------------------------------------------------
458
459 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200460 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200462 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200463 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200465 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200467 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
468 {
469 return FALSE;
470 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200471 $query = $query->result_object();
472
473 $retval = array();
474 for ($i = 0, $c = count($query); $i < $c; $i++)
475 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200476 $retval[$i] = new stdClass();
477 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200478
479 sscanf($query[$i]->Type, '%[a-z](%d)',
480 $retval[$i]->type,
481 $retval[$i]->max_length
482 );
483
Andrey Andreev3722e502012-03-03 15:04:38 +0200484 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200485 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
486 }
487
488 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490
491 // --------------------------------------------------------------------
492
493 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200494 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200496 * Returns an array containing code and message of the last
Claudio Galdiolob993b4b2015-01-28 12:22:38 -0500497 * database error that has occurred.
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200499 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200501 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Andrey Andreevf3ddda72015-12-30 21:38:54 +0200503 if ( ! empty($this->_mysqli->connect_errno))
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300504 {
505 return array(
Andrey Andreeva8382792016-07-28 16:40:12 +0300506 'code' => $this->_mysqli->connect_errno,
507 'message' => $this->_mysqli->connect_error
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300508 );
509 }
510
Andrey Andreev992f1752012-03-19 16:58:43 +0200511 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300517 * FROM tables
518 *
519 * Groups tables in FROM clauses if needed, so there is no confusion
520 * about operator precedence.
521 *
522 * @return string
523 */
524 protected function _from_tables()
525 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300526 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300527 {
528 return '('.implode(', ', $this->qb_from).')';
529 }
530
531 return implode(', ', $this->qb_from);
532 }
533
534 // --------------------------------------------------------------------
535
536 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * Close DB Connection
538 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * @return void
540 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300541 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200543 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546}