blob: ea571492246f2d8a2ecf49a9d0cef8b943544742 [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 Andreevbd44d5a2012-03-20 22:59:29 +020046 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 Andreevfbba54e2012-06-23 20:26:31 +030081 if ($this->subdriver === 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040082 {
Andrey Andreeve9f20952012-04-06 19:30:41 +030083 $this->_escape_char = '`';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040084 $this->_like_escape_str = '';
Andrey Andreevdef568f2012-05-25 01:06:54 +030085 $this->_like_escape_chr = '\\';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040086 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +030087 elseif ($this->subdriver === 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040088 {
89 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040090 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +030091 elseif ( ! in_array($this->subdriver, array('sqlsrv', 'mssql', 'dblib', 'sybase')))
Timothy Warrenc7ba6642011-09-14 12:25:14 -040092 {
Andrey Andreeve9f20952012-04-06 19:30:41 +030093 $this->_escape_char = '"';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040094 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020095
96 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -040097 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
98 }
99
100 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700101 * Connection String
102 *
Taufan Aditya18209332012-02-09 16:07:27 +0700103 * @param array
104 * @return void
105 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200106 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700107 {
108 if (strpos($this->hostname, ':'))
109 {
110 // hostname generally would have this prototype
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300111 // $db['hostname'] = 'subdriver:host(/Server(/DSN))=hostname(/DSN);';
112 // We need to get the prefix (subdriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500113 $dsnarray = explode(':', $this->hostname);
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300114 $this->subdriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700115
116 // End dsn with a semicolon for extra backward compability
117 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500118 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500119 {
120 $this->dsn .= rtrim($this->hostname, ';').';';
121 }
Taufan Aditya18209332012-02-09 16:07:27 +0700122 }
123 else
124 {
125 // Invalid DSN, display an error
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300126 if ( ! array_key_exists('subdriver', $params))
Taufan Aditya18209332012-02-09 16:07:27 +0700127 {
128 show_error('Invalid DB Connection String for PDO');
129 }
130
131 // Assuming that the following DSN string format is used:
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300132 // $dsn = 'pdo://username:password@hostname:port/database?subdriver=pgsql';
133 $this->dsn = $this->subdriver.':';
Taufan Aditya18209332012-02-09 16:07:27 +0700134
135 // Add hostname to the DSN for databases that need it
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200136 if ( ! empty($this->hostname)
Timothy Warren928d83c2012-02-13 14:07:57 -0500137 && strpos($this->hostname, ':') === FALSE
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300138 && in_array($this->subdriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700139 {
140 $this->dsn .= 'host='.$this->hostname.';';
141 }
142
143 // Add a port to the DSN for databases that can use it
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300144 if ( ! empty($this->port) && in_array($this->subdriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700145 {
146 $this->dsn .= 'port='.$this->port.';';
147 }
148 }
149
150 // Add the database name to the DSN, if needed
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200151 if (stripos($this->dsn, 'dbname') === FALSE
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300152 && in_array($this->subdriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700153 {
154 $this->dsn .= 'dbname='.$this->database.';';
155 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300156 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->subdriver, array('ibm', 'sqlsrv')))
Taufan Aditya18209332012-02-09 16:07:27 +0700157 {
158 if (stripos($this->dsn, 'dsn') === FALSE)
159 {
160 $this->dsn .= 'database='.$this->database.';';
161 }
162 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300163 elseif ($this->subdriver === 'sqlite' && $this->dsn === 'sqlite:')
Taufan Aditya18209332012-02-09 16:07:27 +0700164 {
165 if ($this->database !== ':memory')
166 {
167 if ( ! file_exists($this->database))
168 {
169 show_error('Invalid DB Connection string for PDO SQLite');
170 }
171
172 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
173 }
174
175 $this->dsn .= $this->database;
176 }
177
178 // Add charset to the DSN, if needed
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300179 if ( ! empty($this->char_set) && in_array($this->subdriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
Taufan Aditya18209332012-02-09 16:07:27 +0700180 {
181 $this->dsn .= 'charset='.$this->char_set.';';
182 }
183 }
184
Andrey Andreev2387ed32012-04-07 00:11:14 +0300185 // --------------------------------------------------------------------
186
Taufan Aditya18209332012-02-09 16:07:27 +0700187 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400188 * Non-persistent database connection
189 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200190 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200192 public function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400193 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300194 return $this->_pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Persistent database connection
201 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200202 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400203 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200204 public function db_pconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400205 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300206 return $this->_pdo_connect(TRUE);
Taufan Aditya18209332012-02-09 16:07:27 +0700207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * PDO connection
213 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300214 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200215 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700216 */
Andrey Andreev2387ed32012-04-07 00:11:14 +0300217 protected function _pdo_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700218 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300219 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
220 $persistent === FALSE OR $this->options[PDO::ATTR_PERSISTENT] = TRUE;
221
222 /* Prior to PHP 5.3.6, even if the charset was supplied in the DSN
223 * on connect - it was ignored. This is a work-around for the issue.
224 *
225 * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php
226 */
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300227 if ($this->subdriver === 'mysql' && ! is_php('5.3.6') && ! empty($this->char_set))
Taufan Aditya18209332012-02-09 16:07:27 +0700228 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300229 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set
230 .( ! empty($this->db_collat) ? " COLLATE '".$this->dbcollat."'" : '');
Taufan Aditya18209332012-02-09 16:07:27 +0700231 }
232
233 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200234 try
Taufan Aditya18209332012-02-09 16:07:27 +0700235 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300236 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200237 }
238 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700239 {
240 if ($this->db_debug && empty($this->failover))
241 {
242 $this->display_error($e->getMessage(), '', TRUE);
243 }
244
245 return FALSE;
246 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400247 }
248
249 // --------------------------------------------------------------------
250
251 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200252 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400253 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 * @return string
255 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200256 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400257 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200258 return isset($this->data_cache['version'])
259 ? $this->data_cache['version']
260 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Execute the query
267 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400268 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200269 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400270 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200271 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400272 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300273 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400274 }
275
276 // --------------------------------------------------------------------
277
278 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400279 * Begin Transaction
280 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400281 * @return bool
282 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200283 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400284 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400285 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300286 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400287 {
288 return TRUE;
289 }
290
291 // Reset the transaction failure flag.
292 // If the $test_mode flag is set to TRUE transactions will be rolled back
293 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300294 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400295
Timothy Warrenab347582011-08-23 12:29:29 -0400296 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Commit Transaction
303 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400304 * @return bool
305 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200306 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400307 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400308 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300309 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400310 {
311 return TRUE;
312 }
313
Andrey Andreev80144bf2012-04-06 22:19:26 +0300314 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Rollback Transaction
321 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400322 * @return bool
323 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200324 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400325 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400326 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300327 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400328 {
329 return TRUE;
330 }
331
Andrey Andreev80144bf2012-04-06 22:19:26 +0300332 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * Escape String
339 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400340 * @param string
341 * @param bool whether or not the string will be used in a LIKE condition
342 * @return string
343 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200344 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400345 {
346 if (is_array($str))
347 {
348 foreach ($str as $key => $val)
349 {
350 $str[$key] = $this->escape_str($val, $like);
351 }
352
353 return $str;
354 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200355
Andrey Andreev2387ed32012-04-07 00:11:14 +0300356 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400357 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200358
Andrey Andreev2387ed32012-04-07 00:11:14 +0300359 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400360 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400361 {
362 $str = substr($str, 1, -1);
363 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200364
Timothy Warren80ab8162011-08-22 18:26:12 -0400365 // escape LIKE condition wildcards
366 if ($like === TRUE)
367 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200368 return str_replace(array($this->_like_escape_chr, '%', '_'),
369 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
370 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400371 }
372
373 return $str;
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Affected Rows
380 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200381 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400382 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200383 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400384 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300385 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200392 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300393 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200394 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400395 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200396 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400397 {
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300398 if ($this->subdriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400399 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200400 $query = $this->query('SELECT LASTVAL() AS ins_id');
401 $query = $query->row();
402 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400403 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200404
405 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400406 }
407
408 // --------------------------------------------------------------------
409
410 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400411 * Show table query
412 *
413 * Generates a platform-specific query string so that the table names can be fetched
414 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200415 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400416 * @return string
417 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200418 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400419 {
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300420 if ($this->subdriver === 'pgsql')
Taufan Aditya18209332012-02-09 16:07:27 +0700421 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400422 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700423 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
424 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300425 elseif ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700426 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400427 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700428 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
429 }
Taufan Aditya18209332012-02-09 16:07:27 +0700430 else
431 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300432 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700433 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400434
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100435 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400436 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200437 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400438 }
439
440 return $sql;
441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Show column query
447 *
448 * Generates a platform-specific query string so that the column names can be fetched
449 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400450 * @param string the table name
451 * @return string
452 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200453 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400454 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300455 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400456 }
457
458 // --------------------------------------------------------------------
459
460 /**
461 * Field data query
462 *
463 * Generates a platform-specific query so that the column data can be retrieved
464 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400465 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200466 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400467 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200468 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400469 {
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300470 if ($this->subdriver === 'mysql' or $this->subdriver === 'pgsql')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700471 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400472 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300473 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700474 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300475 elseif ($this->subdriver === 'oci')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700476 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400477 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300478 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700479 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300480 elseif ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700481 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400482 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300483 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700484 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200485
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300486 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400487 }
488
489 // --------------------------------------------------------------------
490
491 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200492 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400493 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200494 * Returns an array containing code and message of the last
495 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400496 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200497 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400498 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200499 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400500 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200501 $error = array('code' => '00000', 'message' => '');
502 $pdo_error = $this->conn_id->errorInfo();
503
504 if (empty($pdo_error[0]))
505 {
506 return $error;
507 }
508
509 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
510 if (isset($pdo_error[2]))
511 {
512 $error['message'] = $pdo_error[2];
513 }
514
515 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400516 }
517
518 // --------------------------------------------------------------------
519
520 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400521 * Update_Batch statement
522 *
523 * Generates a platform-specific batch update string from the supplied data
524 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400525 * @param string the table name
526 * @param array the update data
527 * @param array the where clause
528 * @return string
529 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200530 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400531 {
Taufan Aditya18209332012-02-09 16:07:27 +0700532 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100533 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400534
535 foreach ($values as $key => $val)
536 {
537 $ids[] = $val[$index];
538
539 foreach (array_keys($val) as $field)
540 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100541 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400542 {
543 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
544 }
545 }
546 }
547
Andrey Andreeve9f20952012-04-06 19:30:41 +0300548 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400549 $cases = '';
550
551 foreach ($final as $k => $v)
552 {
553 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700554
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400555 foreach ($v as $row)
556 {
557 $cases .= $row."\n";
558 }
559
560 $cases .= 'ELSE '.$k.' END, ';
561 }
562
563 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400564 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
565
566 return $sql;
567 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400568
Timothy Warren80ab8162011-08-22 18:26:12 -0400569 // --------------------------------------------------------------------
570
571 /**
572 * Truncate statement
573 *
574 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300575 *
576 * If the database does not support the truncate() command,
577 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400578 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400579 * @param string the table name
580 * @return string
581 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200582 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400583 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300584 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400585 }
586
587 // --------------------------------------------------------------------
588
589 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400590 * Limit string
591 *
592 * Generates a platform-specific LIMIT clause
593 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400594 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200595 * @param int the number of rows to limit the query to
596 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400597 * @return string
598 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200599 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400600 {
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300601 if ($this->subdriver === 'cubrid' OR $this->subdriver === 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400602 {
Alex Bilbiee7320252012-06-02 19:22:57 +0100603 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400604
Taufan Aditya18209332012-02-09 16:07:27 +0700605 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400606 }
607 else
608 {
Taufan Aditya18209332012-02-09 16:07:27 +0700609 $sql .= 'LIMIT '.$limit;
610 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200611
Timothy Warren0a43ad82011-09-15 20:15:19 -0400612 return $sql;
613 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400614 }
615
Timothy Warren80ab8162011-08-22 18:26:12 -0400616}
617
Timothy Warren80ab8162011-08-22 18:26:12 -0400618/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300619/* Location: ./system/database/drivers/pdo/pdo_driver.php */