blob: 727f097f844a4accc2b8beb972da2555343e376e [file] [log] [blame]
Timothy Warren80ab8162011-08-22 18:26:12 -04001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
30/**
Timothy Warren02615962011-08-24 08:21:36 -040031 * PDO Database Adapter Class
Timothy Warren80ab8162011-08-22 18:26:12 -040032 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Timothy Warrend1a5ba22011-10-21 04:45:28 -040040 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_pdo_driver extends CI_DB {
44
Timothy Warren8c332e72012-03-19 18:09:13 -040045 public $dbdriver = 'pdo';
Timothy Warren80ab8162011-08-22 18:26:12 -040046
47 // the character used to excape - not necessary for PDO
Timothy Warren8c332e72012-03-19 18:09:13 -040048 protected $_escape_char = '';
Taufan Aditya18209332012-02-09 16:07:27 +070049
50 // clause and character used for LIKE escape sequences
Timothy Warren8c332e72012-03-19 18:09:13 -040051 protected $_like_escape_str;
52 protected $_like_escape_chr;
Timothy Warren80ab8162011-08-22 18:26:12 -040053
54 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
Timothy Warren8c332e72012-03-19 18:09:13 -040057 * used for the count_all() and count_all_results() public functions.
Timothy Warren80ab8162011-08-22 18:26:12 -040058 */
Timothy Warren8c332e72012-03-19 18:09:13 -040059 protected $_count_string = "SELECT COUNT(*) AS ";
60 protected $_random_keyword;
Taufan Aditya18209332012-02-09 16:07:27 +070061
Andrey Andreev738f5342012-03-06 20:43:40 +020062 // need to track the pdo driver and options
Timothy Warren8c332e72012-03-19 18:09:13 -040063 protected $pdodriver;
64 protected $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040065
Timothy Warren8c332e72012-03-19 18:09:13 -040066 /**
67 * Pre-connection setup
68 */
69 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040070 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040071 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070072
73 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) == 2)
74 {
75 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070076 // This is for general PDO users, who tend to have a full DSN string.
Taufan Aditya18209332012-02-09 16:07:27 +070077 $this->pdodriver = end($match);
78 }
79 else
80 {
81 // Try to build a complete DSN string from params
82 $this->_connect_string($params);
83 }
84
Timothy Warrenc7ba6642011-09-14 12:25:14 -040085 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070086 // this one depends on the driver being used
87 if ($this->pdodriver == 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040088 {
89 $this->_like_escape_str = '';
90 $this->_like_escape_chr = '';
91 }
Taufan Aditya18209332012-02-09 16:07:27 +070092 elseif ($this->pdodriver == 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040093 {
94 $this->_like_escape_str = " {escape '%s'} ";
95 $this->_like_escape_chr = '!';
96 }
97 else
98 {
99 $this->_like_escape_str = " ESCAPE '%s' ";
100 $this->_like_escape_chr = '!';
101 }
102
Taufan Aditya18209332012-02-09 16:07:27 +0700103 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400104 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
105 }
106
107 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700108 * Connection String
109 *
Taufan Aditya18209332012-02-09 16:07:27 +0700110 * @param array
111 * @return void
112 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400113 protected function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700114 {
115 if (strpos($this->hostname, ':'))
116 {
117 // hostname generally would have this prototype
118 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
119 // We need to get the prefix (pdodriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500120 $dsnarray = explode(':', $this->hostname);
121 $this->pdodriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700122
123 // End dsn with a semicolon for extra backward compability
124 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500125 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500126 {
127 $this->dsn .= rtrim($this->hostname, ';').';';
128 }
Taufan Aditya18209332012-02-09 16:07:27 +0700129 }
130 else
131 {
132 // Invalid DSN, display an error
133 if ( ! array_key_exists('pdodriver', $params))
134 {
135 show_error('Invalid DB Connection String for PDO');
136 }
137
138 // Assuming that the following DSN string format is used:
139 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
140 $this->dsn = $this->pdodriver.':';
141
142 // Add hostname to the DSN for databases that need it
Timothy Warren928d83c2012-02-13 14:07:57 -0500143 if ( ! empty($this->hostname)
144 && strpos($this->hostname, ':') === FALSE
145 && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700146 {
147 $this->dsn .= 'host='.$this->hostname.';';
148 }
149
150 // Add a port to the DSN for databases that can use it
151 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
152 {
153 $this->dsn .= 'port='.$this->port.';';
154 }
155 }
156
157 // Add the database name to the DSN, if needed
158 if (stripos($this->dsn, 'dbname') === FALSE
159 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
160 {
161 $this->dsn .= 'dbname='.$this->database.';';
162 }
163 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
164 {
165 if (stripos($this->dsn, 'dsn') === FALSE)
166 {
167 $this->dsn .= 'database='.$this->database.';';
168 }
169 }
170 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
171 {
172 if ($this->database !== ':memory')
173 {
174 if ( ! file_exists($this->database))
175 {
176 show_error('Invalid DB Connection string for PDO SQLite');
177 }
178
179 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
180 }
181
182 $this->dsn .= $this->database;
183 }
184
185 // Add charset to the DSN, if needed
186 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
187 {
188 $this->dsn .= 'charset='.$this->char_set.';';
189 }
190 }
191
192 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400193 * Non-persistent database connection
194 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400195 * @return resource
196 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400197 protected function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400198 {
Taufan Aditya18209332012-02-09 16:07:27 +0700199 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
200
201 return $this->pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Persistent database connection
208 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400209 * @return resource
210 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400211 protected function db_pconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400212 {
Taufan Aditya18209332012-02-09 16:07:27 +0700213 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
214 $this->options[PDO::ATTR_PERSISTENT] = TRUE;
Timothy Warrend93393f2011-10-26 11:31:49 -0400215
Taufan Aditya18209332012-02-09 16:07:27 +0700216 return $this->pdo_connect();
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * PDO connection
223 *
Taufan Aditya18209332012-02-09 16:07:27 +0700224 * @return resource
225 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400226 protected function pdo_connect()
Taufan Aditya18209332012-02-09 16:07:27 +0700227 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700228 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Taufan Aditya18209332012-02-09 16:07:27 +0700229 if ($this->pdodriver == 'mysql' && is_php('5.3.6'))
230 {
Taufan Aditya55bb97e2012-02-09 16:43:26 +0700231 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'";
Taufan Aditya18209332012-02-09 16:07:27 +0700232 }
233
234 // Connecting...
235 try
236 {
237 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
238 }
239 catch (PDOException $e)
240 {
241 if ($this->db_debug && empty($this->failover))
242 {
243 $this->display_error($e->getMessage(), '', TRUE);
244 }
245
246 return FALSE;
247 }
248
249 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Reconnect
256 *
257 * Keep / reestablish the db connection if no queries have been
258 * sent for a length of time exceeding the server's idle timeout
259 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400260 * @return void
261 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400262 public function reconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400263 {
Timothy Warren02615962011-08-24 08:21:36 -0400264 if ($this->db->db_debug)
265 {
266 return $this->db->display_error('db_unsuported_feature');
267 }
Taufan Aditya18209332012-02-09 16:07:27 +0700268
Timothy Warren02615962011-08-24 08:21:36 -0400269 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Select the database
276 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400277 * @return resource
278 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400279 protected function db_select()
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 {
281 // Not needed for PDO
282 return TRUE;
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200288 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400289 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400290 * @return string
291 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200292 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400293 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200294 return isset($this->data_cache['version'])
295 ? $this->data_cache['version']
296 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Execute the query
303 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400304 * @param string an SQL query
Timothy Warren51a48882011-09-14 13:47:06 -0400305 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400306 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400307 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400308 {
309 $sql = $this->_prep_query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700310
Timothy Warren51a48882011-09-14 13:47:06 -0400311 $result_id = $this->conn_id->query($sql);
312
Timothy Warrend6691532011-10-07 10:03:01 -0400313 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400314 {
315 $this->affect_rows = $result_id->rowCount();
316 }
317 else
318 {
319 $this->affect_rows = 0;
320 }
Timothy Warren51a48882011-09-14 13:47:06 -0400321
322 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Prep the query
329 *
330 * If needed, each database adapter can prep the query string
331 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400332 * @param string an SQL query
333 * @return string
334 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400335 protected function _prep_query($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400336 {
Taufan Aditya18209332012-02-09 16:07:27 +0700337 if ($this->pdodriver === 'pgsql')
338 {
339 // Change the backtick(s) for Postgre
340 $sql = str_replace('`', '"', $sql);
341 }
342 elseif ($this->pdodriver === 'sqlite')
343 {
344 // Change the backtick(s) for SQLite
Timothy Warren8c332e72012-03-19 18:09:13 -0400345 $sql = str_replace('`', '"', $sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700346 }
347
Timothy Warren80ab8162011-08-22 18:26:12 -0400348 return $sql;
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Begin Transaction
355 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400356 * @return bool
357 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400358 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400359 {
360 if ( ! $this->trans_enabled)
361 {
362 return TRUE;
363 }
364
365 // When transactions are nested we only begin/commit/rollback the outermost ones
366 if ($this->_trans_depth > 0)
367 {
368 return TRUE;
369 }
370
371 // Reset the transaction failure flag.
372 // If the $test_mode flag is set to TRUE transactions will be rolled back
373 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400374 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400375
Timothy Warrenab347582011-08-23 12:29:29 -0400376 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Commit Transaction
383 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400384 * @return bool
385 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400386 public function trans_commit()
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->commit();
Taufan Aditya18209332012-02-09 16:07:27 +0700400
Timothy Warren80ab8162011-08-22 18:26:12 -0400401 return $ret;
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Rollback Transaction
408 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400409 * @return bool
410 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400411 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400412 {
413 if ( ! $this->trans_enabled)
414 {
415 return TRUE;
416 }
417
418 // When transactions are nested we only begin/commit/rollback the outermost ones
419 if ($this->_trans_depth > 0)
420 {
421 return TRUE;
422 }
423
Timothy Warrenab347582011-08-23 12:29:29 -0400424 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700425
Timothy Warren80ab8162011-08-22 18:26:12 -0400426 return $ret;
427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Escape String
433 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400434 * @param string
435 * @param bool whether or not the string will be used in a LIKE condition
436 * @return string
437 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400438 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400439 {
440 if (is_array($str))
441 {
442 foreach ($str as $key => $val)
443 {
444 $str[$key] = $this->escape_str($val, $like);
445 }
446
447 return $str;
448 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400449
Timothy Warren47663972011-10-05 16:44:50 -0400450 //Escape the string
451 $str = $this->conn_id->quote($str);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400452
Timothy Warren47663972011-10-05 16:44:50 -0400453 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400454 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400455 {
456 $str = substr($str, 1, -1);
457 }
458
Timothy Warren80ab8162011-08-22 18:26:12 -0400459 // escape LIKE condition wildcards
460 if ($like === TRUE)
461 {
462 $str = str_replace( array('%', '_', $this->_like_escape_chr),
Taufan Aditya18209332012-02-09 16:07:27 +0700463 array($this->_like_escape_chr.'%',
464 $this->_like_escape_chr.'_',
465 $this->_like_escape_chr.$this->_like_escape_chr),
Timothy Warren80ab8162011-08-22 18:26:12 -0400466 $str);
467 }
468
469 return $str;
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Affected Rows
476 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400477 * @return integer
478 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400479 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400480 {
Timothy Warren51a48882011-09-14 13:47:06 -0400481 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400482 }
483
484 // --------------------------------------------------------------------
485
486 /**
487 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200488 *
489 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400490 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200491 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400492 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200493 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400494 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200495 $query = $this->query('SELECT LASTVAL() AS ins_id');
496 $query = $query->row();
497 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400498 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200499
500 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * "Count All" query
507 *
508 * Generates a platform-specific query string that counts all records in
509 * the specified database
510 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400511 * @param string
512 * @return string
513 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400514 public function count_all($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400515 {
516 if ($table == '')
517 {
518 return 0;
519 }
520
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200521 $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Taufan Aditya18209332012-02-09 16:07:27 +0700522 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400523
524 if ($query->num_rows() == 0)
525 {
526 return 0;
527 }
528
529 $row = $query->row();
530 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700531
Timothy Warren80ab8162011-08-22 18:26:12 -0400532 return (int) $row->numrows;
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
538 * Show table query
539 *
540 * Generates a platform-specific query string so that the table names can be fetched
541 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400542 * @param boolean
543 * @return string
544 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400545 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400546 {
Taufan Aditya18209332012-02-09 16:07:27 +0700547 if ($this->pdodriver == 'pgsql')
548 {
Timothy Warren8c332e72012-03-19 18:09:13 -0400549 // Analog public function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700550 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
551 }
Taufan Aditya4e44b342012-02-18 22:47:36 +0700552 elseif ($this->pdodriver == 'sqlite')
553 {
Timothy Warren8c332e72012-03-19 18:09:13 -0400554 // Analog public function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700555 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
556 }
Taufan Aditya18209332012-02-09 16:07:27 +0700557 else
558 {
559 $sql = "SHOW TABLES FROM `".$this->database."`";
560 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400561
562 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
563 {
Taufan Aditya18209332012-02-09 16:07:27 +0700564 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400565 }
566
567 return $sql;
568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Show column query
574 *
575 * Generates a platform-specific query string so that the column names can be fetched
576 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400577 * @param string the table name
578 * @return string
579 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400580 public function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400581 {
Taufan Aditya18209332012-02-09 16:07:27 +0700582 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Field data query
589 *
590 * Generates a platform-specific query so that the column data can be retrieved
591 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400592 * @param string the table name
593 * @return object
594 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400595 public function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400596 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700597 if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql')
598 {
Timothy Warren8c332e72012-03-19 18:09:13 -0400599 // Analog public function for mysql and postgre
Taufan Aditya4e44b342012-02-18 22:47:36 +0700600 return 'SELECT * FROM '.$this->_from_tables($table).' LIMIT 1';
601 }
602 elseif ($this->pdodriver == 'oci')
603 {
Timothy Warren8c332e72012-03-19 18:09:13 -0400604 // Analog public function for oci
Taufan Aditya4e44b342012-02-18 22:47:36 +0700605 return 'SELECT * FROM '.$this->_from_tables($table).' WHERE ROWNUM <= 1';
606 }
607 elseif ($this->pdodriver == 'sqlite')
608 {
Timothy Warren8c332e72012-03-19 18:09:13 -0400609 // Analog public function for sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700610 return 'PRAGMA table_info('.$this->_from_tables($table).')';
611 }
612
Taufan Aditya18209332012-02-09 16:07:27 +0700613 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400614 }
615
616 // --------------------------------------------------------------------
617
618 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200619 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400620 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200621 * Returns an array containing code and message of the last
622 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400623 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200624 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400625 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200626 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400627 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200628 $error = array('code' => '00000', 'message' => '');
629 $pdo_error = $this->conn_id->errorInfo();
630
631 if (empty($pdo_error[0]))
632 {
633 return $error;
634 }
635
636 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
637 if (isset($pdo_error[2]))
638 {
639 $error['message'] = $pdo_error[2];
640 }
641
642 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400643 }
644
645 // --------------------------------------------------------------------
646
647 /**
648 * Escape the SQL Identifiers
649 *
Timothy Warren8c332e72012-03-19 18:09:13 -0400650 * This public function escapes column and table names
Timothy Warren80ab8162011-08-22 18:26:12 -0400651 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400652 * @param string
653 * @return string
654 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400655 protected function _escape_identifiers($item)
Timothy Warren80ab8162011-08-22 18:26:12 -0400656 {
657 if ($this->_escape_char == '')
658 {
659 return $item;
660 }
661
662 foreach ($this->_reserved_identifiers as $id)
663 {
664 if (strpos($item, '.'.$id) !== FALSE)
665 {
666 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
667
668 // remove duplicates if the user already included the escape
669 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
670 }
671 }
672
673 if (strpos($item, '.') !== FALSE)
674 {
Taufan Aditya18209332012-02-09 16:07:27 +0700675 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
676 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400677 }
678 else
679 {
680 $str = $this->_escape_char.$item.$this->_escape_char;
681 }
682
683 // remove duplicates if the user already included the escape
684 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
685 }
686
687 // --------------------------------------------------------------------
688
689 /**
690 * From Tables
691 *
Timothy Warren8c332e72012-03-19 18:09:13 -0400692 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren80ab8162011-08-22 18:26:12 -0400693 * about operator precedence in harmony with SQL standards
694 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400695 * @param type
696 * @return type
697 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400698 public function _from_tables($tables)
Timothy Warren80ab8162011-08-22 18:26:12 -0400699 {
700 if ( ! is_array($tables))
701 {
702 $tables = array($tables);
703 }
704
Taufan Aditya18209332012-02-09 16:07:27 +0700705 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400706 }
707
708 // --------------------------------------------------------------------
709
710 /**
711 * Insert statement
712 *
713 * Generates a platform-specific insert string from the supplied data
714 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400715 * @param string the table name
716 * @param array the insert keys
717 * @param array the insert values
718 * @return string
719 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400720 public function _insert($table, $keys, $values)
Timothy Warren80ab8162011-08-22 18:26:12 -0400721 {
Taufan Aditya18209332012-02-09 16:07:27 +0700722 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400723 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400724
725 // --------------------------------------------------------------------
726
727 /**
728 * Insert_batch statement
729 *
730 * Generates a platform-specific insert string from the supplied data
731 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400732 * @param string the table name
733 * @param array the insert keys
734 * @param array the insert values
735 * @return string
736 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400737 public function _insert_batch($table, $keys, $values)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400738 {
Taufan Aditya18209332012-02-09 16:07:27 +0700739 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400740 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400741
742 // --------------------------------------------------------------------
743
744 /**
745 * Update statement
746 *
747 * Generates a platform-specific update string from the supplied data
748 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400749 * @param string the table name
750 * @param array the update data
751 * @param array the where clause
752 * @param array the orderby clause
753 * @param array the limit clause
754 * @return string
755 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400756 public function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400757 {
758 foreach ($values as $key => $val)
759 {
760 $valstr[] = $key." = ".$val;
761 }
762
Taufan Aditya18209332012-02-09 16:07:27 +0700763 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
764 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400765
Taufan Aditya18209332012-02-09 16:07:27 +0700766 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
767 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400768 $sql .= $orderby.$limit;
769
770 return $sql;
771 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400772
773 // --------------------------------------------------------------------
774
775 /**
776 * Update_Batch statement
777 *
778 * Generates a platform-specific batch update string from the supplied data
779 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400780 * @param string the table name
781 * @param array the update data
782 * @param array the where clause
783 * @return string
784 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400785 public function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400786 {
Taufan Aditya18209332012-02-09 16:07:27 +0700787 $ids = array();
788 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400789
790 foreach ($values as $key => $val)
791 {
792 $ids[] = $val[$index];
793
794 foreach (array_keys($val) as $field)
795 {
796 if ($field != $index)
797 {
798 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
799 }
800 }
801 }
802
Taufan Aditya18209332012-02-09 16:07:27 +0700803 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400804 $cases = '';
805
806 foreach ($final as $k => $v)
807 {
808 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700809
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400810 foreach ($v as $row)
811 {
812 $cases .= $row."\n";
813 }
814
815 $cases .= 'ELSE '.$k.' END, ';
816 }
817
818 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400819 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
820
821 return $sql;
822 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400823
824
825 // --------------------------------------------------------------------
826
827 /**
828 * Truncate statement
829 *
830 * Generates a platform-specific truncate string from the supplied data
831 * If the database does not support the truncate() command
Timothy Warren8c332e72012-03-19 18:09:13 -0400832 * This public function maps to "DELETE FROM table"
Timothy Warren80ab8162011-08-22 18:26:12 -0400833 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400834 * @param string the table name
835 * @return string
836 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400837 public function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400838 {
839 return $this->_delete($table);
840 }
841
842 // --------------------------------------------------------------------
843
844 /**
845 * Delete statement
846 *
847 * Generates a platform-specific delete string from the supplied data
848 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400849 * @param string the table name
850 * @param array the where clause
851 * @param string the limit clause
852 * @return string
853 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400854 public function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400855 {
856 $conditions = '';
857
858 if (count($where) > 0 OR count($like) > 0)
859 {
Taufan Aditya18209332012-02-09 16:07:27 +0700860 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400861 $conditions .= implode("\n", $this->ar_where);
862
863 if (count($where) > 0 && count($like) > 0)
864 {
865 $conditions .= " AND ";
866 }
Taufan Aditya18209332012-02-09 16:07:27 +0700867
Timothy Warren80ab8162011-08-22 18:26:12 -0400868 $conditions .= implode("\n", $like);
869 }
870
871 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
872
Taufan Aditya18209332012-02-09 16:07:27 +0700873 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400874 }
875
876 // --------------------------------------------------------------------
877
878 /**
879 * Limit string
880 *
881 * Generates a platform-specific LIMIT clause
882 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400883 * @param string the sql query string
884 * @param integer the number of rows to limit the query to
885 * @param integer the offset value
886 * @return string
887 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400888 public function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400889 {
Taufan Aditya18209332012-02-09 16:07:27 +0700890 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400891 {
Taufan Aditya18209332012-02-09 16:07:27 +0700892 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400893
Taufan Aditya18209332012-02-09 16:07:27 +0700894 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400895 }
896 else
897 {
Taufan Aditya18209332012-02-09 16:07:27 +0700898 $sql .= 'LIMIT '.$limit;
899 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400900
901 return $sql;
902 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400903 }
904
905 // --------------------------------------------------------------------
906
907 /**
908 * Close DB Connection
909 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400910 * @param resource
911 * @return void
912 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400913 public function _close($conn_id)
Timothy Warren80ab8162011-08-22 18:26:12 -0400914 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400915 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400916 }
917
Timothy Warren80ab8162011-08-22 18:26:12 -0400918}
919
Timothy Warren80ab8162011-08-22 18:26:12 -0400920/* End of file pdo_driver.php */
Timothy Warren8c332e72012-03-19 18:09:13 -0400921/* Location: ./system/database/drivers/pdo/pdo_driver.php */