blob: c6b46f070cc74ab87fec6d21b617f72ea99717f2 [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 Andreev2caf2892012-01-27 00:12:03 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2caf2892012-01-27 00:12:03 +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/**
30 * MySQL Database Adapter Class
31 *
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_mysql_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020049 public $dbdriver = 'mysql';
RH Beckercfdb2322011-10-03 17:28:32 -070050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 /**
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 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020061 * 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 Andreev2caf2892012-01-27 00:12:03 +020067 public $delete_hack = TRUE;
68
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 // --------------------------------------------------------------------
70
Andrey Andreev473130a2012-06-24 02:51:18 +030071 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020072 * Identifier escape character
Andrey Andreev473130a2012-06-24 02:51:18 +030073 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020074 * @var string
75 */
76 protected $_escape_char = '`';
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Class constructor
82 *
83 * @param array $params
Andrey Andreev473130a2012-06-24 02:51:18 +030084 * @return void
85 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020086 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020088 parent::__construct($params);
89
Andrey Andreeve4c30192012-06-04 15:08:24 +030090 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
92 $this->hostname .= ':'.$this->port;
93 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Andrey Andreev473130a2012-06-24 02:51:18 +030096 // --------------------------------------------------------------------
97
Andrey Andreev2caf2892012-01-27 00:12:03 +020098 /**
99 * Non-persistent database connection
100 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200101 * @param bool $persistent
Andrey Andreev2caf2892012-01-27 00:12:03 +0200102 * @return resource
103 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300104 public function db_connect($persistent = FALSE)
Andrey Andreev2caf2892012-01-27 00:12:03 +0200105 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300106 $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS;
107
108 if ($this->encrypt === TRUE)
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200109 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300110 $client_flags = $client_flags | MYSQL_CLIENT_SSL;
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200111 }
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300112
Andrey Andreev98ebf432012-10-12 20:44:03 +0300113 return ($persistent === TRUE)
114 ? @mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags)
115 : @mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // --------------------------------------------------------------------
119
120 /**
121 * Persistent database connection
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200125 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300127 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // --------------------------------------------------------------------
131
132 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000133 * Reconnect
134 *
135 * Keep / reestablish the db connection if no queries have been
136 * sent for a length of time exceeding the server's idle timeout
137 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000138 * @return void
139 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200140 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000141 {
142 if (mysql_ping($this->conn_id) === FALSE)
143 {
144 $this->conn_id = FALSE;
145 }
146 }
147
148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones87cbafc2009-02-27 16:29:59 +0000150 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * Select the database
152 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200153 * @param string $database
Andrey Andreev2caf2892012-01-27 00:12:03 +0200154 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200156 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200158 if ($database === '')
159 {
160 $database = $this->database;
161 }
162
163 if (@mysql_select_db($database, $this->conn_id))
164 {
165 $this->database = $database;
166 return TRUE;
167 }
168
169 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Set client character set
176 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200177 * @param string $charset
Andrey Andreev2caf2892012-01-27 00:12:03 +0200178 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200180 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200182 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184
185 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200188 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @return string
191 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200192 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200194 if (isset($this->data_cache['version']))
195 {
196 return $this->data_cache['version'];
197 }
198 elseif ( ! $this->conn_id)
199 {
200 $this->initialize();
201 }
202
203 if ( ! $this->conn_id OR ($version = @mysql_get_server_info($this->conn_id)) === FALSE)
204 {
205 return FALSE;
206 }
207
208 return $this->data_cache['version'] = $version;
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Execute the query
215 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200216 * @param string $sql an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200217 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200219 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200221 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * Prep the query
228 *
229 * If needed, each database adapter can prep the query string
230 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200231 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200234 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200236 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
237 // modifies the query so that it a proper number of affected rows is returned.
238 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Andrey Andreevbe999662013-01-10 11:40:09 +0200240 return trim($sql).' WHERE 1=1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $sql;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Begin Transaction
250 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200251 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200252 * @return bool
253 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200254 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200257 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 return TRUE;
260 }
261
262 // Reset the transaction failure flag.
263 // If the $test_mode flag is set to TRUE transactions will be rolled back
264 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200265 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $this->simple_query('SET AUTOCOMMIT=0');
268 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
269 return TRUE;
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Commit Transaction
276 *
Barry Mienydd671972010-10-04 16:33:58 +0200277 * @return bool
278 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200279 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200282 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 return TRUE;
285 }
286
287 $this->simple_query('COMMIT');
288 $this->simple_query('SET AUTOCOMMIT=1');
289 return TRUE;
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Rollback Transaction
296 *
Barry Mienydd671972010-10-04 16:33:58 +0200297 * @return bool
298 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200299 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200302 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 return TRUE;
305 }
306
307 $this->simple_query('ROLLBACK');
308 $this->simple_query('SET AUTOCOMMIT=1');
309 return TRUE;
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
315 * Escape String
316 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200317 * @param string $str
318 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return string
320 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200321 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200322 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 if (is_array($str))
324 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500325 foreach ($str as $key => $val)
Andrey Andreev838a9d62012-12-03 14:37:47 +0200326 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000327 $str[$key] = $this->escape_str($val, $like);
Andrey Andreev838a9d62012-12-03 14:37:47 +0200328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Andrey Andreev838a9d62012-12-03 14:37:47 +0200330 return $str;
331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
Andrey Andreevf6a41142012-03-12 15:34:10 +0200333 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Jonese4ed5832009-02-20 21:44:59 +0000335 // escape LIKE condition wildcards
336 if ($like === TRUE)
337 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300338 return str_replace(array($this->_like_escape_chr, '%', '_'),
339 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
340 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Jonese4ed5832009-02-20 21:44:59 +0000343 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Affected Rows
350 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200351 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200353 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 return @mysql_affected_rows($this->conn_id);
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // --------------------------------------------------------------------
359
360 /**
361 * Insert ID
362 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200363 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200365 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 return @mysql_insert_id($this->conn_id);
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * List table query
374 *
375 * Generates a platform-specific query string so that the table names can be fetched
376 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200377 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @return string
379 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200380 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300382 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100384 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200386 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
388
389 return $sql;
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 // --------------------------------------------------------------------
393
394 /**
395 * Show column query
396 *
397 * Generates a platform-specific query string so that the column names can be fetched
398 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200399 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * @return string
401 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300402 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200404 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200410 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200412 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200413 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200415 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100417 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200418 {
419 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
420 }
421
Andrey Andreev10ecc842012-11-16 01:06:20 +0200422 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
423 {
424 return FALSE;
425 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200426 $query = $query->result_object();
427
428 $retval = array();
429 for ($i = 0, $c = count($query); $i < $c; $i++)
430 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200431 $retval[$i] = new stdClass();
432 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200433
434 sscanf($query[$i]->Type, '%[a-z](%d)',
435 $retval[$i]->type,
436 $retval[$i]->max_length
437 );
438
Andrey Andreev3722e502012-03-03 15:04:38 +0200439 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200440 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
441 }
442
443 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445
446 // --------------------------------------------------------------------
447
448 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200449 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 * Returns an array containing code and message of the last
452 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200454 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200456 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200458 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460
461 // --------------------------------------------------------------------
462
463 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600464 * Update_Batch statement
465 *
466 * Generates a platform-specific batch update string from the supplied data
467 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200468 * @param string $table Table name
469 * @param array $values Update data
470 * @param string $index WHERE key
Derek Jones20aa2bd2010-03-02 17:28:07 -0600471 * @return string
472 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300473 protected function _update_batch($table, $values, $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600474 {
475 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500476 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600477 {
478 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200479
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500480 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200481 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100482 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600483 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200484 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600485 }
486 }
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Jones20aa2bd2010-03-02 17:28:07 -0600489 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500490 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600491 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200492 $cases .= $k." = CASE \n"
493 .implode("\n", $v)."\n"
494 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Andrey Andreevb0478652012-07-18 15:34:46 +0300497 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
498
Andrey Andreevd40459d2012-07-18 16:46:39 +0300499 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Derek Jones20aa2bd2010-03-02 17:28:07 -0600500 }
501
502 // --------------------------------------------------------------------
503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300505 * FROM tables
506 *
507 * Groups tables in FROM clauses if needed, so there is no confusion
508 * about operator precedence.
509 *
510 * @return string
511 */
512 protected function _from_tables()
513 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300514 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300515 {
516 return '('.implode(', ', $this->qb_from).')';
517 }
518
519 return implode(', ', $this->qb_from);
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * Close DB Connection
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @return void
528 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300529 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300531 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534}
535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300537/* Location: ./system/database/drivers/mysql/mysql_driver.php */