blob: 13ecb24a18abcc568b0084705a57f4028d3a489f [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
186 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400187 * Non-persistent database connection
188 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200189 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400190 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200191 public function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 {
Taufan Aditya18209332012-02-09 16:07:27 +0700193 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
194
195 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 Andreevbd44d5a2012-03-20 22:59:29 +0200207 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Taufan Aditya18209332012-02-09 16:07:27 +0700208 $this->options[PDO::ATTR_PERSISTENT] = TRUE;
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200209
Taufan Aditya18209332012-02-09 16:07:27 +0700210 return $this->pdo_connect();
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * PDO connection
217 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200218 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700219 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200220 public function pdo_connect()
Taufan Aditya18209332012-02-09 16:07:27 +0700221 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700222 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200223 if ($this->pdodriver === 'mysql' && ! is_php('5.3.6'))
Taufan Aditya18209332012-02-09 16:07:27 +0700224 {
Taufan Aditya55bb97e2012-02-09 16:43:26 +0700225 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set 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 {
231 $db = 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 }
242
243 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400244 }
245
246 // --------------------------------------------------------------------
247
248 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200249 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 * @return string
252 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200253 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200255 return isset($this->data_cache['version'])
256 ? $this->data_cache['version']
257 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Execute the query
264 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200266 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400267 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200268 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400269 {
Timothy Warren51a48882011-09-14 13:47:06 -0400270 $result_id = $this->conn_id->query($sql);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200271
Timothy Warrend6691532011-10-07 10:03:01 -0400272 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400273 {
274 $this->affect_rows = $result_id->rowCount();
275 }
276 else
277 {
278 $this->affect_rows = 0;
279 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200280
Timothy Warren51a48882011-09-14 13:47:06 -0400281 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400282 }
283
284 // --------------------------------------------------------------------
285
286 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400287 * Begin Transaction
288 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400289 * @return bool
290 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200291 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400292 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400293 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300294 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400295 {
296 return TRUE;
297 }
298
299 // Reset the transaction failure flag.
300 // If the $test_mode flag is set to TRUE transactions will be rolled back
301 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400302 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400303
Timothy Warrenab347582011-08-23 12:29:29 -0400304 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Commit Transaction
311 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400312 * @return bool
313 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200314 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400315 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400316 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300317 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400318 {
319 return TRUE;
320 }
321
Andrey Andreev80144bf2012-04-06 22:19:26 +0300322 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Rollback Transaction
329 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400330 * @return bool
331 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200332 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400333 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400334 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300335 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400336 {
337 return TRUE;
338 }
339
Andrey Andreev80144bf2012-04-06 22:19:26 +0300340 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Escape String
347 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400348 * @param string
349 * @param bool whether or not the string will be used in a LIKE condition
350 * @return string
351 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200352 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400353 {
354 if (is_array($str))
355 {
356 foreach ($str as $key => $val)
357 {
358 $str[$key] = $this->escape_str($val, $like);
359 }
360
361 return $str;
362 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200363
Timothy Warren47663972011-10-05 16:44:50 -0400364 //Escape the string
365 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200366
Timothy Warren47663972011-10-05 16:44:50 -0400367 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400368 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400369 {
370 $str = substr($str, 1, -1);
371 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200372
Timothy Warren80ab8162011-08-22 18:26:12 -0400373 // escape LIKE condition wildcards
374 if ($like === TRUE)
375 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200376 return str_replace(array($this->_like_escape_chr, '%', '_'),
377 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
378 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400379 }
380
381 return $str;
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Affected Rows
388 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200389 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400390 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200391 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400392 {
Timothy Warren51a48882011-09-14 13:47:06 -0400393 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200400 *
401 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400402 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200403 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400404 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200405 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400406 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200407 $query = $this->query('SELECT LASTVAL() AS ins_id');
408 $query = $query->row();
409 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400410 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200411
412 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * "Count All" query
419 *
420 * Generates a platform-specific query string that counts all records in
421 * the specified database
422 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400423 * @param string
424 * @return string
425 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300426 public function count_all($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400427 {
428 if ($table == '')
429 {
430 return 0;
431 }
432
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200433 $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Taufan Aditya18209332012-02-09 16:07:27 +0700434 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400435
436 if ($query->num_rows() == 0)
437 {
438 return 0;
439 }
440
441 $row = $query->row();
442 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700443
Timothy Warren80ab8162011-08-22 18:26:12 -0400444 return (int) $row->numrows;
445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * Show table query
451 *
452 * Generates a platform-specific query string so that the table names can be fetched
453 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200454 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400455 * @return string
456 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200457 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400458 {
Taufan Aditya18209332012-02-09 16:07:27 +0700459 if ($this->pdodriver == 'pgsql')
460 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400461 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700462 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
463 }
Taufan Aditya4e44b342012-02-18 22:47:36 +0700464 elseif ($this->pdodriver == 'sqlite')
465 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400466 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700467 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
468 }
Taufan Aditya18209332012-02-09 16:07:27 +0700469 else
470 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300471 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700472 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400473
474 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
475 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200476 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400477 }
478
479 return $sql;
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * Show column query
486 *
487 * Generates a platform-specific query string so that the column names can be fetched
488 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400489 * @param string the table name
490 * @return string
491 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200492 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400493 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300494 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Field data query
501 *
502 * Generates a platform-specific query so that the column data can be retrieved
503 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400504 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200505 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400506 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200507 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400508 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700509 if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql')
510 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400511 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300512 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700513 }
514 elseif ($this->pdodriver == 'oci')
515 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400516 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300517 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700518 }
519 elseif ($this->pdodriver == 'sqlite')
520 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400521 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300522 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700523 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200524
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300525 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400526 }
527
528 // --------------------------------------------------------------------
529
530 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200531 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400532 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200533 * Returns an array containing code and message of the last
534 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400535 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200536 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400537 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200538 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400539 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200540 $error = array('code' => '00000', 'message' => '');
541 $pdo_error = $this->conn_id->errorInfo();
542
543 if (empty($pdo_error[0]))
544 {
545 return $error;
546 }
547
548 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
549 if (isset($pdo_error[2]))
550 {
551 $error['message'] = $pdo_error[2];
552 }
553
554 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400555 }
556
557 // --------------------------------------------------------------------
558
559 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400560 * From Tables
561 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400562 * This function implicitly groups FROM tables so there is no confusion
Timothy Warren80ab8162011-08-22 18:26:12 -0400563 * about operator precedence in harmony with SQL standards
564 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200565 * @param array
566 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400567 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200568 protected function _from_tables($tables)
Timothy Warren80ab8162011-08-22 18:26:12 -0400569 {
570 if ( ! is_array($tables))
571 {
572 $tables = array($tables);
573 }
574
Andrey Andreeve9f20952012-04-06 19:30:41 +0300575 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400576 }
577
578 // --------------------------------------------------------------------
579
580 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400581 * Update_Batch statement
582 *
583 * Generates a platform-specific batch update string from the supplied data
584 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400585 * @param string the table name
586 * @param array the update data
587 * @param array the where clause
588 * @return string
589 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200590 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400591 {
Taufan Aditya18209332012-02-09 16:07:27 +0700592 $ids = array();
593 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400594
595 foreach ($values as $key => $val)
596 {
597 $ids[] = $val[$index];
598
599 foreach (array_keys($val) as $field)
600 {
601 if ($field != $index)
602 {
603 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
604 }
605 }
606 }
607
Andrey Andreeve9f20952012-04-06 19:30:41 +0300608 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400609 $cases = '';
610
611 foreach ($final as $k => $v)
612 {
613 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700614
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400615 foreach ($v as $row)
616 {
617 $cases .= $row."\n";
618 }
619
620 $cases .= 'ELSE '.$k.' END, ';
621 }
622
623 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400624 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
625
626 return $sql;
627 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400628
Timothy Warren80ab8162011-08-22 18:26:12 -0400629 // --------------------------------------------------------------------
630
631 /**
632 * Truncate statement
633 *
634 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300635 *
636 * If the database does not support the truncate() command,
637 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400638 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400639 * @param string the table name
640 * @return string
641 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200642 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400643 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300644 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400645 }
646
647 // --------------------------------------------------------------------
648
649 /**
650 * Delete statement
651 *
652 * Generates a platform-specific delete string from the supplied data
653 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400654 * @param string the table name
655 * @param array the where clause
656 * @param string the limit clause
657 * @return string
658 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200659 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400660 {
661 $conditions = '';
662
663 if (count($where) > 0 OR count($like) > 0)
664 {
Taufan Aditya18209332012-02-09 16:07:27 +0700665 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400666 $conditions .= implode("\n", $this->ar_where);
667
668 if (count($where) > 0 && count($like) > 0)
669 {
670 $conditions .= " AND ";
671 }
Taufan Aditya18209332012-02-09 16:07:27 +0700672
Timothy Warren80ab8162011-08-22 18:26:12 -0400673 $conditions .= implode("\n", $like);
674 }
675
676 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
677
Andrey Andreeve9f20952012-04-06 19:30:41 +0300678 return 'DELETE FROM '.$table.$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Limit string
685 *
686 * Generates a platform-specific LIMIT clause
687 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400688 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200689 * @param int the number of rows to limit the query to
690 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400691 * @return string
692 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200693 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400694 {
Taufan Aditya18209332012-02-09 16:07:27 +0700695 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400696 {
Taufan Aditya18209332012-02-09 16:07:27 +0700697 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400698
Taufan Aditya18209332012-02-09 16:07:27 +0700699 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400700 }
701 else
702 {
Taufan Aditya18209332012-02-09 16:07:27 +0700703 $sql .= 'LIMIT '.$limit;
704 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200705
Timothy Warren0a43ad82011-09-15 20:15:19 -0400706 return $sql;
707 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400708 }
709
710 // --------------------------------------------------------------------
711
712 /**
713 * Close DB Connection
714 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400715 * @param resource
716 * @return void
717 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200718 protected function _close($conn_id)
Timothy Warren80ab8162011-08-22 18:26:12 -0400719 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400720 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400721 }
722
Timothy Warren80ab8162011-08-22 18:26:12 -0400723}
724
Timothy Warren80ab8162011-08-22 18:26:12 -0400725/* End of file pdo_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400726/* Location: ./system/database/drivers/pdo/pdo_driver.php */