blob: 94a12100e22326a84314b8766d1226730a74d0e2 [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 Andreev7151e802012-06-25 00:47:41 +030071 $this->subdriver = $match[1];
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
Andrey Andreev7151e802012-06-25 00:47:41 +030079 if (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
80 {
81 $this->subdriver = 'dblib';
82 }
83
Timothy Warrenc7ba6642011-09-14 12:25:14 -040084 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070085 // this one depends on the driver being used
Andrey Andreev1732b1b2012-06-24 02:42:38 +030086 if ($this->subdriver === 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040087 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +030088 $this->_escape_char = '';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040089 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040090 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020091
92 $this->trans_enabled = FALSE;
Andrey Andreevd42cc462012-06-24 23:52:40 +030093// $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Timothy Warren80ab8162011-08-22 18:26:12 -040094 }
95
96 /**
Taufan Aditya18209332012-02-09 16:07:27 +070097 * Connection String
98 *
Taufan Aditya18209332012-02-09 16:07:27 +070099 * @param array
100 * @return void
101 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200102 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700103 {
104 if (strpos($this->hostname, ':'))
105 {
106 // hostname generally would have this prototype
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300107 // $db['hostname'] = 'subdriver:host(/Server(/DSN))=hostname(/DSN);';
108 // We need to get the prefix (subdriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500109 $dsnarray = explode(':', $this->hostname);
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300110 $this->subdriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700111
112 // End dsn with a semicolon for extra backward compability
113 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500114 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500115 {
116 $this->dsn .= rtrim($this->hostname, ';').';';
117 }
Taufan Aditya18209332012-02-09 16:07:27 +0700118 }
119 else
120 {
121 // Invalid DSN, display an error
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300122 if ( ! array_key_exists('subdriver', $params))
Taufan Aditya18209332012-02-09 16:07:27 +0700123 {
124 show_error('Invalid DB Connection String for PDO');
125 }
126
127 // Assuming that the following DSN string format is used:
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300128 // $dsn = 'pdo://username:password@hostname:port/database?subdriver=pgsql';
129 $this->dsn = $this->subdriver.':';
Taufan Aditya18209332012-02-09 16:07:27 +0700130
131 // Add hostname to the DSN for databases that need it
Andrey Andreev7151e802012-06-25 00:47:41 +0300132 if ( ! empty($this->hostname) && strpos($this->hostname, ':') === FALSE
133 && in_array($this->subdriver, array('informix', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700134 {
135 $this->dsn .= 'host='.$this->hostname.';';
136 }
137
138 // Add a port to the DSN for databases that can use it
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300139 if ( ! empty($this->port) && in_array($this->subdriver, array('informix', 'ibm', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700140 {
Andrey Andreev7151e802012-06-25 00:47:41 +0300141 $this->dsn .= 'port='.$this->port.';';
Taufan Aditya18209332012-02-09 16:07:27 +0700142 }
143 }
144
145 // Add the database name to the DSN, if needed
Andrey Andreev7151e802012-06-25 00:47:41 +0300146 if (stripos($this->dsn, 'dbname') === FALSE && in_array($this->subdriver, array('4D', 'firebird', 'cubrid')))
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300147 {
148 $this->dsn .= 'dbname='.$this->database.';';
149 }
Andrey Andreev7151e802012-06-25 00:47:41 +0300150 elseif (stripos($this->dsn, 'database') === FALSE && $this->subdriver === 'ibm')
Andrey Andreevd42cc462012-06-24 23:52:40 +0300151 {
152 if (stripos($this->dsn, 'dsn') === FALSE)
153 {
154 $this->dsn .= 'database='.$this->database.';';
155 }
156 }
Taufan Aditya18209332012-02-09 16:07:27 +0700157
Andrey Andreevd42cc462012-06-24 23:52:40 +0300158 // Add charset to the DSN, if needed
Andrey Andreev7151e802012-06-25 00:47:41 +0300159 if ( ! empty($this->char_set) && $this->subdriver === '4D')
Andrey Andreevd42cc462012-06-24 23:52:40 +0300160 {
161 $this->dsn .= 'charset='.$this->char_set.';';
162 }
Taufan Aditya18209332012-02-09 16:07:27 +0700163 }
164
Andrey Andreev2387ed32012-04-07 00:11:14 +0300165 // --------------------------------------------------------------------
166
Taufan Aditya18209332012-02-09 16:07:27 +0700167 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400168 * Non-persistent database connection
169 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300170 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200171 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700172 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300173 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700174 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300175 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300176 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300177
Taufan Aditya18209332012-02-09 16:07:27 +0700178 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200179 try
Taufan Aditya18209332012-02-09 16:07:27 +0700180 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300181 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200182 }
183 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700184 {
185 if ($this->db_debug && empty($this->failover))
186 {
187 $this->display_error($e->getMessage(), '', TRUE);
188 }
189
190 return FALSE;
191 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 }
193
194 // --------------------------------------------------------------------
195
196 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300197 * Persistent database connection
198 *
199 * @return object
200 */
201 public function db_pconnect()
202 {
203 return $this->db_connect(TRUE);
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200209 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400210 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400211 * @return string
212 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200213 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400214 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200215 return isset($this->data_cache['version'])
216 ? $this->data_cache['version']
217 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Execute the query
224 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400225 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200226 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400227 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200228 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400229 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300230 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400231 }
232
233 // --------------------------------------------------------------------
234
235 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400236 * Begin Transaction
237 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400238 * @return bool
239 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200240 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400241 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400242 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300243 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400244 {
245 return TRUE;
246 }
247
248 // Reset the transaction failure flag.
249 // If the $test_mode flag is set to TRUE transactions will be rolled back
250 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300251 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400252
Timothy Warrenab347582011-08-23 12:29:29 -0400253 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Commit Transaction
260 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400261 * @return bool
262 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200263 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400264 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300266 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400267 {
268 return TRUE;
269 }
270
Andrey Andreev80144bf2012-04-06 22:19:26 +0300271 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Rollback Transaction
278 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400279 * @return bool
280 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200281 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400282 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400283 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300284 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400285 {
286 return TRUE;
287 }
288
Andrey Andreev80144bf2012-04-06 22:19:26 +0300289 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Escape String
296 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400297 * @param string
298 * @param bool whether or not the string will be used in a LIKE condition
299 * @return string
300 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200301 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400302 {
303 if (is_array($str))
304 {
305 foreach ($str as $key => $val)
306 {
307 $str[$key] = $this->escape_str($val, $like);
308 }
309
310 return $str;
311 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200312
Andrey Andreev2387ed32012-04-07 00:11:14 +0300313 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400314 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200315
Andrey Andreev2387ed32012-04-07 00:11:14 +0300316 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400317 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400318 {
319 $str = substr($str, 1, -1);
320 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200321
Timothy Warren80ab8162011-08-22 18:26:12 -0400322 // escape LIKE condition wildcards
323 if ($like === TRUE)
324 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200325 return str_replace(array($this->_like_escape_chr, '%', '_'),
326 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
327 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400328 }
329
330 return $str;
331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Affected Rows
337 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200338 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400339 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200340 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400341 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300342 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200349 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300350 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200351 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400352 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200353 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400354 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200355 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400356 }
357
358 // --------------------------------------------------------------------
359
360 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400361 * Show table query
362 *
363 * Generates a platform-specific query string so that the table names can be fetched
364 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200365 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400366 * @return string
367 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200368 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400369 {
Andrey Andreev27105662012-06-24 22:44:00 +0300370 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Timothy Warren80ab8162011-08-22 18:26:12 -0400371
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100372 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400373 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200374 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400375 }
376
377 return $sql;
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Show column query
384 *
385 * Generates a platform-specific query string so that the column names can be fetched
386 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400387 * @param string the table name
388 * @return string
389 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200390 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400391 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300392 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Field data query
399 *
400 * Generates a platform-specific query so that the column data can be retrieved
401 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400402 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200403 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400404 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200405 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400406 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300407 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400408 }
409
410 // --------------------------------------------------------------------
411
412 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200413 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400414 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200415 * Returns an array containing code and message of the last
416 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400417 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200418 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400419 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200420 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400421 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200422 $error = array('code' => '00000', 'message' => '');
423 $pdo_error = $this->conn_id->errorInfo();
424
425 if (empty($pdo_error[0]))
426 {
427 return $error;
428 }
429
430 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
431 if (isset($pdo_error[2]))
432 {
433 $error['message'] = $pdo_error[2];
434 }
435
436 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400442 * Update_Batch statement
443 *
444 * Generates a platform-specific batch update string from the supplied data
445 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400446 * @param string the table name
447 * @param array the update data
448 * @param array the where clause
449 * @return string
450 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200451 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400452 {
Taufan Aditya18209332012-02-09 16:07:27 +0700453 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100454 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400455
456 foreach ($values as $key => $val)
457 {
458 $ids[] = $val[$index];
459
460 foreach (array_keys($val) as $field)
461 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100462 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400463 {
464 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
465 }
466 }
467 }
468
Andrey Andreeve9f20952012-04-06 19:30:41 +0300469 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400470 $cases = '';
471
472 foreach ($final as $k => $v)
473 {
474 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700475
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400476 foreach ($v as $row)
477 {
478 $cases .= $row."\n";
479 }
480
481 $cases .= 'ELSE '.$k.' END, ';
482 }
483
484 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400485 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
486
487 return $sql;
488 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400489
Timothy Warren80ab8162011-08-22 18:26:12 -0400490 // --------------------------------------------------------------------
491
492 /**
493 * Truncate statement
494 *
495 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300496 *
497 * If the database does not support the truncate() command,
498 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400499 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400500 * @param string the table name
501 * @return string
502 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200503 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400504 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300505 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400506 }
507
508 // --------------------------------------------------------------------
509
510 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400511 * Limit string
512 *
513 * Generates a platform-specific LIMIT clause
514 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400515 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200516 * @param int the number of rows to limit the query to
517 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400518 * @return string
519 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200520 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400521 {
Andrey Andreev27105662012-06-24 22:44:00 +0300522 $offset = ($offset == 0) ? '' : $offset.', ';
523 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400524 }
525
Timothy Warren80ab8162011-08-22 18:26:12 -0400526}
527
Timothy Warren80ab8162011-08-22 18:26:12 -0400528/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300529/* Location: ./system/database/drivers/pdo/pdo_driver.php */