blob: 19338e30fd489a547bbf0062cc1a624406945706 [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 Andreevbd44d5a2012-03-20 22:59:29 +020049 protected $_like_escape_str;
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 {
84 $this->_like_escape_str = '';
85 $this->_like_escape_chr = '';
86 }
Taufan Aditya18209332012-02-09 16:07:27 +070087 elseif ($this->pdodriver == 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040088 {
89 $this->_like_escape_str = " {escape '%s'} ";
90 $this->_like_escape_chr = '!';
91 }
92 else
93 {
94 $this->_like_escape_str = " ESCAPE '%s' ";
95 $this->_like_escape_chr = '!';
96 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020097
98 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -040099 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
100 }
101
102 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700103 * Connection String
104 *
Taufan Aditya18209332012-02-09 16:07:27 +0700105 * @param array
106 * @return void
107 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200108 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700109 {
110 if (strpos($this->hostname, ':'))
111 {
112 // hostname generally would have this prototype
113 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
114 // We need to get the prefix (pdodriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500115 $dsnarray = explode(':', $this->hostname);
116 $this->pdodriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700117
118 // End dsn with a semicolon for extra backward compability
119 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500120 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500121 {
122 $this->dsn .= rtrim($this->hostname, ';').';';
123 }
Taufan Aditya18209332012-02-09 16:07:27 +0700124 }
125 else
126 {
127 // Invalid DSN, display an error
128 if ( ! array_key_exists('pdodriver', $params))
129 {
130 show_error('Invalid DB Connection String for PDO');
131 }
132
133 // Assuming that the following DSN string format is used:
134 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
135 $this->dsn = $this->pdodriver.':';
136
137 // Add hostname to the DSN for databases that need it
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200138 if ( ! empty($this->hostname)
Timothy Warren928d83c2012-02-13 14:07:57 -0500139 && strpos($this->hostname, ':') === FALSE
140 && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700141 {
142 $this->dsn .= 'host='.$this->hostname.';';
143 }
144
145 // Add a port to the DSN for databases that can use it
146 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
147 {
148 $this->dsn .= 'port='.$this->port.';';
149 }
150 }
151
152 // Add the database name to the DSN, if needed
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200153 if (stripos($this->dsn, 'dbname') === FALSE
Taufan Aditya18209332012-02-09 16:07:27 +0700154 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
155 {
156 $this->dsn .= 'dbname='.$this->database.';';
157 }
158 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
159 {
160 if (stripos($this->dsn, 'dsn') === FALSE)
161 {
162 $this->dsn .= 'database='.$this->database.';';
163 }
164 }
165 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
166 {
167 if ($this->database !== ':memory')
168 {
169 if ( ! file_exists($this->database))
170 {
171 show_error('Invalid DB Connection string for PDO SQLite');
172 }
173
174 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
175 }
176
177 $this->dsn .= $this->database;
178 }
179
180 // Add charset to the DSN, if needed
181 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
182 {
183 $this->dsn .= 'charset='.$this->char_set.';';
184 }
185 }
186
187 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400188 * Non-persistent database connection
189 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200190 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200192 public function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400193 {
Taufan Aditya18209332012-02-09 16:07:27 +0700194 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
195
196 return $this->pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Persistent database connection
203 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200204 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400205 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200206 public function db_pconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400207 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200208 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Taufan Aditya18209332012-02-09 16:07:27 +0700209 $this->options[PDO::ATTR_PERSISTENT] = TRUE;
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200210
Taufan Aditya18209332012-02-09 16:07:27 +0700211 return $this->pdo_connect();
212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * PDO connection
218 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200219 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700220 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200221 public function pdo_connect()
Taufan Aditya18209332012-02-09 16:07:27 +0700222 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700223 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200224 if ($this->pdodriver === 'mysql' && ! is_php('5.3.6'))
Taufan Aditya18209332012-02-09 16:07:27 +0700225 {
Taufan Aditya55bb97e2012-02-09 16:43:26 +0700226 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'";
Taufan Aditya18209332012-02-09 16:07:27 +0700227 }
228
229 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200230 try
Taufan Aditya18209332012-02-09 16:07:27 +0700231 {
232 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200233 }
234 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700235 {
236 if ($this->db_debug && empty($this->failover))
237 {
238 $this->display_error($e->getMessage(), '', TRUE);
239 }
240
241 return FALSE;
242 }
243
244 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400245 }
246
247 // --------------------------------------------------------------------
248
249 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 * Select the database
251 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400252 * @return resource
253 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200254 public function db_select()
Timothy Warren80ab8162011-08-22 18:26:12 -0400255 {
256 // Not needed for PDO
257 return TRUE;
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200263 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400264 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 * @return string
266 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200267 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400268 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200269 return isset($this->data_cache['version'])
270 ? $this->data_cache['version']
271 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Execute the query
278 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400279 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200280 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400281 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200282 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400283 {
284 $sql = $this->_prep_query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700285
Timothy Warren51a48882011-09-14 13:47:06 -0400286 $result_id = $this->conn_id->query($sql);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200287
Timothy Warrend6691532011-10-07 10:03:01 -0400288 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400289 {
290 $this->affect_rows = $result_id->rowCount();
291 }
292 else
293 {
294 $this->affect_rows = 0;
295 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200296
Timothy Warren51a48882011-09-14 13:47:06 -0400297 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Prep the query
304 *
305 * If needed, each database adapter can prep the query string
306 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400307 * @param string an SQL query
308 * @return string
309 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200310 protected function _prep_query($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400311 {
Taufan Aditya18209332012-02-09 16:07:27 +0700312 if ($this->pdodriver === 'pgsql')
313 {
314 // Change the backtick(s) for Postgre
315 $sql = str_replace('`', '"', $sql);
316 }
317 elseif ($this->pdodriver === 'sqlite')
318 {
319 // Change the backtick(s) for SQLite
Timothy Warren667c9fe2012-03-19 19:06:34 -0400320 $sql = str_replace('`', '', $sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700321 }
322
Timothy Warren80ab8162011-08-22 18:26:12 -0400323 return $sql;
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * Begin Transaction
330 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400331 * @return bool
332 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200333 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400334 {
335 if ( ! $this->trans_enabled)
336 {
337 return TRUE;
338 }
339
340 // When transactions are nested we only begin/commit/rollback the outermost ones
341 if ($this->_trans_depth > 0)
342 {
343 return TRUE;
344 }
345
346 // Reset the transaction failure flag.
347 // If the $test_mode flag is set to TRUE transactions will be rolled back
348 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400349 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400350
Timothy Warrenab347582011-08-23 12:29:29 -0400351 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Commit Transaction
358 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400359 * @return bool
360 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200361 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400362 {
363 if ( ! $this->trans_enabled)
364 {
365 return TRUE;
366 }
367
368 // When transactions are nested we only begin/commit/rollback the outermost ones
369 if ($this->_trans_depth > 0)
370 {
371 return TRUE;
372 }
373
Timothy Warrenab347582011-08-23 12:29:29 -0400374 $ret = $this->conn->commit();
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200375
Timothy Warren80ab8162011-08-22 18:26:12 -0400376 return $ret;
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Rollback Transaction
383 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400384 * @return bool
385 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200386 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400387 {
388 if ( ! $this->trans_enabled)
389 {
390 return TRUE;
391 }
392
393 // When transactions are nested we only begin/commit/rollback the outermost ones
394 if ($this->_trans_depth > 0)
395 {
396 return TRUE;
397 }
398
Timothy Warrenab347582011-08-23 12:29:29 -0400399 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700400
Timothy Warren80ab8162011-08-22 18:26:12 -0400401 return $ret;
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Escape String
408 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400409 * @param string
410 * @param bool whether or not the string will be used in a LIKE condition
411 * @return string
412 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200413 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400414 {
415 if (is_array($str))
416 {
417 foreach ($str as $key => $val)
418 {
419 $str[$key] = $this->escape_str($val, $like);
420 }
421
422 return $str;
423 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200424
Timothy Warren47663972011-10-05 16:44:50 -0400425 //Escape the string
426 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200427
Timothy Warren47663972011-10-05 16:44:50 -0400428 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400429 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400430 {
431 $str = substr($str, 1, -1);
432 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200433
Timothy Warren80ab8162011-08-22 18:26:12 -0400434 // escape LIKE condition wildcards
435 if ($like === TRUE)
436 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200437 return str_replace(array($this->_like_escape_chr, '%', '_'),
438 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
439 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400440 }
441
442 return $str;
443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * Affected Rows
449 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200450 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400451 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200452 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400453 {
Timothy Warren51a48882011-09-14 13:47:06 -0400454 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200461 *
462 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400463 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200464 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400465 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200466 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400467 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200468 $query = $this->query('SELECT LASTVAL() AS ins_id');
469 $query = $query->row();
470 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400471 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200472
473 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * "Count All" query
480 *
481 * Generates a platform-specific query string that counts all records in
482 * the specified database
483 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400484 * @param string
485 * @return string
486 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400487 function count_all($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400488 {
489 if ($table == '')
490 {
491 return 0;
492 }
493
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200494 $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Taufan Aditya18209332012-02-09 16:07:27 +0700495 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400496
497 if ($query->num_rows() == 0)
498 {
499 return 0;
500 }
501
502 $row = $query->row();
503 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700504
Timothy Warren80ab8162011-08-22 18:26:12 -0400505 return (int) $row->numrows;
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Show table query
512 *
513 * Generates a platform-specific query string so that the table names can be fetched
514 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200515 * @param bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400516 * @return string
517 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200518 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400519 {
Taufan Aditya18209332012-02-09 16:07:27 +0700520 if ($this->pdodriver == 'pgsql')
521 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400522 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700523 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
524 }
Taufan Aditya4e44b342012-02-18 22:47:36 +0700525 elseif ($this->pdodriver == 'sqlite')
526 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400527 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700528 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
529 }
Taufan Aditya18209332012-02-09 16:07:27 +0700530 else
531 {
532 $sql = "SHOW TABLES FROM `".$this->database."`";
533 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400534
535 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
536 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200537 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400538 }
539
540 return $sql;
541 }
542
543 // --------------------------------------------------------------------
544
545 /**
546 * Show column query
547 *
548 * Generates a platform-specific query string so that the column names can be fetched
549 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400550 * @param string the table name
551 * @return string
552 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200553 protected function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400554 {
Taufan Aditya18209332012-02-09 16:07:27 +0700555 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400556 }
557
558 // --------------------------------------------------------------------
559
560 /**
561 * Field data query
562 *
563 * Generates a platform-specific query so that the column data can be retrieved
564 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400565 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200566 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400567 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200568 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400569 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700570 if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql')
571 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400572 // Analog function for mysql and postgre
Taufan Aditya4e44b342012-02-18 22:47:36 +0700573 return 'SELECT * FROM '.$this->_from_tables($table).' LIMIT 1';
574 }
575 elseif ($this->pdodriver == 'oci')
576 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400577 // Analog function for oci
Taufan Aditya4e44b342012-02-18 22:47:36 +0700578 return 'SELECT * FROM '.$this->_from_tables($table).' WHERE ROWNUM <= 1';
579 }
580 elseif ($this->pdodriver == 'sqlite')
581 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400582 // Analog function for sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700583 return 'PRAGMA table_info('.$this->_from_tables($table).')';
584 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200585
Taufan Aditya18209332012-02-09 16:07:27 +0700586 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400587 }
588
589 // --------------------------------------------------------------------
590
591 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200592 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400593 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200594 * Returns an array containing code and message of the last
595 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400596 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200597 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400598 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200599 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400600 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200601 $error = array('code' => '00000', 'message' => '');
602 $pdo_error = $this->conn_id->errorInfo();
603
604 if (empty($pdo_error[0]))
605 {
606 return $error;
607 }
608
609 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
610 if (isset($pdo_error[2]))
611 {
612 $error['message'] = $pdo_error[2];
613 }
614
615 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400616 }
617
618 // --------------------------------------------------------------------
619
620 /**
621 * Escape the SQL Identifiers
622 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400623 * This function escapes column and table names
Timothy Warren80ab8162011-08-22 18:26:12 -0400624 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400625 * @param string
626 * @return string
627 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200628 public function _escape_identifiers($item)
Timothy Warren80ab8162011-08-22 18:26:12 -0400629 {
630 if ($this->_escape_char == '')
631 {
632 return $item;
633 }
634
635 foreach ($this->_reserved_identifiers as $id)
636 {
637 if (strpos($item, '.'.$id) !== FALSE)
638 {
639 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
640
641 // remove duplicates if the user already included the escape
642 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
643 }
644 }
645
646 if (strpos($item, '.') !== FALSE)
647 {
Taufan Aditya18209332012-02-09 16:07:27 +0700648 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
649 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400650 }
651 else
652 {
653 $str = $this->_escape_char.$item.$this->_escape_char;
654 }
655
656 // remove duplicates if the user already included the escape
657 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
658 }
659
660 // --------------------------------------------------------------------
661
662 /**
663 * From Tables
664 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400665 * This function implicitly groups FROM tables so there is no confusion
Timothy Warren80ab8162011-08-22 18:26:12 -0400666 * about operator precedence in harmony with SQL standards
667 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200668 * @param array
669 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400670 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200671 protected function _from_tables($tables)
Timothy Warren80ab8162011-08-22 18:26:12 -0400672 {
673 if ( ! is_array($tables))
674 {
675 $tables = array($tables);
676 }
677
Taufan Aditya18209332012-02-09 16:07:27 +0700678 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Insert statement
685 *
686 * Generates a platform-specific insert string from the supplied data
687 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400688 * @param string the table name
689 * @param array the insert keys
690 * @param array the insert values
691 * @return string
692 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200693 protected function _insert($table, $keys, $values)
Timothy Warren80ab8162011-08-22 18:26:12 -0400694 {
Taufan Aditya18209332012-02-09 16:07:27 +0700695 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400696 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200697
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400698 // --------------------------------------------------------------------
699
700 /**
701 * Insert_batch statement
702 *
703 * Generates a platform-specific insert string from the supplied data
704 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200705 * @param string the table name
706 * @param array the insert keys
707 * @param array the insert values
708 * @return string
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400709 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200710 protected function _insert_batch($table, $keys, $values)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400711 {
Taufan Aditya18209332012-02-09 16:07:27 +0700712 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400713 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400714
715 // --------------------------------------------------------------------
716
717 /**
718 * Update statement
719 *
720 * Generates a platform-specific update string from the supplied data
721 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400722 * @param string the table name
723 * @param array the update data
724 * @param array the where clause
725 * @param array the orderby clause
726 * @param array the limit clause
727 * @return string
728 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200729 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400730 {
731 foreach ($values as $key => $val)
732 {
733 $valstr[] = $key." = ".$val;
734 }
735
Taufan Aditya18209332012-02-09 16:07:27 +0700736 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
737 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400738
Taufan Aditya18209332012-02-09 16:07:27 +0700739 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
740 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400741 $sql .= $orderby.$limit;
742
743 return $sql;
744 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200745
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400746 // --------------------------------------------------------------------
747
748 /**
749 * Update_Batch statement
750 *
751 * Generates a platform-specific batch update string from the supplied data
752 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400753 * @param string the table name
754 * @param array the update data
755 * @param array the where clause
756 * @return string
757 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200758 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400759 {
Taufan Aditya18209332012-02-09 16:07:27 +0700760 $ids = array();
761 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400762
763 foreach ($values as $key => $val)
764 {
765 $ids[] = $val[$index];
766
767 foreach (array_keys($val) as $field)
768 {
769 if ($field != $index)
770 {
771 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
772 }
773 }
774 }
775
Taufan Aditya18209332012-02-09 16:07:27 +0700776 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400777 $cases = '';
778
779 foreach ($final as $k => $v)
780 {
781 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700782
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400783 foreach ($v as $row)
784 {
785 $cases .= $row."\n";
786 }
787
788 $cases .= 'ELSE '.$k.' END, ';
789 }
790
791 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400792 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
793
794 return $sql;
795 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400796
Timothy Warren80ab8162011-08-22 18:26:12 -0400797 // --------------------------------------------------------------------
798
799 /**
800 * Truncate statement
801 *
802 * Generates a platform-specific truncate string from the supplied data
803 * If the database does not support the truncate() command
Timothy Warren667c9fe2012-03-19 19:06:34 -0400804 * This function maps to "DELETE FROM table"
Timothy Warren80ab8162011-08-22 18:26:12 -0400805 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400806 * @param string the table name
807 * @return string
808 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200809 protected function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400810 {
811 return $this->_delete($table);
812 }
813
814 // --------------------------------------------------------------------
815
816 /**
817 * Delete statement
818 *
819 * Generates a platform-specific delete string from the supplied data
820 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400821 * @param string the table name
822 * @param array the where clause
823 * @param string the limit clause
824 * @return string
825 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200826 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400827 {
828 $conditions = '';
829
830 if (count($where) > 0 OR count($like) > 0)
831 {
Taufan Aditya18209332012-02-09 16:07:27 +0700832 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400833 $conditions .= implode("\n", $this->ar_where);
834
835 if (count($where) > 0 && count($like) > 0)
836 {
837 $conditions .= " AND ";
838 }
Taufan Aditya18209332012-02-09 16:07:27 +0700839
Timothy Warren80ab8162011-08-22 18:26:12 -0400840 $conditions .= implode("\n", $like);
841 }
842
843 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
844
Taufan Aditya18209332012-02-09 16:07:27 +0700845 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400846 }
847
848 // --------------------------------------------------------------------
849
850 /**
851 * Limit string
852 *
853 * Generates a platform-specific LIMIT clause
854 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400855 * @param string the sql query string
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200856 * @param int the number of rows to limit the query to
857 * @param int the offset value
Timothy Warren80ab8162011-08-22 18:26:12 -0400858 * @return string
859 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200860 protected function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400861 {
Taufan Aditya18209332012-02-09 16:07:27 +0700862 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400863 {
Taufan Aditya18209332012-02-09 16:07:27 +0700864 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400865
Taufan Aditya18209332012-02-09 16:07:27 +0700866 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400867 }
868 else
869 {
Taufan Aditya18209332012-02-09 16:07:27 +0700870 $sql .= 'LIMIT '.$limit;
871 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200872
Timothy Warren0a43ad82011-09-15 20:15:19 -0400873 return $sql;
874 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Close DB Connection
881 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400882 * @param resource
883 * @return void
884 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200885 protected function _close($conn_id)
Timothy Warren80ab8162011-08-22 18:26:12 -0400886 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400887 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400888 }
889
Timothy Warren80ab8162011-08-22 18:26:12 -0400890}
891
Timothy Warren80ab8162011-08-22 18:26:12 -0400892/* End of file pdo_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400893/* Location: ./system/database/drivers/pdo/pdo_driver.php */