blob: 9c140a69be0a9eb48ffb0288dbaa941600c44251 [file] [log] [blame]
Andrey Andreevbd44d5a2012-03-20 22:59:29 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -04002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Timothy Warren80ab8162011-08-22 18:26:12 -04006 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -04007 * NOTICE OF LICENSE
Andrey Andreevbd44d5a2012-03-20 22:59:29 +02008 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -04009 * Licensed under the Open Software License version 3.0
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020010 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -040011 * 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 *
Timothy Warren80ab8162011-08-22 18:26:12 -040019 * @package CodeIgniter
Timothy Warrend1a5ba22011-10-21 04:45:28 -040020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Timothy Warrend1a5ba22011-10-21 04:45:28 -040022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Timothy Warren80ab8162011-08-22 18:26:12 -040023 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040024 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040025 * @filesource
26 */
27
Timothy Warren80ab8162011-08-22 18:26:12 -040028/**
Timothy Warren02615962011-08-24 08:21:36 -040029 * PDO Database Adapter Class
Timothy Warren80ab8162011-08-22 18:26:12 -040030 *
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
Timothy Warren80ab8162011-08-22 18:26:12 -040033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Timothy Warrend1a5ba22011-10-21 04:45:28 -040038 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_pdo_driver extends CI_DB {
42
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020043 public $dbdriver = 'pdo';
Timothy Warren80ab8162011-08-22 18:26:12 -040044
45 // the character used to excape - not necessary for PDO
Andrey Andreev5663b1e2012-06-24 00:28:42 +030046 protected $_escape_char = '"';
Taufan Aditya18209332012-02-09 16:07:27 +070047
48 // clause and character used for LIKE escape sequences
Andrey Andreeve9f20952012-04-06 19:30:41 +030049 protected $_like_escape_str = " ESCAPE '%s' ";
50 protected $_like_escape_chr = '!';
Timothy Warren80ab8162011-08-22 18:26:12 -040051
52 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
Timothy Warren667c9fe2012-03-19 19:06:34 -040055 * used for the count_all() and count_all_results() functions.
Timothy Warren80ab8162011-08-22 18:26:12 -040056 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword;
Taufan Aditya18209332012-02-09 16:07:27 +070059
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030060 protected $trans_enabled = FALSE;
61
Andrey Andreevfbba54e2012-06-23 20:26:31 +030062 // need to track the PDO options
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020063 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040064
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020065 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040066 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040067 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070068
Alex Bilbie48a2baf2012-06-02 11:09:54 +010069 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070070 {
71 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070072 // This is for general PDO users, who tend to have a full DSN string.
Andrey Andreev7151e802012-06-25 00:47:41 +030073 $this->subdriver = $match[1];
Taufan Aditya18209332012-02-09 16:07:27 +070074 }
75 else
76 {
77 // Try to build a complete DSN string from params
78 $this->_connect_string($params);
79 }
80
Andrey Andreev7151e802012-06-25 00:47:41 +030081 if (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
82 {
83 $this->subdriver = 'dblib';
84 }
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030085 elseif ($this->subdriver === '4D')
86 {
87 $this->subdriver = '4d';
88 }
Andrey Andreev7151e802012-06-25 00:47:41 +030089
Timothy Warrenc7ba6642011-09-14 12:25:14 -040090 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070091 // this one depends on the driver being used
Andrey Andreev1732b1b2012-06-24 02:42:38 +030092 if ($this->subdriver === 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040093 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +030094 $this->_escape_char = '';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040095 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040096 }
Timothy Warren80ab8162011-08-22 18:26:12 -040097 }
98
99 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700100 * Connection String
101 *
Taufan Aditya18209332012-02-09 16:07:27 +0700102 * @param array
103 * @return void
104 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200105 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700106 {
107 if (strpos($this->hostname, ':'))
108 {
109 // hostname generally would have this prototype
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300110 // $db['hostname'] = 'subdriver:host(/Server(/DSN))=hostname(/DSN);';
111 // We need to get the prefix (subdriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500112 $dsnarray = explode(':', $this->hostname);
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300113 $this->subdriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700114
115 // End dsn with a semicolon for extra backward compability
116 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500117 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500118 {
119 $this->dsn .= rtrim($this->hostname, ';').';';
120 }
Taufan Aditya18209332012-02-09 16:07:27 +0700121 }
122 else
123 {
124 // Invalid DSN, display an error
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300125 if ( ! array_key_exists('subdriver', $params))
Taufan Aditya18209332012-02-09 16:07:27 +0700126 {
127 show_error('Invalid DB Connection String for PDO');
128 }
129
130 // Assuming that the following DSN string format is used:
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300131 // $dsn = 'pdo://username:password@hostname:port/database?subdriver=pgsql';
132 $this->dsn = $this->subdriver.':';
Taufan Aditya18209332012-02-09 16:07:27 +0700133
Andrey Andreevd42cc462012-06-24 23:52:40 +0300134 }
Taufan Aditya18209332012-02-09 16:07:27 +0700135 }
136
Andrey Andreev2387ed32012-04-07 00:11:14 +0300137 // --------------------------------------------------------------------
138
Taufan Aditya18209332012-02-09 16:07:27 +0700139 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400140 * Non-persistent database connection
141 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300142 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200143 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700144 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300145 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700146 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300147 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300148 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300149
Taufan Aditya18209332012-02-09 16:07:27 +0700150 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200151 try
Taufan Aditya18209332012-02-09 16:07:27 +0700152 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300153 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200154 }
155 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700156 {
157 if ($this->db_debug && empty($this->failover))
158 {
159 $this->display_error($e->getMessage(), '', TRUE);
160 }
161
162 return FALSE;
163 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400164 }
165
166 // --------------------------------------------------------------------
167
168 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300169 * Persistent database connection
170 *
171 * @return object
172 */
173 public function db_pconnect()
174 {
175 return $this->db_connect(TRUE);
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200181 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400182 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400183 * @return string
184 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200185 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400186 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200187 return isset($this->data_cache['version'])
188 ? $this->data_cache['version']
189 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Execute the query
196 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400197 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200198 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400199 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200200 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400201 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300202 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400203 }
204
205 // --------------------------------------------------------------------
206
207 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400208 * Begin Transaction
209 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400210 * @return bool
211 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200212 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400213 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400214 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300215 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400216 {
217 return TRUE;
218 }
219
220 // Reset the transaction failure flag.
221 // If the $test_mode flag is set to TRUE transactions will be rolled back
222 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300223 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400224
Timothy Warrenab347582011-08-23 12:29:29 -0400225 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Commit Transaction
232 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400233 * @return bool
234 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200235 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400236 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400237 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300238 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400239 {
240 return TRUE;
241 }
242
Andrey Andreev80144bf2012-04-06 22:19:26 +0300243 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Rollback Transaction
250 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 * @return bool
252 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200253 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400255 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300256 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400257 {
258 return TRUE;
259 }
260
Andrey Andreev80144bf2012-04-06 22:19:26 +0300261 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Escape String
268 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400269 * @param string
270 * @param bool whether or not the string will be used in a LIKE condition
271 * @return string
272 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200273 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400274 {
275 if (is_array($str))
276 {
277 foreach ($str as $key => $val)
278 {
279 $str[$key] = $this->escape_str($val, $like);
280 }
281
282 return $str;
283 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200284
Andrey Andreev2387ed32012-04-07 00:11:14 +0300285 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400286 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200287
Andrey Andreev2387ed32012-04-07 00:11:14 +0300288 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400289 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400290 {
291 $str = substr($str, 1, -1);
292 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200293
Timothy Warren80ab8162011-08-22 18:26:12 -0400294 // escape LIKE condition wildcards
295 if ($like === TRUE)
296 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200297 return str_replace(array($this->_like_escape_chr, '%', '_'),
298 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
299 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400300 }
301
302 return $str;
303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Affected Rows
309 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200310 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400311 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200312 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400313 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300314 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200321 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300322 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200323 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400324 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200325 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400326 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200327 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400328 }
329
330 // --------------------------------------------------------------------
331
332 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400333 * Show table query
334 *
335 * Generates a platform-specific query string so that the table names can be fetched
336 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200337 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400338 * @return string
339 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200340 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400341 {
Andrey Andreev27105662012-06-24 22:44:00 +0300342 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Timothy Warren80ab8162011-08-22 18:26:12 -0400343
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100344 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400345 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200346 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400347 }
348
349 return $sql;
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Show column query
356 *
357 * Generates a platform-specific query string so that the column names can be fetched
358 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400359 * @param string the table name
360 * @return string
361 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200362 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400363 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300364 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Field data query
371 *
372 * Generates a platform-specific query so that the column data can be retrieved
373 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400374 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200375 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400376 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200377 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400378 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300379 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400380 }
381
382 // --------------------------------------------------------------------
383
384 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400386 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200387 * Returns an array containing code and message of the last
388 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400389 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400391 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400393 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200394 $error = array('code' => '00000', 'message' => '');
395 $pdo_error = $this->conn_id->errorInfo();
396
397 if (empty($pdo_error[0]))
398 {
399 return $error;
400 }
401
402 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
403 if (isset($pdo_error[2]))
404 {
405 $error['message'] = $pdo_error[2];
406 }
407
408 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400409 }
410
411 // --------------------------------------------------------------------
412
413 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400414 * Update_Batch statement
415 *
416 * Generates a platform-specific batch update string from the supplied data
417 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400418 * @param string the table name
419 * @param array the update data
420 * @param array the where clause
421 * @return string
422 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200423 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400424 {
Andrey Andreevdf8894f2012-06-25 16:18:50 +0300425 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100426 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400427
428 foreach ($values as $key => $val)
429 {
430 $ids[] = $val[$index];
431
432 foreach (array_keys($val) as $field)
433 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100434 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400435 {
436 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
437 }
438 }
439 }
440
Andrey Andreeve9f20952012-04-06 19:30:41 +0300441 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400442 $cases = '';
443
444 foreach ($final as $k => $v)
445 {
446 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700447
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400448 foreach ($v as $row)
449 {
450 $cases .= $row."\n";
451 }
452
453 $cases .= 'ELSE '.$k.' END, ';
454 }
455
456 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400457 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
458
459 return $sql;
460 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400461
Timothy Warren80ab8162011-08-22 18:26:12 -0400462}
463
Timothy Warren80ab8162011-08-22 18:26:12 -0400464/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300465/* Location: ./system/database/drivers/pdo/pdo_driver.php */