blob: 658a3d5a0c39dd23a2bba29a84355e6e593f07a4 [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 Warren667c9fe2012-03-19 19:06:34 -040045 var $dbdriver = 'pdo';
Timothy Warren80ab8162011-08-22 18:26:12 -040046
47 // the character used to excape - not necessary for PDO
Timothy Warren667c9fe2012-03-19 19:06:34 -040048 var $_escape_char = '';
Taufan Aditya18209332012-02-09 16:07:27 +070049
50 // clause and character used for LIKE escape sequences
Timothy Warren667c9fe2012-03-19 19:06:34 -040051 var $_like_escape_str;
52 var $_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 Warren667c9fe2012-03-19 19:06:34 -040057 * used for the count_all() and count_all_results() functions.
Timothy Warren80ab8162011-08-22 18:26:12 -040058 */
Timothy Warren667c9fe2012-03-19 19:06:34 -040059 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_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 Warren667c9fe2012-03-19 19:06:34 -040063 var $pdodriver;
64 var $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040065
Timothy Warren667c9fe2012-03-19 19:06:34 -040066 function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040067 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040068 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070069
70 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) == 2)
71 {
72 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070073 // This is for general PDO users, who tend to have a full DSN string.
Taufan Aditya18209332012-02-09 16:07:27 +070074 $this->pdodriver = end($match);
75 }
76 else
77 {
78 // Try to build a complete DSN string from params
79 $this->_connect_string($params);
80 }
81
Timothy Warrenc7ba6642011-09-14 12:25:14 -040082 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070083 // this one depends on the driver being used
84 if ($this->pdodriver == 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040085 {
86 $this->_like_escape_str = '';
87 $this->_like_escape_chr = '';
88 }
Taufan Aditya18209332012-02-09 16:07:27 +070089 elseif ($this->pdodriver == 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040090 {
91 $this->_like_escape_str = " {escape '%s'} ";
92 $this->_like_escape_chr = '!';
93 }
94 else
95 {
96 $this->_like_escape_str = " ESCAPE '%s' ";
97 $this->_like_escape_chr = '!';
98 }
99
Taufan Aditya18209332012-02-09 16:07:27 +0700100 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400101 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
102 }
103
104 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700105 * Connection String
106 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400107 * @access private
Taufan Aditya18209332012-02-09 16:07:27 +0700108 * @param array
109 * @return void
110 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400111 function _connect_string($params)
Taufan Aditya18209332012-02-09 16:07:27 +0700112 {
113 if (strpos($this->hostname, ':'))
114 {
115 // hostname generally would have this prototype
116 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
117 // We need to get the prefix (pdodriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500118 $dsnarray = explode(':', $this->hostname);
119 $this->pdodriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700120
121 // End dsn with a semicolon for extra backward compability
122 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500123 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500124 {
125 $this->dsn .= rtrim($this->hostname, ';').';';
126 }
Taufan Aditya18209332012-02-09 16:07:27 +0700127 }
128 else
129 {
130 // Invalid DSN, display an error
131 if ( ! array_key_exists('pdodriver', $params))
132 {
133 show_error('Invalid DB Connection String for PDO');
134 }
135
136 // Assuming that the following DSN string format is used:
137 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
138 $this->dsn = $this->pdodriver.':';
139
140 // Add hostname to the DSN for databases that need it
Timothy Warren928d83c2012-02-13 14:07:57 -0500141 if ( ! empty($this->hostname)
142 && strpos($this->hostname, ':') === FALSE
143 && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700144 {
145 $this->dsn .= 'host='.$this->hostname.';';
146 }
147
148 // Add a port to the DSN for databases that can use it
149 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
150 {
151 $this->dsn .= 'port='.$this->port.';';
152 }
153 }
154
155 // Add the database name to the DSN, if needed
156 if (stripos($this->dsn, 'dbname') === FALSE
157 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
158 {
159 $this->dsn .= 'dbname='.$this->database.';';
160 }
161 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
162 {
163 if (stripos($this->dsn, 'dsn') === FALSE)
164 {
165 $this->dsn .= 'database='.$this->database.';';
166 }
167 }
168 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
169 {
170 if ($this->database !== ':memory')
171 {
172 if ( ! file_exists($this->database))
173 {
174 show_error('Invalid DB Connection string for PDO SQLite');
175 }
176
177 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
178 }
179
180 $this->dsn .= $this->database;
181 }
182
183 // Add charset to the DSN, if needed
184 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
185 {
186 $this->dsn .= 'charset='.$this->char_set.';';
187 }
188 }
189
190 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 * Non-persistent database connection
192 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400193 * @access private called by the base class
Timothy Warren80ab8162011-08-22 18:26:12 -0400194 * @return resource
195 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400196 function db_connect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400197 {
Taufan Aditya18209332012-02-09 16:07:27 +0700198 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
199
200 return $this->pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Persistent database connection
207 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400208 * @access private called by the base class
Timothy Warren80ab8162011-08-22 18:26:12 -0400209 * @return resource
210 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400211 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 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400224 * @access private called by the PDO driver class
Taufan Aditya18209332012-02-09 16:07:27 +0700225 * @return resource
226 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400227 function pdo_connect()
Taufan Aditya18209332012-02-09 16:07:27 +0700228 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700229 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Taufan Aditya18209332012-02-09 16:07:27 +0700230 if ($this->pdodriver == 'mysql' && is_php('5.3.6'))
231 {
Taufan Aditya55bb97e2012-02-09 16:43:26 +0700232 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'";
Taufan Aditya18209332012-02-09 16:07:27 +0700233 }
234
235 // Connecting...
236 try
237 {
238 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
239 }
240 catch (PDOException $e)
241 {
242 if ($this->db_debug && empty($this->failover))
243 {
244 $this->display_error($e->getMessage(), '', TRUE);
245 }
246
247 return FALSE;
248 }
249
250 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Reconnect
257 *
258 * Keep / reestablish the db connection if no queries have been
259 * sent for a length of time exceeding the server's idle timeout
260 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400261 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400262 * @return void
263 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400264 function reconnect()
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 {
Timothy Warren02615962011-08-24 08:21:36 -0400266 if ($this->db->db_debug)
267 {
268 return $this->db->display_error('db_unsuported_feature');
269 }
Taufan Aditya18209332012-02-09 16:07:27 +0700270
Timothy Warren02615962011-08-24 08:21:36 -0400271 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Select the database
278 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400279 * @access private called by the base class
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 * @return resource
281 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400282 function db_select()
Timothy Warren80ab8162011-08-22 18:26:12 -0400283 {
284 // Not needed for PDO
285 return TRUE;
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200291 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400292 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400293 * @return string
294 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200295 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400296 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200297 return isset($this->data_cache['version'])
298 ? $this->data_cache['version']
299 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Execute the query
306 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400307 * @access private called by the base class
Timothy Warren80ab8162011-08-22 18:26:12 -0400308 * @param string an SQL query
Timothy Warren51a48882011-09-14 13:47:06 -0400309 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400310 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400311 function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400312 {
313 $sql = $this->_prep_query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700314
Timothy Warren51a48882011-09-14 13:47:06 -0400315 $result_id = $this->conn_id->query($sql);
316
Timothy Warrend6691532011-10-07 10:03:01 -0400317 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400318 {
319 $this->affect_rows = $result_id->rowCount();
320 }
321 else
322 {
323 $this->affect_rows = 0;
324 }
Timothy Warren51a48882011-09-14 13:47:06 -0400325
326 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Prep the query
333 *
334 * If needed, each database adapter can prep the query string
335 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400336 * @access private called by execute()
Timothy Warren80ab8162011-08-22 18:26:12 -0400337 * @param string an SQL query
338 * @return string
339 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400340 function _prep_query($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400341 {
Taufan Aditya18209332012-02-09 16:07:27 +0700342 if ($this->pdodriver === 'pgsql')
343 {
344 // Change the backtick(s) for Postgre
345 $sql = str_replace('`', '"', $sql);
346 }
347 elseif ($this->pdodriver === 'sqlite')
348 {
349 // Change the backtick(s) for SQLite
Timothy Warren667c9fe2012-03-19 19:06:34 -0400350 $sql = str_replace('`', '', $sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700351 }
352
Timothy Warren80ab8162011-08-22 18:26:12 -0400353 return $sql;
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Begin Transaction
360 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400361 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400362 * @return bool
363 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400364 function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400365 {
366 if ( ! $this->trans_enabled)
367 {
368 return TRUE;
369 }
370
371 // When transactions are nested we only begin/commit/rollback the outermost ones
372 if ($this->_trans_depth > 0)
373 {
374 return TRUE;
375 }
376
377 // Reset the transaction failure flag.
378 // If the $test_mode flag is set to TRUE transactions will be rolled back
379 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400380 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400381
Timothy Warrenab347582011-08-23 12:29:29 -0400382 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Commit Transaction
389 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400390 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400391 * @return bool
392 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400393 function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400394 {
395 if ( ! $this->trans_enabled)
396 {
397 return TRUE;
398 }
399
400 // When transactions are nested we only begin/commit/rollback the outermost ones
401 if ($this->_trans_depth > 0)
402 {
403 return TRUE;
404 }
405
Timothy Warrenab347582011-08-23 12:29:29 -0400406 $ret = $this->conn->commit();
Taufan Aditya18209332012-02-09 16:07:27 +0700407
Timothy Warren80ab8162011-08-22 18:26:12 -0400408 return $ret;
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Rollback Transaction
415 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400416 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400417 * @return bool
418 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400419 function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400420 {
421 if ( ! $this->trans_enabled)
422 {
423 return TRUE;
424 }
425
426 // When transactions are nested we only begin/commit/rollback the outermost ones
427 if ($this->_trans_depth > 0)
428 {
429 return TRUE;
430 }
431
Timothy Warrenab347582011-08-23 12:29:29 -0400432 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700433
Timothy Warren80ab8162011-08-22 18:26:12 -0400434 return $ret;
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Escape String
441 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400442 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400443 * @param string
444 * @param bool whether or not the string will be used in a LIKE condition
445 * @return string
446 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400447 function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400448 {
449 if (is_array($str))
450 {
451 foreach ($str as $key => $val)
452 {
453 $str[$key] = $this->escape_str($val, $like);
454 }
455
456 return $str;
457 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400458
Timothy Warren47663972011-10-05 16:44:50 -0400459 //Escape the string
460 $str = $this->conn_id->quote($str);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400461
Timothy Warren47663972011-10-05 16:44:50 -0400462 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400463 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400464 {
465 $str = substr($str, 1, -1);
466 }
467
Timothy Warren80ab8162011-08-22 18:26:12 -0400468 // escape LIKE condition wildcards
469 if ($like === TRUE)
470 {
471 $str = str_replace( array('%', '_', $this->_like_escape_chr),
Taufan Aditya18209332012-02-09 16:07:27 +0700472 array($this->_like_escape_chr.'%',
473 $this->_like_escape_chr.'_',
474 $this->_like_escape_chr.$this->_like_escape_chr),
Timothy Warren80ab8162011-08-22 18:26:12 -0400475 $str);
476 }
477
478 return $str;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
484 * Affected Rows
485 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400486 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400487 * @return integer
488 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400489 function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400490 {
Timothy Warren51a48882011-09-14 13:47:06 -0400491 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200498 *
499 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400500 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200501 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400502 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200503 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400504 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200505 $query = $this->query('SELECT LASTVAL() AS ins_id');
506 $query = $query->row();
507 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400508 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200509
510 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * "Count All" query
517 *
518 * Generates a platform-specific query string that counts all records in
519 * the specified database
520 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400521 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400522 * @param string
523 * @return string
524 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400525 function count_all($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400526 {
527 if ($table == '')
528 {
529 return 0;
530 }
531
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200532 $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Taufan Aditya18209332012-02-09 16:07:27 +0700533 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400534
535 if ($query->num_rows() == 0)
536 {
537 return 0;
538 }
539
540 $row = $query->row();
541 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700542
Timothy Warren80ab8162011-08-22 18:26:12 -0400543 return (int) $row->numrows;
544 }
545
546 // --------------------------------------------------------------------
547
548 /**
549 * Show table query
550 *
551 * Generates a platform-specific query string so that the table names can be fetched
552 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400553 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -0400554 * @param boolean
555 * @return string
556 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400557 function _list_tables($prefix_limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400558 {
Taufan Aditya18209332012-02-09 16:07:27 +0700559 if ($this->pdodriver == 'pgsql')
560 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400561 // Analog function to show all tables in postgre
Taufan Aditya18209332012-02-09 16:07:27 +0700562 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
563 }
Taufan Aditya4e44b342012-02-18 22:47:36 +0700564 elseif ($this->pdodriver == 'sqlite')
565 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400566 // Analog function to show all tables in sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700567 $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'";
568 }
Taufan Aditya18209332012-02-09 16:07:27 +0700569 else
570 {
571 $sql = "SHOW TABLES FROM `".$this->database."`";
572 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400573
574 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
575 {
Taufan Aditya18209332012-02-09 16:07:27 +0700576 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400577 }
578
579 return $sql;
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Show column query
586 *
587 * Generates a platform-specific query string so that the column names can be fetched
588 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400589 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400590 * @param string the table name
591 * @return string
592 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400593 function _list_columns($table = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400594 {
Taufan Aditya18209332012-02-09 16:07:27 +0700595 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Field data query
602 *
603 * Generates a platform-specific query so that the column data can be retrieved
604 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400605 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400606 * @param string the table name
607 * @return object
608 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400609 function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400610 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700611 if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql')
612 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400613 // Analog function for mysql and postgre
Taufan Aditya4e44b342012-02-18 22:47:36 +0700614 return 'SELECT * FROM '.$this->_from_tables($table).' LIMIT 1';
615 }
616 elseif ($this->pdodriver == 'oci')
617 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400618 // Analog function for oci
Taufan Aditya4e44b342012-02-18 22:47:36 +0700619 return 'SELECT * FROM '.$this->_from_tables($table).' WHERE ROWNUM <= 1';
620 }
621 elseif ($this->pdodriver == 'sqlite')
622 {
Timothy Warren667c9fe2012-03-19 19:06:34 -0400623 // Analog function for sqlite
Taufan Aditya4e44b342012-02-18 22:47:36 +0700624 return 'PRAGMA table_info('.$this->_from_tables($table).')';
625 }
626
Taufan Aditya18209332012-02-09 16:07:27 +0700627 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400628 }
629
630 // --------------------------------------------------------------------
631
632 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200633 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400634 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200635 * Returns an array containing code and message of the last
636 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400637 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200638 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400639 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200640 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400641 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200642 $error = array('code' => '00000', 'message' => '');
643 $pdo_error = $this->conn_id->errorInfo();
644
645 if (empty($pdo_error[0]))
646 {
647 return $error;
648 }
649
650 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
651 if (isset($pdo_error[2]))
652 {
653 $error['message'] = $pdo_error[2];
654 }
655
656 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400657 }
658
659 // --------------------------------------------------------------------
660
661 /**
662 * Escape the SQL Identifiers
663 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400664 * This function escapes column and table names
Timothy Warren80ab8162011-08-22 18:26:12 -0400665 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400666 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -0400667 * @param string
668 * @return string
669 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400670 function _escape_identifiers($item)
Timothy Warren80ab8162011-08-22 18:26:12 -0400671 {
672 if ($this->_escape_char == '')
673 {
674 return $item;
675 }
676
677 foreach ($this->_reserved_identifiers as $id)
678 {
679 if (strpos($item, '.'.$id) !== FALSE)
680 {
681 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
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 if (strpos($item, '.') !== FALSE)
689 {
Taufan Aditya18209332012-02-09 16:07:27 +0700690 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
691 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400692 }
693 else
694 {
695 $str = $this->_escape_char.$item.$this->_escape_char;
696 }
697
698 // remove duplicates if the user already included the escape
699 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * From Tables
706 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400707 * This function implicitly groups FROM tables so there is no confusion
Timothy Warren80ab8162011-08-22 18:26:12 -0400708 * about operator precedence in harmony with SQL standards
709 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400710 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400711 * @param type
712 * @return type
713 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400714 function _from_tables($tables)
Timothy Warren80ab8162011-08-22 18:26:12 -0400715 {
716 if ( ! is_array($tables))
717 {
718 $tables = array($tables);
719 }
720
Taufan Aditya18209332012-02-09 16:07:27 +0700721 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400722 }
723
724 // --------------------------------------------------------------------
725
726 /**
727 * Insert statement
728 *
729 * Generates a platform-specific insert string from the supplied data
730 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400731 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400732 * @param string the table name
733 * @param array the insert keys
734 * @param array the insert values
735 * @return string
736 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400737 function _insert($table, $keys, $values)
Timothy Warren80ab8162011-08-22 18:26:12 -0400738 {
Taufan Aditya18209332012-02-09 16:07:27 +0700739 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400740 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400741
742 // --------------------------------------------------------------------
743
744 /**
745 * Insert_batch statement
746 *
747 * Generates a platform-specific insert string from the supplied data
748 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400749 * @access public
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400750 * @param string the table name
751 * @param array the insert keys
752 * @param array the insert values
753 * @return string
754 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400755 function _insert_batch($table, $keys, $values)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400756 {
Taufan Aditya18209332012-02-09 16:07:27 +0700757 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400758 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400759
760 // --------------------------------------------------------------------
761
762 /**
763 * Update statement
764 *
765 * Generates a platform-specific update string from the supplied data
766 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400767 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400768 * @param string the table name
769 * @param array the update data
770 * @param array the where clause
771 * @param array the orderby clause
772 * @param array the limit clause
773 * @return string
774 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400775 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400776 {
777 foreach ($values as $key => $val)
778 {
779 $valstr[] = $key." = ".$val;
780 }
781
Taufan Aditya18209332012-02-09 16:07:27 +0700782 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
783 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400784
Taufan Aditya18209332012-02-09 16:07:27 +0700785 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
786 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400787 $sql .= $orderby.$limit;
788
789 return $sql;
790 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400791
792 // --------------------------------------------------------------------
793
794 /**
795 * Update_Batch statement
796 *
797 * Generates a platform-specific batch update string from the supplied data
798 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400799 * @access public
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400800 * @param string the table name
801 * @param array the update data
802 * @param array the where clause
803 * @return string
804 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400805 function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400806 {
Taufan Aditya18209332012-02-09 16:07:27 +0700807 $ids = array();
808 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400809
810 foreach ($values as $key => $val)
811 {
812 $ids[] = $val[$index];
813
814 foreach (array_keys($val) as $field)
815 {
816 if ($field != $index)
817 {
818 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
819 }
820 }
821 }
822
Taufan Aditya18209332012-02-09 16:07:27 +0700823 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400824 $cases = '';
825
826 foreach ($final as $k => $v)
827 {
828 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700829
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400830 foreach ($v as $row)
831 {
832 $cases .= $row."\n";
833 }
834
835 $cases .= 'ELSE '.$k.' END, ';
836 }
837
838 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400839 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
840
841 return $sql;
842 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400843
844
845 // --------------------------------------------------------------------
846
847 /**
848 * Truncate statement
849 *
850 * Generates a platform-specific truncate string from the supplied data
851 * If the database does not support the truncate() command
Timothy Warren667c9fe2012-03-19 19:06:34 -0400852 * This function maps to "DELETE FROM table"
Timothy Warren80ab8162011-08-22 18:26:12 -0400853 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400854 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400855 * @param string the table name
856 * @return string
857 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400858 function _truncate($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400859 {
860 return $this->_delete($table);
861 }
862
863 // --------------------------------------------------------------------
864
865 /**
866 * Delete statement
867 *
868 * Generates a platform-specific delete string from the supplied data
869 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400870 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400871 * @param string the table name
872 * @param array the where clause
873 * @param string the limit clause
874 * @return string
875 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400876 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400877 {
878 $conditions = '';
879
880 if (count($where) > 0 OR count($like) > 0)
881 {
Taufan Aditya18209332012-02-09 16:07:27 +0700882 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400883 $conditions .= implode("\n", $this->ar_where);
884
885 if (count($where) > 0 && count($like) > 0)
886 {
887 $conditions .= " AND ";
888 }
Taufan Aditya18209332012-02-09 16:07:27 +0700889
Timothy Warren80ab8162011-08-22 18:26:12 -0400890 $conditions .= implode("\n", $like);
891 }
892
893 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
894
Taufan Aditya18209332012-02-09 16:07:27 +0700895 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400896 }
897
898 // --------------------------------------------------------------------
899
900 /**
901 * Limit string
902 *
903 * Generates a platform-specific LIMIT clause
904 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400905 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400906 * @param string the sql query string
907 * @param integer the number of rows to limit the query to
908 * @param integer the offset value
909 * @return string
910 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400911 function _limit($sql, $limit, $offset)
Timothy Warren80ab8162011-08-22 18:26:12 -0400912 {
Taufan Aditya18209332012-02-09 16:07:27 +0700913 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400914 {
Taufan Aditya18209332012-02-09 16:07:27 +0700915 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400916
Taufan Aditya18209332012-02-09 16:07:27 +0700917 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400918 }
919 else
920 {
Taufan Aditya18209332012-02-09 16:07:27 +0700921 $sql .= 'LIMIT '.$limit;
922 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400923
924 return $sql;
925 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400926 }
927
928 // --------------------------------------------------------------------
929
930 /**
931 * Close DB Connection
932 *
Timothy Warren667c9fe2012-03-19 19:06:34 -0400933 * @access public
Timothy Warren80ab8162011-08-22 18:26:12 -0400934 * @param resource
935 * @return void
936 */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400937 function _close($conn_id)
Timothy Warren80ab8162011-08-22 18:26:12 -0400938 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400939 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400940 }
941
Timothy Warren80ab8162011-08-22 18:26:12 -0400942}
943
Timothy Warren80ab8162011-08-22 18:26:12 -0400944/* End of file pdo_driver.php */
Timothy Warren667c9fe2012-03-19 19:06:34 -0400945/* Location: ./system/database/drivers/pdo/pdo_driver.php */