blob: 26b2a8a0913654b2356a89d13d28f0974653aed4 [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 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
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 /**
Tim Noltea0f18722015-06-05 13:40:18 -040063 * Database options list
64 *
65 * Used to set various database options and values.
66 *
67 * @example http://php.net/manual/en/mysqli.options.php Allows to set options not built-in/handled by CI.
68 *
69 * <code>
70 * array( MYSQLI_OPT_SSL_VERIFY_SERVER_CERT => true );
71 * </code>
72 *
73 * @var array
74 */
75 public $db_options = array();
76
77 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020078 * Compression flag
79 *
80 * @var bool
81 */
82 public $compress = FALSE;
83
84 /**
85 * DELETE hack flag
86 *
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * Whether to use the MySQL "delete hack" which allows the number
88 * of affected rows to be shown. Uses a preg_replace when enabled,
89 * adding a bit more processing to all queries.
Andrey Andreeva24e52e2012-11-02 03:54:12 +020090 *
91 * @var bool
Barry Mienydd671972010-10-04 16:33:58 +020092 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020093 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000094
Andrey Andreevf8f14f32013-12-10 11:32:32 +020095 /**
96 * Strict ON flag
97 *
98 * Whether we're running in strict SQL mode.
99 *
100 * @var bool
101 */
102 public $stricton = FALSE;
103
Tim Noltea0f18722015-06-05 13:40:18 -0400104 /**
Tim Noltec09ab9d2015-06-08 10:40:26 -0400105 * The path name to the key file.
106 *
Tim Noltea0f18722015-06-05 13:40:18 -0400107 * @see http://php.net/manual/en/mysqli.ssl-set.php Documentation for MySQLi
108 *
109 * @var string
110 */
111 public $ssl_key = '';
112
113 /**
Tim Noltec09ab9d2015-06-08 10:40:26 -0400114 * The path name to the certificate file.
115 *
Tim Noltea0f18722015-06-05 13:40:18 -0400116 * @see http://php.net/manual/en/mysqli.ssl-set.php Documentation for MySQLi
117 *
118 * @var string
119 */
120 public $ssl_cert = '';
121
122 /**
Tim Noltec09ab9d2015-06-08 10:40:26 -0400123 * The path name to the certificate authority file.
124 *
Tim Noltea0f18722015-06-05 13:40:18 -0400125 * @see http://php.net/manual/en/mysqli.ssl-set.php Documentation for MySQLi
126 *
127 * @var string
128 */
129 public $ssl_ca = '';
130
131 /**
Tim Noltec09ab9d2015-06-08 10:40:26 -0400132 * The pathname to a directory that contains trusted SSL CA certificates in PEM format.
133 *
Tim Noltea0f18722015-06-05 13:40:18 -0400134 * @see http://php.net/manual/en/mysqli.ssl-set.php Documentation for MySQLi
135 *
136 * @var string
137 */
138 public $ssl_capath = '';
139
140 /**
Tim Noltec09ab9d2015-06-08 10:40:26 -0400141 * A list of allowable ciphers to use for SSL encryption.
142 *
Tim Noltea0f18722015-06-05 13:40:18 -0400143 * @see http://php.net/manual/en/mysqli.ssl-set.php Documentation for MySQLi
144 *
145 * @var string
146 */
147 public $ssl_cipher = '';
148
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200149 // --------------------------------------------------------------------
150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200152 * Identifier escape character
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200154 * @var string
155 */
156 protected $_escape_char = '`';
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Database connection
162 *
163 * @param bool $persistent
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200164 * @return object
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300165 * @todo SSL support
Barry Mienydd671972010-10-04 16:33:58 +0200166 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300167 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev1fb0cc72014-12-17 19:08:35 +0200169 // Do we have a socket path?
170 if ($this->hostname[0] === '/')
171 {
172 $hostname = NULL;
173 $port = NULL;
174 $socket = $this->hostname;
175 }
176 else
177 {
178 // Persistent connection support was added in PHP 5.3.0
179 $hostname = ($persistent === TRUE && is_php('5.3'))
180 ? 'p:'.$this->hostname : $this->hostname;
181 $port = empty($this->port) ? NULL : $this->port;
182 $socket = NULL;
183 }
184
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300185 $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
Andrey Andreev37c85d72012-10-13 17:08:45 +0300186 $mysqli = mysqli_init();
Michiel Vugteveenc27721f2012-08-20 18:34:24 +0200187
Andrey Andreev9305a8b2015-01-26 12:09:58 +0200188 $mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
Andrey Andreev9305a8b2015-01-26 12:09:58 +0200189
Andrey Andreevf8f14f32013-12-10 11:32:32 +0200190 if ($this->stricton)
191 {
192 $mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode="STRICT_ALL_TABLES"');
193 }
194
Tim Noltea0f18722015-06-05 13:40:18 -0400195 foreach ($this->db_options AS $key => $value)
196 {
197 $mysqli->options($key, $value);
198 }
199
200 if ($this->encrypt === TRUE)
201 {
202 $mysqli->ssl_set($this->ssl_key, $this->ssl_cert, $this->ssl_ca, $this->ssl_capath, $this->ssl_cipher);
203 $client_flags |= MYSQLI_CLIENT_SSL;
204 }
205
206 $connected = @$mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags);
207
208 if ($connected)
209 {
210 // If SSL was requested we want to do some checking and log an error if an SSL connection wasn't established.
211 if ($this->encrypt === TRUE)
212 {
213 $res = $mysqli->query("SHOW STATUS LIKE 'ssl_cipher';");
214 $ssl_status = $res->fetch_row();
215
216 if ($ssl_status[1] == '')
217 {
218 log_message('error',
219 "Problem With MySQLi SSL: An SSL connection was requested but the resulting connection is not using SSL!");
220 }
221 }
222
223 return $mysqli;
224 }
225 else
226 {
227 if ($mysqli->connect_errno)
228 {
229 log_message('error',
230 'msqli connect failed, error: ' . mysqli_connect_error() . " | " . $mysqli->connect_error . " | " . $mysqli->connect_errno);
231 }
232 }
233
234 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 // --------------------------------------------------------------------
238
239 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000240 * Reconnect
241 *
242 * Keep / reestablish the db connection if no queries have been
243 * sent for a length of time exceeding the server's idle timeout
244 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000245 * @return void
246 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200247 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000248 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200249 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000250 {
251 $this->conn_id = FALSE;
252 }
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * Select the database
259 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200260 * @param string $database
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200261 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200263 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200265 if ($database === '')
266 {
267 $database = $this->database;
268 }
269
mdunischd3ddf972014-05-07 15:00:40 +0200270 if ($this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200271 {
272 $this->database = $database;
273 return TRUE;
274 }
275
276 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Set client character set
283 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200284 * @param string $charset
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200285 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200287 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200289 return $this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
291
292 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200295 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * @return string
298 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200299 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200301 if (isset($this->data_cache['version']))
302 {
303 return $this->data_cache['version'];
304 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200305
306 return $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Execute the query
313 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200314 * @param string $sql an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200315 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200316 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200317 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
mdunischd3ddf972014-05-07 15:00:40 +0200319 return $this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // --------------------------------------------------------------------
323
324 /**
325 * Prep the query
326 *
327 * If needed, each database adapter can prep the query string
328 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200329 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200331 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200332 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200334 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
335 // modifies the query so that it a proper number of affected rows is returned.
336 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Andrey Andreevbe999662013-01-10 11:40:09 +0200338 return trim($sql).' WHERE 1=1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 return $sql;
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Begin Transaction
348 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200349 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200350 * @return bool
351 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200352 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200355 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 return TRUE;
358 }
359
360 // Reset the transaction failure flag.
361 // If the $test_mode flag is set to TRUE transactions will be rolled back
362 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200363 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300365 $this->conn_id->autocommit(FALSE);
366 return is_php('5.5')
367 ? $this->conn_id->begin_transaction()
368 : $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * Commit Transaction
375 *
Barry Mienydd671972010-10-04 16:33:58 +0200376 * @return bool
377 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200378 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200381 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 return TRUE;
384 }
385
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300386 if ($this->conn_id->commit())
387 {
388 $this->conn_id->autocommit(TRUE);
389 return TRUE;
390 }
391
392 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Rollback Transaction
399 *
Barry Mienydd671972010-10-04 16:33:58 +0200400 * @return bool
401 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200402 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200405 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 return TRUE;
408 }
409
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300410 if ($this->conn_id->rollback())
411 {
412 $this->conn_id->autocommit(TRUE);
413 return TRUE;
414 }
415
416 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200422 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200424 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @return string
426 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200427 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300429 return $this->conn_id->real_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // --------------------------------------------------------------------
433
434 /**
435 * Affected Rows
436 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200437 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200439 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200441 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // --------------------------------------------------------------------
445
446 /**
447 * Insert ID
448 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200449 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200451 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200453 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 // --------------------------------------------------------------------
457
458 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 * List table query
460 *
461 * Generates a platform-specific query string so that the table names can be fetched
462 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200463 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @return string
465 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200466 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300468 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200469
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100470 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200472 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 return $sql;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Show column query
482 *
483 * Generates a platform-specific query string so that the column names can be fetched
484 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200485 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @return string
487 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200488 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200490 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492
493 // --------------------------------------------------------------------
494
495 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200496 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200498 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200499 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200501 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200503 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
504 {
505 return FALSE;
506 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200507 $query = $query->result_object();
508
509 $retval = array();
510 for ($i = 0, $c = count($query); $i < $c; $i++)
511 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200512 $retval[$i] = new stdClass();
513 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200514
515 sscanf($query[$i]->Type, '%[a-z](%d)',
516 $retval[$i]->type,
517 $retval[$i]->max_length
518 );
519
Andrey Andreev3722e502012-03-03 15:04:38 +0200520 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200521 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
522 }
523
524 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526
527 // --------------------------------------------------------------------
528
529 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200530 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200532 * Returns an array containing code and message of the last
Claudio Galdiolob993b4b2015-01-28 12:22:38 -0500533 * database error that has occurred.
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200535 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200537 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300539 if ( ! empty($this->conn_id->connect_errno))
540 {
541 return array(
542 'code' => $this->conn_id->connect_errno,
543 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
544 );
545 }
546
Andrey Andreev992f1752012-03-19 16:58:43 +0200547 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
551
552 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300553 * FROM tables
554 *
555 * Groups tables in FROM clauses if needed, so there is no confusion
556 * about operator precedence.
557 *
558 * @return string
559 */
560 protected function _from_tables()
561 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300562 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300563 {
564 return '('.implode(', ', $this->qb_from).')';
565 }
566
567 return implode(', ', $this->qb_from);
568 }
569
570 // --------------------------------------------------------------------
571
572 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * Close DB Connection
574 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 * @return void
576 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300577 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200579 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582}