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 |
| 32 | * creates dynamically based on whether the active record |
| 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 | bd44d5a | 2012-03-20 22:59:29 +0200 | [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 | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 49 | protected $_like_escape_str; |
| 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 | 738f534 | 2012-03-06 20:43:40 +0200 | [diff] [blame] | 60 | // need to track the pdo driver and options |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 61 | public $pdodriver; |
| 62 | public $options = array(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 63 | |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 64 | public function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 65 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 66 | parent::__construct($params); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 67 | |
| 68 | if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) == 2) |
| 69 | { |
| 70 | // 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] | 71 | // This is for general PDO users, who tend to have a full DSN string. |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 72 | $this->pdodriver = end($match); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | // Try to build a complete DSN string from params |
| 77 | $this->_connect_string($params); |
| 78 | } |
| 79 | |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 80 | // clause and character used for LIKE escape sequences |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 81 | // this one depends on the driver being used |
| 82 | if ($this->pdodriver == 'mysql') |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 83 | { |
| 84 | $this->_like_escape_str = ''; |
| 85 | $this->_like_escape_chr = ''; |
| 86 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 87 | elseif ($this->pdodriver == 'odbc') |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 88 | { |
| 89 | $this->_like_escape_str = " {escape '%s'} "; |
| 90 | $this->_like_escape_chr = '!'; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | $this->_like_escape_str = " ESCAPE '%s' "; |
| 95 | $this->_like_escape_chr = '!'; |
| 96 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 97 | |
| 98 | $this->trans_enabled = FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 99 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword |
| 100 | } |
| 101 | |
| 102 | /** |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 103 | * Connection String |
| 104 | * |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 105 | * @param array |
| 106 | * @return void |
| 107 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 108 | protected function _connect_string($params) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 109 | { |
| 110 | if (strpos($this->hostname, ':')) |
| 111 | { |
| 112 | // hostname generally would have this prototype |
| 113 | // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);'; |
| 114 | // We need to get the prefix (pdodriver used by PDO). |
Timothy Warren | 928d83c | 2012-02-13 14:07:57 -0500 | [diff] [blame] | 115 | $dsnarray = explode(':', $this->hostname); |
| 116 | $this->pdodriver = $dsnarray[0]; |
Taufan Aditya | 5dcdbc3 | 2012-02-13 21:15:56 +0700 | [diff] [blame] | 117 | |
| 118 | // End dsn with a semicolon for extra backward compability |
| 119 | // if database property was not empty. |
Timothy Warren | f452469 | 2012-02-13 14:13:28 -0500 | [diff] [blame] | 120 | if ( ! empty($this->database)) |
Timothy Warren | 25dc755 | 2012-02-13 14:12:35 -0500 | [diff] [blame] | 121 | { |
| 122 | $this->dsn .= rtrim($this->hostname, ';').';'; |
| 123 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 124 | } |
| 125 | else |
| 126 | { |
| 127 | // Invalid DSN, display an error |
| 128 | if ( ! array_key_exists('pdodriver', $params)) |
| 129 | { |
| 130 | show_error('Invalid DB Connection String for PDO'); |
| 131 | } |
| 132 | |
| 133 | // Assuming that the following DSN string format is used: |
| 134 | // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql'; |
| 135 | $this->dsn = $this->pdodriver.':'; |
| 136 | |
| 137 | // Add hostname to the DSN for databases that need it |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 138 | if ( ! empty($this->hostname) |
Timothy Warren | 928d83c | 2012-02-13 14:07:57 -0500 | [diff] [blame] | 139 | && strpos($this->hostname, ':') === FALSE |
| 140 | && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid'))) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 141 | { |
| 142 | $this->dsn .= 'host='.$this->hostname.';'; |
| 143 | } |
| 144 | |
| 145 | // Add a port to the DSN for databases that can use it |
| 146 | if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid'))) |
| 147 | { |
| 148 | $this->dsn .= 'port='.$this->port.';'; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Add the database name to the DSN, if needed |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 153 | if (stripos($this->dsn, 'dbname') === FALSE |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 154 | && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid'))) |
| 155 | { |
| 156 | $this->dsn .= 'dbname='.$this->database.';'; |
| 157 | } |
| 158 | elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv'))) |
| 159 | { |
| 160 | if (stripos($this->dsn, 'dsn') === FALSE) |
| 161 | { |
| 162 | $this->dsn .= 'database='.$this->database.';'; |
| 163 | } |
| 164 | } |
| 165 | elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:') |
| 166 | { |
| 167 | if ($this->database !== ':memory') |
| 168 | { |
| 169 | if ( ! file_exists($this->database)) |
| 170 | { |
| 171 | show_error('Invalid DB Connection string for PDO SQLite'); |
| 172 | } |
| 173 | |
| 174 | $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : ''; |
| 175 | } |
| 176 | |
| 177 | $this->dsn .= $this->database; |
| 178 | } |
| 179 | |
| 180 | // Add charset to the DSN, if needed |
| 181 | if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci'))) |
| 182 | { |
| 183 | $this->dsn .= 'charset='.$this->char_set.';'; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 188 | * Non-persistent database connection |
| 189 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 190 | * @return object |
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 | public function db_connect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 193 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 194 | $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT; |
| 195 | |
| 196 | return $this->pdo_connect(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // -------------------------------------------------------------------- |
| 200 | |
| 201 | /** |
| 202 | * Persistent database connection |
| 203 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 204 | * @return object |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 205 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 206 | public function db_pconnect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 207 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 208 | $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 209 | $this->options[PDO::ATTR_PERSISTENT] = TRUE; |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 210 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 211 | return $this->pdo_connect(); |
| 212 | } |
| 213 | |
| 214 | // -------------------------------------------------------------------- |
| 215 | |
| 216 | /** |
| 217 | * PDO connection |
| 218 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 219 | * @return object |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 220 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 221 | public function pdo_connect() |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 222 | { |
Taufan Aditya | 62b8cae | 2012-02-09 16:40:39 +0700 | [diff] [blame] | 223 | // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 224 | if ($this->pdodriver === 'mysql' && ! is_php('5.3.6')) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 225 | { |
Taufan Aditya | 55bb97e | 2012-02-09 16:43:26 +0700 | [diff] [blame] | 226 | $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Connecting... |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 230 | try |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 231 | { |
| 232 | $db = new PDO($this->dsn, $this->username, $this->password, $this->options); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 233 | } |
| 234 | catch (PDOException $e) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 235 | { |
| 236 | if ($this->db_debug && empty($this->failover)) |
| 237 | { |
| 238 | $this->display_error($e->getMessage(), '', TRUE); |
| 239 | } |
| 240 | |
| 241 | return FALSE; |
| 242 | } |
| 243 | |
| 244 | return $db; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // -------------------------------------------------------------------- |
| 248 | |
| 249 | /** |
| 250 | * Reconnect |
| 251 | * |
| 252 | * Keep / reestablish the db connection if no queries have been |
| 253 | * sent for a length of time exceeding the server's idle timeout |
| 254 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 255 | * @return void |
| 256 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 257 | public function reconnect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 258 | { |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 259 | if ($this->db->db_debug) |
| 260 | { |
| 261 | return $this->db->display_error('db_unsuported_feature'); |
| 262 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 263 | |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 264 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
| 270 | * Select the database |
| 271 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 272 | * @return resource |
| 273 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 274 | public function db_select() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 275 | { |
| 276 | // Not needed for PDO |
| 277 | return TRUE; |
| 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 283 | * Database version number |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 284 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 285 | * @return string |
| 286 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 287 | public function version() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 288 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 289 | return isset($this->data_cache['version']) |
| 290 | ? $this->data_cache['version'] |
| 291 | : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // -------------------------------------------------------------------- |
| 295 | |
| 296 | /** |
| 297 | * Execute the query |
| 298 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 299 | * @param string an SQL query |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 300 | * @return mixed |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 301 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 302 | protected function _execute($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 303 | { |
| 304 | $sql = $this->_prep_query($sql); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 305 | |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 306 | $result_id = $this->conn_id->query($sql); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 307 | |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 308 | if (is_object($result_id)) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 309 | { |
| 310 | $this->affect_rows = $result_id->rowCount(); |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | $this->affect_rows = 0; |
| 315 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 316 | |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 317 | return $result_id; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // -------------------------------------------------------------------- |
| 321 | |
| 322 | /** |
| 323 | * Prep the query |
| 324 | * |
| 325 | * If needed, each database adapter can prep the query string |
| 326 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 327 | * @param string an SQL query |
| 328 | * @return string |
| 329 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 330 | protected function _prep_query($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 331 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 332 | if ($this->pdodriver === 'pgsql') |
| 333 | { |
| 334 | // Change the backtick(s) for Postgre |
| 335 | $sql = str_replace('`', '"', $sql); |
| 336 | } |
| 337 | elseif ($this->pdodriver === 'sqlite') |
| 338 | { |
| 339 | // Change the backtick(s) for SQLite |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 340 | $sql = str_replace('`', '', $sql); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 341 | } |
| 342 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 343 | return $sql; |
| 344 | } |
| 345 | |
| 346 | // -------------------------------------------------------------------- |
| 347 | |
| 348 | /** |
| 349 | * Begin Transaction |
| 350 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 351 | * @return bool |
| 352 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 353 | public function trans_begin($test_mode = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 354 | { |
| 355 | if ( ! $this->trans_enabled) |
| 356 | { |
| 357 | return TRUE; |
| 358 | } |
| 359 | |
| 360 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 361 | if ($this->_trans_depth > 0) |
| 362 | { |
| 363 | return TRUE; |
| 364 | } |
| 365 | |
| 366 | // Reset the transaction failure flag. |
| 367 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 368 | // even if the queries produce a successful result. |
Timothy Warren | d019fd6 | 2011-10-26 11:26:17 -0400 | [diff] [blame] | 369 | $this->_trans_failure = (bool) ($test_mode === TRUE); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 370 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 371 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // -------------------------------------------------------------------- |
| 375 | |
| 376 | /** |
| 377 | * Commit Transaction |
| 378 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 379 | * @return bool |
| 380 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 381 | public function trans_commit() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 382 | { |
| 383 | if ( ! $this->trans_enabled) |
| 384 | { |
| 385 | return TRUE; |
| 386 | } |
| 387 | |
| 388 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 389 | if ($this->_trans_depth > 0) |
| 390 | { |
| 391 | return TRUE; |
| 392 | } |
| 393 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 394 | $ret = $this->conn->commit(); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 395 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 396 | return $ret; |
| 397 | } |
| 398 | |
| 399 | // -------------------------------------------------------------------- |
| 400 | |
| 401 | /** |
| 402 | * Rollback Transaction |
| 403 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 404 | * @return bool |
| 405 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 406 | public function trans_rollback() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 407 | { |
| 408 | if ( ! $this->trans_enabled) |
| 409 | { |
| 410 | return TRUE; |
| 411 | } |
| 412 | |
| 413 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 414 | if ($this->_trans_depth > 0) |
| 415 | { |
| 416 | return TRUE; |
| 417 | } |
| 418 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 419 | $ret = $this->conn_id->rollBack(); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 420 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 421 | return $ret; |
| 422 | } |
| 423 | |
| 424 | // -------------------------------------------------------------------- |
| 425 | |
| 426 | /** |
| 427 | * Escape String |
| 428 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 429 | * @param string |
| 430 | * @param bool whether or not the string will be used in a LIKE condition |
| 431 | * @return string |
| 432 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 433 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 434 | { |
| 435 | if (is_array($str)) |
| 436 | { |
| 437 | foreach ($str as $key => $val) |
| 438 | { |
| 439 | $str[$key] = $this->escape_str($val, $like); |
| 440 | } |
| 441 | |
| 442 | return $str; |
| 443 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 444 | |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 445 | //Escape the string |
| 446 | $str = $this->conn_id->quote($str); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 447 | |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 448 | //If there are duplicated quotes, trim them away |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 449 | if (strpos($str, "'") === 0) |
Timothy Warren | ec19332 | 2011-10-07 09:53:35 -0400 | [diff] [blame] | 450 | { |
| 451 | $str = substr($str, 1, -1); |
| 452 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 453 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 454 | // escape LIKE condition wildcards |
| 455 | if ($like === TRUE) |
| 456 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 457 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 458 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 459 | $str); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | return $str; |
| 463 | } |
| 464 | |
| 465 | // -------------------------------------------------------------------- |
| 466 | |
| 467 | /** |
| 468 | * Affected Rows |
| 469 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 470 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 471 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 472 | public function affected_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 473 | { |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 474 | return $this->affect_rows; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // -------------------------------------------------------------------- |
| 478 | |
| 479 | /** |
| 480 | * Insert ID |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 481 | * |
| 482 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 483 | */ |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 484 | public function insert_id($name = NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 485 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 486 | if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1') |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 487 | { |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 488 | $query = $this->query('SELECT LASTVAL() AS ins_id'); |
| 489 | $query = $query->row(); |
| 490 | return $query->ins_id; |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 491 | } |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 492 | |
| 493 | return $this->conn_id->lastInsertId($name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // -------------------------------------------------------------------- |
| 497 | |
| 498 | /** |
| 499 | * "Count All" query |
| 500 | * |
| 501 | * Generates a platform-specific query string that counts all records in |
| 502 | * the specified database |
| 503 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 504 | * @param string |
| 505 | * @return string |
| 506 | */ |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 507 | function count_all($table = '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 508 | { |
| 509 | if ($table == '') |
| 510 | { |
| 511 | return 0; |
| 512 | } |
| 513 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 514 | $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 515 | $query = $this->query($sql); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 516 | |
| 517 | if ($query->num_rows() == 0) |
| 518 | { |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | $row = $query->row(); |
| 523 | $this->_reset_select(); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 524 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 525 | return (int) $row->numrows; |
| 526 | } |
| 527 | |
| 528 | // -------------------------------------------------------------------- |
| 529 | |
| 530 | /** |
| 531 | * Show table query |
| 532 | * |
| 533 | * Generates a platform-specific query string so that the table names can be fetched |
| 534 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 535 | * @param bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 536 | * @return string |
| 537 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 538 | protected function _list_tables($prefix_limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 539 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 540 | if ($this->pdodriver == 'pgsql') |
| 541 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 542 | // Analog function to show all tables in postgre |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 543 | $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'"; |
| 544 | } |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 545 | elseif ($this->pdodriver == 'sqlite') |
| 546 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 547 | // Analog function to show all tables in sqlite |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 548 | $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"; |
| 549 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 550 | else |
| 551 | { |
| 552 | $sql = "SHOW TABLES FROM `".$this->database."`"; |
| 553 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 554 | |
| 555 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 556 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 557 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | return $sql; |
| 561 | } |
| 562 | |
| 563 | // -------------------------------------------------------------------- |
| 564 | |
| 565 | /** |
| 566 | * Show column query |
| 567 | * |
| 568 | * Generates a platform-specific query string so that the column names can be fetched |
| 569 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 570 | * @param string the table name |
| 571 | * @return string |
| 572 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 573 | protected function _list_columns($table = '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 574 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 575 | return 'SHOW COLUMNS FROM '.$this->_from_tables($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // -------------------------------------------------------------------- |
| 579 | |
| 580 | /** |
| 581 | * Field data query |
| 582 | * |
| 583 | * Generates a platform-specific query so that the column data can be retrieved |
| 584 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 585 | * @param string the table name |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 586 | * @return string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 587 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 588 | protected function _field_data($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 589 | { |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 590 | if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql') |
| 591 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 592 | // Analog function for mysql and postgre |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 593 | return 'SELECT * FROM '.$this->_from_tables($table).' LIMIT 1'; |
| 594 | } |
| 595 | elseif ($this->pdodriver == 'oci') |
| 596 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 597 | // Analog function for oci |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 598 | return 'SELECT * FROM '.$this->_from_tables($table).' WHERE ROWNUM <= 1'; |
| 599 | } |
| 600 | elseif ($this->pdodriver == 'sqlite') |
| 601 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 602 | // Analog function for sqlite |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 603 | return 'PRAGMA table_info('.$this->_from_tables($table).')'; |
| 604 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 605 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 606 | return 'SELECT TOP 1 FROM '.$this->_from_tables($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // -------------------------------------------------------------------- |
| 610 | |
| 611 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 612 | * Error |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 613 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 614 | * Returns an array containing code and message of the last |
| 615 | * database error that has occured. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 616 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 617 | * @return array |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 618 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 619 | public function error() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 620 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 621 | $error = array('code' => '00000', 'message' => ''); |
| 622 | $pdo_error = $this->conn_id->errorInfo(); |
| 623 | |
| 624 | if (empty($pdo_error[0])) |
| 625 | { |
| 626 | return $error; |
| 627 | } |
| 628 | |
| 629 | $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0]; |
| 630 | if (isset($pdo_error[2])) |
| 631 | { |
| 632 | $error['message'] = $pdo_error[2]; |
| 633 | } |
| 634 | |
| 635 | return $error; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | // -------------------------------------------------------------------- |
| 639 | |
| 640 | /** |
| 641 | * Escape the SQL Identifiers |
| 642 | * |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 643 | * This function escapes column and table names |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 644 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 645 | * @param string |
| 646 | * @return string |
| 647 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 648 | public function _escape_identifiers($item) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 649 | { |
| 650 | if ($this->_escape_char == '') |
| 651 | { |
| 652 | return $item; |
| 653 | } |
| 654 | |
| 655 | foreach ($this->_reserved_identifiers as $id) |
| 656 | { |
| 657 | if (strpos($item, '.'.$id) !== FALSE) |
| 658 | { |
| 659 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 660 | |
| 661 | // remove duplicates if the user already included the escape |
| 662 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (strpos($item, '.') !== FALSE) |
| 667 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 668 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); |
| 669 | $str .= $this->_escape_char; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 670 | } |
| 671 | else |
| 672 | { |
| 673 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 674 | } |
| 675 | |
| 676 | // remove duplicates if the user already included the escape |
| 677 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 678 | } |
| 679 | |
| 680 | // -------------------------------------------------------------------- |
| 681 | |
| 682 | /** |
| 683 | * From Tables |
| 684 | * |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 685 | * This function implicitly groups FROM tables so there is no confusion |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 686 | * about operator precedence in harmony with SQL standards |
| 687 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 688 | * @param array |
| 689 | * @return string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 690 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 691 | protected function _from_tables($tables) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 692 | { |
| 693 | if ( ! is_array($tables)) |
| 694 | { |
| 695 | $tables = array($tables); |
| 696 | } |
| 697 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 698 | return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | // -------------------------------------------------------------------- |
| 702 | |
| 703 | /** |
| 704 | * Insert statement |
| 705 | * |
| 706 | * Generates a platform-specific insert string from the supplied data |
| 707 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 708 | * @param string the table name |
| 709 | * @param array the insert keys |
| 710 | * @param array the insert values |
| 711 | * @return string |
| 712 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 713 | protected function _insert($table, $keys, $values) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 714 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 715 | return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 716 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 717 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 718 | // -------------------------------------------------------------------- |
| 719 | |
| 720 | /** |
| 721 | * Insert_batch statement |
| 722 | * |
| 723 | * Generates a platform-specific insert string from the supplied data |
| 724 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 725 | * @param string the table name |
| 726 | * @param array the insert keys |
| 727 | * @param array the insert values |
| 728 | * @return string |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 729 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 730 | protected function _insert_batch($table, $keys, $values) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 731 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 732 | return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 733 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 734 | |
| 735 | // -------------------------------------------------------------------- |
| 736 | |
| 737 | /** |
| 738 | * Update statement |
| 739 | * |
| 740 | * Generates a platform-specific update string from the supplied data |
| 741 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 742 | * @param string the table name |
| 743 | * @param array the update data |
| 744 | * @param array the where clause |
| 745 | * @param array the orderby clause |
| 746 | * @param array the limit clause |
| 747 | * @return string |
| 748 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 749 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 750 | { |
| 751 | foreach ($values as $key => $val) |
| 752 | { |
| 753 | $valstr[] = $key." = ".$val; |
| 754 | } |
| 755 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 756 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 757 | $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : ''; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 758 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 759 | $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr); |
| 760 | $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : ''; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 761 | $sql .= $orderby.$limit; |
| 762 | |
| 763 | return $sql; |
| 764 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 765 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 766 | // -------------------------------------------------------------------- |
| 767 | |
| 768 | /** |
| 769 | * Update_Batch statement |
| 770 | * |
| 771 | * Generates a platform-specific batch update string from the supplied data |
| 772 | * |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 773 | * @param string the table name |
| 774 | * @param array the update data |
| 775 | * @param array the where clause |
| 776 | * @return string |
| 777 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 778 | protected function _update_batch($table, $values, $index, $where = NULL) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 779 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 780 | $ids = array(); |
| 781 | $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 782 | |
| 783 | foreach ($values as $key => $val) |
| 784 | { |
| 785 | $ids[] = $val[$index]; |
| 786 | |
| 787 | foreach (array_keys($val) as $field) |
| 788 | { |
| 789 | if ($field != $index) |
| 790 | { |
| 791 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 796 | $sql = 'UPDATE '.$this->_from_tables($table).' SET '; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 797 | $cases = ''; |
| 798 | |
| 799 | foreach ($final as $k => $v) |
| 800 | { |
| 801 | $cases .= $k.' = CASE '."\n"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 802 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 803 | foreach ($v as $row) |
| 804 | { |
| 805 | $cases .= $row."\n"; |
| 806 | } |
| 807 | |
| 808 | $cases .= 'ELSE '.$k.' END, '; |
| 809 | } |
| 810 | |
| 811 | $sql .= substr($cases, 0, -2); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 812 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 813 | |
| 814 | return $sql; |
| 815 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 816 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 817 | // -------------------------------------------------------------------- |
| 818 | |
| 819 | /** |
| 820 | * Truncate statement |
| 821 | * |
| 822 | * Generates a platform-specific truncate string from the supplied data |
| 823 | * If the database does not support the truncate() command |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 824 | * This function maps to "DELETE FROM table" |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 825 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 826 | * @param string the table name |
| 827 | * @return string |
| 828 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 829 | protected function _truncate($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 830 | { |
| 831 | return $this->_delete($table); |
| 832 | } |
| 833 | |
| 834 | // -------------------------------------------------------------------- |
| 835 | |
| 836 | /** |
| 837 | * Delete statement |
| 838 | * |
| 839 | * Generates a platform-specific delete string from the supplied data |
| 840 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 841 | * @param string the table name |
| 842 | * @param array the where clause |
| 843 | * @param string the limit clause |
| 844 | * @return string |
| 845 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 846 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 847 | { |
| 848 | $conditions = ''; |
| 849 | |
| 850 | if (count($where) > 0 OR count($like) > 0) |
| 851 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 852 | $conditions = "\nWHERE "; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 853 | $conditions .= implode("\n", $this->ar_where); |
| 854 | |
| 855 | if (count($where) > 0 && count($like) > 0) |
| 856 | { |
| 857 | $conditions .= " AND "; |
| 858 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 859 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 860 | $conditions .= implode("\n", $like); |
| 861 | } |
| 862 | |
| 863 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 864 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 865 | return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // -------------------------------------------------------------------- |
| 869 | |
| 870 | /** |
| 871 | * Limit string |
| 872 | * |
| 873 | * Generates a platform-specific LIMIT clause |
| 874 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 875 | * @param string the sql query string |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 876 | * @param int the number of rows to limit the query to |
| 877 | * @param int the offset value |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 878 | * @return string |
| 879 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 880 | protected function _limit($sql, $limit, $offset) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 881 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 882 | if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite') |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 883 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 884 | $offset = ($offset == 0) ? '' : $offset.', '; |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 885 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 886 | return $sql.'LIMIT '.$offset.$limit; |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 887 | } |
| 888 | else |
| 889 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 890 | $sql .= 'LIMIT '.$limit; |
| 891 | $sql .= ($offset > 0) ? ' OFFSET '.$offset : ''; |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 892 | |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 893 | return $sql; |
| 894 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | // -------------------------------------------------------------------- |
| 898 | |
| 899 | /** |
| 900 | * Close DB Connection |
| 901 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 902 | * @param resource |
| 903 | * @return void |
| 904 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame^] | 905 | protected function _close($conn_id) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 906 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 907 | $this->conn_id = null; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 908 | } |
| 909 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 910 | } |
| 911 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 912 | /* End of file pdo_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 913 | /* Location: ./system/database/drivers/pdo/pdo_driver.php */ |