blob: 7262591ee7e9dcf0a9bdc9c1a3202d67c337f148 [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
Andrey Andreev2caf2892012-01-27 00:12:03 +020048 protected $_random_keyword = ' RAND()'; // database specific random keyword
RH Beckercfdb2322011-10-03 17:28:32 -070049
Derek Allard2067d1a2008-11-13 22:59:24 +000050 /**
Andrey Andreev2caf2892012-01-27 00:12:03 +020051 * Whether to use the MySQL "delete hack" which allows the number
52 * of affected rows to be shown. Uses a preg_replace when enabled,
53 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020055 public $delete_hack = TRUE;
56
Andrey Andreev473130a2012-06-24 02:51:18 +030057 /**
58 * Constructor
59 *
60 * @param array
61 * @return void
62 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020063 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020065 parent::__construct($params);
66
Andrey Andreeve4c30192012-06-04 15:08:24 +030067 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
69 $this->hostname .= ':'.$this->port;
70 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020071 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Andrey Andreev473130a2012-06-24 02:51:18 +030073 // --------------------------------------------------------------------
74
Andrey Andreev2caf2892012-01-27 00:12:03 +020075 /**
76 * Non-persistent database connection
77 *
78 * @return resource
79 */
80 public function db_connect()
81 {
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020082 if ($this->compress === TRUE)
83 {
84 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE, MYSQL_CLIENT_COMPRESS);
85 }
86 else
87 {
88 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
89 }
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 // --------------------------------------------------------------------
93
94 /**
95 * Persistent database connection
96 *
Derek Allard2067d1a2008-11-13 22:59:24 +000097 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020098 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020099 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200101 if ($this->compress === TRUE)
102 {
103 return @mysql_pconnect($this->hostname, $this->username, $this->password, MYSQL_CLIENT_COMPRESS);
104 }
105 else
106 {
107 return @mysql_pconnect($this->hostname, $this->username, $this->password);
108 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // --------------------------------------------------------------------
112
113 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 * Reconnect
115 *
116 * Keep / reestablish the db connection if no queries have been
117 * sent for a length of time exceeding the server's idle timeout
118 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 * @return void
120 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200121 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000122 {
123 if (mysql_ping($this->conn_id) === FALSE)
124 {
125 $this->conn_id = FALSE;
126 }
127 }
128
129 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Jones87cbafc2009-02-27 16:29:59 +0000131 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * Select the database
133 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200134 * @param string database name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200135 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200136 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200137 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200139 if ($database === '')
140 {
141 $database = $this->database;
142 }
143
144 if (@mysql_select_db($database, $this->conn_id))
145 {
146 $this->database = $database;
147 return TRUE;
148 }
149
150 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Set client character set
157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200159 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200161 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200163 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165
166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200169 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @return string
172 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200173 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200175 return isset($this->data_cache['version'])
176 ? $this->data_cache['version']
177 : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Execute the query
184 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200186 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200187 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200188 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200190 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 // --------------------------------------------------------------------
194
195 /**
196 * Prep the query
197 *
198 * If needed, each database adapter can prep the query string
199 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * @param string an SQL query
201 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200202 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200203 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200205 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
206 // modifies the query so that it a proper number of affected rows is returned.
207 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200209 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 return $sql;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Begin Transaction
219 *
Barry Mienydd671972010-10-04 16:33:58 +0200220 * @return bool
221 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200222 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200225 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 return TRUE;
228 }
229
230 // Reset the transaction failure flag.
231 // If the $test_mode flag is set to TRUE transactions will be rolled back
232 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200233 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $this->simple_query('SET AUTOCOMMIT=0');
236 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
237 return TRUE;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Commit Transaction
244 *
Barry Mienydd671972010-10-04 16:33:58 +0200245 * @return bool
246 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200247 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200250 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 return TRUE;
253 }
254
255 $this->simple_query('COMMIT');
256 $this->simple_query('SET AUTOCOMMIT=1');
257 return TRUE;
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Rollback Transaction
264 *
Barry Mienydd671972010-10-04 16:33:58 +0200265 * @return bool
266 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200267 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200270 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 return TRUE;
273 }
274
275 $this->simple_query('ROLLBACK');
276 $this->simple_query('SET AUTOCOMMIT=1');
277 return TRUE;
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 // --------------------------------------------------------------------
281
282 /**
283 * Escape String
284 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000286 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 * @return string
288 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200289 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200290 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 if (is_array($str))
292 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500293 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500294 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000295 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jones37f4b9c2011-07-01 17:56:50 -0500298 return $str;
299 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000300
Andrey Andreevf6a41142012-03-12 15:34:10 +0200301 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jonese4ed5832009-02-20 21:44:59 +0000303 // escape LIKE condition wildcards
304 if ($like === TRUE)
305 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300306 return str_replace(array($this->_like_escape_chr, '%', '_'),
307 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
308 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Jonese4ed5832009-02-20 21:44:59 +0000311 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
315
316 /**
317 * Affected Rows
318 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200319 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200321 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 return @mysql_affected_rows($this->conn_id);
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
327
328 /**
329 * Insert ID
330 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200331 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200333 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 return @mysql_insert_id($this->conn_id);
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * List table query
342 *
343 * Generates a platform-specific query string so that the table names can be fetched
344 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200345 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @return string
347 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200348 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300350 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100352 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200354 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
357 return $sql;
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
361
362 /**
363 * Show column query
364 *
365 * Generates a platform-specific query string so that the column names can be fetched
366 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * @param string the table name
368 * @return string
369 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300370 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200372 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374
375 // --------------------------------------------------------------------
376
377 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200378 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200381 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200383 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100385 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200386 {
387 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
388 }
389
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200390 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200391 $query = $query->result_object();
392
393 $retval = array();
394 for ($i = 0, $c = count($query); $i < $c; $i++)
395 {
396 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
397
398 $retval[$i] = new stdClass();
399 $retval[$i]->name = $query[$i]->Field;
400 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
401 $retval[$i]->default = $query[$i]->Default;
402 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
403 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
404 }
405
406 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
409 // --------------------------------------------------------------------
410
411 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 * Returns an array containing code and message of the last
415 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200417 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200419 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200421 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 // --------------------------------------------------------------------
425
426 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600427 * Update_Batch statement
428 *
429 * Generates a platform-specific batch update string from the supplied data
430 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600431 * @param string the table name
432 * @param array the update data
433 * @param array the where clause
434 * @return string
435 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200436 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600437 {
438 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500439 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600440 {
441 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200442
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500443 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200444 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100445 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600446 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500447 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600448 }
449 }
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Jones20aa2bd2010-03-02 17:28:07 -0600452 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500453 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600454 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200455 $cases .= $k." = CASE \n"
456 .implode("\n", $v)."\n"
457 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Andrey Andreev2caf2892012-01-27 00:12:03 +0200460 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100461 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev2caf2892012-01-27 00:12:03 +0200462 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600463 }
464
465 // --------------------------------------------------------------------
466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300468 * FROM tables
469 *
470 * Groups tables in FROM clauses if needed, so there is no confusion
471 * about operator precedence.
472 *
473 * @return string
474 */
475 protected function _from_tables()
476 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300477 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300478 {
479 return '('.implode(', ', $this->qb_from).')';
480 }
481
482 return implode(', ', $this->qb_from);
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * Close DB Connection
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @return void
491 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300492 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300494 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497}
498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300500/* Location: ./system/database/drivers/mysql/mysql_driver.php */