blob: 3ef376ca5ae9300037c1ba9a2fb7e872bb7abcaa [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 Andreev08856b82012-03-03 03:19:28 +0200253 return isset($this->data_cache['version'])
254 ? $this->data_cache['version']
255 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Execute the query
262 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400263 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200264 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200266 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400267 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300268 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400269 }
270
271 // --------------------------------------------------------------------
272
273 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400274 * Begin Transaction
275 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 * @return bool
277 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200278 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400279 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300281 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400282 {
283 return TRUE;
284 }
285
286 // Reset the transaction failure flag.
287 // If the $test_mode flag is set to TRUE transactions will be rolled back
288 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300289 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400290
Timothy Warrenab347582011-08-23 12:29:29 -0400291 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * Commit Transaction
298 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400299 * @return bool
300 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200301 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400302 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400303 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300304 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400305 {
306 return TRUE;
307 }
308
Andrey Andreev80144bf2012-04-06 22:19:26 +0300309 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Rollback Transaction
316 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400317 * @return bool
318 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200319 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400320 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400321 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300322 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400323 {
324 return TRUE;
325 }
326
Andrey Andreev80144bf2012-04-06 22:19:26 +0300327 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Escape String
334 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400335 * @param string
336 * @param bool whether or not the string will be used in a LIKE condition
337 * @return string
338 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200339 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400340 {
341 if (is_array($str))
342 {
343 foreach ($str as $key => $val)
344 {
345 $str[$key] = $this->escape_str($val, $like);
346 }
347
348 return $str;
349 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200350
Andrey Andreev2387ed32012-04-07 00:11:14 +0300351 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400352 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200353
Andrey Andreev2387ed32012-04-07 00:11:14 +0300354 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400355 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400356 {
357 $str = substr($str, 1, -1);
358 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200359
Timothy Warren80ab8162011-08-22 18:26:12 -0400360 // escape LIKE condition wildcards
361 if ($like === TRUE)
362 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200363 return str_replace(array($this->_like_escape_chr, '%', '_'),
364 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
365 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400366 }
367
368 return $str;
369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * Affected Rows
375 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200376 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400377 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200378 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400379 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300380 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200387 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300388 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200389 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400390 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200391 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400392 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200393 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400394 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200395 $query = $this->query('SELECT LASTVAL() AS ins_id');
396 $query = $query->row();
397 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400398 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200399
400 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400401 }
402
403 // --------------------------------------------------------------------
404
405 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400406 * Show table query
407 *
408 * Generates a platform-specific query string so that the table names can be fetched
409 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200410 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400411 * @return string
412 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200413 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400414 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100415 if ($this->pdodriver === 'pgsql')
Taufan Aditya18209332012-02-09 16:07:27 +0700416 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400417 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700418 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
419 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100420 elseif ($this->pdodriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700421 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400422 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700423 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
424 }
Taufan Aditya18209332012-02-09 16:07:27 +0700425 else
426 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300427 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700428 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400429
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100430 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400431 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200432 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400433 }
434
435 return $sql;
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Show column query
442 *
443 * Generates a platform-specific query string so that the column names can be fetched
444 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400445 * @param string the table name
446 * @return string
447 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200448 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400449 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300450 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * Field data query
457 *
458 * Generates a platform-specific query so that the column data can be retrieved
459 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400460 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200461 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400462 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200463 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400464 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100465 if ($this->pdodriver === 'mysql' or $this->pdodriver === 'pgsql')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700466 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400467 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300468 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700469 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100470 elseif ($this->pdodriver === 'oci')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700471 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400472 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300473 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700474 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100475 elseif ($this->pdodriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700476 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400477 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300478 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700479 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200480
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300481 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400482 }
483
484 // --------------------------------------------------------------------
485
486 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200487 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400488 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200489 * Returns an array containing code and message of the last
490 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400491 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200492 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400493 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200494 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400495 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200496 $error = array('code' => '00000', 'message' => '');
497 $pdo_error = $this->conn_id->errorInfo();
498
499 if (empty($pdo_error[0]))
500 {
501 return $error;
502 }
503
504 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
505 if (isset($pdo_error[2]))
506 {
507 $error['message'] = $pdo_error[2];
508 }
509
510 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400511 }
512
513 // --------------------------------------------------------------------
514
515 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400516 * Update_Batch statement
517 *
518 * Generates a platform-specific batch update string from the supplied data
519 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400520 * @param string the table name
521 * @param array the update data
522 * @param array the where clause
523 * @return string
524 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200525 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400526 {
Taufan Aditya18209332012-02-09 16:07:27 +0700527 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100528 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400529
530 foreach ($values as $key => $val)
531 {
532 $ids[] = $val[$index];
533
534 foreach (array_keys($val) as $field)
535 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100536 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400537 {
538 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
539 }
540 }
541 }
542
Andrey Andreeve9f20952012-04-06 19:30:41 +0300543 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400544 $cases = '';
545
546 foreach ($final as $k => $v)
547 {
548 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700549
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400550 foreach ($v as $row)
551 {
552 $cases .= $row."\n";
553 }
554
555 $cases .= 'ELSE '.$k.' END, ';
556 }
557
558 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400559 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
560
561 return $sql;
562 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400563
Timothy Warren80ab8162011-08-22 18:26:12 -0400564 // --------------------------------------------------------------------
565
566 /**
567 * Truncate statement
568 *
569 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300570 *
571 * If the database does not support the truncate() command,
572 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400573 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400574 * @param string the table name
575 * @return string
576 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200577 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400578 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300579 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400580 }
581
582 // --------------------------------------------------------------------
583
584 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400585 * Limit string
586 *
587 * Generates a platform-specific LIMIT clause
588 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400589 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200590 * @param int the number of rows to limit the query to
591 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400592 * @return string
593 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200594 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400595 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300596 if ($this->pdodriver === 'pgsql')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400597 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300598 return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : '');
Timothy Warren0a43ad82011-09-15 20:15:19 -0400599 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200600
Andrey Andreev2c35b642012-06-24 03:05:26 +0300601 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400602 }
603
Timothy Warren80ab8162011-08-22 18:26:12 -0400604}
605
Timothy Warren80ab8162011-08-22 18:26:12 -0400606/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300607/* Location: ./system/database/drivers/pdo/pdo_driver.php */