Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
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 | */ |
| 27 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 28 | /** |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 29 | * PDO Database Adapter Class |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_pdo_driver extends CI_DB { |
| 42 | |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 43 | public $dbdriver = 'pdo'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 44 | |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 45 | // The character used to escaping |
Andrey Andreev | 5663b1e | 2012-06-24 00:28:42 +0300 | [diff] [blame] | 46 | protected $_escape_char = '"'; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 47 | |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 48 | protected $_random_keyword; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 49 | |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 50 | public $trans_enabled = FALSE; |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame] | 51 | |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 52 | // need to track the PDO options |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 53 | public $options = array(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 54 | |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 55 | /** |
| 56 | * Constructor |
| 57 | * |
| 58 | * Validates the DSN string and/or detects the subdriver |
| 59 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame^] | 60 | * @param array $params |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 61 | * @return void |
| 62 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 63 | public function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 64 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 65 | parent::__construct($params); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 66 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 67 | if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 68 | { |
| 69 | // 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] | 70 | // 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] | 71 | $this->subdriver = $match[1]; |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 72 | return; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 73 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 74 | // Legacy support for DSN specified in the hostname field |
| 75 | elseif (preg_match('/([^;]+):/', $this->hostname, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 76 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 77 | $this->dsn = $this->hostname; |
| 78 | $this->hostname = NULL; |
| 79 | $this->subdriver = $match[1]; |
| 80 | return; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 81 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 82 | elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE)) |
Andrey Andreev | 7151e80 | 2012-06-25 00:47:41 +0300 | [diff] [blame] | 83 | { |
| 84 | $this->subdriver = 'dblib'; |
| 85 | } |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame] | 86 | elseif ($this->subdriver === '4D') |
| 87 | { |
| 88 | $this->subdriver = '4d'; |
| 89 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 90 | 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] | 91 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 92 | log_message('error', 'PDO: Invalid or non-existent subdriver'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 93 | |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 94 | if ($this->db_debug) |
Timothy Warren | 25dc755 | 2012-02-13 14:12:35 -0500 | [diff] [blame] | 95 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 96 | show_error('Invalid or non-existent PDO subdriver'); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 97 | } |
Andrey Andreev | d42cc46 | 2012-06-24 23:52:40 +0300 | [diff] [blame] | 98 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 99 | |
| 100 | $this->dsn = NULL; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 101 | } |
| 102 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 103 | // -------------------------------------------------------------------- |
| 104 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 105 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 106 | * Non-persistent database connection |
| 107 | * |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 108 | * @param bool |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 109 | * @return object |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 110 | */ |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 111 | public function db_connect($persistent = FALSE) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 112 | { |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 113 | $this->options[PDO::ATTR_PERSISTENT] = $persistent; |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 114 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 115 | // Connecting... |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 116 | try |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 117 | { |
Andrey Andreev | f00b9f0 | 2012-07-02 14:40:49 +0300 | [diff] [blame] | 118 | return @new PDO($this->dsn, $this->username, $this->password, $this->options); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 119 | } |
| 120 | catch (PDOException $e) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 121 | { |
| 122 | if ($this->db_debug && empty($this->failover)) |
| 123 | { |
| 124 | $this->display_error($e->getMessage(), '', TRUE); |
| 125 | } |
| 126 | |
| 127 | return FALSE; |
| 128 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 134 | * Persistent database connection |
| 135 | * |
| 136 | * @return object |
| 137 | */ |
| 138 | public function db_pconnect() |
| 139 | { |
| 140 | return $this->db_connect(TRUE); |
| 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 146 | * Database version number |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 147 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 148 | * @return string |
| 149 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 150 | public function version() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 151 | { |
Andrey Andreev | 9e3a83a | 2012-07-05 21:57:41 +0300 | [diff] [blame] | 152 | if (isset($this->data_cache['version'])) |
| 153 | { |
| 154 | return $this->data_cache['version']; |
| 155 | } |
| 156 | |
| 157 | // Not all subdrivers support the getAttribute() method |
| 158 | try |
| 159 | { |
| 160 | return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION); |
| 161 | } |
| 162 | catch (PDOException $e) |
| 163 | { |
| 164 | return parent::version(); |
| 165 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Execute the query |
| 172 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 173 | * @param string an SQL query |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 174 | * @return mixed |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 175 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 176 | protected function _execute($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 177 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 178 | return $this->conn_id->query($sql); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 184 | * Begin Transaction |
| 185 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame^] | 186 | * @param bool $test_mode = FALSE |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 187 | * @return bool |
| 188 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 189 | public function trans_begin($test_mode = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 190 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 191 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 192 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 193 | { |
| 194 | return TRUE; |
| 195 | } |
| 196 | |
| 197 | // Reset the transaction failure flag. |
| 198 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 199 | // even if the queries produce a successful result. |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 200 | $this->_trans_failure = ($test_mode === TRUE); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 201 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 202 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // -------------------------------------------------------------------- |
| 206 | |
| 207 | /** |
| 208 | * Commit Transaction |
| 209 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 210 | * @return bool |
| 211 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 212 | public function trans_commit() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 213 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 214 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 215 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 216 | { |
| 217 | return TRUE; |
| 218 | } |
| 219 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 220 | return $this->conn_id->commit(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
| 226 | * Rollback Transaction |
| 227 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 228 | * @return bool |
| 229 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 230 | public function trans_rollback() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 231 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 232 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 233 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 234 | { |
| 235 | return TRUE; |
| 236 | } |
| 237 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 238 | return $this->conn_id->rollBack(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
| 244 | * Escape String |
| 245 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 246 | * @param string |
| 247 | * @param bool whether or not the string will be used in a LIKE condition |
| 248 | * @return string |
| 249 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 250 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 251 | { |
| 252 | if (is_array($str)) |
| 253 | { |
| 254 | foreach ($str as $key => $val) |
| 255 | { |
| 256 | $str[$key] = $this->escape_str($val, $like); |
| 257 | } |
| 258 | |
| 259 | return $str; |
| 260 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 261 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 262 | // Escape the string |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 263 | $str = $this->conn_id->quote($str); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 264 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 265 | // If there are duplicated quotes, trim them away |
Andrey Andreev | 263d2eb | 2012-06-25 18:17:14 +0300 | [diff] [blame] | 266 | if ($str[0] === "'") |
Timothy Warren | ec19332 | 2011-10-07 09:53:35 -0400 | [diff] [blame] | 267 | { |
| 268 | $str = substr($str, 1, -1); |
| 269 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 270 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 271 | // escape LIKE condition wildcards |
| 272 | if ($like === TRUE) |
| 273 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 274 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 275 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 276 | $str); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return $str; |
| 280 | } |
| 281 | |
| 282 | // -------------------------------------------------------------------- |
| 283 | |
| 284 | /** |
| 285 | * Affected Rows |
| 286 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 287 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 288 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 289 | public function affected_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 290 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 291 | return is_object($this->result_id) ? $this->result_id->rowCount() : 0; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // -------------------------------------------------------------------- |
| 295 | |
| 296 | /** |
| 297 | * Insert ID |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 298 | * |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 299 | * @param string |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 300 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 301 | */ |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 302 | public function insert_id($name = NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 303 | { |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 304 | return $this->conn_id->lastInsertId($name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // -------------------------------------------------------------------- |
| 308 | |
| 309 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 310 | * Field data query |
| 311 | * |
| 312 | * Generates a platform-specific query so that the column data can be retrieved |
| 313 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 314 | * @param string the table name |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 315 | * @return string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 316 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 317 | protected function _field_data($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 318 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 319 | return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 325 | * Error |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 326 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 327 | * Returns an array containing code and message of the last |
| 328 | * database error that has occured. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 329 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 330 | * @return array |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 331 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 332 | public function error() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 333 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 334 | $error = array('code' => '00000', 'message' => ''); |
| 335 | $pdo_error = $this->conn_id->errorInfo(); |
| 336 | |
| 337 | if (empty($pdo_error[0])) |
| 338 | { |
| 339 | return $error; |
| 340 | } |
| 341 | |
| 342 | $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0]; |
| 343 | if (isset($pdo_error[2])) |
| 344 | { |
| 345 | $error['message'] = $pdo_error[2]; |
| 346 | } |
| 347 | |
| 348 | return $error; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // -------------------------------------------------------------------- |
| 352 | |
| 353 | /** |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 354 | * Update_Batch statement |
| 355 | * |
| 356 | * Generates a platform-specific batch update string from the supplied data |
| 357 | * |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 358 | * @param string the table name |
| 359 | * @param array the update data |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 360 | * @param string the where key |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 361 | * @return string |
| 362 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 363 | protected function _update_batch($table, $values, $index) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 364 | { |
Andrey Andreev | df8894f | 2012-06-25 16:18:50 +0300 | [diff] [blame] | 365 | $ids = array(); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 366 | foreach ($values as $key => $val) |
| 367 | { |
| 368 | $ids[] = $val[$index]; |
| 369 | |
| 370 | foreach (array_keys($val) as $field) |
| 371 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 372 | if ($field !== $index) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 373 | { |
| 374 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 379 | $cases = ''; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 380 | foreach ($final as $k => $v) |
| 381 | { |
| 382 | $cases .= $k.' = CASE '."\n"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 383 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 384 | foreach ($v as $row) |
| 385 | { |
| 386 | $cases .= $row."\n"; |
| 387 | } |
| 388 | |
| 389 | $cases .= 'ELSE '.$k.' END, '; |
| 390 | } |
| 391 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 392 | $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 393 | |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 394 | 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] | 395 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 396 | |
Andrey Andreev | e61b19f | 2012-06-25 18:11:39 +0300 | [diff] [blame] | 397 | // -------------------------------------------------------------------- |
| 398 | |
| 399 | /** |
| 400 | * Truncate statement |
| 401 | * |
| 402 | * Generates a platform-specific truncate string from the supplied data |
| 403 | * |
| 404 | * If the database does not support the truncate() command, |
| 405 | * then this method maps to 'DELETE FROM table' |
| 406 | * |
| 407 | * @param string the table name |
| 408 | * @return string |
| 409 | */ |
| 410 | protected function _truncate($table) |
| 411 | { |
| 412 | return 'TRUNCATE TABLE '.$table; |
| 413 | } |
| 414 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 417 | /* End of file pdo_driver.php */ |
Andrey Andreev | f00b9f0 | 2012-07-02 14:40:49 +0300 | [diff] [blame] | 418 | /* Location: ./system/database/drivers/pdo/pdo_driver.php */ |