blob: 8274700781aa26410ef8fffc586d0199b245a77d [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 /**
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 */
87 public $stricton = FALSE;
88
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 /**
101 * Database connection
102 *
103 * @param bool $persistent
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200104 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200105 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300106 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Andrey Andreev1fb0cc72014-12-17 19:08:35 +0200108 // Do we have a socket path?
109 if ($this->hostname[0] === '/')
110 {
111 $hostname = NULL;
112 $port = NULL;
113 $socket = $this->hostname;
114 }
115 else
116 {
117 // Persistent connection support was added in PHP 5.3.0
118 $hostname = ($persistent === TRUE && is_php('5.3'))
119 ? 'p:'.$this->hostname : $this->hostname;
120 $port = empty($this->port) ? NULL : $this->port;
121 $socket = NULL;
122 }
123
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300124 $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
Andrey Andreev37c85d72012-10-13 17:08:45 +0300125 $mysqli = mysqli_init();
Michiel Vugteveenc27721f2012-08-20 18:34:24 +0200126
Andrey Andreev9305a8b2015-01-26 12:09:58 +0200127 $mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
Andrey Andreev9305a8b2015-01-26 12:09:58 +0200128
Andrey Andreevf8f14f32013-12-10 11:32:32 +0200129 if ($this->stricton)
130 {
131 $mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode="STRICT_ALL_TABLES"');
132 }
133
Andrey Andreev76e643e2015-07-16 13:14:49 +0300134 if (is_array($this->encrypt))
Tim Noltea0f18722015-06-05 13:40:18 -0400135 {
Andrey Andreev76e643e2015-07-16 13:14:49 +0300136 $ssl = array();
137 empty($this->encrypt['ssl_key']) OR $ssl['key'] = $this->encrypt['ssl_key'];
138 empty($this->encrypt['ssl_cert']) OR $ssl['cert'] = $this->encrypt['ssl_cert'];
139 empty($this->encrypt['ssl_ca']) OR $ssl['ca'] = $this->encrypt['ssl_ca'];
140 empty($this->encrypt['ssl_capath']) OR $ssl['capath'] = $this->encrypt['ssl_capath'];
141 empty($this->encrypt['ssl_cipher']) OR $ssl['cipher'] = $this->encrypt['ssl_cipher'];
Tim Nolteced557b2015-06-18 15:28:43 -0400142
Andrey Andreev76e643e2015-07-16 13:14:49 +0300143 if ( ! empty($ssl))
144 {
Andrey Andreevcfc9e772015-07-16 16:17:27 +0300145 if ( ! empty($this->encrypt['ssl_verify']) && defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT'))
146 {
147 $mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, TRUE);
148 }
149
Andrey Andreev76e643e2015-07-16 13:14:49 +0300150 $client_flags |= MYSQLI_CLIENT_SSL;
151 $mysqli->ssl_set(
152 isset($ssl['key']) ? $ssl['key'] : NULL,
153 isset($ssl['cert']) ? $ssl['cert'] : NULL,
154 isset($ssl['ca']) ? $ssl['ca'] : NULL,
155 isset($ssl['capath']) ? $ssl['capath'] : NULL,
156 isset($ssl['cipher']) ? $ssl['cipher'] : NULL
157 );
158 }
Tim Noltea0f18722015-06-05 13:40:18 -0400159 }
160
Andrey Andreev76e643e2015-07-16 13:14:49 +0300161 if ($mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags))
Tim Noltea0f18722015-06-05 13:40:18 -0400162 {
Andrey Andreev76e643e2015-07-16 13:14:49 +0300163 // Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
Andrey Andreev9194b492015-07-16 14:23:51 +0300164 if (
165 ($client_flags & MYSQLI_CLIENT_SSL)
166 && version_compare($mysqli->client_info, '5.7.3', '<=')
167 && empty($mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)
168 )
Tim Noltea0f18722015-06-05 13:40:18 -0400169 {
Andrey Andreev9194b492015-07-16 14:23:51 +0300170 $mysqli->close();
171 $message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!';
172 log_message('error', $message);
173 return ($this->db->db_debug) ? $this->db->display_error($message, '', TRUE) : FALSE;
Tim Noltea0f18722015-06-05 13:40:18 -0400174 }
175
176 return $mysqli;
177 }
Tim Noltea0f18722015-06-05 13:40:18 -0400178
179 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181
182 // --------------------------------------------------------------------
183
184 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000185 * Reconnect
186 *
187 * Keep / reestablish the db connection if no queries have been
188 * sent for a length of time exceeding the server's idle timeout
189 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000190 * @return void
191 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200192 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000193 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200194 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000195 {
196 $this->conn_id = FALSE;
197 }
198 }
199
200 // --------------------------------------------------------------------
201
202 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * Select the database
204 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200205 * @param string $database
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200206 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200207 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200208 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200210 if ($database === '')
211 {
212 $database = $this->database;
213 }
214
mdunischd3ddf972014-05-07 15:00:40 +0200215 if ($this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200216 {
217 $this->database = $database;
218 return TRUE;
219 }
220
221 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Set client character set
228 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200229 * @param string $charset
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200230 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200232 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200234 return $this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200240 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @return string
243 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200244 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200246 if (isset($this->data_cache['version']))
247 {
248 return $this->data_cache['version'];
249 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200250
251 return $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Execute the query
258 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200259 * @param string $sql an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200260 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200261 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200262 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
mdunischd3ddf972014-05-07 15:00:40 +0200264 return $this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // --------------------------------------------------------------------
268
269 /**
270 * Prep the query
271 *
272 * If needed, each database adapter can prep the query string
273 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200274 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200276 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200277 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200279 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
280 // modifies the query so that it a proper number of affected rows is returned.
281 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreevbe999662013-01-10 11:40:09 +0200283 return trim($sql).' WHERE 1=1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 return $sql;
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Begin Transaction
293 *
Barry Mienydd671972010-10-04 16:33:58 +0200294 * @return bool
295 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300296 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300298 $this->conn_id->autocommit(FALSE);
299 return is_php('5.5')
300 ? $this->conn_id->begin_transaction()
301 : $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Commit Transaction
308 *
Barry Mienydd671972010-10-04 16:33:58 +0200309 * @return bool
310 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300311 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300313 if ($this->conn_id->commit())
314 {
315 $this->conn_id->autocommit(TRUE);
316 return TRUE;
317 }
318
319 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Rollback Transaction
326 *
Barry Mienydd671972010-10-04 16:33:58 +0200327 * @return bool
328 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300329 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreev3c5ec852013-09-13 15:01:08 +0300331 if ($this->conn_id->rollback())
332 {
333 $this->conn_id->autocommit(TRUE);
334 return TRUE;
335 }
336
337 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200343 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200345 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @return string
347 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200348 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300350 return $this->conn_id->real_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // --------------------------------------------------------------------
354
355 /**
356 * Affected Rows
357 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200358 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200360 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200362 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // --------------------------------------------------------------------
366
367 /**
368 * Insert ID
369 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200370 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200372 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200374 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 // --------------------------------------------------------------------
378
379 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * List table query
381 *
382 * Generates a platform-specific query string so that the table names can be fetched
383 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200384 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 * @return string
386 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200387 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300389 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200390
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100391 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200393 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 return $sql;
397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Show column query
403 *
404 * Generates a platform-specific query string so that the column names can be fetched
405 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200406 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * @return string
408 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200409 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200411 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
413
414 // --------------------------------------------------------------------
415
416 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200417 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200419 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200420 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200422 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200424 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
425 {
426 return FALSE;
427 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200428 $query = $query->result_object();
429
430 $retval = array();
431 for ($i = 0, $c = count($query); $i < $c; $i++)
432 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200433 $retval[$i] = new stdClass();
434 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200435
436 sscanf($query[$i]->Type, '%[a-z](%d)',
437 $retval[$i]->type,
438 $retval[$i]->max_length
439 );
440
Andrey Andreev3722e502012-03-03 15:04:38 +0200441 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200442 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
443 }
444
445 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 // --------------------------------------------------------------------
449
450 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200453 * Returns an array containing code and message of the last
Claudio Galdiolob993b4b2015-01-28 12:22:38 -0500454 * database error that has occurred.
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200456 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200458 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300460 if ( ! empty($this->conn_id->connect_errno))
461 {
462 return array(
463 'code' => $this->conn_id->connect_errno,
464 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
465 );
466 }
467
Andrey Andreev992f1752012-03-19 16:58:43 +0200468 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300474 * FROM tables
475 *
476 * Groups tables in FROM clauses if needed, so there is no confusion
477 * about operator precedence.
478 *
479 * @return string
480 */
481 protected function _from_tables()
482 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300483 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300484 {
485 return '('.implode(', ', $this->qb_from).')';
486 }
487
488 return implode(', ', $this->qb_from);
489 }
490
491 // --------------------------------------------------------------------
492
493 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * Close DB Connection
495 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @return void
497 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300498 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200500 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503}