blob: f77176c162f5de35222815b931d14a92a94f4677 [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';
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030044 public $compress = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 // The character used for escaping
Andrey Andreev1ff49e02012-01-27 11:30:41 +020047 protected $_escape_char = '`';
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Andrey Andreev1ff49e02012-01-27 11:30:41 +020049 protected $_random_keyword = ' RAND()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000050
51 /**
52 * Whether to use the MySQL "delete hack" which allows the number
53 * of affected rows to be shown. Uses a preg_replace when enabled,
54 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020055 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020056 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000057
58 /**
59 * Non-persistent database connection
60 *
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030061 * @param bool
Andrey Andreev1ff49e02012-01-27 11:30:41 +020062 * @return object
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030063 * @todo SSL support
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030065 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030067 // Persistent connection support was added in PHP 5.3.0
68 $hostname = ($persistent === TRUE && is_php('5.3'))
69 ? 'p:'.$this->hostname : $this->hostname;
70 $port = empty($this->port) ? NULL : $this->port;
71 $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
72 $mysqli = new mysqli();
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020073
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030074 return @$mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, NULL, $client_flags)
75 ? $mysqli : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Persistent database connection
82 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020083 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020084 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020085 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030087 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 // --------------------------------------------------------------------
91
92 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000093 * Reconnect
94 *
95 * Keep / reestablish the db connection if no queries have been
96 * sent for a length of time exceeding the server's idle timeout
97 *
Derek Jones87cbafc2009-02-27 16:29:59 +000098 * @return void
99 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200100 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000101 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200102 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000103 {
104 $this->conn_id = FALSE;
105 }
106 }
107
108 // --------------------------------------------------------------------
109
110 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * Select the database
112 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200113 * @param string database name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200114 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200115 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200116 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200118 if ($database === '')
119 {
120 $database = $this->database;
121 }
122
Andrey Andreev992f1752012-03-19 16:58:43 +0200123 if (@$this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200124 {
125 $this->database = $database;
126 return TRUE;
127 }
128
129 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Set client character set
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200138 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200140 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200142 return @$this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144
145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200148 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @return string
151 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200152 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200154 return isset($this->data_cache['version'])
155 ? $this->data_cache['version']
Andrey Andreev992f1752012-03-19 16:58:43 +0200156 : $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Execute the query
163 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200165 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200166 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200167 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200169 return @$this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // --------------------------------------------------------------------
173
174 /**
175 * Prep the query
176 *
177 * If needed, each database adapter can prep the query string
178 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @param string an SQL query
180 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200181 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200182 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200184 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
185 // modifies the query so that it a proper number of affected rows is returned.
186 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200188 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 return $sql;
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Begin Transaction
198 *
Barry Mienydd671972010-10-04 16:33:58 +0200199 * @return bool
200 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200201 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200204 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 return TRUE;
207 }
208
209 // Reset the transaction failure flag.
210 // If the $test_mode flag is set to TRUE transactions will be rolled back
211 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200212 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000213
214 $this->simple_query('SET AUTOCOMMIT=0');
215 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
216 return TRUE;
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Commit Transaction
223 *
Barry Mienydd671972010-10-04 16:33:58 +0200224 * @return bool
225 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200226 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200229 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
231 return TRUE;
232 }
233
234 $this->simple_query('COMMIT');
235 $this->simple_query('SET AUTOCOMMIT=1');
236 return TRUE;
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Rollback Transaction
243 *
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return bool
245 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200246 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200249 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 return TRUE;
252 }
253
254 $this->simple_query('ROLLBACK');
255 $this->simple_query('SET AUTOCOMMIT=1');
256 return TRUE;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Escape String
263 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000265 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @return string
267 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200268 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000270 if (is_array($str))
271 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500272 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200273 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000274 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200275 }
276
277 return $str;
278 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000279
Andrey Andreev992f1752012-03-19 16:58:43 +0200280 $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Jonese4ed5832009-02-20 21:44:59 +0000282 // escape LIKE condition wildcards
283 if ($like === TRUE)
284 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300285 return str_replace(array($this->_like_escape_chr, '%', '_'),
286 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
287 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jonese4ed5832009-02-20 21:44:59 +0000290 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // --------------------------------------------------------------------
294
295 /**
296 * Affected Rows
297 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200298 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200300 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200302 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // --------------------------------------------------------------------
306
307 /**
308 * Insert ID
309 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200310 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200312 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200314 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316
317 // --------------------------------------------------------------------
318
319 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * List table query
321 *
322 * Generates a platform-specific query string so that the table names can be fetched
323 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200324 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @return string
326 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200327 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300329 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200330
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100331 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200333 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 return $sql;
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Show column query
343 *
344 * Generates a platform-specific query string so that the column names can be fetched
345 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @param string the table name
347 * @return string
348 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200349 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200351 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353
354 // --------------------------------------------------------------------
355
356 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200357 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200360 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200362 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100364 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200365 {
366 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
367 }
368
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200369 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200370 $query = $query->result_object();
371
372 $retval = array();
373 for ($i = 0, $c = count($query); $i < $c; $i++)
374 {
375 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
376
377 $retval[$i] = new stdClass();
378 $retval[$i]->name = $query[$i]->Field;
379 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
380 $retval[$i]->default = $query[$i]->Default;
381 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
382 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
383 }
384
385 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387
388 // --------------------------------------------------------------------
389
390 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200391 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200393 * Returns an array containing code and message of the last
394 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200396 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200398 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300400 if ( ! empty($this->conn_id->connect_errno))
401 {
402 return array(
403 'code' => $this->conn_id->connect_errno,
404 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
405 );
406 }
407
Andrey Andreev992f1752012-03-19 16:58:43 +0200408 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
412
413 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500414 * Update_Batch statement
415 *
416 * Generates a platform-specific batch update string from the supplied data
417 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500418 * @param string the table name
419 * @param array the update data
420 * @param array the where clause
421 * @return string
422 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200423 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500424 {
425 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500426 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500427 {
428 $ids[] = $val[$index];
429
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500430 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500431 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100432 if ($field !== $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500433 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500434 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500435 }
436 }
437 }
438
Pascal Kriete43ded232011-01-07 15:05:40 -0500439 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500440 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500441 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200442 $cases .= $k.' = CASE '."\n"
443 .implode("\n", $v)."\n"
444 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500445 }
446
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100447 $where = ($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500448
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200449 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100450 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200451 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // --------------------------------------------------------------------
455
456 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300457 * FROM tables
458 *
459 * Groups tables in FROM clauses if needed, so there is no confusion
460 * about operator precedence.
461 *
462 * @return string
463 */
464 protected function _from_tables()
465 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300466 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300467 {
468 return '('.implode(', ', $this->qb_from).')';
469 }
470
471 return implode(', ', $this->qb_from);
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * Close DB Connection
478 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @return void
480 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300481 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200483 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486}
487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488/* End of file mysqli_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300489/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */