blob: 291ad16f2d67ec19d17b9c5e95c69bd4f87680ab [file] [log] [blame]
Andrey Andreev1ff49e02012-01-27 11:30:41 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1ff49e02012-01-27 11:30:41 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1ff49e02012-01-27 11:30:41 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Andrey Andreev1ff49e02012-01-27 11:30:41 +020029 * MySQLi Database Adapter Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_mysqli_driver extends CI_DB {
42
Andrey Andreev1ff49e02012-01-27 11:30:41 +020043 public $dbdriver = 'mysqli';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // The character used for escaping
Andrey Andreev1ff49e02012-01-27 11:30:41 +020046 protected $_escape_char = '`';
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Andrey Andreev1ff49e02012-01-27 11:30:41 +020048 protected $_random_keyword = ' RAND()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000049
50 /**
51 * Whether to use the MySQL "delete hack" which allows the number
52 * of affected rows to be shown. Uses a preg_replace when enabled,
53 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020055 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000056
57 /**
58 * Non-persistent database connection
59 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020060 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020061 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020062 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
Michiel Vugteveencdb481b2012-08-21 10:11:16 +020064 // Use MySQL client compression?
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020065 if ($this->compress === TRUE)
66 {
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020067 $port = empty($this->port) ? NULL : $this->port;
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020068
Andrey Andreevdbad54e2012-10-05 21:53:32 +030069 $mysqli = new mysqli();
70 @$mysqli->real_connect($this->hostname, $this->username, $this->password, $this->database, $port, NULL, MYSQLI_CLIENT_COMPRESS);
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020071
Michiel Vugteveencdb481b2012-08-21 10:11:16 +020072 return $mysqli;
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020073 }
74
Andrey Andreeve4c30192012-06-04 15:08:24 +030075 return empty($this->port)
76 ? @new mysqli($this->hostname, $this->username, $this->password, $this->database)
77 : @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port);
Derek Allard2067d1a2008-11-13 22:59:24 +000078 }
79
80 // --------------------------------------------------------------------
81
82 /**
83 * Persistent database connection
84 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020085 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020087 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreevf055fa92012-01-27 20:36:23 +020089 // Persistent connection support was added in PHP 5.3.0
90 if ( ! is_php('5.3'))
91 {
92 return $this->db_connect();
93 }
94
Michiel Vugteveencdb481b2012-08-21 10:11:16 +020095 // Use MySQL client compression?
96 if ($this->compress === TRUE)
97 {
98 $port = empty($this->port) ? NULL : $this->port;
99
100 $mysqli = mysqli_init();
101 $mysqli->real_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $port, NULL, MYSQLI_CLIENT_COMPRESS);
102
103 return $mysqli;
104 }
105
Andrey Andreeve4c30192012-06-04 15:08:24 +0300106 return empty($this->port)
107 ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database)
108 : @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port);
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // --------------------------------------------------------------------
112
113 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 * Reconnect
115 *
116 * Keep / reestablish the db connection if no queries have been
117 * sent for a length of time exceeding the server's idle timeout
118 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 * @return void
120 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200121 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000122 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200123 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000124 {
125 $this->conn_id = FALSE;
126 }
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * Select the database
133 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200134 * @param string database name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200135 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200136 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200137 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200139 if ($database === '')
140 {
141 $database = $this->database;
142 }
143
Andrey Andreev992f1752012-03-19 16:58:43 +0200144 if (@$this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200145 {
146 $this->database = $database;
147 return TRUE;
148 }
149
150 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Set client character set
157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200159 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200161 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200163 return @$this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165
166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200169 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @return string
172 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200173 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200175 return isset($this->data_cache['version'])
176 ? $this->data_cache['version']
Andrey Andreev992f1752012-03-19 16:58:43 +0200177 : $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Execute the query
184 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200186 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200187 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200188 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200190 return @$this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 // --------------------------------------------------------------------
194
195 /**
196 * Prep the query
197 *
198 * If needed, each database adapter can prep the query string
199 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * @param string an SQL query
201 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200202 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200203 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200205 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
206 // modifies the query so that it a proper number of affected rows is returned.
207 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200209 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 return $sql;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Begin Transaction
219 *
Barry Mienydd671972010-10-04 16:33:58 +0200220 * @return bool
221 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200222 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200225 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 return TRUE;
228 }
229
230 // Reset the transaction failure flag.
231 // If the $test_mode flag is set to TRUE transactions will be rolled back
232 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200233 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000234
235 $this->simple_query('SET AUTOCOMMIT=0');
236 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
237 return TRUE;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Commit Transaction
244 *
Barry Mienydd671972010-10-04 16:33:58 +0200245 * @return bool
246 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200247 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200250 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 return TRUE;
253 }
254
255 $this->simple_query('COMMIT');
256 $this->simple_query('SET AUTOCOMMIT=1');
257 return TRUE;
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Rollback Transaction
264 *
Barry Mienydd671972010-10-04 16:33:58 +0200265 * @return bool
266 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200267 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200270 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 return TRUE;
273 }
274
275 $this->simple_query('ROLLBACK');
276 $this->simple_query('SET AUTOCOMMIT=1');
277 return TRUE;
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Escape String
284 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000286 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 * @return string
288 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200289 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 if (is_array($str))
292 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500293 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200294 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000295 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200296 }
297
298 return $str;
299 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000300
Andrey Andreev992f1752012-03-19 16:58:43 +0200301 $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jonese4ed5832009-02-20 21:44:59 +0000303 // escape LIKE condition wildcards
304 if ($like === TRUE)
305 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300306 return str_replace(array($this->_like_escape_chr, '%', '_'),
307 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
308 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Jonese4ed5832009-02-20 21:44:59 +0000311 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
315
316 /**
317 * Affected Rows
318 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200319 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200321 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200323 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
327
328 /**
329 * Insert ID
330 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200331 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200333 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200335 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337
338 // --------------------------------------------------------------------
339
340 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * List table query
342 *
343 * Generates a platform-specific query string so that the table names can be fetched
344 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200345 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @return string
347 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200348 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300350 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200351
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100352 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200354 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 return $sql;
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Show column query
364 *
365 * Generates a platform-specific query string so that the column names can be fetched
366 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * @param string the table name
368 * @return string
369 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200370 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200372 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374
375 // --------------------------------------------------------------------
376
377 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200378 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200381 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200383 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100385 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200386 {
387 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
388 }
389
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200390 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200391 $query = $query->result_object();
392
393 $retval = array();
394 for ($i = 0, $c = count($query); $i < $c; $i++)
395 {
396 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
397
398 $retval[$i] = new stdClass();
399 $retval[$i]->name = $query[$i]->Field;
400 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
401 $retval[$i]->default = $query[$i]->Default;
402 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
403 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
404 }
405
406 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
409 // --------------------------------------------------------------------
410
411 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 * Returns an array containing code and message of the last
415 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200417 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200419 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300421 if ( ! empty($this->conn_id->connect_errno))
422 {
423 return array(
424 'code' => $this->conn_id->connect_errno,
425 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
426 );
427 }
428
Andrey Andreev992f1752012-03-19 16:58:43 +0200429 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
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 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500435 * Update_Batch statement
436 *
437 * Generates a platform-specific batch update string from the supplied data
438 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500439 * @param string the table name
440 * @param array the update data
441 * @param array the where clause
442 * @return string
443 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200444 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500445 {
446 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500447 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500448 {
449 $ids[] = $val[$index];
450
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500451 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500452 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100453 if ($field !== $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500454 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500455 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500456 }
457 }
458 }
459
Pascal Kriete43ded232011-01-07 15:05:40 -0500460 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500461 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500462 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200463 $cases .= $k.' = CASE '."\n"
464 .implode("\n", $v)."\n"
465 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500466 }
467
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100468 $where = ($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500469
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200470 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100471 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200472 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300478 * FROM tables
479 *
480 * Groups tables in FROM clauses if needed, so there is no confusion
481 * about operator precedence.
482 *
483 * @return string
484 */
485 protected function _from_tables()
486 {
487 if ( ! empty($this->qb_join) && count($this->qb_from) > 0)
488 {
489 return '('.implode(', ', $this->qb_from).')';
490 }
491
492 return implode(', ', $this->qb_from);
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * Close DB Connection
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @return void
501 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300502 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200504 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507}
508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509/* End of file mysqli_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300510/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */