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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 2.1.0 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 39 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 40 | /** |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 41 | * PDO Database Adapter Class |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 42 | * |
| 43 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 44 | * creates dynamically based on whether the query builder |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 45 | * class is being used or not. |
| 46 | * |
| 47 | * @package CodeIgniter |
| 48 | * @subpackage Drivers |
| 49 | * @category Database |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 50 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 51 | * @link https://codeigniter.com/user_guide/database/ |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 52 | */ |
| 53 | class CI_DB_pdo_driver extends CI_DB { |
| 54 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 55 | /** |
| 56 | * Database driver |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 60 | public $dbdriver = 'pdo'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 61 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 62 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 63 | * PDO Options |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 67 | public $options = array(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 68 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 69 | // -------------------------------------------------------------------- |
| 70 | |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 71 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 72 | * Class constructor |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 73 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 74 | * Validates the DSN string and/or detects the subdriver. |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 75 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 76 | * @param array $params |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 77 | * @return void |
| 78 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 79 | public function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 80 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 81 | parent::__construct($params); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 82 | |
Andrey Andreev | 4211344 | 2013-07-29 09:55:29 +0300 | [diff] [blame] | 83 | if (preg_match('/([^:]+):/', $this->dsn, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 84 | { |
| 85 | // 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] | 86 | // 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] | 87 | $this->subdriver = $match[1]; |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 88 | return; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 89 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 90 | // Legacy support for DSN specified in the hostname field |
Andrey Andreev | 4211344 | 2013-07-29 09:55:29 +0300 | [diff] [blame] | 91 | elseif (preg_match('/([^:]+):/', $this->hostname, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 92 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 93 | $this->dsn = $this->hostname; |
| 94 | $this->hostname = NULL; |
| 95 | $this->subdriver = $match[1]; |
| 96 | return; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 97 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 98 | elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE)) |
Andrey Andreev | 7151e80 | 2012-06-25 00:47:41 +0300 | [diff] [blame] | 99 | { |
| 100 | $this->subdriver = 'dblib'; |
| 101 | } |
Andrey Andreev | 6b4bffa | 2012-06-25 02:29:20 +0300 | [diff] [blame] | 102 | elseif ($this->subdriver === '4D') |
| 103 | { |
| 104 | $this->subdriver = '4d'; |
| 105 | } |
Andrey Andreev | 279256f | 2014-01-30 13:50:00 +0200 | [diff] [blame] | 106 | elseif ( ! in_array($this->subdriver, array('4d', 'cubrid', 'dblib', 'firebird', 'ibm', 'informix', 'mysql', 'oci', 'odbc', 'pgsql', 'sqlite', 'sqlsrv'), TRUE)) |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 107 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 108 | log_message('error', 'PDO: Invalid or non-existent subdriver'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 109 | |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 110 | if ($this->db_debug) |
Timothy Warren | 25dc755 | 2012-02-13 14:12:35 -0500 | [diff] [blame] | 111 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 112 | show_error('Invalid or non-existent PDO subdriver'); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 113 | } |
Andrey Andreev | d42cc46 | 2012-06-24 23:52:40 +0300 | [diff] [blame] | 114 | } |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 115 | |
| 116 | $this->dsn = NULL; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 117 | } |
| 118 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 119 | // -------------------------------------------------------------------- |
| 120 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 121 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 122 | * Database connection |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 123 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 124 | * @param bool $persistent |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 125 | * @return object |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 126 | */ |
Andrey Andreev | 3b0130d | 2012-06-24 21:41:42 +0300 | [diff] [blame] | 127 | public function db_connect($persistent = FALSE) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 128 | { |
Andrey Andreev | ac71862 | 2016-06-22 13:01:51 +0300 | [diff] [blame] | 129 | if ($persistent === TRUE) |
| 130 | { |
| 131 | $this->options[PDO::ATTR_PERSISTENT] = TRUE; |
| 132 | } |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 133 | |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 134 | try |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 135 | { |
Andrey Andreev | f2818bd | 2014-02-25 15:26:20 +0200 | [diff] [blame] | 136 | return new PDO($this->dsn, $this->username, $this->password, $this->options); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 137 | } |
| 138 | catch (PDOException $e) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 139 | { |
| 140 | if ($this->db_debug && empty($this->failover)) |
| 141 | { |
| 142 | $this->display_error($e->getMessage(), '', TRUE); |
| 143 | } |
| 144 | |
| 145 | return FALSE; |
| 146 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // -------------------------------------------------------------------- |
| 150 | |
| 151 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 152 | * Database version number |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 153 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 154 | * @return string |
| 155 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 156 | public function version() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 157 | { |
Andrey Andreev | 9e3a83a | 2012-07-05 21:57:41 +0300 | [diff] [blame] | 158 | if (isset($this->data_cache['version'])) |
| 159 | { |
| 160 | return $this->data_cache['version']; |
| 161 | } |
| 162 | |
| 163 | // Not all subdrivers support the getAttribute() method |
| 164 | try |
| 165 | { |
| 166 | return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION); |
| 167 | } |
| 168 | catch (PDOException $e) |
| 169 | { |
| 170 | return parent::version(); |
| 171 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
| 177 | * Execute the query |
| 178 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 179 | * @param string $sql SQL query |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 180 | * @return mixed |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 181 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 182 | protected function _execute($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 183 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 184 | return $this->conn_id->query($sql); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // -------------------------------------------------------------------- |
| 188 | |
| 189 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 190 | * Begin Transaction |
| 191 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 192 | * @return bool |
| 193 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 194 | protected function _trans_begin() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 195 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 196 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // -------------------------------------------------------------------- |
| 200 | |
| 201 | /** |
| 202 | * Commit Transaction |
| 203 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 204 | * @return bool |
| 205 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 206 | protected function _trans_commit() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 207 | { |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 208 | return $this->conn_id->commit(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // -------------------------------------------------------------------- |
| 212 | |
| 213 | /** |
| 214 | * Rollback Transaction |
| 215 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 216 | * @return bool |
| 217 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 218 | protected function _trans_rollback() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 219 | { |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 220 | return $this->conn_id->rollBack(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 226 | * Platform-dependent string escape |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 227 | * |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 228 | * @param string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 229 | * @return string |
| 230 | */ |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 231 | protected function _escape_str($str) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 232 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 233 | // Escape the string |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 234 | $str = $this->conn_id->quote($str); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 235 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 236 | // If there are duplicated quotes, trim them away |
Andrey Andreev | 0b6a492 | 2013-01-10 16:53:44 +0200 | [diff] [blame] | 237 | return ($str[0] === "'") |
| 238 | ? substr($str, 1, -1) |
| 239 | : $str; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // -------------------------------------------------------------------- |
| 243 | |
| 244 | /** |
| 245 | * Affected Rows |
| 246 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 247 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 248 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 249 | public function affected_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 250 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 251 | return is_object($this->result_id) ? $this->result_id->rowCount() : 0; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // -------------------------------------------------------------------- |
| 255 | |
| 256 | /** |
| 257 | * Insert ID |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 258 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 259 | * @param string $name |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 260 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 261 | */ |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 262 | public function insert_id($name = NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 263 | { |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 264 | return $this->conn_id->lastInsertId($name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 270 | * Field data query |
| 271 | * |
| 272 | * Generates a platform-specific query so that the column data can be retrieved |
| 273 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 274 | * @param string $table |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 275 | * @return string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 276 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 277 | protected function _field_data($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 278 | { |
Andrey Andreev | 5029305 | 2012-06-25 17:48:49 +0300 | [diff] [blame] | 279 | return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // -------------------------------------------------------------------- |
| 283 | |
| 284 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 285 | * Error |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 286 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 287 | * Returns an array containing code and message of the last |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 288 | * database error that has occurred. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 289 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 290 | * @return array |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 291 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 292 | public function error() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 293 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 294 | $error = array('code' => '00000', 'message' => ''); |
| 295 | $pdo_error = $this->conn_id->errorInfo(); |
| 296 | |
| 297 | if (empty($pdo_error[0])) |
| 298 | { |
| 299 | return $error; |
| 300 | } |
| 301 | |
| 302 | $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0]; |
| 303 | if (isset($pdo_error[2])) |
| 304 | { |
| 305 | $error['message'] = $pdo_error[2]; |
| 306 | } |
| 307 | |
| 308 | return $error; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // -------------------------------------------------------------------- |
| 312 | |
| 313 | /** |
Andrey Andreev | e61b19f | 2012-06-25 18:11:39 +0300 | [diff] [blame] | 314 | * Truncate statement |
| 315 | * |
| 316 | * Generates a platform-specific truncate string from the supplied data |
| 317 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 318 | * If the database does not support the TRUNCATE statement, |
Andrey Andreev | e61b19f | 2012-06-25 18:11:39 +0300 | [diff] [blame] | 319 | * then this method maps to 'DELETE FROM table' |
| 320 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 321 | * @param string $table |
Andrey Andreev | e61b19f | 2012-06-25 18:11:39 +0300 | [diff] [blame] | 322 | * @return string |
| 323 | */ |
| 324 | protected function _truncate($table) |
| 325 | { |
| 326 | return 'TRUNCATE TABLE '.$table; |
| 327 | } |
| 328 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 329 | } |