blob: 923f0e1251062743a53024ac6f860850a384b77b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -040028
Timothy Warren80ab8162011-08-22 18:26:12 -040029/**
Timothy Warren02615962011-08-24 08:21:36 -040030 * PDO Database Adapter Class
Timothy Warren80ab8162011-08-22 18:26:12 -040031 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Timothy Warren80ab8162011-08-22 18:26:12 -040034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Timothy Warrend1a5ba22011-10-21 04:45:28 -040039 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_pdo_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020049 public $dbdriver = 'pdo';
Timothy Warren80ab8162011-08-22 18:26:12 -040050
Andrey Andreeva24e52e2012-11-02 03:54:12 +020051 /**
52 * Transaction enabled flag
53 *
54 * @var bool
55 */
Andrey Andreev44107772012-06-25 18:38:34 +030056 public $trans_enabled = FALSE;
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030057
Andrey Andreeva24e52e2012-11-02 03:54:12 +020058 /**
59 * PDO Options
60 *
61 * @var array
62 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020063 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040064
Andrey Andreeva24e52e2012-11-02 03:54:12 +020065 // --------------------------------------------------------------------
66
Andrey Andreev50293052012-06-25 17:48:49 +030067 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020068 * Class constructor
Andrey Andreev50293052012-06-25 17:48:49 +030069 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020070 * Validates the DSN string and/or detects the subdriver.
Andrey Andreev50293052012-06-25 17:48:49 +030071 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030072 * @param array $params
Andrey Andreev50293052012-06-25 17:48:49 +030073 * @return void
74 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020075 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040076 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040077 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070078
Alex Bilbie48a2baf2012-06-02 11:09:54 +010079 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070080 {
81 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070082 // This is for general PDO users, who tend to have a full DSN string.
Andrey Andreev7151e802012-06-25 00:47:41 +030083 $this->subdriver = $match[1];
Andrey Andreev50293052012-06-25 17:48:49 +030084 return;
Taufan Aditya18209332012-02-09 16:07:27 +070085 }
Andrey Andreev50293052012-06-25 17:48:49 +030086 // Legacy support for DSN specified in the hostname field
87 elseif (preg_match('/([^;]+):/', $this->hostname, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070088 {
Andrey Andreev50293052012-06-25 17:48:49 +030089 $this->dsn = $this->hostname;
90 $this->hostname = NULL;
91 $this->subdriver = $match[1];
92 return;
Taufan Aditya18209332012-02-09 16:07:27 +070093 }
Andrey Andreev50293052012-06-25 17:48:49 +030094 elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
Andrey Andreev7151e802012-06-25 00:47:41 +030095 {
96 $this->subdriver = 'dblib';
97 }
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030098 elseif ($this->subdriver === '4D')
99 {
100 $this->subdriver = '4d';
101 }
Andrey Andreev50293052012-06-25 17:48:49 +0300102 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 -0400103 {
Andrey Andreev50293052012-06-25 17:48:49 +0300104 log_message('error', 'PDO: Invalid or non-existent subdriver');
Timothy Warren80ab8162011-08-22 18:26:12 -0400105
Andrey Andreev50293052012-06-25 17:48:49 +0300106 if ($this->db_debug)
Timothy Warren25dc7552012-02-13 14:12:35 -0500107 {
Andrey Andreev50293052012-06-25 17:48:49 +0300108 show_error('Invalid or non-existent PDO subdriver');
Taufan Aditya18209332012-02-09 16:07:27 +0700109 }
Andrey Andreevd42cc462012-06-24 23:52:40 +0300110 }
Andrey Andreev50293052012-06-25 17:48:49 +0300111
112 $this->dsn = NULL;
Taufan Aditya18209332012-02-09 16:07:27 +0700113 }
114
Andrey Andreev2387ed32012-04-07 00:11:14 +0300115 // --------------------------------------------------------------------
116
Taufan Aditya18209332012-02-09 16:07:27 +0700117 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200118 * Database connection
Timothy Warren80ab8162011-08-22 18:26:12 -0400119 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200120 * @param bool $persistent
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200121 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700122 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300123 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700124 {
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300125 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300126
Taufan Aditya18209332012-02-09 16:07:27 +0700127 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200128 try
Taufan Aditya18209332012-02-09 16:07:27 +0700129 {
Andrey Andreevf00b9f02012-07-02 14:40:49 +0300130 return @new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200131 }
132 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700133 {
134 if ($this->db_debug && empty($this->failover))
135 {
136 $this->display_error($e->getMessage(), '', TRUE);
137 }
138
139 return FALSE;
140 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400141 }
142
143 // --------------------------------------------------------------------
144
145 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300146 * Persistent database connection
147 *
148 * @return object
149 */
150 public function db_pconnect()
151 {
152 return $this->db_connect(TRUE);
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200158 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400159 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400160 * @return string
161 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200162 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400163 {
Andrey Andreev9e3a83a2012-07-05 21:57:41 +0300164 if (isset($this->data_cache['version']))
165 {
166 return $this->data_cache['version'];
167 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200168 elseif ( ! $this->conn_id)
169 {
170 $this->initialize();
171 }
Andrey Andreev9e3a83a2012-07-05 21:57:41 +0300172
173 // Not all subdrivers support the getAttribute() method
174 try
175 {
176 return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
177 }
178 catch (PDOException $e)
179 {
180 return parent::version();
181 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Execute the query
188 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200189 * @param string $sql SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200190 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200192 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400193 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300194 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400195 }
196
197 // --------------------------------------------------------------------
198
199 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400200 * Begin Transaction
201 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200202 * @param bool $test_mode
Timothy Warren80ab8162011-08-22 18:26:12 -0400203 * @return bool
204 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200205 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400206 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400207 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300208 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400209 {
210 return TRUE;
211 }
212
213 // Reset the transaction failure flag.
214 // If the $test_mode flag is set to TRUE transactions will be rolled back
215 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300216 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400217
Timothy Warrenab347582011-08-23 12:29:29 -0400218 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400219 }
220
221 // --------------------------------------------------------------------
222
223 /**
224 * Commit Transaction
225 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400226 * @return bool
227 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200228 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400229 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400230 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300231 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400232 {
233 return TRUE;
234 }
235
Andrey Andreev80144bf2012-04-06 22:19:26 +0300236 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Rollback Transaction
243 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400244 * @return bool
245 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200246 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400247 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400248 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300249 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 {
251 return TRUE;
252 }
253
Andrey Andreev80144bf2012-04-06 22:19:26 +0300254 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Escape String
261 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200262 * @param string $str
263 * @param bool $like Whether or not the string will be used in a LIKE condition
Timothy Warren80ab8162011-08-22 18:26:12 -0400264 * @return string
265 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200266 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400267 {
268 if (is_array($str))
269 {
270 foreach ($str as $key => $val)
271 {
272 $str[$key] = $this->escape_str($val, $like);
273 }
274
275 return $str;
276 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200277
Andrey Andreev2387ed32012-04-07 00:11:14 +0300278 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400279 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200280
Andrey Andreev2387ed32012-04-07 00:11:14 +0300281 // If there are duplicated quotes, trim them away
Andrey Andreev263d2eb2012-06-25 18:17:14 +0300282 if ($str[0] === "'")
Timothy Warrenec193322011-10-07 09:53:35 -0400283 {
284 $str = substr($str, 1, -1);
285 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200286
Timothy Warren80ab8162011-08-22 18:26:12 -0400287 // escape LIKE condition wildcards
288 if ($like === TRUE)
289 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200290 return str_replace(array($this->_like_escape_chr, '%', '_'),
291 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
292 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400293 }
294
295 return $str;
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Affected Rows
302 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200303 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400304 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200305 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400306 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300307 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200314 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200315 * @param string $name
Andrey Andreeva39d6992012-03-01 19:11:39 +0200316 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400317 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200318 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400319 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200320 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400321 }
322
323 // --------------------------------------------------------------------
324
325 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400326 * Field data query
327 *
328 * Generates a platform-specific query so that the column data can be retrieved
329 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200330 * @param string $table
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200331 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400332 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200333 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400334 {
Andrey Andreev50293052012-06-25 17:48:49 +0300335 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400336 }
337
338 // --------------------------------------------------------------------
339
340 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200341 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400342 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200343 * Returns an array containing code and message of the last
344 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400345 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200346 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400347 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200348 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400349 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200350 $error = array('code' => '00000', 'message' => '');
351 $pdo_error = $this->conn_id->errorInfo();
352
353 if (empty($pdo_error[0]))
354 {
355 return $error;
356 }
357
358 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
359 if (isset($pdo_error[2]))
360 {
361 $error['message'] = $pdo_error[2];
362 }
363
364 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400365 }
366
367 // --------------------------------------------------------------------
368
369 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400370 * Update_Batch statement
371 *
372 * Generates a platform-specific batch update string from the supplied data
373 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200374 * @param string $table Table name
375 * @param array $values Update data
376 * @param string $index WHERE key
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400377 * @return string
378 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300379 protected function _update_batch($table, $values, $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400380 {
Andrey Andreevdf8894f2012-06-25 16:18:50 +0300381 $ids = array();
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400382 foreach ($values as $key => $val)
383 {
384 $ids[] = $val[$index];
385
386 foreach (array_keys($val) as $field)
387 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100388 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400389 {
390 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
391 }
392 }
393 }
394
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400395 $cases = '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400396 foreach ($final as $k => $v)
397 {
398 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700399
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400400 foreach ($v as $row)
401 {
402 $cases .= $row."\n";
403 }
404
405 $cases .= 'ELSE '.$k.' END, ';
406 }
407
Andrey Andreevb0478652012-07-18 15:34:46 +0300408 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400409
Andrey Andreevd40459d2012-07-18 16:46:39 +0300410 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400411 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400412
Andrey Andreeve61b19f2012-06-25 18:11:39 +0300413 // --------------------------------------------------------------------
414
415 /**
416 * Truncate statement
417 *
418 * Generates a platform-specific truncate string from the supplied data
419 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200420 * If the database does not support the TRUNCATE statement,
Andrey Andreeve61b19f2012-06-25 18:11:39 +0300421 * then this method maps to 'DELETE FROM table'
422 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200423 * @param string $table
Andrey Andreeve61b19f2012-06-25 18:11:39 +0300424 * @return string
425 */
426 protected function _truncate($table)
427 {
428 return 'TRUNCATE TABLE '.$table;
429 }
430
Timothy Warren80ab8162011-08-22 18:26:12 -0400431}
432
Timothy Warren80ab8162011-08-22 18:26:12 -0400433/* End of file pdo_driver.php */
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200434/* Location: ./system/database/drivers/pdo/pdo_driver.php */