blob: 7d2507b4064d767e9e212ceabb550aeefa27f7dc [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Andrey Andreev1ff49e02012-01-27 11:30:41 +020030 * MySQLi Database Adapter Class
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_mysqli_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020049 public $dbdriver = 'mysqli';
Derek Allard2067d1a2008-11-13 22:59:24 +000050
51 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020052 * Compression flag
53 *
54 * @var bool
55 */
56 public $compress = FALSE;
57
58 /**
59 * DELETE hack flag
60 *
Derek Allard2067d1a2008-11-13 22:59:24 +000061 * Whether to use the MySQL "delete hack" which allows the number
62 * of affected rows to be shown. Uses a preg_replace when enabled,
63 * adding a bit more processing to all queries.
Andrey Andreeva24e52e2012-11-02 03:54:12 +020064 *
65 * @var bool
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020067 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000068
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 // --------------------------------------------------------------------
70
Derek Allard2067d1a2008-11-13 22:59:24 +000071 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020072 * Identifier escape character
Derek Allard2067d1a2008-11-13 22:59:24 +000073 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020074 * @var string
75 */
76 protected $_escape_char = '`';
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Database connection
82 *
83 * @param bool $persistent
Andrey Andreev1ff49e02012-01-27 11:30:41 +020084 * @return object
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030085 * @todo SSL support
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030087 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030089 // Persistent connection support was added in PHP 5.3.0
90 $hostname = ($persistent === TRUE && is_php('5.3'))
91 ? 'p:'.$this->hostname : $this->hostname;
92 $port = empty($this->port) ? NULL : $this->port;
93 $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
Andrey Andreev37c85d72012-10-13 17:08:45 +030094 $mysqli = mysqli_init();
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020095
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030096 return @$mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, NULL, $client_flags)
97 ? $mysqli : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Persistent database connection
104 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200105 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200106 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200107 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300109 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // --------------------------------------------------------------------
113
114 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000115 * Reconnect
116 *
117 * Keep / reestablish the db connection if no queries have been
118 * sent for a length of time exceeding the server's idle timeout
119 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000120 * @return void
121 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200122 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000123 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200124 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000125 {
126 $this->conn_id = FALSE;
127 }
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 * Select the database
134 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200135 * @param string $database
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200136 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200137 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200138 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200140 if ($database === '')
141 {
142 $database = $this->database;
143 }
144
Andrey Andreev992f1752012-03-19 16:58:43 +0200145 if (@$this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200146 {
147 $this->database = $database;
148 return TRUE;
149 }
150
151 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Set client character set
158 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200159 * @param string $charset
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200160 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200162 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200164 return @$this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
167 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200170 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @return string
173 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200174 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200176 return isset($this->data_cache['version'])
177 ? $this->data_cache['version']
Andrey Andreev992f1752012-03-19 16:58:43 +0200178 : $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Execute the query
185 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200186 * @param string $sql an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200187 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200188 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200189 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200191 return @$this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // --------------------------------------------------------------------
195
196 /**
197 * Prep the query
198 *
199 * If needed, each database adapter can prep the query string
200 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200201 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200203 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200204 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200206 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
207 // modifies the query so that it a proper number of affected rows is returned.
208 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200210 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 return $sql;
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Begin Transaction
220 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200221 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200222 * @return bool
223 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200224 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200227 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return TRUE;
230 }
231
232 // Reset the transaction failure flag.
233 // If the $test_mode flag is set to TRUE transactions will be rolled back
234 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200235 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000236
237 $this->simple_query('SET AUTOCOMMIT=0');
238 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
239 return TRUE;
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Commit Transaction
246 *
Barry Mienydd671972010-10-04 16:33:58 +0200247 * @return bool
248 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200249 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200252 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 return TRUE;
255 }
256
257 $this->simple_query('COMMIT');
258 $this->simple_query('SET AUTOCOMMIT=1');
259 return TRUE;
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Rollback Transaction
266 *
Barry Mienydd671972010-10-04 16:33:58 +0200267 * @return bool
268 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200269 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200272 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 return TRUE;
275 }
276
277 $this->simple_query('ROLLBACK');
278 $this->simple_query('SET AUTOCOMMIT=1');
279 return TRUE;
280 }
281
282 // --------------------------------------------------------------------
283
284 /**
285 * Escape String
286 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200287 * @param string $str
288 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * @return string
290 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200291 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000293 if (is_array($str))
294 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500295 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000297 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200298 }
299
300 return $str;
301 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000302
Andrey Andreev992f1752012-03-19 16:58:43 +0200303 $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Jonese4ed5832009-02-20 21:44:59 +0000305 // escape LIKE condition wildcards
306 if ($like === TRUE)
307 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300308 return str_replace(array($this->_like_escape_chr, '%', '_'),
309 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
310 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Jonese4ed5832009-02-20 21:44:59 +0000313 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // --------------------------------------------------------------------
317
318 /**
319 * Affected Rows
320 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200321 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200323 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200325 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // --------------------------------------------------------------------
329
330 /**
331 * Insert ID
332 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200333 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200335 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200337 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * List table query
344 *
345 * Generates a platform-specific query string so that the table names can be fetched
346 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200347 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 * @return string
349 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200350 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300352 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200353
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100354 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200356 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 return $sql;
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Show column query
366 *
367 * Generates a platform-specific query string so that the column names can be fetched
368 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200369 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @return string
371 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200372 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200374 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 // --------------------------------------------------------------------
378
379 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200380 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200382 * @param string $table
Andrey Andreev3722e502012-03-03 15:04:38 +0200383 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200385 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100387 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200388 {
389 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
390 }
391
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200392 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200393 $query = $query->result_object();
394
395 $retval = array();
396 for ($i = 0, $c = count($query); $i < $c; $i++)
397 {
398 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
399
400 $retval[$i] = new stdClass();
401 $retval[$i]->name = $query[$i]->Field;
402 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
403 $retval[$i]->default = $query[$i]->Default;
404 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
405 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
406 }
407
408 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410
411 // --------------------------------------------------------------------
412
413 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200416 * Returns an array containing code and message of the last
417 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200419 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200421 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300423 if ( ! empty($this->conn_id->connect_errno))
424 {
425 return array(
426 'code' => $this->conn_id->connect_errno,
427 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
428 );
429 }
430
Andrey Andreev992f1752012-03-19 16:58:43 +0200431 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
435
436 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500437 * Update_Batch statement
438 *
439 * Generates a platform-specific batch update string from the supplied data
440 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200441 * @param string $table Table name
442 * @param array $values Update data
443 * @param string $index WHERE key
Pascal Kriete43ded232011-01-07 15:05:40 -0500444 * @return string
445 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300446 protected function _update_batch($table, $values, $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500447 {
448 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500449 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500450 {
451 $ids[] = $val[$index];
452
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500453 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500454 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100455 if ($field !== $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500456 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500457 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500458 }
459 }
460 }
461
Pascal Kriete43ded232011-01-07 15:05:40 -0500462 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500463 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500464 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200465 $cases .= $k.' = CASE '."\n"
466 .implode("\n", $v)."\n"
467 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500468 }
469
Andrey Andreevb0478652012-07-18 15:34:46 +0300470 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
Pascal Kriete43ded232011-01-07 15:05:40 -0500471
Andrey Andreevd40459d2012-07-18 16:46:39 +0300472 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
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 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300487 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300488 {
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 */