blob: 35473016ff48f55744abbace27ae1ef4c125ba67 [file] [log] [blame]
Andrey Andreev2caf2892012-01-27 00:12:03 +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 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
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/**
29 * MySQL Database Adapter Class
30 *
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_mysql_driver extends CI_DB {
42
Andrey Andreev2caf2892012-01-27 00:12:03 +020043 public $dbdriver = 'mysql';
Derek Allard2067d1a2008-11-13 22:59:24 +000044
45 // The character used for escaping
Andrey Andreev2caf2892012-01-27 00:12:03 +020046 protected $_escape_char = '`';
Derek Jonese4ed5832009-02-20 21:44:59 +000047
48 // clause and character used for LIKE escape sequences - not used in MySQL
Andrey Andreev2caf2892012-01-27 00:12:03 +020049 protected $_like_escape_str = '';
Andrey Andreev21cb2d32012-05-25 01:01:06 +030050 protected $_like_escape_chr = '\\';
Barry Mienydd671972010-10-04 16:33:58 +020051
Andrey Andreev2caf2892012-01-27 00:12:03 +020052 protected $_random_keyword = ' RAND()'; // database specific random keyword
RH Beckercfdb2322011-10-03 17:28:32 -070053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 /**
Andrey Andreev2caf2892012-01-27 00:12:03 +020055 * 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 Andreev2caf2892012-01-27 00:12:03 +020059 public $delete_hack = TRUE;
60
Andrey Andreev473130a2012-06-24 02:51:18 +030061 /**
62 * Constructor
63 *
64 * @param array
65 * @return void
66 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020067 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020069 parent::__construct($params);
70
Andrey Andreeve4c30192012-06-04 15:08:24 +030071 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
73 $this->hostname .= ':'.$this->port;
74 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020075 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Andrey Andreev473130a2012-06-24 02:51:18 +030077 // --------------------------------------------------------------------
78
Andrey Andreev2caf2892012-01-27 00:12:03 +020079 /**
80 * Non-persistent database connection
81 *
82 * @return resource
83 */
84 public function db_connect()
85 {
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020086 if ($this->compress === TRUE)
87 {
88 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE, MYSQL_CLIENT_COMPRESS);
89 }
90 else
91 {
92 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
93 }
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // --------------------------------------------------------------------
97
98 /**
99 * Persistent database connection
100 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200103 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200105 if ($this->compress === TRUE)
106 {
107 return @mysql_pconnect($this->hostname, $this->username, $this->password, MYSQL_CLIENT_COMPRESS);
108 }
109 else
110 {
111 return @mysql_pconnect($this->hostname, $this->username, $this->password);
112 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // --------------------------------------------------------------------
116
117 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000118 * Reconnect
119 *
120 * Keep / reestablish the db connection if no queries have been
121 * sent for a length of time exceeding the server's idle timeout
122 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000123 * @return void
124 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200125 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000126 {
127 if (mysql_ping($this->conn_id) === FALSE)
128 {
129 $this->conn_id = FALSE;
130 }
131 }
132
133 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Jones87cbafc2009-02-27 16:29:59 +0000135 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * Select the database
137 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200138 * @param string database name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200139 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200140 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200141 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200143 if ($database === '')
144 {
145 $database = $this->database;
146 }
147
148 if (@mysql_select_db($database, $this->conn_id))
149 {
150 $this->database = $database;
151 return TRUE;
152 }
153
154 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Set client character set
161 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200163 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200165 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200167 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200173 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @return string
176 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200177 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200179 return isset($this->data_cache['version'])
180 ? $this->data_cache['version']
181 : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Execute the query
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200190 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200191 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200192 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200194 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
200 * Prep the query
201 *
202 * If needed, each database adapter can prep the query string
203 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @param string an SQL query
205 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200206 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200207 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200209 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
210 // modifies the query so that it a proper number of affected rows is returned.
211 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200213 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 return $sql;
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Begin Transaction
223 *
Barry Mienydd671972010-10-04 16:33:58 +0200224 * @return bool
225 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200226 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200229 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
231 return TRUE;
232 }
233
234 // Reset the transaction failure flag.
235 // If the $test_mode flag is set to TRUE transactions will be rolled back
236 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200237 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 $this->simple_query('SET AUTOCOMMIT=0');
240 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
241 return TRUE;
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Commit Transaction
248 *
Barry Mienydd671972010-10-04 16:33:58 +0200249 * @return bool
250 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200251 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200254 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 return TRUE;
257 }
258
259 $this->simple_query('COMMIT');
260 $this->simple_query('SET AUTOCOMMIT=1');
261 return TRUE;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Rollback Transaction
268 *
Barry Mienydd671972010-10-04 16:33:58 +0200269 * @return bool
270 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200271 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200274 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 return TRUE;
277 }
278
279 $this->simple_query('ROLLBACK');
280 $this->simple_query('SET AUTOCOMMIT=1');
281 return TRUE;
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // --------------------------------------------------------------------
285
286 /**
287 * Escape String
288 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000290 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @return string
292 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200293 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200294 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 if (is_array($str))
296 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500297 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500298 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jones37f4b9c2011-07-01 17:56:50 -0500302 return $str;
303 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000304
Andrey Andreevf6a41142012-03-12 15:34:10 +0200305 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jonese4ed5832009-02-20 21:44:59 +0000307 // escape LIKE condition wildcards
308 if ($like === TRUE)
309 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300310 return str_replace(array($this->_like_escape_chr, '%', '_'),
311 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
312 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Jonese4ed5832009-02-20 21:44:59 +0000315 return $str;
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 * Affected Rows
322 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200323 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200325 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 return @mysql_affected_rows($this->conn_id);
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // --------------------------------------------------------------------
331
332 /**
333 * Insert ID
334 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200335 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200337 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 return @mysql_insert_id($this->conn_id);
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 * List table query
346 *
347 * Generates a platform-specific query string so that the table names can be fetched
348 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200349 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return string
351 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200352 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300354 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000355
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100356 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200358 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
361 return $sql;
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // --------------------------------------------------------------------
365
366 /**
367 * Show column query
368 *
369 * Generates a platform-specific query string so that the column names can be fetched
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param string the table name
372 * @return string
373 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300374 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200376 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378
379 // --------------------------------------------------------------------
380
381 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200382 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200385 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200387 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100389 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200390 {
391 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
392 }
393
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200394 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200395 $query = $query->result_object();
396
397 $retval = array();
398 for ($i = 0, $c = count($query); $i < $c; $i++)
399 {
400 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
401
402 $retval[$i] = new stdClass();
403 $retval[$i]->name = $query[$i]->Field;
404 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
405 $retval[$i]->default = $query[$i]->Default;
406 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
407 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
408 }
409
410 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
412
413 // --------------------------------------------------------------------
414
415 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200416 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200418 * Returns an array containing code and message of the last
419 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200421 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200423 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200425 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427
428 // --------------------------------------------------------------------
429
430 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600431 * Update_Batch statement
432 *
433 * Generates a platform-specific batch update string from the supplied data
434 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600435 * @param string the table name
436 * @param array the update data
437 * @param array the where clause
438 * @return string
439 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200440 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600441 {
442 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500443 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600444 {
445 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200446
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500447 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200448 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100449 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600450 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500451 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600452 }
453 }
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Jones20aa2bd2010-03-02 17:28:07 -0600456 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500457 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600458 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200459 $cases .= $k." = CASE \n"
460 .implode("\n", $v)."\n"
461 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Andrey Andreev2caf2892012-01-27 00:12:03 +0200464 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100465 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev2caf2892012-01-27 00:12:03 +0200466 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600467 }
468
469 // --------------------------------------------------------------------
470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * Close DB Connection
473 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @return void
475 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300476 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300478 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481}
482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300484/* Location: ./system/database/drivers/mysql/mysql_driver.php */