blob: 521bdfd9f186cad8f4af2e3d4ca28e931e2c0295 [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
134 // Add hostname to the DSN for databases that need it
Andrey Andreev85d1bd82012-06-25 01:46:32 +0300135 if ( ! empty($this->hostname) && strpos($this->hostname, ':') === FALSE && $this->subdriver === 'informix')
Taufan Aditya18209332012-02-09 16:07:27 +0700136 {
137 $this->dsn .= 'host='.$this->hostname.';';
138 }
139
140 // Add a port to the DSN for databases that can use it
Andrey Andreev85d1bd82012-06-25 01:46:32 +0300141 if ( ! empty($this->port) && in_array($this->subdriver, array('informix', 'ibm'), TRUE))
Taufan Aditya18209332012-02-09 16:07:27 +0700142 {
Andrey Andreev7151e802012-06-25 00:47:41 +0300143 $this->dsn .= 'port='.$this->port.';';
Taufan Aditya18209332012-02-09 16:07:27 +0700144 }
145 }
146
147 // Add the database name to the DSN, if needed
Andrey Andreev6b4bffa2012-06-25 02:29:20 +0300148 if (stripos($this->dsn, 'database') === FALSE && $this->subdriver === 'ibm')
Andrey Andreevd42cc462012-06-24 23:52:40 +0300149 {
150 if (stripos($this->dsn, 'dsn') === FALSE)
151 {
152 $this->dsn .= 'database='.$this->database.';';
153 }
154 }
Taufan Aditya18209332012-02-09 16:07:27 +0700155 }
156
Andrey Andreev2387ed32012-04-07 00:11:14 +0300157 // --------------------------------------------------------------------
158
Taufan Aditya18209332012-02-09 16:07:27 +0700159 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400160 * Non-persistent database connection
161 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300162 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200163 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700164 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300165 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700166 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300167 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300168 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300169
Taufan Aditya18209332012-02-09 16:07:27 +0700170 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200171 try
Taufan Aditya18209332012-02-09 16:07:27 +0700172 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300173 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200174 }
175 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700176 {
177 if ($this->db_debug && empty($this->failover))
178 {
179 $this->display_error($e->getMessage(), '', TRUE);
180 }
181
182 return FALSE;
183 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400184 }
185
186 // --------------------------------------------------------------------
187
188 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300189 * Persistent database connection
190 *
191 * @return object
192 */
193 public function db_pconnect()
194 {
195 return $this->db_connect(TRUE);
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200201 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400202 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400203 * @return string
204 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200205 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400206 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200207 return isset($this->data_cache['version'])
208 ? $this->data_cache['version']
209 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Execute the query
216 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400217 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200218 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400219 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200220 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400221 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300222 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400223 }
224
225 // --------------------------------------------------------------------
226
227 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400228 * Begin Transaction
229 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400230 * @return bool
231 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200232 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400233 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400234 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300235 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400236 {
237 return TRUE;
238 }
239
240 // Reset the transaction failure flag.
241 // If the $test_mode flag is set to TRUE transactions will be rolled back
242 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300243 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400244
Timothy Warrenab347582011-08-23 12:29:29 -0400245 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Commit Transaction
252 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400253 * @return bool
254 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200255 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400256 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400257 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300258 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400259 {
260 return TRUE;
261 }
262
Andrey Andreev80144bf2012-04-06 22:19:26 +0300263 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Rollback Transaction
270 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400271 * @return bool
272 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200273 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400274 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400275 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300276 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400277 {
278 return TRUE;
279 }
280
Andrey Andreev80144bf2012-04-06 22:19:26 +0300281 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Escape String
288 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400289 * @param string
290 * @param bool whether or not the string will be used in a LIKE condition
291 * @return string
292 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200293 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400294 {
295 if (is_array($str))
296 {
297 foreach ($str as $key => $val)
298 {
299 $str[$key] = $this->escape_str($val, $like);
300 }
301
302 return $str;
303 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200304
Andrey Andreev2387ed32012-04-07 00:11:14 +0300305 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400306 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200307
Andrey Andreev2387ed32012-04-07 00:11:14 +0300308 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400309 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400310 {
311 $str = substr($str, 1, -1);
312 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200313
Timothy Warren80ab8162011-08-22 18:26:12 -0400314 // escape LIKE condition wildcards
315 if ($like === TRUE)
316 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200317 return str_replace(array($this->_like_escape_chr, '%', '_'),
318 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
319 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400320 }
321
322 return $str;
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Affected Rows
329 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200330 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400331 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200332 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400333 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300334 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200341 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300342 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200343 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400344 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200345 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400346 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200347 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400348 }
349
350 // --------------------------------------------------------------------
351
352 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400353 * Show table query
354 *
355 * Generates a platform-specific query string so that the table names can be fetched
356 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200357 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400358 * @return string
359 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200360 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400361 {
Andrey Andreev27105662012-06-24 22:44:00 +0300362 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Timothy Warren80ab8162011-08-22 18:26:12 -0400363
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100364 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400365 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200366 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400367 }
368
369 return $sql;
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Show column query
376 *
377 * Generates a platform-specific query string so that the column names can be fetched
378 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400379 * @param string the table name
380 * @return string
381 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200382 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400383 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300384 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * Field data query
391 *
392 * Generates a platform-specific query so that the column data can be retrieved
393 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400394 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200395 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400396 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200397 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400398 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300399 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400400 }
401
402 // --------------------------------------------------------------------
403
404 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200405 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400406 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200407 * Returns an array containing code and message of the last
408 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400409 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200410 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400411 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400413 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 $error = array('code' => '00000', 'message' => '');
415 $pdo_error = $this->conn_id->errorInfo();
416
417 if (empty($pdo_error[0]))
418 {
419 return $error;
420 }
421
422 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
423 if (isset($pdo_error[2]))
424 {
425 $error['message'] = $pdo_error[2];
426 }
427
428 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400429 }
430
431 // --------------------------------------------------------------------
432
433 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400434 * Update_Batch statement
435 *
436 * Generates a platform-specific batch update string from the supplied data
437 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400438 * @param string the table name
439 * @param array the update data
440 * @param array the where clause
441 * @return string
442 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200443 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400444 {
Taufan Aditya18209332012-02-09 16:07:27 +0700445 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100446 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400447
448 foreach ($values as $key => $val)
449 {
450 $ids[] = $val[$index];
451
452 foreach (array_keys($val) as $field)
453 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100454 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400455 {
456 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
457 }
458 }
459 }
460
Andrey Andreeve9f20952012-04-06 19:30:41 +0300461 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400462 $cases = '';
463
464 foreach ($final as $k => $v)
465 {
466 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700467
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400468 foreach ($v as $row)
469 {
470 $cases .= $row."\n";
471 }
472
473 $cases .= 'ELSE '.$k.' END, ';
474 }
475
476 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400477 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
478
479 return $sql;
480 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400481
Timothy Warren80ab8162011-08-22 18:26:12 -0400482 // --------------------------------------------------------------------
483
484 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400485 * Limit string
486 *
487 * Generates a platform-specific LIMIT clause
488 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400489 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200490 * @param int the number of rows to limit the query to
491 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400492 * @return string
493 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200494 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400495 {
Andrey Andreev27105662012-06-24 22:44:00 +0300496 $offset = ($offset == 0) ? '' : $offset.', ';
497 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400498 }
499
Timothy Warren80ab8162011-08-22 18:26:12 -0400500}
501
Timothy Warren80ab8162011-08-22 18:26:12 -0400502/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300503/* Location: ./system/database/drivers/pdo/pdo_driver.php */