blob: 26ad3831a02a78fd72b7f3414dea8bb185ab925d [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
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020052 protected $_random_keyword;
Taufan Aditya18209332012-02-09 16:07:27 +070053
Andrey Andreev44107772012-06-25 18:38:34 +030054 public $trans_enabled = FALSE;
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030055
Andrey Andreevfbba54e2012-06-23 20:26:31 +030056 // need to track the PDO options
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020057 public $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040058
Andrey Andreev50293052012-06-25 17:48:49 +030059 /**
60 * Constructor
61 *
62 * Validates the DSN string and/or detects the subdriver
63 *
64 * @param array
65 * @return void
66 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +020067 public function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040068 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040069 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070070
Alex Bilbie48a2baf2012-06-02 11:09:54 +010071 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070072 {
73 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070074 // This is for general PDO users, who tend to have a full DSN string.
Andrey Andreev7151e802012-06-25 00:47:41 +030075 $this->subdriver = $match[1];
Andrey Andreev50293052012-06-25 17:48:49 +030076 return;
Taufan Aditya18209332012-02-09 16:07:27 +070077 }
Andrey Andreev50293052012-06-25 17:48:49 +030078 // Legacy support for DSN specified in the hostname field
79 elseif (preg_match('/([^;]+):/', $this->hostname, $match) && count($match) === 2)
Taufan Aditya18209332012-02-09 16:07:27 +070080 {
Andrey Andreev50293052012-06-25 17:48:49 +030081 $this->dsn = $this->hostname;
82 $this->hostname = NULL;
83 $this->subdriver = $match[1];
84 return;
Taufan Aditya18209332012-02-09 16:07:27 +070085 }
Andrey Andreev50293052012-06-25 17:48:49 +030086 elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
Andrey Andreev7151e802012-06-25 00:47:41 +030087 {
88 $this->subdriver = 'dblib';
89 }
Andrey Andreev6b4bffa2012-06-25 02:29:20 +030090 elseif ($this->subdriver === '4D')
91 {
92 $this->subdriver = '4d';
93 }
Andrey Andreev50293052012-06-25 17:48:49 +030094 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 -040095 {
Andrey Andreev50293052012-06-25 17:48:49 +030096 log_message('error', 'PDO: Invalid or non-existent subdriver');
Timothy Warren80ab8162011-08-22 18:26:12 -040097
Andrey Andreev50293052012-06-25 17:48:49 +030098 if ($this->db_debug)
Timothy Warren25dc7552012-02-13 14:12:35 -050099 {
Andrey Andreev50293052012-06-25 17:48:49 +0300100 show_error('Invalid or non-existent PDO subdriver');
Taufan Aditya18209332012-02-09 16:07:27 +0700101 }
Andrey Andreevd42cc462012-06-24 23:52:40 +0300102 }
Andrey Andreev50293052012-06-25 17:48:49 +0300103
104 $this->dsn = NULL;
Taufan Aditya18209332012-02-09 16:07:27 +0700105 }
106
Andrey Andreev2387ed32012-04-07 00:11:14 +0300107 // --------------------------------------------------------------------
108
Taufan Aditya18209332012-02-09 16:07:27 +0700109 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400110 * Non-persistent database connection
111 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300112 * @param bool
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200113 * @return object
Taufan Aditya18209332012-02-09 16:07:27 +0700114 */
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300115 public function db_connect($persistent = FALSE)
Taufan Aditya18209332012-02-09 16:07:27 +0700116 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300117 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300118 $this->options[PDO::ATTR_PERSISTENT] = $persistent;
Andrey Andreev2387ed32012-04-07 00:11:14 +0300119
Taufan Aditya18209332012-02-09 16:07:27 +0700120 // Connecting...
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200121 try
Taufan Aditya18209332012-02-09 16:07:27 +0700122 {
Andrey Andreevf00b9f02012-07-02 14:40:49 +0300123 return @new PDO($this->dsn, $this->username, $this->password, $this->options);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200124 }
125 catch (PDOException $e)
Taufan Aditya18209332012-02-09 16:07:27 +0700126 {
127 if ($this->db_debug && empty($this->failover))
128 {
129 $this->display_error($e->getMessage(), '', TRUE);
130 }
131
132 return FALSE;
133 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400134 }
135
136 // --------------------------------------------------------------------
137
138 /**
Andrey Andreev3b0130d2012-06-24 21:41:42 +0300139 * Persistent database connection
140 *
141 * @return object
142 */
143 public function db_pconnect()
144 {
145 return $this->db_connect(TRUE);
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200151 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400152 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400153 * @return string
154 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200155 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400156 {
Andrey Andreev9e3a83a2012-07-05 21:57:41 +0300157 if (isset($this->data_cache['version']))
158 {
159 return $this->data_cache['version'];
160 }
161
162 // Not all subdrivers support the getAttribute() method
163 try
164 {
165 return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
166 }
167 catch (PDOException $e)
168 {
169 return parent::version();
170 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Execute the query
177 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400178 * @param string an SQL query
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200179 * @return mixed
Timothy Warren80ab8162011-08-22 18:26:12 -0400180 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200181 protected function _execute($sql)
Timothy Warren80ab8162011-08-22 18:26:12 -0400182 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300183 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400184 }
185
186 // --------------------------------------------------------------------
187
188 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400189 * Begin Transaction
190 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 * @return bool
192 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200193 public function trans_begin($test_mode = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400194 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400195 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300196 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400197 {
198 return TRUE;
199 }
200
201 // Reset the transaction failure flag.
202 // If the $test_mode flag is set to TRUE transactions will be rolled back
203 // even if the queries produce a successful result.
Andrey Andreev2387ed32012-04-07 00:11:14 +0300204 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400205
Timothy Warrenab347582011-08-23 12:29:29 -0400206 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Commit Transaction
213 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400214 * @return bool
215 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200216 public function trans_commit()
Timothy Warren80ab8162011-08-22 18:26:12 -0400217 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400218 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300219 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400220 {
221 return TRUE;
222 }
223
Andrey Andreev80144bf2012-04-06 22:19:26 +0300224 return $this->conn_id->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Rollback Transaction
231 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400232 * @return bool
233 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200234 public function trans_rollback()
Timothy Warren80ab8162011-08-22 18:26:12 -0400235 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400236 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev80144bf2012-04-06 22:19:26 +0300237 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400238 {
239 return TRUE;
240 }
241
Andrey Andreev80144bf2012-04-06 22:19:26 +0300242 return $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * Escape String
249 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 * @param string
251 * @param bool whether or not the string will be used in a LIKE condition
252 * @return string
253 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200254 public function escape_str($str, $like = FALSE)
Timothy Warren80ab8162011-08-22 18:26:12 -0400255 {
256 if (is_array($str))
257 {
258 foreach ($str as $key => $val)
259 {
260 $str[$key] = $this->escape_str($val, $like);
261 }
262
263 return $str;
264 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200265
Andrey Andreev2387ed32012-04-07 00:11:14 +0300266 // Escape the string
Timothy Warren47663972011-10-05 16:44:50 -0400267 $str = $this->conn_id->quote($str);
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200268
Andrey Andreev2387ed32012-04-07 00:11:14 +0300269 // If there are duplicated quotes, trim them away
Andrey Andreev263d2eb2012-06-25 18:17:14 +0300270 if ($str[0] === "'")
Timothy Warrenec193322011-10-07 09:53:35 -0400271 {
272 $str = substr($str, 1, -1);
273 }
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200274
Timothy Warren80ab8162011-08-22 18:26:12 -0400275 // escape LIKE condition wildcards
276 if ($like === TRUE)
277 {
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200278 return str_replace(array($this->_like_escape_chr, '%', '_'),
279 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
280 $str);
Timothy Warren80ab8162011-08-22 18:26:12 -0400281 }
282
283 return $str;
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Affected Rows
290 *
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200291 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400292 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200293 public function affected_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -0400294 {
Andrey Andreev2387ed32012-04-07 00:11:14 +0300295 return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
Timothy Warren80ab8162011-08-22 18:26:12 -0400296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200302 *
Andrey Andreev2387ed32012-04-07 00:11:14 +0300303 * @param string
Andrey Andreeva39d6992012-03-01 19:11:39 +0200304 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400305 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200306 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400307 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200308 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400309 }
310
311 // --------------------------------------------------------------------
312
313 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400314 * Field data query
315 *
316 * Generates a platform-specific query so that the column data can be retrieved
317 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400318 * @param string the table name
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200319 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400320 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200321 protected function _field_data($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400322 {
Andrey Andreev50293052012-06-25 17:48:49 +0300323 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400324 }
325
326 // --------------------------------------------------------------------
327
328 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200329 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400330 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200331 * Returns an array containing code and message of the last
332 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400333 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200334 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400335 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200336 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400337 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200338 $error = array('code' => '00000', 'message' => '');
339 $pdo_error = $this->conn_id->errorInfo();
340
341 if (empty($pdo_error[0]))
342 {
343 return $error;
344 }
345
346 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
347 if (isset($pdo_error[2]))
348 {
349 $error['message'] = $pdo_error[2];
350 }
351
352 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400358 * Update_Batch statement
359 *
360 * Generates a platform-specific batch update string from the supplied data
361 *
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400362 * @param string the table name
363 * @param array the update data
364 * @param array the where clause
365 * @return string
366 */
Andrey Andreevbd44d5a2012-03-20 22:59:29 +0200367 protected function _update_batch($table, $values, $index, $where = NULL)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400368 {
Andrey Andreevdf8894f2012-06-25 16:18:50 +0300369 $ids = array();
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100370 $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400371
372 foreach ($values as $key => $val)
373 {
374 $ids[] = $val[$index];
375
376 foreach (array_keys($val) as $field)
377 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100378 if ($field !== $index)
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400379 {
380 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
381 }
382 }
383 }
384
Andrey Andreeve9f20952012-04-06 19:30:41 +0300385 $sql = 'UPDATE '.$table.' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400386 $cases = '';
387
388 foreach ($final as $k => $v)
389 {
390 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700391
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400392 foreach ($v as $row)
393 {
394 $cases .= $row."\n";
395 }
396
397 $cases .= 'ELSE '.$k.' END, ';
398 }
399
400 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400401 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
402
403 return $sql;
404 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400405
Andrey Andreeve61b19f2012-06-25 18:11:39 +0300406 // --------------------------------------------------------------------
407
408 /**
409 * Truncate statement
410 *
411 * Generates a platform-specific truncate string from the supplied data
412 *
413 * If the database does not support the truncate() command,
414 * then this method maps to 'DELETE FROM table'
415 *
416 * @param string the table name
417 * @return string
418 */
419 protected function _truncate($table)
420 {
421 return 'TRUNCATE TABLE '.$table;
422 }
423
Timothy Warren80ab8162011-08-22 18:26:12 -0400424}
425
Timothy Warren80ab8162011-08-22 18:26:12 -0400426/* End of file pdo_driver.php */
Andrey Andreevf00b9f02012-07-02 14:40:49 +0300427/* Location: ./system/database/drivers/pdo/pdo_driver.php */