blob: 78a4bef0f10f90174af396288a70de17fe454d40 [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 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 Andreev1ff49e02012-01-27 11:30:41 +0200217 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
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 /**
292 * Escape String
293 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200294 * @param string $str
295 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @return string
297 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200298 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000300 if (is_array($str))
301 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500302 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200303 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000304 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200305 }
306
307 return $str;
308 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000309
Andrey Andreev992f1752012-03-19 16:58:43 +0200310 $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Jonese4ed5832009-02-20 21:44:59 +0000312 // escape LIKE condition wildcards
313 if ($like === TRUE)
314 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300315 return str_replace(array($this->_like_escape_chr, '%', '_'),
316 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
317 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Jonese4ed5832009-02-20 21:44:59 +0000320 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // --------------------------------------------------------------------
324
325 /**
326 * Affected Rows
327 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200328 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200330 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200332 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
336
337 /**
338 * Insert ID
339 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200340 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200342 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200344 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
347 // --------------------------------------------------------------------
348
349 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * List table query
351 *
352 * Generates a platform-specific query string so that the table names can be fetched
353 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200354 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return string
356 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200357 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300359 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200360
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100361 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200363 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 return $sql;
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Show column query
373 *
374 * Generates a platform-specific query string so that the column names can be fetched
375 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200376 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @return string
378 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200379 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200381 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200387 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200389 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200390 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200392 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100394 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200395 {
396 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
397 }
398
Andrey Andreev10ecc842012-11-16 01:06:20 +0200399 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
400 {
401 return FALSE;
402 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200403 $query = $query->result_object();
404
405 $retval = array();
406 for ($i = 0, $c = count($query); $i < $c; $i++)
407 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200408 $retval[$i] = new stdClass();
409 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200410
411 sscanf($query[$i]->Type, '%[a-z](%d)',
412 $retval[$i]->type,
413 $retval[$i]->max_length
414 );
415
Andrey Andreev3722e502012-03-03 15:04:38 +0200416 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200417 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
418 }
419
420 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200426 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200428 * Returns an array containing code and message of the last
429 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200431 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200433 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreevdbad54e2012-10-05 21:53:32 +0300435 if ( ! empty($this->conn_id->connect_errno))
436 {
437 return array(
438 'code' => $this->conn_id->connect_errno,
439 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
440 );
441 }
442
Andrey Andreev992f1752012-03-19 16:58:43 +0200443 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 // --------------------------------------------------------------------
447
448 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500449 * Update_Batch statement
450 *
451 * Generates a platform-specific batch update string from the supplied data
452 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200453 * @param string $table Table name
454 * @param array $values Update data
455 * @param string $index WHERE key
Pascal Kriete43ded232011-01-07 15:05:40 -0500456 * @return string
457 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300458 protected function _update_batch($table, $values, $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500459 {
460 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500461 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500462 {
463 $ids[] = $val[$index];
464
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500465 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500466 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100467 if ($field !== $index)
Pascal Kriete43ded232011-01-07 15:05:40 -0500468 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500469 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500470 }
471 }
472 }
473
Pascal Kriete43ded232011-01-07 15:05:40 -0500474 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500475 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500476 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200477 $cases .= $k.' = CASE '."\n"
478 .implode("\n", $v)."\n"
479 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500480 }
481
Andrey Andreevb0478652012-07-18 15:34:46 +0300482 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
Pascal Kriete43ded232011-01-07 15:05:40 -0500483
Andrey Andreevd40459d2012-07-18 16:46:39 +0300484 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Pascal Kriete43ded232011-01-07 15:05:40 -0500485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 // --------------------------------------------------------------------
488
489 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300490 * FROM tables
491 *
492 * Groups tables in FROM clauses if needed, so there is no confusion
493 * about operator precedence.
494 *
495 * @return string
496 */
497 protected function _from_tables()
498 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300499 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300500 {
501 return '('.implode(', ', $this->qb_from).')';
502 }
503
504 return implode(', ', $this->qb_from);
505 }
506
507 // --------------------------------------------------------------------
508
509 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * Close DB Connection
511 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @return void
513 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300514 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200516 $this->conn_id->close();
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519}
520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521/* End of file mysqli_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300522/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */