blob: dc03864a669c607fe695d7989e78f29f6f8be8a5 [file] [log] [blame]
Andrey Andreevbd44d5a2012-03-20 22:59:29 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -04002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Timothy Warren80ab8162011-08-22 18:26:12 -04006 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -04007 * NOTICE OF LICENSE
Andrey Andreevbd44d5a2012-03-20 22:59:29 +02008 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -04009 * Licensed under the Open Software License version 3.0
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020010 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -040011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Timothy Warren80ab8162011-08-22 18:26:12 -040019 * @package CodeIgniter
Timothy Warrend1a5ba22011-10-21 04:45:28 -040020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Timothy Warrend1a5ba22011-10-21 04:45:28 -040022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Timothy Warren80ab8162011-08-22 18:26:12 -040023 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040024 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040025 * @filesource
26 */
27
Timothy Warren80ab8162011-08-22 18:26:12 -040028/**
Timothy Warren02615962011-08-24 08:21:36 -040029 * PDO Database Adapter Class
Timothy Warren80ab8162011-08-22 18:26:12 -040030 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Timothy Warren80ab8162011-08-22 18:26:12 -040033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Timothy Warrend1a5ba22011-10-21 04:45:28 -040038 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_pdo_driver extends CI_DB {
42
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020043 public $dbdriver = 'pdo';
Timothy Warren80ab8162011-08-22 18:26:12 -040044
45 // the character used to excape - not necessary for PDO
Andrey Andreev5663b1e2012-06-24 00:28:42 +030046 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 Andreevfbba54e2012-06-23 20:26:31 +030060 // need to track the PDO options
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020061 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040062
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020063 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040064 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040065 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070066
Alex Bilbie48a2baf2012-06-02 11:09:54 +010067 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070068 {
69 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070070 // This is for general PDO users, who tend to have a full DSN string.
Andrey Andreevfbba54e2012-06-23 20:26:31 +030071 $this->subdriver = end($match);
Taufan Aditya18209332012-02-09 16:07:27 +070072 }
73 else
74 {
75 // Try to build a complete DSN string from params
76 $this->_connect_string($params);
77 }
78
Timothy Warrenc7ba6642011-09-14 12:25:14 -040079 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070080 // this one depends on the driver being used
Andrey Andreev1732b1b2012-06-24 02:42:38 +030081 if ($this->subdriver === 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040082 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +030083 $this->_escape_char = '';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040084 $this->_like_escape_str = " {escape '%s'} ";
Timothy Warrenc7ba6642011-09-14 12:25:14 -040085 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020086
87 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -040088 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
89 }
90
91 /**
Taufan Aditya18209332012-02-09 16:07:27 +070092 * Connection String
93 *
Taufan Aditya18209332012-02-09 16:07:27 +070094 * @param array
95 * @return void
96 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020097 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +070098 {
99 if (strpos($this->hostname, ':'))
100 {
101 // hostname generally would have this prototype
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300102 // $db['hostname'] = 'subdriver:host(/Server(/DSN))=hostname(/DSN);';
103 // We need to get the prefix (subdriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500104 $dsnarray = explode(':', $this->hostname);
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300105 $this->subdriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700106
107 // End dsn with a semicolon for extra backward compability
108 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500109 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500110 {
111 $this->dsn .= rtrim($this->hostname, ';').';';
112 }
Taufan Aditya18209332012-02-09 16:07:27 +0700113 }
114 else
115 {
116 // Invalid DSN, display an error
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300117 if ( ! array_key_exists('subdriver', $params))
Taufan Aditya18209332012-02-09 16:07:27 +0700118 {
119 show_error('Invalid DB Connection String for PDO');
120 }
121
122 // Assuming that the following DSN string format is used:
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300123 // $dsn = 'pdo://username:password@hostname:port/database?subdriver=pgsql';
124 $this->dsn = $this->subdriver.':';
Taufan Aditya18209332012-02-09 16:07:27 +0700125
126 // Add hostname to the DSN for databases that need it
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200127 if ( ! empty($this->hostname)
Timothy Warren928d83c2012-02-13 14:07:57 -0500128 && strpos($this->hostname, ':') === FALSE
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300129 && in_array($this->subdriver, array('informix', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700130 {
131 $this->dsn .= 'host='.$this->hostname.';';
132 }
133
134 // Add a port to the DSN for databases that can use it
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300135 if ( ! empty($this->port) && in_array($this->subdriver, array('informix', 'ibm', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700136 {
137 $this->dsn .= 'port='.$this->port.';';
138 }
139 }
140
141 // Add the database name to the DSN, if needed
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300142 if (stripos($this->dsn, 'dbname') === FALSE
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300143 && in_array($this->subdriver, array('4D', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300144 {
145 $this->dsn .= 'dbname='.$this->database.';';
146 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300147 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->subdriver, array('ibm', 'sqlsrv')))
Taufan Aditya18209332012-02-09 16:07:27 +0700148 {
149 if (stripos($this->dsn, 'dsn') === FALSE)
150 {
151 $this->dsn .= 'database='.$this->database.';';
152 }
153 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300154 elseif ($this->subdriver === 'sqlite' && $this->dsn === 'sqlite:')
Taufan Aditya18209332012-02-09 16:07:27 +0700155 {
156 if ($this->database !== ':memory')
157 {
158 if ( ! file_exists($this->database))
159 {
160 show_error('Invalid DB Connection string for PDO SQLite');
161 }
162
163 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
164 }
165
166 $this->dsn .= $this->database;
167 }
168
169 // Add charset to the DSN, if needed
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300170 if ( ! empty($this->char_set) && in_array($this->subdriver, array('4D', 'sybase', 'mssql', 'dblib', 'oci')))
Taufan Aditya18209332012-02-09 16:07:27 +0700171 {
172 $this->dsn .= 'charset='.$this->char_set.';';
173 }
174 }
175
Andrey Andreev2387ed32012-04-07 00:11:14 +0300176 // --------------------------------------------------------------------
177
Taufan Aditya18209332012-02-09 16:07:27 +0700178 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400179 * Non-persistent database connection
180 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200181 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400182 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200183 public function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400184 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300185 return $this->_pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Persistent database connection
192 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200193 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400194 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200195 public function db_pconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400196 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300197 return $this->_pdo_connect(TRUE);
Taufan Aditya18209332012-02-09 16:07:27 +0700198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * PDO connection
204 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300205 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200206 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700207 */
Andrey Andreev2387ed32012-04-07 00:11:14 +0300208 protected function _pdo_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700209 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300210 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
211 $persistent === FALSE OR $this->options[PDO::ATTR_PERSISTENT] = TRUE;
212
Taufan Aditya18209332012-02-09 16:07:27 +0700213 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200214 try
Taufan Aditya18209332012-02-09 16:07:27 +0700215 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300216 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200217 }
218 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700219 {
220 if ($this->db_debug && empty($this->failover))
221 {
222 $this->display_error($e->getMessage(), '', TRUE);
223 }
224
225 return FALSE;
226 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400227 }
228
229 // --------------------------------------------------------------------
230
231 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200232 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400233 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400234 * @return string
235 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200236 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400237 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200238 return isset($this->data_cache['version'])
239 ? $this->data_cache['version']
240 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Execute the query
247 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400248 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200249 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200251 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400252 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300253 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 }
255
256 // --------------------------------------------------------------------
257
258 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400259 * Begin Transaction
260 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400261 * @return bool
262 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200263 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400264 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300266 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400267 {
268 return TRUE;
269 }
270
271 // Reset the transaction failure flag.
272 // If the $test_mode flag is set to TRUE transactions will be rolled back
273 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300274 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400275
Timothy Warrenab347582011-08-23 12:29:29 -0400276 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Commit Transaction
283 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400284 * @return bool
285 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200286 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400287 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400288 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300289 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400290 {
291 return TRUE;
292 }
293
Andrey Andreev80144bf2012-04-06 22:19:26 +0300294 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * Rollback Transaction
301 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400302 * @return bool
303 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200304 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400305 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400306 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300307 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400308 {
309 return TRUE;
310 }
311
Andrey Andreev80144bf2012-04-06 22:19:26 +0300312 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Escape String
319 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400320 * @param string
321 * @param bool whether or not the string will be used in a LIKE condition
322 * @return string
323 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200324 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400325 {
326 if (is_array($str))
327 {
328 foreach ($str as $key => $val)
329 {
330 $str[$key] = $this->escape_str($val, $like);
331 }
332
333 return $str;
334 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200335
Andrey Andreev2387ed32012-04-07 00:11:14 +0300336 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400337 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200338
Andrey Andreev2387ed32012-04-07 00:11:14 +0300339 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400340 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400341 {
342 $str = substr($str, 1, -1);
343 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200344
Timothy Warren80ab8162011-08-22 18:26:12 -0400345 // escape LIKE condition wildcards
346 if ($like === TRUE)
347 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200348 return str_replace(array($this->_like_escape_chr, '%', '_'),
349 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
350 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400351 }
352
353 return $str;
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Affected Rows
360 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200361 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400362 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200363 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400364 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300365 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200372 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300373 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200374 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400375 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200376 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400377 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200378 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400379 }
380
381 // --------------------------------------------------------------------
382
383 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400384 * Show table query
385 *
386 * Generates a platform-specific query string so that the table names can be fetched
387 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200388 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400389 * @return string
390 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200391 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400392 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300393 if ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700394 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400395 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700396 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
397 }
Taufan Aditya18209332012-02-09 16:07:27 +0700398 else
399 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300400 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700401 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400402
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100403 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400404 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200405 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400406 }
407
408 return $sql;
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Show column query
415 *
416 * Generates a platform-specific query string so that the column names can be fetched
417 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400418 * @param string the table name
419 * @return string
420 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200421 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400422 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300423 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400424 }
425
426 // --------------------------------------------------------------------
427
428 /**
429 * Field data query
430 *
431 * Generates a platform-specific query so that the column data can be retrieved
432 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400433 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200434 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400435 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200436 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400437 {
Andrey Andreev1732b1b2012-06-24 02:42:38 +0300438 if ($this->subdriver === 'oci')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700439 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400440 // Analog function for oci
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300441 return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700442 }
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300443 elseif ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700444 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400445 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300446 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700447 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200448
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300449 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400450 }
451
452 // --------------------------------------------------------------------
453
454 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200455 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400456 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200457 * Returns an array containing code and message of the last
458 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400459 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200460 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400461 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200462 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400463 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200464 $error = array('code' => '00000', 'message' => '');
465 $pdo_error = $this->conn_id->errorInfo();
466
467 if (empty($pdo_error[0]))
468 {
469 return $error;
470 }
471
472 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
473 if (isset($pdo_error[2]))
474 {
475 $error['message'] = $pdo_error[2];
476 }
477
478 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400479 }
480
481 // --------------------------------------------------------------------
482
483 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400484 * Update_Batch statement
485 *
486 * Generates a platform-specific batch update string from the supplied data
487 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400488 * @param string the table name
489 * @param array the update data
490 * @param array the where clause
491 * @return string
492 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200493 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400494 {
Taufan Aditya18209332012-02-09 16:07:27 +0700495 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100496 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400497
498 foreach ($values as $key => $val)
499 {
500 $ids[] = $val[$index];
501
502 foreach (array_keys($val) as $field)
503 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100504 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400505 {
506 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
507 }
508 }
509 }
510
Andrey Andreeve9f20952012-04-06 19:30:41 +0300511 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400512 $cases = '';
513
514 foreach ($final as $k => $v)
515 {
516 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700517
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400518 foreach ($v as $row)
519 {
520 $cases .= $row."\n";
521 }
522
523 $cases .= 'ELSE '.$k.' END, ';
524 }
525
526 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400527 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
528
529 return $sql;
530 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400531
Timothy Warren80ab8162011-08-22 18:26:12 -0400532 // --------------------------------------------------------------------
533
534 /**
535 * Truncate statement
536 *
537 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300538 *
539 * If the database does not support the truncate() command,
540 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400541 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400542 * @param string the table name
543 * @return string
544 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200545 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400546 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300547 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400548 }
549
550 // --------------------------------------------------------------------
551
552 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400553 * Limit string
554 *
555 * Generates a platform-specific LIMIT clause
556 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400557 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200558 * @param int the number of rows to limit the query to
559 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400560 * @return string
561 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200562 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400563 {
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300564 if ($this->subdriver === 'cubrid' OR $this->subdriver === 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400565 {
Alex Bilbiee7320252012-06-02 19:22:57 +0100566 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400567
Taufan Aditya18209332012-02-09 16:07:27 +0700568 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400569 }
570 else
571 {
Taufan Aditya18209332012-02-09 16:07:27 +0700572 $sql .= 'LIMIT '.$limit;
573 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200574
Timothy Warren0a43ad82011-09-15 20:15:19 -0400575 return $sql;
576 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400577 }
578
Timothy Warren80ab8162011-08-22 18:26:12 -0400579}
580
Timothy Warren80ab8162011-08-22 18:26:12 -0400581/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300582/* Location: ./system/database/drivers/pdo/pdo_driver.php */