blob: b1349ae4e9fffc31472872bf0f1cd17ba8b6cc4c [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 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 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +030089 $this->_escape_char = '';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040090 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040091 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020092
93 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -040094 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
95 }
96
97 /**
Taufan Aditya18209332012-02-09 16:07:27 +070098 * Connection String
99 *
Taufan Aditya18209332012-02-09 16:07:27 +0700100 * @param array
101 * @return void
102 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200103 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700104 {
105 if (strpos($this->hostname, ':'))
106 {
107 // hostname generally would have this prototype
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300108 // $db['hostname'] = 'subdriver:host(/Server(/DSN))=hostname(/DSN);';
109 // We need to get the prefix (subdriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500110 $dsnarray = explode(':', $this->hostname);
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300111 $this->subdriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700112
113 // End dsn with a semicolon for extra backward compability
114 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500115 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500116 {
117 $this->dsn .= rtrim($this->hostname, ';').';';
118 }
Taufan Aditya18209332012-02-09 16:07:27 +0700119 }
120 else
121 {
122 // Invalid DSN, display an error
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300123 if ( ! array_key_exists('subdriver', $params))
Taufan Aditya18209332012-02-09 16:07:27 +0700124 {
125 show_error('Invalid DB Connection String for PDO');
126 }
127
128 // Assuming that the following DSN string format is used:
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300129 // $dsn = 'pdo://username:password@hostname:port/database?subdriver=pgsql';
130 $this->dsn = $this->subdriver.':';
Taufan Aditya18209332012-02-09 16:07:27 +0700131
132 // Add hostname to the DSN for databases that need it
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200133 if ( ! empty($this->hostname)
Timothy Warren928d83c2012-02-13 14:07:57 -0500134 && strpos($this->hostname, ':') === FALSE
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300135 && in_array($this->subdriver, array('informix', 'mysql', 'sybase', 'mssql', 'dblib', 'cubrid')))
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 Andreev5663b1e2012-06-24 00:28:42 +0300141 if ( ! empty($this->port) && in_array($this->subdriver, array('informix', 'mysql', 'ibm', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700142 {
143 $this->dsn .= 'port='.$this->port.';';
144 }
145 }
146
147 // Add the database name to the DSN, if needed
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300148 if (stripos($this->dsn, 'dbname') === FALSE
149 && in_array($this->subdriver, array('4D', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
150 {
151 $this->dsn .= 'dbname='.$this->database.';';
152 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300153 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->subdriver, array('ibm', 'sqlsrv')))
Taufan Aditya18209332012-02-09 16:07:27 +0700154 {
155 if (stripos($this->dsn, 'dsn') === FALSE)
156 {
157 $this->dsn .= 'database='.$this->database.';';
158 }
159 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300160 elseif ($this->subdriver === 'sqlite' && $this->dsn === 'sqlite:')
Taufan Aditya18209332012-02-09 16:07:27 +0700161 {
162 if ($this->database !== ':memory')
163 {
164 if ( ! file_exists($this->database))
165 {
166 show_error('Invalid DB Connection string for PDO SQLite');
167 }
168
169 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
170 }
171
172 $this->dsn .= $this->database;
173 }
174
175 // Add charset to the DSN, if needed
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300176 if ( ! empty($this->char_set) && in_array($this->subdriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
Taufan Aditya18209332012-02-09 16:07:27 +0700177 {
178 $this->dsn .= 'charset='.$this->char_set.';';
179 }
180 }
181
Andrey Andreev2387ed32012-04-07 00:11:14 +0300182 // --------------------------------------------------------------------
183
Taufan Aditya18209332012-02-09 16:07:27 +0700184 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400185 * Non-persistent database connection
186 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200187 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400188 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200189 public function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400190 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300191 return $this->_pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Persistent database connection
198 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200199 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400200 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200201 public function db_pconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400202 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300203 return $this->_pdo_connect(TRUE);
Taufan Aditya18209332012-02-09 16:07:27 +0700204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * PDO connection
210 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300211 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200212 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700213 */
Andrey Andreev2387ed32012-04-07 00:11:14 +0300214 protected function _pdo_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700215 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300216 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
217 $persistent === FALSE OR $this->options[PDO::ATTR_PERSISTENT] = TRUE;
218
219 /* Prior to PHP 5.3.6, even if the charset was supplied in the DSN
220 * on connect - it was ignored. This is a work-around for the issue.
221 *
222 * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php
223 */
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300224 if ($this->subdriver === 'mysql' && ! is_php('5.3.6') && ! empty($this->char_set))
Taufan Aditya18209332012-02-09 16:07:27 +0700225 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300226 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set
227 .( ! empty($this->db_collat) ? " COLLATE '".$this->dbcollat."'" : '');
Taufan Aditya18209332012-02-09 16:07:27 +0700228 }
229
230 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200231 try
Taufan Aditya18209332012-02-09 16:07:27 +0700232 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300233 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200234 }
235 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700236 {
237 if ($this->db_debug && empty($this->failover))
238 {
239 $this->display_error($e->getMessage(), '', TRUE);
240 }
241
242 return FALSE;
243 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400244 }
245
246 // --------------------------------------------------------------------
247
248 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200249 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 * @return string
252 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200253 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200255 return isset($this->data_cache['version'])
256 ? $this->data_cache['version']
257 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Execute the query
264 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200266 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400267 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200268 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400269 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300270 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400271 }
272
273 // --------------------------------------------------------------------
274
275 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 * Begin Transaction
277 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400278 * @return bool
279 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200280 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400281 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400282 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300283 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400284 {
285 return TRUE;
286 }
287
288 // Reset the transaction failure flag.
289 // If the $test_mode flag is set to TRUE transactions will be rolled back
290 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300291 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400292
Timothy Warrenab347582011-08-23 12:29:29 -0400293 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400294 }
295
296 // --------------------------------------------------------------------
297
298 /**
299 * Commit Transaction
300 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400301 * @return bool
302 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200303 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400304 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400305 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300306 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400307 {
308 return TRUE;
309 }
310
Andrey Andreev80144bf2012-04-06 22:19:26 +0300311 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Rollback Transaction
318 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400319 * @return bool
320 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200321 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400322 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400323 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300324 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400325 {
326 return TRUE;
327 }
328
Andrey Andreev80144bf2012-04-06 22:19:26 +0300329 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Escape String
336 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400337 * @param string
338 * @param bool whether or not the string will be used in a LIKE condition
339 * @return string
340 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200341 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400342 {
343 if (is_array($str))
344 {
345 foreach ($str as $key => $val)
346 {
347 $str[$key] = $this->escape_str($val, $like);
348 }
349
350 return $str;
351 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200352
Andrey Andreev2387ed32012-04-07 00:11:14 +0300353 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400354 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200355
Andrey Andreev2387ed32012-04-07 00:11:14 +0300356 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400357 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400358 {
359 $str = substr($str, 1, -1);
360 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200361
Timothy Warren80ab8162011-08-22 18:26:12 -0400362 // escape LIKE condition wildcards
363 if ($like === TRUE)
364 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200365 return str_replace(array($this->_like_escape_chr, '%', '_'),
366 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
367 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400368 }
369
370 return $str;
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * Affected Rows
377 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200378 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400379 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200380 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400381 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300382 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200389 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300390 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200391 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400392 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200393 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400394 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200395 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400396 }
397
398 // --------------------------------------------------------------------
399
400 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400401 * Show table query
402 *
403 * Generates a platform-specific query string so that the table names can be fetched
404 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200405 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400406 * @return string
407 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200408 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400409 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300410 if ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700411 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400412 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700413 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
414 }
Taufan Aditya18209332012-02-09 16:07:27 +0700415 else
416 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300417 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700418 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400419
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100420 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400421 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200422 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400423 }
424
425 return $sql;
426 }
427
428 // --------------------------------------------------------------------
429
430 /**
431 * Show column query
432 *
433 * Generates a platform-specific query string so that the column names can be fetched
434 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400435 * @param string the table name
436 * @return string
437 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200438 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400439 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300440 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Field data query
447 *
448 * Generates a platform-specific query so that the column data can be retrieved
449 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400450 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200451 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400452 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200453 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400454 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300455 if ($this->subdriver === 'mysql')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700456 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400457 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300458 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700459 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300460 elseif ($this->subdriver === 'oci')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700461 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400462 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300463 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700464 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300465 elseif ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700466 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400467 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300468 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700469 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200470
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300471 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400472 }
473
474 // --------------------------------------------------------------------
475
476 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200477 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400478 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200479 * Returns an array containing code and message of the last
480 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400481 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200482 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400483 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200484 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400485 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200486 $error = array('code' => '00000', 'message' => '');
487 $pdo_error = $this->conn_id->errorInfo();
488
489 if (empty($pdo_error[0]))
490 {
491 return $error;
492 }
493
494 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
495 if (isset($pdo_error[2]))
496 {
497 $error['message'] = $pdo_error[2];
498 }
499
500 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400501 }
502
503 // --------------------------------------------------------------------
504
505 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400506 * Update_Batch statement
507 *
508 * Generates a platform-specific batch update string from the supplied data
509 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400510 * @param string the table name
511 * @param array the update data
512 * @param array the where clause
513 * @return string
514 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200515 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400516 {
Taufan Aditya18209332012-02-09 16:07:27 +0700517 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100518 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400519
520 foreach ($values as $key => $val)
521 {
522 $ids[] = $val[$index];
523
524 foreach (array_keys($val) as $field)
525 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100526 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400527 {
528 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
529 }
530 }
531 }
532
Andrey Andreeve9f20952012-04-06 19:30:41 +0300533 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400534 $cases = '';
535
536 foreach ($final as $k => $v)
537 {
538 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700539
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400540 foreach ($v as $row)
541 {
542 $cases .= $row."\n";
543 }
544
545 $cases .= 'ELSE '.$k.' END, ';
546 }
547
548 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400549 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
550
551 return $sql;
552 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400553
Timothy Warren80ab8162011-08-22 18:26:12 -0400554 // --------------------------------------------------------------------
555
556 /**
557 * Truncate statement
558 *
559 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300560 *
561 * If the database does not support the truncate() command,
562 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400563 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400564 * @param string the table name
565 * @return string
566 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200567 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400568 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300569 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400570 }
571
572 // --------------------------------------------------------------------
573
574 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400575 * Limit string
576 *
577 * Generates a platform-specific LIMIT clause
578 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400579 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200580 * @param int the number of rows to limit the query to
581 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400582 * @return string
583 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200584 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400585 {
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300586 if ($this->subdriver === 'cubrid' OR $this->subdriver === 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400587 {
Alex Bilbiee7320252012-06-02 19:22:57 +0100588 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400589
Taufan Aditya18209332012-02-09 16:07:27 +0700590 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400591 }
592 else
593 {
Taufan Aditya18209332012-02-09 16:07:27 +0700594 $sql .= 'LIMIT '.$limit;
595 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200596
Timothy Warren0a43ad82011-09-15 20:15:19 -0400597 return $sql;
598 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400599 }
600
Timothy Warren80ab8162011-08-22 18:26:12 -0400601}
602
Timothy Warren80ab8162011-08-22 18:26:12 -0400603/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300604/* Location: ./system/database/drivers/pdo/pdo_driver.php */