blob: 919bb9c00db33cf41505eca257fa5c80efd66c0a [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 {
293 if ( ! $this->trans_enabled)
294 {
295 return TRUE;
296 }
297
298 // When transactions are nested we only begin/commit/rollback the outermost ones
299 if ($this->_trans_depth > 0)
300 {
301 return TRUE;
302 }
303
304 // Reset the transaction failure flag.
305 // If the $test_mode flag is set to TRUE transactions will be rolled back
306 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400307 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400308
Timothy Warrenab347582011-08-23 12:29:29 -0400309 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Commit Transaction
316 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400317 * @return bool
318 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200319 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400320 {
321 if ( ! $this->trans_enabled)
322 {
323 return TRUE;
324 }
325
326 // When transactions are nested we only begin/commit/rollback the outermost ones
327 if ($this->_trans_depth > 0)
328 {
329 return TRUE;
330 }
331
Timothy Warrenab347582011-08-23 12:29:29 -0400332 $ret = $this->conn->commit();
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200333
Timothy Warren80ab8162011-08-22 18:26:12 -0400334 return $ret;
335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Rollback Transaction
341 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400342 * @return bool
343 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200344 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400345 {
346 if ( ! $this->trans_enabled)
347 {
348 return TRUE;
349 }
350
351 // When transactions are nested we only begin/commit/rollback the outermost ones
352 if ($this->_trans_depth > 0)
353 {
354 return TRUE;
355 }
356
Timothy Warrenab347582011-08-23 12:29:29 -0400357 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700358
Timothy Warren80ab8162011-08-22 18:26:12 -0400359 return $ret;
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Escape String
366 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400367 * @param string
368 * @param bool whether or not the string will be used in a LIKE condition
369 * @return string
370 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200371 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400372 {
373 if (is_array($str))
374 {
375 foreach ($str as $key => $val)
376 {
377 $str[$key] = $this->escape_str($val, $like);
378 }
379
380 return $str;
381 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200382
Timothy Warren47663972011-10-05 16:44:50 -0400383 //Escape the string
384 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200385
Timothy Warren47663972011-10-05 16:44:50 -0400386 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400387 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400388 {
389 $str = substr($str, 1, -1);
390 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200391
Timothy Warren80ab8162011-08-22 18:26:12 -0400392 // escape LIKE condition wildcards
393 if ($like === TRUE)
394 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200395 return str_replace(array($this->_like_escape_chr, '%', '_'),
396 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
397 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400398 }
399
400 return $str;
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Affected Rows
407 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200408 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400409 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200410 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400411 {
Timothy Warren51a48882011-09-14 13:47:06 -0400412 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200419 *
420 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400421 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200422 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400423 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200424 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400425 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200426 $query = $this->query('SELECT LASTVAL() AS ins_id');
427 $query = $query->row();
428 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400429 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200430
431 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * "Count All" query
438 *
439 * Generates a platform-specific query string that counts all records in
440 * the specified database
441 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400442 * @param string
443 * @return string
444 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300445 public function count_all($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400446 {
447 if ($table == '')
448 {
449 return 0;
450 }
451
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200452 $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Taufan Aditya18209332012-02-09 16:07:27 +0700453 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400454
455 if ($query->num_rows() == 0)
456 {
457 return 0;
458 }
459
460 $row = $query->row();
461 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700462
Timothy Warren80ab8162011-08-22 18:26:12 -0400463 return (int) $row->numrows;
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Show table query
470 *
471 * Generates a platform-specific query string so that the table names can be fetched
472 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200473 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400474 * @return string
475 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200476 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400477 {
Taufan Aditya18209332012-02-09 16:07:27 +0700478 if ($this->pdodriver == 'pgsql')
479 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400480 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700481 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
482 }
Taufan Aditya4e44b342012-02-18 22:47:36 +0700483 elseif ($this->pdodriver == 'sqlite')
484 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400485 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700486 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
487 }
Taufan Aditya18209332012-02-09 16:07:27 +0700488 else
489 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300490 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700491 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400492
493 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
494 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200495 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400496 }
497
498 return $sql;
499 }
500
501 // --------------------------------------------------------------------
502
503 /**
504 * Show column query
505 *
506 * Generates a platform-specific query string so that the column names can be fetched
507 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400508 * @param string the table name
509 * @return string
510 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200511 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400512 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300513 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400514 }
515
516 // --------------------------------------------------------------------
517
518 /**
519 * Field data query
520 *
521 * Generates a platform-specific query so that the column data can be retrieved
522 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400523 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200524 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400525 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200526 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400527 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700528 if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql')
529 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400530 // Analog function for mysql and postgre
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300531 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700532 }
533 elseif ($this->pdodriver == 'oci')
534 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400535 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300536 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700537 }
538 elseif ($this->pdodriver == 'sqlite')
539 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400540 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300541 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700542 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200543
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300544 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400545 }
546
547 // --------------------------------------------------------------------
548
549 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200550 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400551 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200552 * Returns an array containing code and message of the last
553 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400554 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200555 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400556 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200557 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400558 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200559 $error = array('code' => '00000', 'message' => '');
560 $pdo_error = $this->conn_id->errorInfo();
561
562 if (empty($pdo_error[0]))
563 {
564 return $error;
565 }
566
567 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
568 if (isset($pdo_error[2]))
569 {
570 $error['message'] = $pdo_error[2];
571 }
572
573 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400574 }
575
576 // --------------------------------------------------------------------
577
578 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400579 * From Tables
580 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400581 * This function implicitly groups FROM tables so there is no confusion
Timothy Warren80ab8162011-08-22 18:26:12 -0400582 * about operator precedence in harmony with SQL standards
583 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200584 * @param array
585 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400586 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200587 protected function _from_tables($tables)
Timothy Warren80ab8162011-08-22 18:26:12 -0400588 {
589 if ( ! is_array($tables))
590 {
591 $tables = array($tables);
592 }
593
Andrey Andreeve9f20952012-04-06 19:30:41 +0300594 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400595 }
596
597 // --------------------------------------------------------------------
598
599 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400600 * Update_Batch statement
601 *
602 * Generates a platform-specific batch update string from the supplied data
603 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400604 * @param string the table name
605 * @param array the update data
606 * @param array the where clause
607 * @return string
608 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200609 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400610 {
Taufan Aditya18209332012-02-09 16:07:27 +0700611 $ids = array();
612 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400613
614 foreach ($values as $key => $val)
615 {
616 $ids[] = $val[$index];
617
618 foreach (array_keys($val) as $field)
619 {
620 if ($field != $index)
621 {
622 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
623 }
624 }
625 }
626
Andrey Andreeve9f20952012-04-06 19:30:41 +0300627 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400628 $cases = '';
629
630 foreach ($final as $k => $v)
631 {
632 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700633
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400634 foreach ($v as $row)
635 {
636 $cases .= $row."\n";
637 }
638
639 $cases .= 'ELSE '.$k.' END, ';
640 }
641
642 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400643 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
644
645 return $sql;
646 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400647
Timothy Warren80ab8162011-08-22 18:26:12 -0400648 // --------------------------------------------------------------------
649
650 /**
651 * Truncate statement
652 *
653 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300654 *
655 * If the database does not support the truncate() command,
656 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400657 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400658 * @param string the table name
659 * @return string
660 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200661 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400662 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300663 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Delete statement
670 *
671 * Generates a platform-specific delete string from the supplied data
672 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400673 * @param string the table name
674 * @param array the where clause
675 * @param string the limit clause
676 * @return string
677 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200678 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400679 {
680 $conditions = '';
681
682 if (count($where) > 0 OR count($like) > 0)
683 {
Taufan Aditya18209332012-02-09 16:07:27 +0700684 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400685 $conditions .= implode("\n", $this->ar_where);
686
687 if (count($where) > 0 && count($like) > 0)
688 {
689 $conditions .= " AND ";
690 }
Taufan Aditya18209332012-02-09 16:07:27 +0700691
Timothy Warren80ab8162011-08-22 18:26:12 -0400692 $conditions .= implode("\n", $like);
693 }
694
695 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
696
Andrey Andreeve9f20952012-04-06 19:30:41 +0300697 return 'DELETE FROM '.$table.$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400698 }
699
700 // --------------------------------------------------------------------
701
702 /**
703 * Limit string
704 *
705 * Generates a platform-specific LIMIT clause
706 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400707 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200708 * @param int the number of rows to limit the query to
709 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400710 * @return string
711 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200712 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400713 {
Taufan Aditya18209332012-02-09 16:07:27 +0700714 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400715 {
Taufan Aditya18209332012-02-09 16:07:27 +0700716 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400717
Taufan Aditya18209332012-02-09 16:07:27 +0700718 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400719 }
720 else
721 {
Taufan Aditya18209332012-02-09 16:07:27 +0700722 $sql .= 'LIMIT '.$limit;
723 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200724
Timothy Warren0a43ad82011-09-15 20:15:19 -0400725 return $sql;
726 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400727 }
728
729 // --------------------------------------------------------------------
730
731 /**
732 * Close DB Connection
733 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400734 * @param resource
735 * @return void
736 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200737 protected function _close($conn_id)
Timothy Warren80ab8162011-08-22 18:26:12 -0400738 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400739 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400740 }
741
Timothy Warren80ab8162011-08-22 18:26:12 -0400742}
743
Timothy Warren80ab8162011-08-22 18:26:12 -0400744/* End of file pdo_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400745/* Location: ./system/database/drivers/pdo/pdo_driver.php */