blob: 492b07861ca0baadbce9f2eb1837078f1390a0aa [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 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200315 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200317 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @return string
319 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200320 protected function _escape_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200321 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200322 return is_resource($this->conn_id)
323 ? mysql_real_escape_string($str, $this->conn_id)
324 : addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
328
329 /**
330 * Affected Rows
331 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200332 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200334 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 return @mysql_affected_rows($this->conn_id);
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
340
341 /**
342 * Insert ID
343 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200344 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200346 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 return @mysql_insert_id($this->conn_id);
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * List table query
355 *
356 * Generates a platform-specific query string so that the table names can be fetched
357 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200358 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @return string
360 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200361 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300363 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100365 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200367 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369
370 return $sql;
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // --------------------------------------------------------------------
374
375 /**
376 * Show column query
377 *
378 * Generates a platform-specific query string so that the column names can be fetched
379 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200380 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @return string
382 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300383 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200385 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387
388 // --------------------------------------------------------------------
389
390 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200391 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200393 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200394 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200396 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100398 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200399 {
400 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
401 }
402
Andrey Andreev10ecc842012-11-16 01:06:20 +0200403 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
404 {
405 return FALSE;
406 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200407 $query = $query->result_object();
408
409 $retval = array();
410 for ($i = 0, $c = count($query); $i < $c; $i++)
411 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200412 $retval[$i] = new stdClass();
413 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200414
415 sscanf($query[$i]->Type, '%[a-z](%d)',
416 $retval[$i]->type,
417 $retval[$i]->max_length
418 );
419
Andrey Andreev3722e502012-03-03 15:04:38 +0200420 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200421 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
422 }
423
424 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200430 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200432 * Returns an array containing code and message of the last
433 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200435 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200437 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200439 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600445 * Update_Batch statement
446 *
447 * Generates a platform-specific batch update string from the supplied data
448 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200449 * @param string $table Table name
450 * @param array $values Update data
451 * @param string $index WHERE key
Derek Jones20aa2bd2010-03-02 17:28:07 -0600452 * @return string
453 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300454 protected function _update_batch($table, $values, $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600455 {
456 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500457 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600458 {
459 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200460
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500461 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200462 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100463 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600464 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200465 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600466 }
467 }
468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Jones20aa2bd2010-03-02 17:28:07 -0600470 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500471 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600472 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200473 $cases .= $k." = CASE \n"
474 .implode("\n", $v)."\n"
475 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Andrey Andreevb0478652012-07-18 15:34:46 +0300478 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
479
Andrey Andreevd40459d2012-07-18 16:46:39 +0300480 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Derek Jones20aa2bd2010-03-02 17:28:07 -0600481 }
482
483 // --------------------------------------------------------------------
484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300486 * FROM tables
487 *
488 * Groups tables in FROM clauses if needed, so there is no confusion
489 * about operator precedence.
490 *
491 * @return string
492 */
493 protected function _from_tables()
494 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300495 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300496 {
497 return '('.implode(', ', $this->qb_from).')';
498 }
499
500 return implode(', ', $this->qb_from);
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * Close DB Connection
507 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @return void
509 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300510 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300512 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515}
516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300518/* Location: ./system/database/drivers/mysql/mysql_driver.php */