blob: a54c1b9a3aa1557618d61059405428e2fc8c6e0f [file] [log] [blame]
Andrey Andreevbd44d5a2012-03-20 22:59:29 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -04002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Timothy Warren80ab8162011-08-22 18:26:12 -04006 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -04007 * NOTICE OF LICENSE
Andrey Andreevbd44d5a2012-03-20 22:59:29 +02008 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -04009 * Licensed under the Open Software License version 3.0
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020010 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -040011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Timothy Warren80ab8162011-08-22 18:26:12 -040019 * @package CodeIgniter
Timothy Warrend1a5ba22011-10-21 04:45:28 -040020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Timothy Warrend1a5ba22011-10-21 04:45:28 -040022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Timothy Warren80ab8162011-08-22 18:26:12 -040023 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040024 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040025 * @filesource
26 */
27
Timothy Warren80ab8162011-08-22 18:26:12 -040028/**
Timothy Warren02615962011-08-24 08:21:36 -040029 * PDO Database Adapter Class
Timothy Warren80ab8162011-08-22 18:26:12 -040030 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Timothy Warren80ab8162011-08-22 18:26:12 -040033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Timothy Warrend1a5ba22011-10-21 04:45:28 -040038 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_pdo_driver extends CI_DB {
42
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020043 public $dbdriver = 'pdo';
Timothy Warren80ab8162011-08-22 18:26:12 -040044
Andrey Andreev50293052012-06-25 17:48:49 +030045 // The character used to escaping
Andrey Andreev5663b1e2012-06-24 00:28:42 +030046 protected $_escape_char = '"';
Taufan Aditya18209332012-02-09 16:07:27 +070047
48 // clause and character used for LIKE escape sequences
Andrey Andreeve9f20952012-04-06 19:30:41 +030049 protected $_like_escape_str = " ESCAPE '%s' ";
50 protected $_like_escape_chr = '!';
Timothy Warren80ab8162011-08-22 18:26:12 -040051
52 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
Timothy Warren667c9fe2012-03-19 19:06:34 -040055 * used for the count_all() and count_all_results() functions.
Timothy Warren80ab8162011-08-22 18:26:12 -040056 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword;
Taufan Aditya18209332012-02-09 16:07:27 +070059
Andrey Andreev44107772012-06-25 18:38:34 +030060 public $trans_enabled = FALSE;
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030061
Andrey Andreevfbba54e2012-06-23 20:26:31 +030062 // need to track the PDO options
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020063 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040064
Andrey Andreev50293052012-06-25 17:48:49 +030065 /**
66 * Constructor
67 *
68 * Validates the DSN string and/or detects the subdriver
69 *
70 * @param array
71 * @return void
72 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020073 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040074 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040075 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070076
Alex Bilbie48a2baf2012-06-02 11:09:54 +010077 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070078 {
79 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070080 // This is for general PDO users, who tend to have a full DSN string.
Andrey Andreev7151e802012-06-25 00:47:41 +030081 $this->subdriver = $match[1];
Andrey Andreev50293052012-06-25 17:48:49 +030082 return;
Taufan Aditya18209332012-02-09 16:07:27 +070083 }
Andrey Andreev50293052012-06-25 17:48:49 +030084 // Legacy support for DSN specified in the hostname field
85 elseif (preg_match('/([^;]+):/', $this->hostname, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070086 {
Andrey Andreev50293052012-06-25 17:48:49 +030087 $this->dsn = $this->hostname;
88 $this->hostname = NULL;
89 $this->subdriver = $match[1];
90 return;
Taufan Aditya18209332012-02-09 16:07:27 +070091 }
Andrey Andreev50293052012-06-25 17:48:49 +030092 elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
Andrey Andreev7151e802012-06-25 00:47:41 +030093 {
94 $this->subdriver = 'dblib';
95 }
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030096 elseif ($this->subdriver === '4D')
97 {
98 $this->subdriver = '4d';
99 }
Andrey Andreev50293052012-06-25 17:48:49 +0300100 elseif ( ! in_array($this->subdriver, array('4d', 'cubrid', 'dblib', 'firebird', 'ibm', 'informix', 'mysql', 'oci', 'odbc', 'sqlite', 'sqlsrv'), TRUE))
Timothy Warrenc7ba6642011-09-14 12:25:14 -0400101 {
Andrey Andreev50293052012-06-25 17:48:49 +0300102 log_message('error', 'PDO: Invalid or non-existent subdriver');
Timothy Warren80ab8162011-08-22 18:26:12 -0400103
Andrey Andreev50293052012-06-25 17:48:49 +0300104 if ($this->db_debug)
Timothy Warren25dc7552012-02-13 14:12:35 -0500105 {
Andrey Andreev50293052012-06-25 17:48:49 +0300106 show_error('Invalid or non-existent PDO subdriver');
Taufan Aditya18209332012-02-09 16:07:27 +0700107 }
Andrey Andreevd42cc462012-06-24 23:52:40 +0300108 }
Andrey Andreev50293052012-06-25 17:48:49 +0300109
110 $this->dsn = NULL;
Taufan Aditya18209332012-02-09 16:07:27 +0700111 }
112
Andrey Andreev2387ed32012-04-07 00:11:14 +0300113 // --------------------------------------------------------------------
114
Taufan Aditya18209332012-02-09 16:07:27 +0700115 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400116 * Non-persistent database connection
117 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300118 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200119 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700120 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300121 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700122 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300123 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300124 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300125
Taufan Aditya18209332012-02-09 16:07:27 +0700126 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200127 try
Taufan Aditya18209332012-02-09 16:07:27 +0700128 {
Andrey Andreevf00b9f02012-07-02 14:40:49 +0300129 return @new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200130 }
131 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700132 {
133 if ($this->db_debug && empty($this->failover))
134 {
135 $this->display_error($e->getMessage(), '', TRUE);
136 }
137
138 return FALSE;
139 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400140 }
141
142 // --------------------------------------------------------------------
143
144 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300145 * Persistent database connection
146 *
147 * @return object
148 */
149 public function db_pconnect()
150 {
151 return $this->db_connect(TRUE);
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200157 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400158 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400159 * @return string
160 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200161 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400162 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200163 return isset($this->data_cache['version'])
164 ? $this->data_cache['version']
165 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Execute the query
172 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400173 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200174 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400175 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200176 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400177 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300178 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400179 }
180
181 // --------------------------------------------------------------------
182
183 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400184 * Begin Transaction
185 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400186 * @return bool
187 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200188 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400189 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400190 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300191 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 {
193 return TRUE;
194 }
195
196 // Reset the transaction failure flag.
197 // If the $test_mode flag is set to TRUE transactions will be rolled back
198 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300199 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400200
Timothy Warrenab347582011-08-23 12:29:29 -0400201 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Commit Transaction
208 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400209 * @return bool
210 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200211 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400212 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400213 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300214 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400215 {
216 return TRUE;
217 }
218
Andrey Andreev80144bf2012-04-06 22:19:26 +0300219 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400220 }
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Rollback Transaction
226 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400227 * @return bool
228 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200229 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400230 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400231 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300232 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400233 {
234 return TRUE;
235 }
236
Andrey Andreev80144bf2012-04-06 22:19:26 +0300237 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Escape String
244 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400245 * @param string
246 * @param bool whether or not the string will be used in a LIKE condition
247 * @return string
248 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200249 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 {
251 if (is_array($str))
252 {
253 foreach ($str as $key => $val)
254 {
255 $str[$key] = $this->escape_str($val, $like);
256 }
257
258 return $str;
259 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200260
Andrey Andreev2387ed32012-04-07 00:11:14 +0300261 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400262 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200263
Andrey Andreev2387ed32012-04-07 00:11:14 +0300264 // If there are duplicated quotes, trim them away
Andrey Andreev263d2eb2012-06-25 18:17:14 +0300265 if ($str[0] === "'")
Timothy Warrenec193322011-10-07 09:53:35 -0400266 {
267 $str = substr($str, 1, -1);
268 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200269
Timothy Warren80ab8162011-08-22 18:26:12 -0400270 // escape LIKE condition wildcards
271 if ($like === TRUE)
272 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200273 return str_replace(array($this->_like_escape_chr, '%', '_'),
274 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
275 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 }
277
278 return $str;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Affected Rows
285 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200286 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400287 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200288 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400289 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300290 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200297 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300298 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200299 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400300 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200301 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400302 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200303 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400304 }
305
306 // --------------------------------------------------------------------
307
308 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400309 * Field data query
310 *
311 * Generates a platform-specific query so that the column data can be retrieved
312 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400313 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200314 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400315 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200316 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400317 {
Andrey Andreev50293052012-06-25 17:48:49 +0300318 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400319 }
320
321 // --------------------------------------------------------------------
322
323 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200324 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400325 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200326 * Returns an array containing code and message of the last
327 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400328 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200329 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400330 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200331 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400332 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200333 $error = array('code' => '00000', 'message' => '');
334 $pdo_error = $this->conn_id->errorInfo();
335
336 if (empty($pdo_error[0]))
337 {
338 return $error;
339 }
340
341 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
342 if (isset($pdo_error[2]))
343 {
344 $error['message'] = $pdo_error[2];
345 }
346
347 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400348 }
349
350 // --------------------------------------------------------------------
351
352 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400353 * Update_Batch statement
354 *
355 * Generates a platform-specific batch update string from the supplied data
356 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400357 * @param string the table name
358 * @param array the update data
359 * @param array the where clause
360 * @return string
361 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200362 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400363 {
Andrey Andreevdf8894f2012-06-25 16:18:50 +0300364 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100365 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400366
367 foreach ($values as $key => $val)
368 {
369 $ids[] = $val[$index];
370
371 foreach (array_keys($val) as $field)
372 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100373 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400374 {
375 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
376 }
377 }
378 }
379
Andrey Andreeve9f20952012-04-06 19:30:41 +0300380 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400381 $cases = '';
382
383 foreach ($final as $k => $v)
384 {
385 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700386
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400387 foreach ($v as $row)
388 {
389 $cases .= $row."\n";
390 }
391
392 $cases .= 'ELSE '.$k.' END, ';
393 }
394
395 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400396 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
397
398 return $sql;
399 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400400
Andrey Andreeve61b19f2012-06-25 18:11:39 +0300401 // --------------------------------------------------------------------
402
403 /**
404 * Truncate statement
405 *
406 * Generates a platform-specific truncate string from the supplied data
407 *
408 * If the database does not support the truncate() command,
409 * then this method maps to 'DELETE FROM table'
410 *
411 * @param string the table name
412 * @return string
413 */
414 protected function _truncate($table)
415 {
416 return 'TRUNCATE TABLE '.$table;
417 }
418
Timothy Warren80ab8162011-08-22 18:26:12 -0400419}
420
Timothy Warren80ab8162011-08-22 18:26:12 -0400421/* End of file pdo_driver.php */
Andrey Andreevf00b9f02012-07-02 14:40:49 +0300422/* Location: ./system/database/drivers/pdo/pdo_driver.php */