blob: a3ad469008fb2fb2f07cfafd524f046d059db503 [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 Andreev738f5342012-03-06 20:43:40 +020060 // need to track the pdo driver and options
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020061 public $pdodriver;
62 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040063
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020064 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040065 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040066 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070067
Alex Bilbie48a2baf2012-06-02 11:09:54 +010068 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070069 {
70 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070071 // This is for general PDO users, who tend to have a full DSN string.
Taufan Aditya18209332012-02-09 16:07:27 +070072 $this->pdodriver = end($match);
73 }
74 else
75 {
76 // Try to build a complete DSN string from params
77 $this->_connect_string($params);
78 }
79
Timothy Warrenc7ba6642011-09-14 12:25:14 -040080 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070081 // this one depends on the driver being used
Andrey Andreevdef568f2012-05-25 01:06:54 +030082 if ($this->pdodriver === 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040083 {
Andrey Andreeve9f20952012-04-06 19:30:41 +030084 $this->_escape_char = '`';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040085 $this->_like_escape_str = '';
Andrey Andreevdef568f2012-05-25 01:06:54 +030086 $this->_like_escape_chr = '\\';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040087 }
Andrey Andreevdef568f2012-05-25 01:06:54 +030088 elseif ($this->pdodriver === 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040089 {
90 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040091 }
Andrey Andreeve9f20952012-04-06 19:30:41 +030092 elseif ( ! in_array($this->pdodriver, array('sqlsrv', 'mssql', 'dblib', 'sybase')))
Timothy Warrenc7ba6642011-09-14 12:25:14 -040093 {
Andrey Andreeve9f20952012-04-06 19:30:41 +030094 $this->_escape_char = '"';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040095 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020096
97 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -040098 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
99 }
100
101 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700102 * Connection String
103 *
Taufan Aditya18209332012-02-09 16:07:27 +0700104 * @param array
105 * @return void
106 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200107 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700108 {
109 if (strpos($this->hostname, ':'))
110 {
111 // hostname generally would have this prototype
112 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
113 // We need to get the prefix (pdodriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500114 $dsnarray = explode(':', $this->hostname);
115 $this->pdodriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700116
117 // End dsn with a semicolon for extra backward compability
118 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500119 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500120 {
121 $this->dsn .= rtrim($this->hostname, ';').';';
122 }
Taufan Aditya18209332012-02-09 16:07:27 +0700123 }
124 else
125 {
126 // Invalid DSN, display an error
127 if ( ! array_key_exists('pdodriver', $params))
128 {
129 show_error('Invalid DB Connection String for PDO');
130 }
131
132 // Assuming that the following DSN string format is used:
133 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
134 $this->dsn = $this->pdodriver.':';
135
136 // Add hostname to the DSN for databases that need it
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200137 if ( ! empty($this->hostname)
Timothy Warren928d83c2012-02-13 14:07:57 -0500138 && strpos($this->hostname, ':') === FALSE
139 && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700140 {
141 $this->dsn .= 'host='.$this->hostname.';';
142 }
143
144 // Add a port to the DSN for databases that can use it
145 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
146 {
147 $this->dsn .= 'port='.$this->port.';';
148 }
149 }
150
151 // Add the database name to the DSN, if needed
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200152 if (stripos($this->dsn, 'dbname') === FALSE
Taufan Aditya18209332012-02-09 16:07:27 +0700153 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
154 {
155 $this->dsn .= 'dbname='.$this->database.';';
156 }
157 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
158 {
159 if (stripos($this->dsn, 'dsn') === FALSE)
160 {
161 $this->dsn .= 'database='.$this->database.';';
162 }
163 }
164 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
165 {
166 if ($this->database !== ':memory')
167 {
168 if ( ! file_exists($this->database))
169 {
170 show_error('Invalid DB Connection string for PDO SQLite');
171 }
172
173 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
174 }
175
176 $this->dsn .= $this->database;
177 }
178
179 // Add charset to the DSN, if needed
180 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
181 {
182 $this->dsn .= 'charset='.$this->char_set.';';
183 }
184 }
185
Andrey Andreev2387ed32012-04-07 00:11:14 +0300186 // --------------------------------------------------------------------
187
Taufan Aditya18209332012-02-09 16:07:27 +0700188 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400189 * Non-persistent database connection
190 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200191 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200193 public function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400194 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300195 return $this->_pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Persistent database connection
202 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200203 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400204 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200205 public function db_pconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400206 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300207 return $this->_pdo_connect(TRUE);
Taufan Aditya18209332012-02-09 16:07:27 +0700208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * PDO connection
214 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300215 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200216 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700217 */
Andrey Andreev2387ed32012-04-07 00:11:14 +0300218 protected function _pdo_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700219 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300220 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
221 $persistent === FALSE OR $this->options[PDO::ATTR_PERSISTENT] = TRUE;
222
223 /* Prior to PHP 5.3.6, even if the charset was supplied in the DSN
224 * on connect - it was ignored. This is a work-around for the issue.
225 *
226 * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php
227 */
Taufan Adityae16192c2012-04-27 02:13:21 +0700228 if ($this->pdodriver === 'mysql' && ! is_php('5.3.6') && ! empty($this->char_set))
Taufan Aditya18209332012-02-09 16:07:27 +0700229 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300230 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set
231 .( ! empty($this->db_collat) ? " COLLATE '".$this->dbcollat."'" : '');
Taufan Aditya18209332012-02-09 16:07:27 +0700232 }
233
234 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200235 try
Taufan Aditya18209332012-02-09 16:07:27 +0700236 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300237 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200238 }
239 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700240 {
241 if ($this->db_debug && empty($this->failover))
242 {
243 $this->display_error($e->getMessage(), '', TRUE);
244 }
245
246 return FALSE;
247 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400248 }
249
250 // --------------------------------------------------------------------
251
252 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200253 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400255 * @return string
256 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200257 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400258 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200259 return isset($this->data_cache['version'])
260 ? $this->data_cache['version']
261 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Execute the query
268 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400269 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200270 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400271 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200272 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400273 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300274 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400275 }
276
277 // --------------------------------------------------------------------
278
279 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 * Begin Transaction
281 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400282 * @return bool
283 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200284 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400285 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400286 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300287 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400288 {
289 return TRUE;
290 }
291
292 // Reset the transaction failure flag.
293 // If the $test_mode flag is set to TRUE transactions will be rolled back
294 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300295 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400296
Timothy Warrenab347582011-08-23 12:29:29 -0400297 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Commit Transaction
304 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400305 * @return bool
306 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200307 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400308 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400309 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300310 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400311 {
312 return TRUE;
313 }
314
Andrey Andreev80144bf2012-04-06 22:19:26 +0300315 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Rollback Transaction
322 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400323 * @return bool
324 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200325 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400326 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400327 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300328 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400329 {
330 return TRUE;
331 }
332
Andrey Andreev80144bf2012-04-06 22:19:26 +0300333 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Escape String
340 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400341 * @param string
342 * @param bool whether or not the string will be used in a LIKE condition
343 * @return string
344 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200345 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400346 {
347 if (is_array($str))
348 {
349 foreach ($str as $key => $val)
350 {
351 $str[$key] = $this->escape_str($val, $like);
352 }
353
354 return $str;
355 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200356
Andrey Andreev2387ed32012-04-07 00:11:14 +0300357 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400358 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200359
Andrey Andreev2387ed32012-04-07 00:11:14 +0300360 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400361 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400362 {
363 $str = substr($str, 1, -1);
364 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200365
Timothy Warren80ab8162011-08-22 18:26:12 -0400366 // escape LIKE condition wildcards
367 if ($like === TRUE)
368 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200369 return str_replace(array($this->_like_escape_chr, '%', '_'),
370 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
371 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400372 }
373
374 return $str;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Affected Rows
381 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200382 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400383 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200384 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400385 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300386 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200393 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300394 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200395 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400396 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200397 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400398 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200399 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400400 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200401 $query = $this->query('SELECT LASTVAL() AS ins_id');
402 $query = $query->row();
403 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400404 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200405
406 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400407 }
408
409 // --------------------------------------------------------------------
410
411 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400412 * Show table query
413 *
414 * Generates a platform-specific query string so that the table names can be fetched
415 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200416 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400417 * @return string
418 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200419 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400420 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100421 if ($this->pdodriver === 'pgsql')
Taufan Aditya18209332012-02-09 16:07:27 +0700422 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400423 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700424 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
425 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100426 elseif ($this->pdodriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700427 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400428 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700429 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
430 }
Taufan Aditya18209332012-02-09 16:07:27 +0700431 else
432 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300433 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700434 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400435
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100436 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400437 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200438 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400439 }
440
441 return $sql;
442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Show column query
448 *
449 * Generates a platform-specific query string so that the column names can be fetched
450 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400451 * @param string the table name
452 * @return string
453 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200454 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400455 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300456 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * Field data query
463 *
464 * Generates a platform-specific query so that the column data can be retrieved
465 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400466 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200467 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400468 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200469 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400470 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100471 if ($this->pdodriver === 'mysql' or $this->pdodriver === 'pgsql')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700472 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400473 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300474 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700475 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100476 elseif ($this->pdodriver === 'oci')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700477 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400478 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300479 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700480 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100481 elseif ($this->pdodriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700482 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400483 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300484 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700485 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200486
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300487 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400488 }
489
490 // --------------------------------------------------------------------
491
492 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200493 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400494 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200495 * Returns an array containing code and message of the last
496 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400497 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200498 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400499 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200500 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400501 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200502 $error = array('code' => '00000', 'message' => '');
503 $pdo_error = $this->conn_id->errorInfo();
504
505 if (empty($pdo_error[0]))
506 {
507 return $error;
508 }
509
510 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
511 if (isset($pdo_error[2]))
512 {
513 $error['message'] = $pdo_error[2];
514 }
515
516 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400517 }
518
519 // --------------------------------------------------------------------
520
521 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400522 * Update_Batch statement
523 *
524 * Generates a platform-specific batch update string from the supplied data
525 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400526 * @param string the table name
527 * @param array the update data
528 * @param array the where clause
529 * @return string
530 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200531 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400532 {
Taufan Aditya18209332012-02-09 16:07:27 +0700533 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100534 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400535
536 foreach ($values as $key => $val)
537 {
538 $ids[] = $val[$index];
539
540 foreach (array_keys($val) as $field)
541 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100542 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400543 {
544 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
545 }
546 }
547 }
548
Andrey Andreeve9f20952012-04-06 19:30:41 +0300549 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400550 $cases = '';
551
552 foreach ($final as $k => $v)
553 {
554 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700555
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400556 foreach ($v as $row)
557 {
558 $cases .= $row."\n";
559 }
560
561 $cases .= 'ELSE '.$k.' END, ';
562 }
563
564 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400565 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
566
567 return $sql;
568 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400569
Timothy Warren80ab8162011-08-22 18:26:12 -0400570 // --------------------------------------------------------------------
571
572 /**
573 * Truncate statement
574 *
575 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300576 *
577 * If the database does not support the truncate() command,
578 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400579 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400580 * @param string the table name
581 * @return string
582 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200583 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400584 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300585 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400586 }
587
588 // --------------------------------------------------------------------
589
590 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400591 * Limit string
592 *
593 * Generates a platform-specific LIMIT clause
594 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400595 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200596 * @param int the number of rows to limit the query to
597 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400598 * @return string
599 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200600 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400601 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300602 if ($this->pdodriver === 'pgsql')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400603 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300604 return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : '');
Timothy Warren0a43ad82011-09-15 20:15:19 -0400605 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200606
Andrey Andreev2c35b642012-06-24 03:05:26 +0300607 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400608 }
609
Timothy Warren80ab8162011-08-22 18:26:12 -0400610}
611
Timothy Warren80ab8162011-08-22 18:26:12 -0400612/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300613/* Location: ./system/database/drivers/pdo/pdo_driver.php */