blob: 9fbdfdd55a86cccb4524ae8dc6976093f6f6472c [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 Andreev3b0130d2012-06-24 21:41:42 +0300170 if ( ! empty($this->char_set) && in_array($this->subdriver, array('4D', 'sybase', 'mssql', 'dblib')))
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 Andreev2387ed32012-04-07 00:11:14 +0300181 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200182 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700183 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300184 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700185 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300186 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300187 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300188
Taufan Aditya18209332012-02-09 16:07:27 +0700189 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200190 try
Taufan Aditya18209332012-02-09 16:07:27 +0700191 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300192 return new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200193 }
194 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700195 {
196 if ($this->db_debug && empty($this->failover))
197 {
198 $this->display_error($e->getMessage(), '', TRUE);
199 }
200
201 return FALSE;
202 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400203 }
204
205 // --------------------------------------------------------------------
206
207 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300208 * Persistent database connection
209 *
210 * @return object
211 */
212 public function db_pconnect()
213 {
214 return $this->db_connect(TRUE);
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200220 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400221 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400222 * @return string
223 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200224 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400225 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200226 return isset($this->data_cache['version'])
227 ? $this->data_cache['version']
228 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Execute the query
235 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400236 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200237 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400238 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200239 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400240 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300241 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400247 * Begin Transaction
248 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400249 * @return bool
250 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200251 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400252 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400253 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300254 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400255 {
256 return TRUE;
257 }
258
259 // Reset the transaction failure flag.
260 // If the $test_mode flag is set to TRUE transactions will be rolled back
261 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300262 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400263
Timothy Warrenab347582011-08-23 12:29:29 -0400264 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Commit Transaction
271 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400272 * @return bool
273 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200274 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400275 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300277 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400278 {
279 return TRUE;
280 }
281
Andrey Andreev80144bf2012-04-06 22:19:26 +0300282 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Rollback Transaction
289 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400290 * @return bool
291 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200292 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400293 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400294 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300295 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400296 {
297 return TRUE;
298 }
299
Andrey Andreev80144bf2012-04-06 22:19:26 +0300300 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Escape String
307 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400308 * @param string
309 * @param bool whether or not the string will be used in a LIKE condition
310 * @return string
311 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200312 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400313 {
314 if (is_array($str))
315 {
316 foreach ($str as $key => $val)
317 {
318 $str[$key] = $this->escape_str($val, $like);
319 }
320
321 return $str;
322 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200323
Andrey Andreev2387ed32012-04-07 00:11:14 +0300324 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400325 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200326
Andrey Andreev2387ed32012-04-07 00:11:14 +0300327 // If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400328 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400329 {
330 $str = substr($str, 1, -1);
331 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200332
Timothy Warren80ab8162011-08-22 18:26:12 -0400333 // escape LIKE condition wildcards
334 if ($like === TRUE)
335 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200336 return str_replace(array($this->_like_escape_chr, '%', '_'),
337 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
338 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400339 }
340
341 return $str;
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Affected Rows
348 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200349 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400350 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200351 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400352 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300353 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200360 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300361 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200362 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400363 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200364 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400365 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200366 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400367 }
368
369 // --------------------------------------------------------------------
370
371 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400372 * Show table query
373 *
374 * Generates a platform-specific query string so that the table names can be fetched
375 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200376 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400377 * @return string
378 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200379 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400380 {
Andrey Andreev5663b1e2012-06-24 00:28:42 +0300381 if ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700382 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400383 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700384 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
385 }
Taufan Aditya18209332012-02-09 16:07:27 +0700386 else
387 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300388 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Taufan Aditya18209332012-02-09 16:07:27 +0700389 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400390
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100391 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400392 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200393 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400394 }
395
396 return $sql;
397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Show column query
403 *
404 * Generates a platform-specific query string so that the column names can be fetched
405 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400406 * @param string the table name
407 * @return string
408 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200409 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400410 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300411 return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400412 }
413
414 // --------------------------------------------------------------------
415
416 /**
417 * Field data query
418 *
419 * Generates a platform-specific query so that the column data can be retrieved
420 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400421 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200422 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400423 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200424 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400425 {
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300426 if ($this->subdriver === 'sqlite')
Taufan Aditya4e44b342012-02-18 22:47:36 +0700427 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400428 // Analog function for sqlite
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300429 return 'PRAGMA table_info('.$this->escape_identifiers($table).')';
Taufan Aditya4e44b342012-02-18 22:47:36 +0700430 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200431
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300432 return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400433 }
434
435 // --------------------------------------------------------------------
436
437 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200438 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400439 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200440 * Returns an array containing code and message of the last
441 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400442 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200443 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400444 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200445 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400446 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200447 $error = array('code' => '00000', 'message' => '');
448 $pdo_error = $this->conn_id->errorInfo();
449
450 if (empty($pdo_error[0]))
451 {
452 return $error;
453 }
454
455 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
456 if (isset($pdo_error[2]))
457 {
458 $error['message'] = $pdo_error[2];
459 }
460
461 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400462 }
463
464 // --------------------------------------------------------------------
465
466 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400467 * Update_Batch statement
468 *
469 * Generates a platform-specific batch update string from the supplied data
470 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400471 * @param string the table name
472 * @param array the update data
473 * @param array the where clause
474 * @return string
475 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200476 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400477 {
Taufan Aditya18209332012-02-09 16:07:27 +0700478 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100479 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400480
481 foreach ($values as $key => $val)
482 {
483 $ids[] = $val[$index];
484
485 foreach (array_keys($val) as $field)
486 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100487 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400488 {
489 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
490 }
491 }
492 }
493
Andrey Andreeve9f20952012-04-06 19:30:41 +0300494 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400495 $cases = '';
496
497 foreach ($final as $k => $v)
498 {
499 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700500
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400501 foreach ($v as $row)
502 {
503 $cases .= $row."\n";
504 }
505
506 $cases .= 'ELSE '.$k.' END, ';
507 }
508
509 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400510 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
511
512 return $sql;
513 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400514
Timothy Warren80ab8162011-08-22 18:26:12 -0400515 // --------------------------------------------------------------------
516
517 /**
518 * Truncate statement
519 *
520 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300521 *
522 * If the database does not support the truncate() command,
523 * then this method maps to 'DELETE FROM table'
Timothy Warren80ab8162011-08-22 18:26:12 -0400524 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400525 * @param string the table name
526 * @return string
527 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200528 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400529 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300530 return 'DELETE FROM '.$table;
Timothy Warren80ab8162011-08-22 18:26:12 -0400531 }
532
533 // --------------------------------------------------------------------
534
535 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400536 * Limit string
537 *
538 * Generates a platform-specific LIMIT clause
539 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400540 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200541 * @param int the number of rows to limit the query to
542 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400543 * @return string
544 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200545 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400546 {
Andrey Andreevfbba54e2012-06-23 20:26:31 +0300547 if ($this->subdriver === 'cubrid' OR $this->subdriver === 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400548 {
Alex Bilbiee7320252012-06-02 19:22:57 +0100549 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400550
Taufan Aditya18209332012-02-09 16:07:27 +0700551 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400552 }
553 else
554 {
Taufan Aditya18209332012-02-09 16:07:27 +0700555 $sql .= 'LIMIT '.$limit;
556 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200557
Timothy Warren0a43ad82011-09-15 20:15:19 -0400558 return $sql;
559 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400560 }
561
Timothy Warren80ab8162011-08-22 18:26:12 -0400562}
563
Timothy Warren80ab8162011-08-22 18:26:12 -0400564/* End of file pdo_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300565/* Location: ./system/database/drivers/pdo/pdo_driver.php */