blob: 947c477848e9e71a03c8305bc99362ba58de995b [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
Derek Jonese4ed5832009-02-20 21:44:59 +000048 // clause and character used for LIKE escape sequences - not used in MySQL
Andrey Andreev1ff49e02012-01-27 11:30:41 +020049 protected $_like_escape_str = '';
Andrey Andreev21cb2d32012-05-25 01:01:06 +030050 protected $_like_escape_chr = '\\';
Derek Jonese4ed5832009-02-20 21:44:59 +000051
Andrey Andreev1ff49e02012-01-27 11:30:41 +020052 protected $_random_keyword = ' RAND()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000053
54 /**
55 * Whether to use the MySQL "delete hack" which allows the number
56 * of affected rows to be shown. Uses a preg_replace when enabled,
57 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020058 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020059 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000060
61 /**
62 * Non-persistent database connection
63 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020064 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020065 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020066 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020068 if ($this->compress === TRUE)
69 {
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020070 $port = empty($this->port) ? NULL : $this->port;
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020071
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020072 $link = mysqli_init();
73 $link->real_connect($this->hostname, $this->username, $this->password, $this->database, $port, NULL, MYSQLI_CLIENT_COMPRESS);
74
75 return $link;
Michiel Vugteveenc27721f2012-08-20 18:34:24 +020076 }
77
Andrey Andreeve4c30192012-06-04 15:08:24 +030078 return empty($this->port)
79 ? @new mysqli($this->hostname, $this->username, $this->password, $this->database)
80 : @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port);
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Persistent database connection
87 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020088 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020089 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020090 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreevf055fa92012-01-27 20:36:23 +020092 // Persistent connection support was added in PHP 5.3.0
93 if ( ! is_php('5.3'))
94 {
95 return $this->db_connect();
96 }
97
Andrey Andreeve4c30192012-06-04 15:08:24 +030098 return empty($this->port)
99 ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database)
100 : @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port);
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // --------------------------------------------------------------------
104
105 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000106 * Reconnect
107 *
108 * Keep / reestablish the db connection if no queries have been
109 * sent for a length of time exceeding the server's idle timeout
110 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 * @return void
112 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200113 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200115 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000116 {
117 $this->conn_id = FALSE;
118 }
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * Select the database
125 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200126 * @param string database name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200127 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200128 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200129 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200131 if ($database === '')
132 {
133 $database = $this->database;
134 }
135
Andrey Andreev992f1752012-03-19 16:58:43 +0200136 if (@$this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200137 {
138 $this->database = $database;
139 return TRUE;
140 }
141
142 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Set client character set
149 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200151 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200153 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200155 return @$this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157
158 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200161 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 * @return string
164 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200165 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200167 return isset($this->data_cache['version'])
168 ? $this->data_cache['version']
Andrey Andreev992f1752012-03-19 16:58:43 +0200169 : $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Execute the query
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200178 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200180 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200182 return @$this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 // --------------------------------------------------------------------
186
187 /**
188 * Prep the query
189 *
190 * If needed, each database adapter can prep the query string
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string an SQL query
193 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200194 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200195 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200197 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
198 // modifies the query so that it a proper number of affected rows is returned.
199 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200201 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 return $sql;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Begin Transaction
211 *
Barry Mienydd671972010-10-04 16:33:58 +0200212 * @return bool
213 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200214 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200217 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
219 return TRUE;
220 }
221
222 // Reset the transaction failure flag.
223 // If the $test_mode flag is set to TRUE transactions will be rolled back
224 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200225 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000226
227 $this->simple_query('SET AUTOCOMMIT=0');
228 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
229 return TRUE;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Commit Transaction
236 *
Barry Mienydd671972010-10-04 16:33:58 +0200237 * @return bool
238 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200239 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200242 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
244 return TRUE;
245 }
246
247 $this->simple_query('COMMIT');
248 $this->simple_query('SET AUTOCOMMIT=1');
249 return TRUE;
250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Rollback Transaction
256 *
Barry Mienydd671972010-10-04 16:33:58 +0200257 * @return bool
258 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200259 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200262 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 return TRUE;
265 }
266
267 $this->simple_query('ROLLBACK');
268 $this->simple_query('SET AUTOCOMMIT=1');
269 return TRUE;
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Escape String
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000278 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * @return string
280 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200281 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000283 if (is_array($str))
284 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500285 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200286 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000287 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200288 }
289
290 return $str;
291 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000292
Andrey Andreev992f1752012-03-19 16:58:43 +0200293 $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Jonese4ed5832009-02-20 21:44:59 +0000295 // escape LIKE condition wildcards
296 if ($like === TRUE)
297 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300298 return str_replace(array($this->_like_escape_chr, '%', '_'),
299 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
300 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jonese4ed5832009-02-20 21:44:59 +0000303 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // --------------------------------------------------------------------
307
308 /**
309 * Affected Rows
310 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200311 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200313 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200315 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
319
320 /**
321 * Insert ID
322 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200323 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200325 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200327 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329
330 // --------------------------------------------------------------------
331
332 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * List table query
334 *
335 * Generates a platform-specific query string so that the table names can be fetched
336 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200337 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @return string
339 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200340 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300342 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200343
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100344 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200346 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 return $sql;
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Show column query
356 *
357 * Generates a platform-specific query string so that the column names can be fetched
358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @param string the table name
360 * @return string
361 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200362 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200364 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
367 // --------------------------------------------------------------------
368
369 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200370 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200373 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200375 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100377 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200378 {
379 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
380 }
381
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200382 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200383 $query = $query->result_object();
384
385 $retval = array();
386 for ($i = 0, $c = count($query); $i < $c; $i++)
387 {
388 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
389
390 $retval[$i] = new stdClass();
391 $retval[$i]->name = $query[$i]->Field;
392 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
393 $retval[$i]->default = $query[$i]->Default;
394 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
395 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
396 }
397
398 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
400
401 // --------------------------------------------------------------------
402
403 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200404 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200406 * Returns an array containing code and message of the last
407 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200409 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200411 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200413 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // --------------------------------------------------------------------
417
418 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500419 * Update_Batch statement
420 *
421 * Generates a platform-specific batch update string from the supplied data
422 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500423 * @param string the table name
424 * @param array the update data
425 * @param array the where clause
426 * @return string
427 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200428 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500429 {
430 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500431 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500432 {
433 $ids[] = $val[$index];
434
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500435 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500436 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100437 if ($field !== $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500438 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500439 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500440 }
441 }
442 }
443
Pascal Kriete43ded232011-01-07 15:05:40 -0500444 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500445 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500446 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200447 $cases .= $k.' = CASE '."\n"
448 .implode("\n", $v)."\n"
449 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500450 }
451
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100452 $where = ($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500453
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200454 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100455 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200456 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // --------------------------------------------------------------------
460
461 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * Close DB Connection
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @return void
465 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300466 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200468 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471}
472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473/* End of file mysqli_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300474/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */