blob: d7d4db358e78515230d76386bad6677b23497ebf [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
32 * creates dynamically based on whether the active record
33 * 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
68 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) == 2)
69 {
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
82 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 = '';
86 $this->_like_escape_chr = '';
87 }
Taufan Aditya18209332012-02-09 16:07:27 +070088 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 /**
412 * "Count All" query
413 *
414 * Generates a platform-specific query string that counts all records in
415 * the specified database
416 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400417 * @param string
418 * @return string
419 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300420 public function count_all($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400421 {
422 if ($table == '')
423 {
424 return 0;
425 }
426
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200427 $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Taufan Aditya18209332012-02-09 16:07:27 +0700428 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400429
430 if ($query->num_rows() == 0)
431 {
432 return 0;
433 }
434
435 $row = $query->row();
436 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700437
Timothy Warren80ab8162011-08-22 18:26:12 -0400438 return (int) $row->numrows;
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Show table query
445 *
446 * Generates a platform-specific query string so that the table names can be fetched
447 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200448 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400449 * @return string
450 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200451 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400452 {
Taufan Aditya18209332012-02-09 16:07:27 +0700453 if ($this->pdodriver == 'pgsql')
454 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400455 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700456 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
457 }
Taufan Aditya4e44b342012-02-18 22:47:36 +0700458 elseif ($this->pdodriver == 'sqlite')
459 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400460 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700461 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
462 }
Taufan Aditya18209332012-02-09 16:07:27 +0700463 else
464 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300465 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700466 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400467
468 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
469 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200470 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400471 }
472
473 return $sql;
474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * Show column query
480 *
481 * Generates a platform-specific query string so that the column names can be fetched
482 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400483 * @param string the table name
484 * @return string
485 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200486 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400487 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300488 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400489 }
490
491 // --------------------------------------------------------------------
492
493 /**
494 * Field data query
495 *
496 * Generates a platform-specific query so that the column data can be retrieved
497 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400498 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200499 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400500 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200501 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400502 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700503 if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql')
504 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400505 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300506 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700507 }
508 elseif ($this->pdodriver == 'oci')
509 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400510 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300511 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700512 }
513 elseif ($this->pdodriver == 'sqlite')
514 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400515 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300516 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700517 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200518
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300519 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400520 }
521
522 // --------------------------------------------------------------------
523
524 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200525 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400526 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200527 * Returns an array containing code and message of the last
528 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400529 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200530 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400531 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200532 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400533 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200534 $error = array('code' => '00000', 'message' => '');
535 $pdo_error = $this->conn_id->errorInfo();
536
537 if (empty($pdo_error[0]))
538 {
539 return $error;
540 }
541
542 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
543 if (isset($pdo_error[2]))
544 {
545 $error['message'] = $pdo_error[2];
546 }
547
548 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400549 }
550
551 // --------------------------------------------------------------------
552
553 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400554 * From Tables
555 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400556 * This function implicitly groups FROM tables so there is no confusion
Timothy Warren80ab8162011-08-22 18:26:12 -0400557 * about operator precedence in harmony with SQL standards
558 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200559 * @param array
560 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400561 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200562 protected function _from_tables($tables)
Timothy Warren80ab8162011-08-22 18:26:12 -0400563 {
564 if ( ! is_array($tables))
565 {
566 $tables = array($tables);
567 }
568
Andrey Andreeve9f20952012-04-06 19:30:41 +0300569 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400570 }
571
572 // --------------------------------------------------------------------
573
574 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400575 * Update_Batch statement
576 *
577 * Generates a platform-specific batch update string from the supplied data
578 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400579 * @param string the table name
580 * @param array the update data
581 * @param array the where clause
582 * @return string
583 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200584 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400585 {
Taufan Aditya18209332012-02-09 16:07:27 +0700586 $ids = array();
587 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400588
589 foreach ($values as $key => $val)
590 {
591 $ids[] = $val[$index];
592
593 foreach (array_keys($val) as $field)
594 {
595 if ($field != $index)
596 {
597 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
598 }
599 }
600 }
601
Andrey Andreeve9f20952012-04-06 19:30:41 +0300602 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400603 $cases = '';
604
605 foreach ($final as $k => $v)
606 {
607 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700608
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400609 foreach ($v as $row)
610 {
611 $cases .= $row."\n";
612 }
613
614 $cases .= 'ELSE '.$k.' END, ';
615 }
616
617 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400618 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
619
620 return $sql;
621 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400622
Timothy Warren80ab8162011-08-22 18:26:12 -0400623 // --------------------------------------------------------------------
624
625 /**
626 * Truncate statement
627 *
628 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300629 *
630 * If the database does not support the truncate() command,
631 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400632 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400633 * @param string the table name
634 * @return string
635 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200636 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400637 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300638 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400639 }
640
641 // --------------------------------------------------------------------
642
643 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400644 * Limit string
645 *
646 * Generates a platform-specific LIMIT clause
647 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400648 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200649 * @param int the number of rows to limit the query to
650 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400651 * @return string
652 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200653 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400654 {
Taufan Aditya18209332012-02-09 16:07:27 +0700655 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400656 {
Taufan Aditya18209332012-02-09 16:07:27 +0700657 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400658
Taufan Aditya18209332012-02-09 16:07:27 +0700659 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400660 }
661 else
662 {
Taufan Aditya18209332012-02-09 16:07:27 +0700663 $sql .= 'LIMIT '.$limit;
664 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200665
Timothy Warren0a43ad82011-09-15 20:15:19 -0400666 return $sql;
667 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400668 }
669
670 // --------------------------------------------------------------------
671
672 /**
673 * Close DB Connection
674 *
Andrey Andreev918be792012-04-09 16:29:01 +0300675 * @param object
Timothy Warren80ab8162011-08-22 18:26:12 -0400676 * @return void
677 */
Andrey Andreev918be792012-04-09 16:29:01 +0300678 protected function _close($conn_id)
Timothy Warren80ab8162011-08-22 18:26:12 -0400679 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300680 $this->conn_id = NULL;
Timothy Warren80ab8162011-08-22 18:26:12 -0400681 }
682
Timothy Warren80ab8162011-08-22 18:26:12 -0400683}
684
Timothy Warren80ab8162011-08-22 18:26:12 -0400685/* End of file pdo_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400686/* Location: ./system/database/drivers/pdo/pdo_driver.php */