blob: 38a9fec802a2af70c73fd3b23b6136551622af4e [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
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020052 protected $_random_keyword;
Taufan Aditya18209332012-02-09 16:07:27 +070053
Andrey Andreev738f5342012-03-06 20:43:40 +020054 // need to track the pdo driver and options
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020055 public $pdodriver;
56 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040057
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020058 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040059 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040060 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070061
Alex Bilbie48a2baf2012-06-02 11:09:54 +010062 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070063 {
64 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070065 // This is for general PDO users, who tend to have a full DSN string.
Taufan Aditya18209332012-02-09 16:07:27 +070066 $this->pdodriver = end($match);
67 }
68 else
69 {
70 // Try to build a complete DSN string from params
71 $this->_connect_string($params);
72 }
73
Timothy Warrenc7ba6642011-09-14 12:25:14 -040074 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070075 // this one depends on the driver being used
Andrey Andreevdef568f2012-05-25 01:06:54 +030076 if ($this->pdodriver === 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040077 {
Andrey Andreeve9f20952012-04-06 19:30:41 +030078 $this->_escape_char = '`';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040079 $this->_like_escape_str = '';
Andrey Andreevdef568f2012-05-25 01:06:54 +030080 $this->_like_escape_chr = '\\';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040081 }
Andrey Andreevdef568f2012-05-25 01:06:54 +030082 elseif ($this->pdodriver === 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040083 {
84 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040085 }
Andrey Andreeve9f20952012-04-06 19:30:41 +030086 elseif ( ! in_array($this->pdodriver, array('sqlsrv', 'mssql', 'dblib', 'sybase')))
Timothy Warrenc7ba6642011-09-14 12:25:14 -040087 {
Andrey Andreeve9f20952012-04-06 19:30:41 +030088 $this->_escape_char = '"';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040089 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020090
91 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -040092 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
93 }
94
95 /**
Taufan Aditya18209332012-02-09 16:07:27 +070096 * Connection String
97 *
Taufan Aditya18209332012-02-09 16:07:27 +070098 * @param array
99 * @return void
100 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200101 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700102 {
103 if (strpos($this->hostname, ':'))
104 {
105 // hostname generally would have this prototype
106 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
107 // We need to get the prefix (pdodriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500108 $dsnarray = explode(':', $this->hostname);
109 $this->pdodriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700110
111 // End dsn with a semicolon for extra backward compability
112 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500113 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500114 {
115 $this->dsn .= rtrim($this->hostname, ';').';';
116 }
Taufan Aditya18209332012-02-09 16:07:27 +0700117 }
118 else
119 {
120 // Invalid DSN, display an error
121 if ( ! array_key_exists('pdodriver', $params))
122 {
123 show_error('Invalid DB Connection String for PDO');
124 }
125
126 // Assuming that the following DSN string format is used:
127 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
128 $this->dsn = $this->pdodriver.':';
129
130 // Add hostname to the DSN for databases that need it
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200131 if ( ! empty($this->hostname)
Timothy Warren928d83c2012-02-13 14:07:57 -0500132 && strpos($this->hostname, ':') === FALSE
133 && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', '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
139 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
140 {
141 $this->dsn .= 'port='.$this->port.';';
142 }
143 }
144
145 // Add the database name to the DSN, if needed
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200146 if (stripos($this->dsn, 'dbname') === FALSE
Taufan Aditya18209332012-02-09 16:07:27 +0700147 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
148 {
149 $this->dsn .= 'dbname='.$this->database.';';
150 }
151 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
152 {
153 if (stripos($this->dsn, 'dsn') === FALSE)
154 {
155 $this->dsn .= 'database='.$this->database.';';
156 }
157 }
158 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
159 {
160 if ($this->database !== ':memory')
161 {
162 if ( ! file_exists($this->database))
163 {
164 show_error('Invalid DB Connection string for PDO SQLite');
165 }
166
167 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
168 }
169
170 $this->dsn .= $this->database;
171 }
172
173 // Add charset to the DSN, if needed
174 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
175 {
176 $this->dsn .= 'charset='.$this->char_set.';';
177 }
178 }
179
Andrey Andreev2387ed32012-04-07 00:11:14 +0300180 // --------------------------------------------------------------------
181
Taufan Aditya18209332012-02-09 16:07:27 +0700182 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400183 * Non-persistent database connection
184 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200185 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400186 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200187 public function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400188 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300189 return $this->_pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Persistent database connection
196 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200197 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400198 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200199 public function db_pconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400200 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300201 return $this->_pdo_connect(TRUE);
Taufan Aditya18209332012-02-09 16:07:27 +0700202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * PDO connection
208 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300209 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200210 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700211 */
Andrey Andreev2387ed32012-04-07 00:11:14 +0300212 protected function _pdo_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700213 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300214 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
215 $persistent === FALSE OR $this->options[PDO::ATTR_PERSISTENT] = TRUE;
216
217 /* Prior to PHP 5.3.6, even if the charset was supplied in the DSN
218 * on connect - it was ignored. This is a work-around for the issue.
219 *
220 * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php
221 */
Taufan Adityae16192c2012-04-27 02:13:21 +0700222 if ($this->pdodriver === 'mysql' && ! is_php('5.3.6') && ! empty($this->char_set))
Taufan Aditya18209332012-02-09 16:07:27 +0700223 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300224 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set
225 .( ! empty($this->db_collat) ? " COLLATE '".$this->dbcollat."'" : '');
Taufan Aditya18209332012-02-09 16:07:27 +0700226 }
227
228 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200229 try
Taufan Aditya18209332012-02-09 16:07:27 +0700230 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300231 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200232 }
233 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700234 {
235 if ($this->db_debug && empty($this->failover))
236 {
237 $this->display_error($e->getMessage(), '', TRUE);
238 }
239
240 return FALSE;
241 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200247 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400248 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400249 * @return string
250 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200251 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400252 {
Andrey Andreev9e3a83a2012-07-05 21:57:41 +0300253 if (isset($this->data_cache['version']))
254 {
255 return $this->data_cache['version'];
256 }
257
258 // Not all subdrivers support the getAttribute() method
259 try
260 {
261 return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
262 }
263 catch (PDOException $e)
264 {
265 return parent::version();
266 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Execute the query
273 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400274 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200275 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200277 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400278 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300279 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 }
281
282 // --------------------------------------------------------------------
283
284 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400285 * Begin Transaction
286 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400287 * @return bool
288 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200289 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400290 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400291 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300292 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400293 {
294 return TRUE;
295 }
296
297 // Reset the transaction failure flag.
298 // If the $test_mode flag is set to TRUE transactions will be rolled back
299 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300300 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400301
Timothy Warrenab347582011-08-23 12:29:29 -0400302 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Commit Transaction
309 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400310 * @return bool
311 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200312 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400313 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400314 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300315 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400316 {
317 return TRUE;
318 }
319
Andrey Andreev80144bf2012-04-06 22:19:26 +0300320 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Rollback Transaction
327 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400328 * @return bool
329 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200330 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400331 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400332 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300333 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400334 {
335 return TRUE;
336 }
337
Andrey Andreev80144bf2012-04-06 22:19:26 +0300338 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * Escape String
345 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400346 * @param string
347 * @param bool whether or not the string will be used in a LIKE condition
348 * @return string
349 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200350 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400351 {
352 if (is_array($str))
353 {
354 foreach ($str as $key => $val)
355 {
356 $str[$key] = $this->escape_str($val, $like);
357 }
358
359 return $str;
360 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200361
Andrey Andreev2387ed32012-04-07 00:11:14 +0300362 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400363 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200364
Andrey Andreev2387ed32012-04-07 00:11:14 +0300365 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400366 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400367 {
368 $str = substr($str, 1, -1);
369 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200370
Timothy Warren80ab8162011-08-22 18:26:12 -0400371 // escape LIKE condition wildcards
372 if ($like === TRUE)
373 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200374 return str_replace(array($this->_like_escape_chr, '%', '_'),
375 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
376 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400377 }
378
379 return $str;
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Affected Rows
386 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200387 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400388 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200389 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400390 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300391 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200398 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300399 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200400 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400401 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200402 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400403 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200404 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400405 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200406 $query = $this->query('SELECT LASTVAL() AS ins_id');
407 $query = $query->row();
408 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400409 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200410
411 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400412 }
413
414 // --------------------------------------------------------------------
415
416 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400417 * Show table query
418 *
419 * Generates a platform-specific query string so that the table names can be fetched
420 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200421 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400422 * @return string
423 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200424 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400425 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100426 if ($this->pdodriver === 'pgsql')
Taufan Aditya18209332012-02-09 16:07:27 +0700427 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400428 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700429 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
430 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100431 elseif ($this->pdodriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700432 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400433 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700434 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
435 }
Taufan Aditya18209332012-02-09 16:07:27 +0700436 else
437 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300438 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700439 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400440
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100441 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400442 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200443 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400444 }
445
446 return $sql;
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
452 * Show column query
453 *
454 * Generates a platform-specific query string so that the column names can be fetched
455 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400456 * @param string the table name
457 * @return string
458 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200459 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400460 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300461 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Field data query
468 *
469 * Generates a platform-specific query so that the column data can be retrieved
470 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400471 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200472 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400473 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200474 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400475 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100476 if ($this->pdodriver === 'mysql' or $this->pdodriver === 'pgsql')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700477 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400478 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300479 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700480 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100481 elseif ($this->pdodriver === 'oci')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700482 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400483 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300484 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700485 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100486 elseif ($this->pdodriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700487 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400488 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300489 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700490 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200491
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300492 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400493 }
494
495 // --------------------------------------------------------------------
496
497 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200498 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400499 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200500 * Returns an array containing code and message of the last
501 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400502 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200503 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400504 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200505 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400506 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200507 $error = array('code' => '00000', 'message' => '');
508 $pdo_error = $this->conn_id->errorInfo();
509
510 if (empty($pdo_error[0]))
511 {
512 return $error;
513 }
514
515 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
516 if (isset($pdo_error[2]))
517 {
518 $error['message'] = $pdo_error[2];
519 }
520
521 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400522 }
523
524 // --------------------------------------------------------------------
525
526 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400527 * Update_Batch statement
528 *
529 * Generates a platform-specific batch update string from the supplied data
530 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400531 * @param string the table name
532 * @param array the update data
533 * @param array the where clause
534 * @return string
535 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200536 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400537 {
Taufan Aditya18209332012-02-09 16:07:27 +0700538 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100539 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400540
541 foreach ($values as $key => $val)
542 {
543 $ids[] = $val[$index];
544
545 foreach (array_keys($val) as $field)
546 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100547 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400548 {
549 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
550 }
551 }
552 }
553
Andrey Andreeve9f20952012-04-06 19:30:41 +0300554 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400555 $cases = '';
556
557 foreach ($final as $k => $v)
558 {
559 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700560
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400561 foreach ($v as $row)
562 {
563 $cases .= $row."\n";
564 }
565
566 $cases .= 'ELSE '.$k.' END, ';
567 }
568
569 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400570 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
571
572 return $sql;
573 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400574
Timothy Warren80ab8162011-08-22 18:26:12 -0400575 // --------------------------------------------------------------------
576
577 /**
578 * Truncate statement
579 *
580 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300581 *
582 * If the database does not support the truncate() command,
583 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400584 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400585 * @param string the table name
586 * @return string
587 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200588 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400589 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300590 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400591 }
592
593 // --------------------------------------------------------------------
594
595 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400596 * Limit string
597 *
598 * Generates a platform-specific LIMIT clause
599 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400600 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200601 * @param int the number of rows to limit the query to
602 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400603 * @return string
604 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200605 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400606 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300607 if ($this->pdodriver === 'pgsql')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400608 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300609 return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : '');
Timothy Warren0a43ad82011-09-15 20:15:19 -0400610 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200611
Andrey Andreev2c35b642012-06-24 03:05:26 +0300612 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400613 }
614
Timothy Warren80ab8162011-08-22 18:26:12 -0400615}
616
Timothy Warren80ab8162011-08-22 18:26:12 -0400617/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300618/* Location: ./system/database/drivers/pdo/pdo_driver.php */