blob: b64a7a2e801569d3b777fbc7a5415edaa83a4083 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 Andreev2b730372012-11-05 17:01:11 +0200176 if (isset($this->data_cache['version']))
177 {
178 return $this->data_cache['version'];
179 }
180 elseif ( ! $this->conn_id)
181 {
182 $this->initialize();
183 }
184
185 return $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Execute the query
192 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200193 * @param string $sql an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200194 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200195 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200196 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200198 return @$this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // --------------------------------------------------------------------
202
203 /**
204 * Prep the query
205 *
206 * If needed, each database adapter can prep the query string
207 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200208 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200210 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200211 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200213 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
214 // modifies the query so that it a proper number of affected rows is returned.
215 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreevbe999662013-01-10 11:40:09 +0200217 return trim($sql).' WHERE 1=1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 return $sql;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Begin Transaction
227 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200228 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200229 * @return bool
230 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200231 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200234 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 return TRUE;
237 }
238
239 // Reset the transaction failure flag.
240 // If the $test_mode flag is set to TRUE transactions will be rolled back
241 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200242 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243
244 $this->simple_query('SET AUTOCOMMIT=0');
245 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
246 return TRUE;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Commit Transaction
253 *
Barry Mienydd671972010-10-04 16:33:58 +0200254 * @return bool
255 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200256 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200259 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 return TRUE;
262 }
263
264 $this->simple_query('COMMIT');
265 $this->simple_query('SET AUTOCOMMIT=1');
266 return TRUE;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Rollback Transaction
273 *
Barry Mienydd671972010-10-04 16:33:58 +0200274 * @return bool
275 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200276 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200279 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 return TRUE;
282 }
283
284 $this->simple_query('ROLLBACK');
285 $this->simple_query('SET AUTOCOMMIT=1');
286 return TRUE;
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200292 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200294 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @return string
296 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200297 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200299 return is_object($this->conn_id)
300 ? $this->conn_id->real_escape_string($str)
301 : addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // --------------------------------------------------------------------
305
306 /**
307 * Affected Rows
308 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200309 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200311 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200313 return $this->conn_id->affected_rows;
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 * Insert ID
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 insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200325 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * List table query
332 *
333 * Generates a platform-specific query string so that the table names can be fetched
334 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200335 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * @return string
337 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200338 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300340 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200341
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100342 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200344 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 return $sql;
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Show column query
354 *
355 * Generates a platform-specific query string so that the column names can be fetched
356 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200357 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * @return string
359 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200360 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200362 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 // --------------------------------------------------------------------
366
367 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200368 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200370 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200371 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200373 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100375 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200376 {
377 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
378 }
379
Andrey Andreev10ecc842012-11-16 01:06:20 +0200380 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
381 {
382 return FALSE;
383 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200384 $query = $query->result_object();
385
386 $retval = array();
387 for ($i = 0, $c = count($query); $i < $c; $i++)
388 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200389 $retval[$i] = new stdClass();
390 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200391
392 sscanf($query[$i]->Type, '%[a-z](%d)',
393 $retval[$i]->type,
394 $retval[$i]->max_length
395 );
396
Andrey Andreev3722e502012-03-03 15:04:38 +0200397 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200398 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
399 }
400
401 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
404 // --------------------------------------------------------------------
405
406 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200407 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200409 * Returns an array containing code and message of the last
410 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300416 if ( ! empty($this->conn_id->connect_errno))
417 {
418 return array(
419 'code' => $this->conn_id->connect_errno,
420 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
421 );
422 }
423
Andrey Andreev992f1752012-03-19 16:58:43 +0200424 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // --------------------------------------------------------------------
428
429 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500430 * Update_Batch statement
431 *
432 * Generates a platform-specific batch update string from the supplied data
433 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200434 * @param string $table Table name
435 * @param array $values Update data
436 * @param string $index WHERE key
Pascal Kriete43ded232011-01-07 15:05:40 -0500437 * @return string
438 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300439 protected function _update_batch($table, $values, $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500440 {
441 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500442 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500443 {
444 $ids[] = $val[$index];
445
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500446 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500447 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100448 if ($field !== $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500449 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200450 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500451 }
452 }
453 }
454
Pascal Kriete43ded232011-01-07 15:05:40 -0500455 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500456 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500457 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200458 $cases .= $k.' = CASE '."\n"
459 .implode("\n", $v)."\n"
460 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500461 }
462
Andrey Andreevb0478652012-07-18 15:34:46 +0300463 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
Pascal Kriete43ded232011-01-07 15:05:40 -0500464
Andrey Andreevd40459d2012-07-18 16:46:39 +0300465 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Pascal Kriete43ded232011-01-07 15:05:40 -0500466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
469
470 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300471 * FROM tables
472 *
473 * Groups tables in FROM clauses if needed, so there is no confusion
474 * about operator precedence.
475 *
476 * @return string
477 */
478 protected function _from_tables()
479 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300480 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300481 {
482 return '('.implode(', ', $this->qb_from).')';
483 }
484
485 return implode(', ', $this->qb_from);
486 }
487
488 // --------------------------------------------------------------------
489
490 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 * Close DB Connection
492 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @return void
494 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300495 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200497 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500}
501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502/* End of file mysqli_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300503/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */