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 | |
| 45 | // the character used to excape - not necessary for PDO |
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 | |
| 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | e9f2095 | 2012-04-06 19:30:41 +0300 | [diff] [blame] | 49 | protected $_like_escape_str = " ESCAPE '%s' "; |
| 50 | protected $_like_escape_chr = '!'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 55 | * used for the count_all() and count_all_results() functions. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 56 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | protected $_random_keyword; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 59 | |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame^] | 60 | protected $trans_enabled = FALSE; |
| 61 | |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 62 | // need to track the PDO options |
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 | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 65 | public function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 66 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 67 | parent::__construct($params); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 68 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 69 | if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 70 | { |
| 71 | // 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] | 72 | // 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] | 73 | $this->subdriver = $match[1]; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 74 | } |
| 75 | else |
| 76 | { |
| 77 | // Try to build a complete DSN string from params |
| 78 | $this->_connect_string($params); |
| 79 | } |
| 80 | |
Andrey Andreev | 7151e80 | 2012-06-25 00:47:41 +0300 | [diff] [blame] | 81 | if (in_array($this->subdriver, array('mssql', 'sybase'), TRUE)) |
| 82 | { |
| 83 | $this->subdriver = 'dblib'; |
| 84 | } |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame^] | 85 | elseif ($this->subdriver === '4D') |
| 86 | { |
| 87 | $this->subdriver = '4d'; |
| 88 | } |
Andrey Andreev | 7151e80 | 2012-06-25 00:47:41 +0300 | [diff] [blame] | 89 | |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 90 | // clause and character used for LIKE escape sequences |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 91 | // this one depends on the driver being used |
Andrey Andreev | 1732b1b | 2012-06-24 02:42:38 +0300 | [diff] [blame] | 92 | if ($this->subdriver === 'odbc') |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 93 | { |
Andrey Andreev | 5663b1e | 2012-06-24 00:28:42 +0300 | [diff] [blame] | 94 | $this->_escape_char = ''; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 95 | $this->_like_escape_str = " {escape '%s'} "; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 96 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 100 | * Connection String |
| 101 | * |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 102 | * @param array |
| 103 | * @return void |
| 104 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 105 | protected function _connect_string($params) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 106 | { |
| 107 | if (strpos($this->hostname, ':')) |
| 108 | { |
| 109 | // hostname generally would have this prototype |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 110 | // $db['hostname'] = 'subdriver:host(/Server(/DSN))=hostname(/DSN);'; |
| 111 | // We need to get the prefix (subdriver used by PDO). |
Timothy Warren | 928d83c | 2012-02-13 14:07:57 -0500 | [diff] [blame] | 112 | $dsnarray = explode(':', $this->hostname); |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 113 | $this->subdriver = $dsnarray[0]; |
Taufan Aditya | 5dcdbc3 | 2012-02-13 21:15:56 +0700 | [diff] [blame] | 114 | |
| 115 | // End dsn with a semicolon for extra backward compability |
| 116 | // if database property was not empty. |
Timothy Warren | f452469 | 2012-02-13 14:13:28 -0500 | [diff] [blame] | 117 | if ( ! empty($this->database)) |
Timothy Warren | 25dc755 | 2012-02-13 14:12:35 -0500 | [diff] [blame] | 118 | { |
| 119 | $this->dsn .= rtrim($this->hostname, ';').';'; |
| 120 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 121 | } |
| 122 | else |
| 123 | { |
| 124 | // Invalid DSN, display an error |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 125 | if ( ! array_key_exists('subdriver', $params)) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 126 | { |
| 127 | show_error('Invalid DB Connection String for PDO'); |
| 128 | } |
| 129 | |
| 130 | // Assuming that the following DSN string format is used: |
Andrey Andreev | fbba54e | 2012-06-23 20:26:31 +0300 | [diff] [blame] | 131 | // $dsn = 'pdo://username:password@hostname:port/database?subdriver=pgsql'; |
| 132 | $this->dsn = $this->subdriver.':'; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 133 | |
| 134 | // Add hostname to the DSN for databases that need it |
Andrey Andreev | 85d1bd8 | 2012-06-25 01:46:32 +0300 | [diff] [blame] | 135 | if ( ! empty($this->hostname) && strpos($this->hostname, ':') === FALSE && $this->subdriver === 'informix') |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 136 | { |
| 137 | $this->dsn .= 'host='.$this->hostname.';'; |
| 138 | } |
| 139 | |
| 140 | // Add a port to the DSN for databases that can use it |
Andrey Andreev | 85d1bd8 | 2012-06-25 01:46:32 +0300 | [diff] [blame] | 141 | if ( ! empty($this->port) && in_array($this->subdriver, array('informix', 'ibm'), TRUE)) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 142 | { |
Andrey Andreev | 7151e80 | 2012-06-25 00:47:41 +0300 | [diff] [blame] | 143 | $this->dsn .= 'port='.$this->port.';'; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | // Add the database name to the DSN, if needed |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame^] | 148 | if (stripos($this->dsn, 'database') === FALSE && $this->subdriver === 'ibm') |
Andrey Andreev | d42cc46 | 2012-06-24 23:52:40 +0300 | [diff] [blame] | 149 | { |
| 150 | if (stripos($this->dsn, 'dsn') === FALSE) |
| 151 | { |
| 152 | $this->dsn .= 'database='.$this->database.';'; |
| 153 | } |
| 154 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 155 | } |
| 156 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 157 | // -------------------------------------------------------------------- |
| 158 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 159 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 160 | * Non-persistent database connection |
| 161 | * |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 162 | * @param bool |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 163 | * @return object |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 164 | */ |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 165 | public function db_connect($persistent = FALSE) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 166 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 167 | $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT; |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 168 | $this->options[PDO::ATTR_PERSISTENT] = $persistent; |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 169 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 170 | // Connecting... |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 171 | try |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 172 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 173 | return new PDO($this->dsn, $this->username, $this->password, $this->options); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 174 | } |
| 175 | catch (PDOException $e) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 176 | { |
| 177 | if ($this->db_debug && empty($this->failover)) |
| 178 | { |
| 179 | $this->display_error($e->getMessage(), '', TRUE); |
| 180 | } |
| 181 | |
| 182 | return FALSE; |
| 183 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 189 | * Persistent database connection |
| 190 | * |
| 191 | * @return object |
| 192 | */ |
| 193 | public function db_pconnect() |
| 194 | { |
| 195 | return $this->db_connect(TRUE); |
| 196 | } |
| 197 | |
| 198 | // -------------------------------------------------------------------- |
| 199 | |
| 200 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 201 | * Database version number |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 202 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 203 | * @return string |
| 204 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 205 | public function version() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 206 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 207 | return isset($this->data_cache['version']) |
| 208 | ? $this->data_cache['version'] |
| 209 | : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // -------------------------------------------------------------------- |
| 213 | |
| 214 | /** |
| 215 | * Execute the query |
| 216 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 217 | * @param string an SQL query |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 218 | * @return mixed |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 219 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 220 | protected function _execute($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 221 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 222 | return $this->conn_id->query($sql); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // -------------------------------------------------------------------- |
| 226 | |
| 227 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 228 | * Begin Transaction |
| 229 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 230 | * @return bool |
| 231 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 232 | public function trans_begin($test_mode = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 233 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 234 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 235 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 236 | { |
| 237 | return TRUE; |
| 238 | } |
| 239 | |
| 240 | // Reset the transaction failure flag. |
| 241 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 242 | // even if the queries produce a successful result. |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 243 | $this->_trans_failure = ($test_mode === TRUE); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 244 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 245 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // -------------------------------------------------------------------- |
| 249 | |
| 250 | /** |
| 251 | * Commit Transaction |
| 252 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 253 | * @return bool |
| 254 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 255 | public function trans_commit() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 256 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 257 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 258 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 259 | { |
| 260 | return TRUE; |
| 261 | } |
| 262 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 263 | return $this->conn_id->commit(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // -------------------------------------------------------------------- |
| 267 | |
| 268 | /** |
| 269 | * Rollback Transaction |
| 270 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 271 | * @return bool |
| 272 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 273 | public function trans_rollback() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 274 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 275 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 276 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 277 | { |
| 278 | return TRUE; |
| 279 | } |
| 280 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 281 | return $this->conn_id->rollBack(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // -------------------------------------------------------------------- |
| 285 | |
| 286 | /** |
| 287 | * Escape String |
| 288 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 289 | * @param string |
| 290 | * @param bool whether or not the string will be used in a LIKE condition |
| 291 | * @return string |
| 292 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 293 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 294 | { |
| 295 | if (is_array($str)) |
| 296 | { |
| 297 | foreach ($str as $key => $val) |
| 298 | { |
| 299 | $str[$key] = $this->escape_str($val, $like); |
| 300 | } |
| 301 | |
| 302 | return $str; |
| 303 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 304 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 305 | // Escape the string |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 306 | $str = $this->conn_id->quote($str); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 307 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 308 | // If there are duplicated quotes, trim them away |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 309 | if (strpos($str, "'") === 0) |
Timothy Warren | ec19332 | 2011-10-07 09:53:35 -0400 | [diff] [blame] | 310 | { |
| 311 | $str = substr($str, 1, -1); |
| 312 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 313 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 314 | // escape LIKE condition wildcards |
| 315 | if ($like === TRUE) |
| 316 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 317 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 318 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 319 | $str); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return $str; |
| 323 | } |
| 324 | |
| 325 | // -------------------------------------------------------------------- |
| 326 | |
| 327 | /** |
| 328 | * Affected Rows |
| 329 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 330 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 331 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 332 | public function affected_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 333 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 334 | return is_object($this->result_id) ? $this->result_id->rowCount() : 0; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // -------------------------------------------------------------------- |
| 338 | |
| 339 | /** |
| 340 | * Insert ID |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 341 | * |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 342 | * @param string |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 343 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 344 | */ |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 345 | public function insert_id($name = NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 346 | { |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 347 | return $this->conn_id->lastInsertId($name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // -------------------------------------------------------------------- |
| 351 | |
| 352 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 353 | * Show table query |
| 354 | * |
| 355 | * Generates a platform-specific query string so that the table names can be fetched |
| 356 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 357 | * @param bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 358 | * @return string |
| 359 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 360 | protected function _list_tables($prefix_limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 361 | { |
Andrey Andreev | 2710566 | 2012-06-24 22:44:00 +0300 | [diff] [blame] | 362 | $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 363 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 364 | if ($prefix_limit !== FALSE AND $this->dbprefix !== '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 365 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 366 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return $sql; |
| 370 | } |
| 371 | |
| 372 | // -------------------------------------------------------------------- |
| 373 | |
| 374 | /** |
| 375 | * Show column query |
| 376 | * |
| 377 | * Generates a platform-specific query string so that the column names can be fetched |
| 378 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 379 | * @param string the table name |
| 380 | * @return string |
| 381 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 382 | protected function _list_columns($table = '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 383 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 384 | return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // -------------------------------------------------------------------- |
| 388 | |
| 389 | /** |
| 390 | * Field data query |
| 391 | * |
| 392 | * Generates a platform-specific query so that the column data can be retrieved |
| 393 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 394 | * @param string the table name |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 395 | * @return string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 396 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 397 | protected function _field_data($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 398 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 399 | return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // -------------------------------------------------------------------- |
| 403 | |
| 404 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 405 | * Error |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 406 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 407 | * Returns an array containing code and message of the last |
| 408 | * database error that has occured. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 409 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 410 | * @return array |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 411 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 412 | public function error() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 413 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 414 | $error = array('code' => '00000', 'message' => ''); |
| 415 | $pdo_error = $this->conn_id->errorInfo(); |
| 416 | |
| 417 | if (empty($pdo_error[0])) |
| 418 | { |
| 419 | return $error; |
| 420 | } |
| 421 | |
| 422 | $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0]; |
| 423 | if (isset($pdo_error[2])) |
| 424 | { |
| 425 | $error['message'] = $pdo_error[2]; |
| 426 | } |
| 427 | |
| 428 | return $error; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // -------------------------------------------------------------------- |
| 432 | |
| 433 | /** |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 434 | * Update_Batch statement |
| 435 | * |
| 436 | * Generates a platform-specific batch update string from the supplied data |
| 437 | * |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 438 | * @param string the table name |
| 439 | * @param array the update data |
| 440 | * @param array the where clause |
| 441 | * @return string |
| 442 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 443 | protected function _update_batch($table, $values, $index, $where = NULL) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 444 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 445 | $ids = array(); |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 446 | $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 447 | |
| 448 | foreach ($values as $key => $val) |
| 449 | { |
| 450 | $ids[] = $val[$index]; |
| 451 | |
| 452 | foreach (array_keys($val) as $field) |
| 453 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 454 | if ($field !== $index) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 455 | { |
| 456 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Andrey Andreev | e9f2095 | 2012-04-06 19:30:41 +0300 | [diff] [blame] | 461 | $sql = 'UPDATE '.$table.' SET '; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 462 | $cases = ''; |
| 463 | |
| 464 | foreach ($final as $k => $v) |
| 465 | { |
| 466 | $cases .= $k.' = CASE '."\n"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 467 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 468 | foreach ($v as $row) |
| 469 | { |
| 470 | $cases .= $row."\n"; |
| 471 | } |
| 472 | |
| 473 | $cases .= 'ELSE '.$k.' END, '; |
| 474 | } |
| 475 | |
| 476 | $sql .= substr($cases, 0, -2); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 477 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 478 | |
| 479 | return $sql; |
| 480 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 481 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 482 | // -------------------------------------------------------------------- |
| 483 | |
| 484 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 485 | * Limit string |
| 486 | * |
| 487 | * Generates a platform-specific LIMIT clause |
| 488 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 489 | * @param string the sql query string |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 490 | * @param int the number of rows to limit the query to |
| 491 | * @param int the offset value |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 492 | * @return string |
| 493 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 494 | protected function _limit($sql, $limit, $offset) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 495 | { |
Andrey Andreev | 2710566 | 2012-06-24 22:44:00 +0300 | [diff] [blame] | 496 | $offset = ($offset == 0) ? '' : $offset.', '; |
| 497 | return $sql.'LIMIT '.$offset.$limit; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 502 | /* End of file pdo_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 503 | /* Location: ./system/database/drivers/pdo/pdo_driver.php */ |