blob: 0029e1ebe84c62764910065105c644bf3e8a8b70 [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 Andreevfbba54e2012-06-23 20:26:31 +030060 // need to track the PDO options
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020061 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040062
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020063 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040064 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040065 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070066
Alex Bilbie48a2baf2012-06-02 11:09:54 +010067 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070068 {
69 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070070 // This is for general PDO users, who tend to have a full DSN string.
Andrey Andreevfbba54e2012-06-23 20:26:31 +030071 $this->subdriver = end($match);
Taufan Aditya18209332012-02-09 16:07:27 +070072 }
73 else
74 {
75 // Try to build a complete DSN string from params
76 $this->_connect_string($params);
77 }
78
Timothy Warrenc7ba6642011-09-14 12:25:14 -040079 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070080 // this one depends on the driver being used
Andrey Andreev1732b1b2012-06-24 02:42:38 +030081 if ($this->subdriver === 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040082 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +030083 $this->_escape_char = '';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040084 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040085 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020086
87 $this->trans_enabled = FALSE;
Andrey Andreevd42cc462012-06-24 23:52:40 +030088// $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Timothy Warren80ab8162011-08-22 18:26:12 -040089 }
90
91 /**
Taufan Aditya18209332012-02-09 16:07:27 +070092 * Connection String
93 *
Taufan Aditya18209332012-02-09 16:07:27 +070094 * @param array
95 * @return void
96 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020097 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +070098 {
99 if (strpos($this->hostname, ':'))
100 {
101 // hostname generally would have this prototype
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300102 // $db['hostname'] = 'subdriver:host(/Server(/DSN))=hostname(/DSN);';
103 // We need to get the prefix (subdriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500104 $dsnarray = explode(':', $this->hostname);
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300105 $this->subdriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700106
107 // End dsn with a semicolon for extra backward compability
108 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500109 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500110 {
111 $this->dsn .= rtrim($this->hostname, ';').';';
112 }
Taufan Aditya18209332012-02-09 16:07:27 +0700113 }
114 else
115 {
116 // Invalid DSN, display an error
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300117 if ( ! array_key_exists('subdriver', $params))
Taufan Aditya18209332012-02-09 16:07:27 +0700118 {
119 show_error('Invalid DB Connection String for PDO');
120 }
121
122 // Assuming that the following DSN string format is used:
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300123 // $dsn = 'pdo://username:password@hostname:port/database?subdriver=pgsql';
124 $this->dsn = $this->subdriver.':';
Taufan Aditya18209332012-02-09 16:07:27 +0700125
126 // Add hostname to the DSN for databases that need it
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200127 if ( ! empty($this->hostname)
Timothy Warren928d83c2012-02-13 14:07:57 -0500128 && strpos($this->hostname, ':') === FALSE
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300129 && in_array($this->subdriver, array('informix', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700130 {
131 $this->dsn .= 'host='.$this->hostname.';';
132 }
133
134 // Add a port to the DSN for databases that can use it
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300135 if ( ! empty($this->port) && in_array($this->subdriver, array('informix', 'ibm', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700136 {
137 $this->dsn .= 'port='.$this->port.';';
138 }
139 }
140
141 // Add the database name to the DSN, if needed
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300142 if (stripos($this->dsn, 'dbname') === FALSE
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300143 && in_array($this->subdriver, array('4D', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300144 {
145 $this->dsn .= 'dbname='.$this->database.';';
146 }
Andrey Andreevd42cc462012-06-24 23:52:40 +0300147 elseif (stripos($this->dsn, 'database') === FALSE && $this->subdriver === ibm')
148 {
149 if (stripos($this->dsn, 'dsn') === FALSE)
150 {
151 $this->dsn .= 'database='.$this->database.';';
152 }
153 }
Taufan Aditya18209332012-02-09 16:07:27 +0700154
Andrey Andreevd42cc462012-06-24 23:52:40 +0300155 // Add charset to the DSN, if needed
156 if ( ! empty($this->char_set) && in_array($this->subdriver, array('4D', 'sybase', 'mssql', 'dblib')))
157 {
158 $this->dsn .= 'charset='.$this->char_set.';';
159 }
Taufan Aditya18209332012-02-09 16:07:27 +0700160 }
161
Andrey Andreev2387ed32012-04-07 00:11:14 +0300162 // --------------------------------------------------------------------
163
Taufan Aditya18209332012-02-09 16:07:27 +0700164 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400165 * Non-persistent database connection
166 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300167 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200168 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700169 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300170 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700171 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300172 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300173 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300174
Taufan Aditya18209332012-02-09 16:07:27 +0700175 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200176 try
Taufan Aditya18209332012-02-09 16:07:27 +0700177 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300178 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200179 }
180 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700181 {
182 if ($this->db_debug && empty($this->failover))
183 {
184 $this->display_error($e->getMessage(), '', TRUE);
185 }
186
187 return FALSE;
188 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400189 }
190
191 // --------------------------------------------------------------------
192
193 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300194 * Persistent database connection
195 *
196 * @return object
197 */
198 public function db_pconnect()
199 {
200 return $this->db_connect(TRUE);
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200206 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400207 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400208 * @return string
209 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200210 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400211 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200212 return isset($this->data_cache['version'])
213 ? $this->data_cache['version']
214 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Execute the query
221 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400222 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200223 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400224 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200225 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400226 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300227 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400228 }
229
230 // --------------------------------------------------------------------
231
232 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400233 * Begin Transaction
234 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400235 * @return bool
236 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200237 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400238 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400239 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300240 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400241 {
242 return TRUE;
243 }
244
245 // Reset the transaction failure flag.
246 // If the $test_mode flag is set to TRUE transactions will be rolled back
247 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300248 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400249
Timothy Warrenab347582011-08-23 12:29:29 -0400250 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Commit Transaction
257 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400258 * @return bool
259 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200260 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400261 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400262 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300263 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400264 {
265 return TRUE;
266 }
267
Andrey Andreev80144bf2012-04-06 22:19:26 +0300268 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Rollback Transaction
275 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 * @return bool
277 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200278 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400279 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300281 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400282 {
283 return TRUE;
284 }
285
Andrey Andreev80144bf2012-04-06 22:19:26 +0300286 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Escape String
293 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400294 * @param string
295 * @param bool whether or not the string will be used in a LIKE condition
296 * @return string
297 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200298 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400299 {
300 if (is_array($str))
301 {
302 foreach ($str as $key => $val)
303 {
304 $str[$key] = $this->escape_str($val, $like);
305 }
306
307 return $str;
308 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200309
Andrey Andreev2387ed32012-04-07 00:11:14 +0300310 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400311 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200312
Andrey Andreev2387ed32012-04-07 00:11:14 +0300313 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400314 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400315 {
316 $str = substr($str, 1, -1);
317 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200318
Timothy Warren80ab8162011-08-22 18:26:12 -0400319 // escape LIKE condition wildcards
320 if ($like === TRUE)
321 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200322 return str_replace(array($this->_like_escape_chr, '%', '_'),
323 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
324 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400325 }
326
327 return $str;
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Affected Rows
334 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200335 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400336 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200337 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400338 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300339 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200346 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300347 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200348 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400349 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200350 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400351 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200352 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400358 * Show table query
359 *
360 * Generates a platform-specific query string so that the table names can be fetched
361 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200362 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400363 * @return string
364 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200365 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400366 {
Andrey Andreev27105662012-06-24 22:44:00 +0300367 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Timothy Warren80ab8162011-08-22 18:26:12 -0400368
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100369 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400370 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200371 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400372 }
373
374 return $sql;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Show column query
381 *
382 * Generates a platform-specific query string so that the column names can be fetched
383 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400384 * @param string the table name
385 * @return string
386 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200387 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400388 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300389 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * Field data query
396 *
397 * Generates a platform-specific query so that the column data can be retrieved
398 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400399 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200400 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400401 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200402 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400403 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300404 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200410 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400411 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 * Returns an array containing code and message of the last
413 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400414 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200415 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400416 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200417 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400418 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200419 $error = array('code' => '00000', 'message' => '');
420 $pdo_error = $this->conn_id->errorInfo();
421
422 if (empty($pdo_error[0]))
423 {
424 return $error;
425 }
426
427 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
428 if (isset($pdo_error[2]))
429 {
430 $error['message'] = $pdo_error[2];
431 }
432
433 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400434 }
435
436 // --------------------------------------------------------------------
437
438 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400439 * Update_Batch statement
440 *
441 * Generates a platform-specific batch update string from the supplied data
442 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400443 * @param string the table name
444 * @param array the update data
445 * @param array the where clause
446 * @return string
447 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200448 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400449 {
Taufan Aditya18209332012-02-09 16:07:27 +0700450 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100451 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400452
453 foreach ($values as $key => $val)
454 {
455 $ids[] = $val[$index];
456
457 foreach (array_keys($val) as $field)
458 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100459 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400460 {
461 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
462 }
463 }
464 }
465
Andrey Andreeve9f20952012-04-06 19:30:41 +0300466 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400467 $cases = '';
468
469 foreach ($final as $k => $v)
470 {
471 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700472
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400473 foreach ($v as $row)
474 {
475 $cases .= $row."\n";
476 }
477
478 $cases .= 'ELSE '.$k.' END, ';
479 }
480
481 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400482 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
483
484 return $sql;
485 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400486
Timothy Warren80ab8162011-08-22 18:26:12 -0400487 // --------------------------------------------------------------------
488
489 /**
490 * Truncate statement
491 *
492 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300493 *
494 * If the database does not support the truncate() command,
495 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400496 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400497 * @param string the table name
498 * @return string
499 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200500 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400501 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300502 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400503 }
504
505 // --------------------------------------------------------------------
506
507 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400508 * Limit string
509 *
510 * Generates a platform-specific LIMIT clause
511 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400512 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200513 * @param int the number of rows to limit the query to
514 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400515 * @return string
516 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200517 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400518 {
Andrey Andreev27105662012-06-24 22:44:00 +0300519 $offset = ($offset == 0) ? '' : $offset.', ';
520 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400521 }
522
Timothy Warren80ab8162011-08-22 18:26:12 -0400523}
524
Timothy Warren80ab8162011-08-22 18:26:12 -0400525/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300526/* Location: ./system/database/drivers/pdo/pdo_driver.php */