Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 6 | * |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 8 | * |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 10 | * |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 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 Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 19 | * @package CodeIgniter |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Timothy Warren | 018af7a | 2011-09-07 12:07:35 -0400 | [diff] [blame] | 24 | * @since Version 2.1.0 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 28 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 29 | /** |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 30 | * PDO Database Adapter Class |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 31 | * |
| 32 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 33 | * creates dynamically based on whether the query builder |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 34 | * class is being used or not. |
| 35 | * |
| 36 | * @package CodeIgniter |
| 37 | * @subpackage Drivers |
| 38 | * @category Database |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 39 | * @author EllisLab Dev Team |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | */ |
| 42 | class CI_DB_pdo_driver extends CI_DB { |
| 43 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Database driver |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 49 | public $dbdriver = 'pdo'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 50 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 51 | /** |
| 52 | * Transaction enabled flag |
| 53 | * |
| 54 | * @var bool |
| 55 | */ |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 56 | public $trans_enabled = FALSE; |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame] | 57 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 58 | /** |
| 59 | * PDO Options |
| 60 | * |
| 61 | * @var array |
| 62 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 63 | public $options = array(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 64 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 65 | // -------------------------------------------------------------------- |
| 66 | |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 67 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 68 | * Class constructor |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 69 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 70 | * Validates the DSN string and/or detects the subdriver. |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 71 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 72 | * @param array $params |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 73 | * @return void |
| 74 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 75 | public function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 76 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 77 | parent::__construct($params); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 78 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 79 | if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 80 | { |
| 81 | // If there is a minimum valid dsn string pattern found, we're done |
Taufan Aditya | fdd6ad0 | 2012-02-10 13:10:27 +0700 | [diff] [blame] | 82 | // This is for general PDO users, who tend to have a full DSN string. |
Andrey Andreev | 7151e80 | 2012-06-25 00:47:41 +0300 | [diff] [blame] | 83 | $this->subdriver = $match[1]; |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 84 | return; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 85 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 86 | // Legacy support for DSN specified in the hostname field |
| 87 | elseif (preg_match('/([^;]+):/', $this->hostname, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 88 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 89 | $this->dsn = $this->hostname; |
| 90 | $this->hostname = NULL; |
| 91 | $this->subdriver = $match[1]; |
| 92 | return; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 93 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 94 | elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE)) |
Andrey Andreev | 7151e80 | 2012-06-25 00:47:41 +0300 | [diff] [blame] | 95 | { |
| 96 | $this->subdriver = 'dblib'; |
| 97 | } |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame] | 98 | elseif ($this->subdriver === '4D') |
| 99 | { |
| 100 | $this->subdriver = '4d'; |
| 101 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 102 | elseif ( ! in_array($this->subdriver, array('4d', 'cubrid', 'dblib', 'firebird', 'ibm', 'informix', 'mysql', 'oci', 'odbc', 'sqlite', 'sqlsrv'), TRUE)) |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 103 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 104 | log_message('error', 'PDO: Invalid or non-existent subdriver'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 105 | |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 106 | if ($this->db_debug) |
Timothy Warren | 25dc755 | 2012-02-13 14:12:35 -0500 | [diff] [blame] | 107 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 108 | show_error('Invalid or non-existent PDO subdriver'); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 109 | } |
Andrey Andreev | d42cc46 | 2012-06-24 23:52:40 +0300 | [diff] [blame] | 110 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 111 | |
| 112 | $this->dsn = NULL; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 113 | } |
| 114 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 115 | // -------------------------------------------------------------------- |
| 116 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 117 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 118 | * Database connection |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 119 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 120 | * @param bool $persistent |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 121 | * @return object |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 122 | */ |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 123 | public function db_connect($persistent = FALSE) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 124 | { |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 125 | $this->options[PDO::ATTR_PERSISTENT] = $persistent; |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 126 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 127 | // Connecting... |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 128 | try |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 129 | { |
Andrey Andreev | f00b9f0 | 2012-07-02 14:40:49 +0300 | [diff] [blame] | 130 | return @new PDO($this->dsn, $this->username, $this->password, $this->options); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 131 | } |
| 132 | catch (PDOException $e) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 133 | { |
| 134 | if ($this->db_debug && empty($this->failover)) |
| 135 | { |
| 136 | $this->display_error($e->getMessage(), '', TRUE); |
| 137 | } |
| 138 | |
| 139 | return FALSE; |
| 140 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | /** |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 146 | * 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 Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 158 | * Database version number |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 159 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 160 | * @return string |
| 161 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 162 | public function version() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 163 | { |
Andrey Andreev | 9e3a83a | 2012-07-05 21:57:41 +0300 | [diff] [blame] | 164 | if (isset($this->data_cache['version'])) |
| 165 | { |
| 166 | return $this->data_cache['version']; |
| 167 | } |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame] | 168 | elseif ( ! $this->conn_id) |
| 169 | { |
| 170 | $this->initialize(); |
| 171 | } |
Andrey Andreev | 9e3a83a | 2012-07-05 21:57:41 +0300 | [diff] [blame] | 172 | |
| 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 Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
| 187 | * Execute the query |
| 188 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 189 | * @param string $sql SQL query |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 190 | * @return mixed |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 191 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 192 | protected function _execute($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 193 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 194 | return $this->conn_id->query($sql); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // -------------------------------------------------------------------- |
| 198 | |
| 199 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 200 | * Begin Transaction |
| 201 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 202 | * @param bool $test_mode |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 203 | * @return bool |
| 204 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 205 | public function trans_begin($test_mode = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 206 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 207 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 208 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 209 | { |
| 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 Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 216 | $this->_trans_failure = ($test_mode === TRUE); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 217 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 218 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // -------------------------------------------------------------------- |
| 222 | |
| 223 | /** |
| 224 | * Commit Transaction |
| 225 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 226 | * @return bool |
| 227 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 228 | public function trans_commit() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 229 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 230 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 231 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 232 | { |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 236 | return $this->conn_id->commit(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // -------------------------------------------------------------------- |
| 240 | |
| 241 | /** |
| 242 | * Rollback Transaction |
| 243 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 244 | * @return bool |
| 245 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 246 | public function trans_rollback() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 247 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 248 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 249 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 250 | { |
| 251 | return TRUE; |
| 252 | } |
| 253 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 254 | return $this->conn_id->rollBack(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // -------------------------------------------------------------------- |
| 258 | |
| 259 | /** |
| 260 | * Escape String |
| 261 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 262 | * @param string $str |
| 263 | * @param bool $like Whether or not the string will be used in a LIKE condition |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 264 | * @return string |
| 265 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 266 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 267 | { |
| 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 Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 277 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 278 | // Escape the string |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 279 | $str = $this->conn_id->quote($str); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 280 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 281 | // If there are duplicated quotes, trim them away |
Andrey Andreev | 263d2eb | 2012-06-25 18:17:14 +0300 | [diff] [blame] | 282 | if ($str[0] === "'") |
Timothy Warren | ec19332 | 2011-10-07 09:53:35 -0400 | [diff] [blame] | 283 | { |
| 284 | $str = substr($str, 1, -1); |
| 285 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 286 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 287 | // escape LIKE condition wildcards |
| 288 | if ($like === TRUE) |
| 289 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 290 | 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 Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return $str; |
| 296 | } |
| 297 | |
| 298 | // -------------------------------------------------------------------- |
| 299 | |
| 300 | /** |
| 301 | * Affected Rows |
| 302 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 303 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 304 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 305 | public function affected_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 306 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 307 | return is_object($this->result_id) ? $this->result_id->rowCount() : 0; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // -------------------------------------------------------------------- |
| 311 | |
| 312 | /** |
| 313 | * Insert ID |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 314 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 315 | * @param string $name |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 316 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 317 | */ |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 318 | public function insert_id($name = NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 319 | { |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 320 | return $this->conn_id->lastInsertId($name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // -------------------------------------------------------------------- |
| 324 | |
| 325 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 326 | * Field data query |
| 327 | * |
| 328 | * Generates a platform-specific query so that the column data can be retrieved |
| 329 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 330 | * @param string $table |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 331 | * @return string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 332 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 333 | protected function _field_data($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 334 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 335 | return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // -------------------------------------------------------------------- |
| 339 | |
| 340 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 341 | * Error |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 342 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 343 | * Returns an array containing code and message of the last |
| 344 | * database error that has occured. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 345 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 346 | * @return array |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 347 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 348 | public function error() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 349 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 350 | $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 Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 370 | * Update_Batch statement |
| 371 | * |
| 372 | * Generates a platform-specific batch update string from the supplied data |
| 373 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 374 | * @param string $table Table name |
| 375 | * @param array $values Update data |
| 376 | * @param string $index WHERE key |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 377 | * @return string |
| 378 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 379 | protected function _update_batch($table, $values, $index) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 380 | { |
Andrey Andreev | df8894f | 2012-06-25 16:18:50 +0300 | [diff] [blame] | 381 | $ids = array(); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 382 | foreach ($values as $key => $val) |
| 383 | { |
| 384 | $ids[] = $val[$index]; |
| 385 | |
| 386 | foreach (array_keys($val) as $field) |
| 387 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 388 | if ($field !== $index) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 389 | { |
| 390 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 395 | $cases = ''; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 396 | foreach ($final as $k => $v) |
| 397 | { |
| 398 | $cases .= $k.' = CASE '."\n"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 399 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 400 | foreach ($v as $row) |
| 401 | { |
| 402 | $cases .= $row."\n"; |
| 403 | } |
| 404 | |
| 405 | $cases .= 'ELSE '.$k.' END, '; |
| 406 | } |
| 407 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 408 | $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 409 | |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 410 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 411 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 412 | |
Andrey Andreev | e61b19f | 2012-06-25 18:11:39 +0300 | [diff] [blame] | 413 | // -------------------------------------------------------------------- |
| 414 | |
| 415 | /** |
| 416 | * Truncate statement |
| 417 | * |
| 418 | * Generates a platform-specific truncate string from the supplied data |
| 419 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 420 | * If the database does not support the TRUNCATE statement, |
Andrey Andreev | e61b19f | 2012-06-25 18:11:39 +0300 | [diff] [blame] | 421 | * then this method maps to 'DELETE FROM table' |
| 422 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 423 | * @param string $table |
Andrey Andreev | e61b19f | 2012-06-25 18:11:39 +0300 | [diff] [blame] | 424 | * @return string |
| 425 | */ |
| 426 | protected function _truncate($table) |
| 427 | { |
| 428 | return 'TRUNCATE TABLE '.$table; |
| 429 | } |
| 430 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 433 | /* End of file pdo_driver.php */ |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 434 | /* Location: ./system/database/drivers/pdo/pdo_driver.php */ |