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