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 |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 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 = ''; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 49 | var $_like_escape_str; |
| 50 | var $_like_escape_chr; |
| 51 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * The syntax to count rows is slightly different across different |
| 55 | * database engines, so this string appears in each driver and is |
| 56 | * used for the count_all() and count_all_results() functions. |
| 57 | */ |
| 58 | var $_count_string = "SELECT COUNT(*) AS "; |
| 59 | var $_random_keyword; |
Timothy Warren | 7aae368 | 2011-10-25 10:37:31 -0400 | [diff] [blame] | 60 | |
| 61 | var $options = array(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 62 | |
Timothy Warren | 51b0e64 | 2011-09-13 13:30:27 -0400 | [diff] [blame] | 63 | function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 64 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 65 | parent::__construct($params); |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 66 | |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 67 | // clause and character used for LIKE escape sequences |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 68 | if (strpos($this->hostname, 'mysql') !== FALSE) |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 69 | { |
| 70 | $this->_like_escape_str = ''; |
| 71 | $this->_like_escape_chr = ''; |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 72 | |
Timothy Warren | 7d834be | 2011-10-26 11:26:17 -0400 | [diff] [blame^] | 73 | //Prior to this version, the charset can't be set in the dsn |
| 74 | if(is_php('5.3.6')) |
| 75 | { |
| 76 | $this->hostname .= ";charset={$this->char_set}"; |
| 77 | } |
| 78 | |
Timothy Warren | 7aae368 | 2011-10-25 10:37:31 -0400 | [diff] [blame] | 79 | //Set the charset with the connection options |
| 80 | $this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES {$this->char_set}"; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 81 | } |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 82 | else if (strpos($this->hostname, 'odbc') !== FALSE) |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 83 | { |
| 84 | $this->_like_escape_str = " {escape '%s'} "; |
| 85 | $this->_like_escape_chr = '!'; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | $this->_like_escape_str = " ESCAPE '%s' "; |
| 90 | $this->_like_escape_chr = '!'; |
| 91 | } |
| 92 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 93 | $this->hostname = $this->hostname . ";dbname=".$this->database; |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 94 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 95 | $this->trans_enabled = FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 96 | |
| 97 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Non-persistent database connection |
| 102 | * |
| 103 | * @access private called by the base class |
| 104 | * @return resource |
| 105 | */ |
| 106 | function db_connect() |
| 107 | { |
Timothy Warren | 7aae368 | 2011-10-25 10:37:31 -0400 | [diff] [blame] | 108 | $this->options['PDO::ATTR_ERRMODE'] = PDO::ERRMODE_SILENT; |
| 109 | return new PDO($this->hostname,$this->username,$this->password, $this->options); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // -------------------------------------------------------------------- |
| 113 | |
| 114 | /** |
| 115 | * Persistent database connection |
| 116 | * |
| 117 | * @access private called by the base class |
| 118 | * @return resource |
| 119 | */ |
| 120 | function db_pconnect() |
| 121 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 122 | return new PDO($this->hostname,$this->username,$this->password, array( |
| 123 | PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT, |
| 124 | PDO::ATTR_PERSISTENT => true |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 125 | )); |
| 126 | } |
| 127 | |
| 128 | // -------------------------------------------------------------------- |
| 129 | |
| 130 | /** |
| 131 | * Reconnect |
| 132 | * |
| 133 | * Keep / reestablish the db connection if no queries have been |
| 134 | * sent for a length of time exceeding the server's idle timeout |
| 135 | * |
| 136 | * @access public |
| 137 | * @return void |
| 138 | */ |
| 139 | function reconnect() |
| 140 | { |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 141 | if ($this->db->db_debug) |
| 142 | { |
| 143 | return $this->db->display_error('db_unsuported_feature'); |
| 144 | } |
| 145 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
| 151 | * Select the database |
| 152 | * |
| 153 | * @access private called by the base class |
| 154 | * @return resource |
| 155 | */ |
| 156 | function db_select() |
| 157 | { |
| 158 | // Not needed for PDO |
| 159 | return TRUE; |
| 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Set client character set |
| 166 | * |
| 167 | * @access public |
| 168 | * @param string |
| 169 | * @param string |
| 170 | * @return resource |
| 171 | */ |
| 172 | function db_set_charset($charset, $collation) |
| 173 | { |
| 174 | // @todo - add support if needed |
| 175 | return TRUE; |
| 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
| 181 | * Version number query string |
| 182 | * |
| 183 | * @access public |
| 184 | * @return string |
| 185 | */ |
| 186 | function _version() |
| 187 | { |
Timothy Warren | 36fb8de | 2011-08-24 08:29:05 -0400 | [diff] [blame] | 188 | return $this->conn_id->getAttribute(PDO::ATTR_CLIENT_VERSION); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // -------------------------------------------------------------------- |
| 192 | |
| 193 | /** |
| 194 | * Execute the query |
| 195 | * |
| 196 | * @access private called by the base class |
| 197 | * @param string an SQL query |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 198 | * @return object |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 199 | */ |
| 200 | function _execute($sql) |
| 201 | { |
| 202 | $sql = $this->_prep_query($sql); |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 203 | $result_id = $this->conn_id->query($sql); |
| 204 | |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 205 | if (is_object($result_id)) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 206 | { |
| 207 | $this->affect_rows = $result_id->rowCount(); |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | $this->affect_rows = 0; |
| 212 | } |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 213 | |
| 214 | return $result_id; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // -------------------------------------------------------------------- |
| 218 | |
| 219 | /** |
| 220 | * Prep the query |
| 221 | * |
| 222 | * If needed, each database adapter can prep the query string |
| 223 | * |
| 224 | * @access private called by execute() |
| 225 | * @param string an SQL query |
| 226 | * @return string |
| 227 | */ |
| 228 | function _prep_query($sql) |
| 229 | { |
| 230 | return $sql; |
| 231 | } |
| 232 | |
| 233 | // -------------------------------------------------------------------- |
| 234 | |
| 235 | /** |
| 236 | * Begin Transaction |
| 237 | * |
| 238 | * @access public |
| 239 | * @return bool |
| 240 | */ |
| 241 | function trans_begin($test_mode = FALSE) |
| 242 | { |
| 243 | if ( ! $this->trans_enabled) |
| 244 | { |
| 245 | return TRUE; |
| 246 | } |
| 247 | |
| 248 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 249 | if ($this->_trans_depth > 0) |
| 250 | { |
| 251 | return TRUE; |
| 252 | } |
| 253 | |
| 254 | // Reset the transaction failure flag. |
| 255 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 256 | // even if the queries produce a successful result. |
| 257 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 258 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 259 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // -------------------------------------------------------------------- |
| 263 | |
| 264 | /** |
| 265 | * Commit Transaction |
| 266 | * |
| 267 | * @access public |
| 268 | * @return bool |
| 269 | */ |
| 270 | function trans_commit() |
| 271 | { |
| 272 | if ( ! $this->trans_enabled) |
| 273 | { |
| 274 | return TRUE; |
| 275 | } |
| 276 | |
| 277 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 278 | if ($this->_trans_depth > 0) |
| 279 | { |
| 280 | return TRUE; |
| 281 | } |
| 282 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 283 | $ret = $this->conn->commit(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 284 | return $ret; |
| 285 | } |
| 286 | |
| 287 | // -------------------------------------------------------------------- |
| 288 | |
| 289 | /** |
| 290 | * Rollback Transaction |
| 291 | * |
| 292 | * @access public |
| 293 | * @return bool |
| 294 | */ |
| 295 | function trans_rollback() |
| 296 | { |
| 297 | if ( ! $this->trans_enabled) |
| 298 | { |
| 299 | return TRUE; |
| 300 | } |
| 301 | |
| 302 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 303 | if ($this->_trans_depth > 0) |
| 304 | { |
| 305 | return TRUE; |
| 306 | } |
| 307 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 308 | $ret = $this->conn_id->rollBack(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 309 | return $ret; |
| 310 | } |
| 311 | |
| 312 | // -------------------------------------------------------------------- |
| 313 | |
| 314 | /** |
| 315 | * Escape String |
| 316 | * |
| 317 | * @access public |
| 318 | * @param string |
| 319 | * @param bool whether or not the string will be used in a LIKE condition |
| 320 | * @return string |
| 321 | */ |
| 322 | function escape_str($str, $like = FALSE) |
| 323 | { |
| 324 | if (is_array($str)) |
| 325 | { |
| 326 | foreach ($str as $key => $val) |
| 327 | { |
| 328 | $str[$key] = $this->escape_str($val, $like); |
| 329 | } |
| 330 | |
| 331 | return $str; |
| 332 | } |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 333 | |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 334 | //Escape the string |
| 335 | $str = $this->conn_id->quote($str); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 336 | |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 337 | //If there are duplicated quotes, trim them away |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 338 | if (strpos($str, "'") === 0) |
Timothy Warren | ec19332 | 2011-10-07 09:53:35 -0400 | [diff] [blame] | 339 | { |
| 340 | $str = substr($str, 1, -1); |
| 341 | } |
| 342 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 343 | // escape LIKE condition wildcards |
| 344 | if ($like === TRUE) |
| 345 | { |
| 346 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
| 347 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 348 | $str); |
| 349 | } |
| 350 | |
| 351 | return $str; |
| 352 | } |
| 353 | |
| 354 | // -------------------------------------------------------------------- |
| 355 | |
| 356 | /** |
| 357 | * Affected Rows |
| 358 | * |
| 359 | * @access public |
| 360 | * @return integer |
| 361 | */ |
| 362 | function affected_rows() |
| 363 | { |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 364 | return $this->affect_rows; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
| 370 | * Insert ID |
Timothy Warren | 57cea51 | 2011-09-14 14:26:28 -0400 | [diff] [blame] | 371 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 372 | * @access public |
| 373 | * @return integer |
| 374 | */ |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 375 | function insert_id($name=NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 376 | { |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 377 | //Convenience method for postgres insertid |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 378 | if (strpos($this->hostname, 'pgsql') !== FALSE) |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 379 | { |
| 380 | $v = $this->_version(); |
| 381 | |
| 382 | $table = func_num_args() > 0 ? func_get_arg(0) : NULL; |
| 383 | |
| 384 | if ($table == NULL && $v >= '8.1') |
| 385 | { |
| 386 | $sql='SELECT LASTVAL() as ins_id'; |
| 387 | } |
| 388 | $query = $this->query($sql); |
| 389 | $row = $query->row(); |
| 390 | return $row->ins_id; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | return $this->conn_id->lastInsertId($name); |
| 395 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // -------------------------------------------------------------------- |
| 399 | |
| 400 | /** |
| 401 | * "Count All" query |
| 402 | * |
| 403 | * Generates a platform-specific query string that counts all records in |
| 404 | * the specified database |
| 405 | * |
| 406 | * @access public |
| 407 | * @param string |
| 408 | * @return string |
| 409 | */ |
| 410 | function count_all($table = '') |
| 411 | { |
| 412 | if ($table == '') |
| 413 | { |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 418 | |
| 419 | if ($query->num_rows() == 0) |
| 420 | { |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | $row = $query->row(); |
| 425 | $this->_reset_select(); |
| 426 | return (int) $row->numrows; |
| 427 | } |
| 428 | |
| 429 | // -------------------------------------------------------------------- |
| 430 | |
| 431 | /** |
| 432 | * Show table query |
| 433 | * |
| 434 | * Generates a platform-specific query string so that the table names can be fetched |
| 435 | * |
| 436 | * @access private |
| 437 | * @param boolean |
| 438 | * @return string |
| 439 | */ |
| 440 | function _list_tables($prefix_limit = FALSE) |
| 441 | { |
| 442 | $sql = "SHOW TABLES FROM `".$this->database."`"; |
| 443 | |
| 444 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 445 | { |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 446 | //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 447 | return FALSE; // not currently supported |
| 448 | } |
| 449 | |
| 450 | return $sql; |
| 451 | } |
| 452 | |
| 453 | // -------------------------------------------------------------------- |
| 454 | |
| 455 | /** |
| 456 | * Show column query |
| 457 | * |
| 458 | * Generates a platform-specific query string so that the column names can be fetched |
| 459 | * |
| 460 | * @access public |
| 461 | * @param string the table name |
| 462 | * @return string |
| 463 | */ |
| 464 | function _list_columns($table = '') |
| 465 | { |
| 466 | return "SHOW COLUMNS FROM ".$table; |
| 467 | } |
| 468 | |
| 469 | // -------------------------------------------------------------------- |
| 470 | |
| 471 | /** |
| 472 | * Field data query |
| 473 | * |
| 474 | * Generates a platform-specific query so that the column data can be retrieved |
| 475 | * |
| 476 | * @access public |
| 477 | * @param string the table name |
| 478 | * @return object |
| 479 | */ |
| 480 | function _field_data($table) |
| 481 | { |
| 482 | return "SELECT TOP 1 FROM ".$table; |
| 483 | } |
| 484 | |
| 485 | // -------------------------------------------------------------------- |
| 486 | |
| 487 | /** |
| 488 | * The error message string |
| 489 | * |
| 490 | * @access private |
| 491 | * @return string |
| 492 | */ |
| 493 | function _error_message() |
| 494 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 495 | $error_array = $this->conn_id->errorInfo(); |
| 496 | return $error_array[2]; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // -------------------------------------------------------------------- |
| 500 | |
| 501 | /** |
| 502 | * The error message number |
| 503 | * |
| 504 | * @access private |
| 505 | * @return integer |
| 506 | */ |
| 507 | function _error_number() |
| 508 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 509 | return $this->conn_id->errorCode(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // -------------------------------------------------------------------- |
| 513 | |
| 514 | /** |
| 515 | * Escape the SQL Identifiers |
| 516 | * |
| 517 | * This function escapes column and table names |
| 518 | * |
| 519 | * @access private |
| 520 | * @param string |
| 521 | * @return string |
| 522 | */ |
| 523 | function _escape_identifiers($item) |
| 524 | { |
| 525 | if ($this->_escape_char == '') |
| 526 | { |
| 527 | return $item; |
| 528 | } |
| 529 | |
| 530 | foreach ($this->_reserved_identifiers as $id) |
| 531 | { |
| 532 | if (strpos($item, '.'.$id) !== FALSE) |
| 533 | { |
| 534 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 535 | |
| 536 | // remove duplicates if the user already included the escape |
| 537 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (strpos($item, '.') !== FALSE) |
| 542 | { |
| 543 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Timothy Warren | 018af7a | 2011-09-07 12:07:35 -0400 | [diff] [blame] | 544 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 545 | } |
| 546 | else |
| 547 | { |
| 548 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 549 | } |
| 550 | |
| 551 | // remove duplicates if the user already included the escape |
| 552 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 553 | } |
| 554 | |
| 555 | // -------------------------------------------------------------------- |
| 556 | |
| 557 | /** |
| 558 | * From Tables |
| 559 | * |
| 560 | * This function implicitly groups FROM tables so there is no confusion |
| 561 | * about operator precedence in harmony with SQL standards |
| 562 | * |
| 563 | * @access public |
| 564 | * @param type |
| 565 | * @return type |
| 566 | */ |
| 567 | function _from_tables($tables) |
| 568 | { |
| 569 | if ( ! is_array($tables)) |
| 570 | { |
| 571 | $tables = array($tables); |
| 572 | } |
| 573 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 574 | return (count($tables) == 1) ? $tables[0] : '('.implode(', ', $tables).')'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // -------------------------------------------------------------------- |
| 578 | |
| 579 | /** |
| 580 | * Insert statement |
| 581 | * |
| 582 | * Generates a platform-specific insert string from the supplied data |
| 583 | * |
| 584 | * @access public |
| 585 | * @param string the table name |
| 586 | * @param array the insert keys |
| 587 | * @param array the insert values |
| 588 | * @return string |
| 589 | */ |
| 590 | function _insert($table, $keys, $values) |
| 591 | { |
| 592 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 593 | } |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 594 | |
| 595 | // -------------------------------------------------------------------- |
| 596 | |
| 597 | /** |
| 598 | * Insert_batch statement |
| 599 | * |
| 600 | * Generates a platform-specific insert string from the supplied data |
| 601 | * |
| 602 | * @access public |
| 603 | * @param string the table name |
| 604 | * @param array the insert keys |
| 605 | * @param array the insert values |
| 606 | * @return string |
| 607 | */ |
| 608 | function _insert_batch($table, $keys, $values) |
| 609 | { |
| 610 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); |
| 611 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 612 | |
| 613 | // -------------------------------------------------------------------- |
| 614 | |
| 615 | /** |
| 616 | * Update statement |
| 617 | * |
| 618 | * Generates a platform-specific update string from the supplied data |
| 619 | * |
| 620 | * @access public |
| 621 | * @param string the table name |
| 622 | * @param array the update data |
| 623 | * @param array the where clause |
| 624 | * @param array the orderby clause |
| 625 | * @param array the limit clause |
| 626 | * @return string |
| 627 | */ |
| 628 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 629 | { |
| 630 | foreach ($values as $key => $val) |
| 631 | { |
| 632 | $valstr[] = $key." = ".$val; |
| 633 | } |
| 634 | |
| 635 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 636 | |
| 637 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
| 638 | |
| 639 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 640 | |
| 641 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 642 | |
| 643 | $sql .= $orderby.$limit; |
| 644 | |
| 645 | return $sql; |
| 646 | } |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 647 | |
| 648 | // -------------------------------------------------------------------- |
| 649 | |
| 650 | /** |
| 651 | * Update_Batch statement |
| 652 | * |
| 653 | * Generates a platform-specific batch update string from the supplied data |
| 654 | * |
| 655 | * @access public |
| 656 | * @param string the table name |
| 657 | * @param array the update data |
| 658 | * @param array the where clause |
| 659 | * @return string |
| 660 | */ |
| 661 | function _update_batch($table, $values, $index, $where = NULL) |
| 662 | { |
| 663 | $ids = array(); |
| 664 | $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
| 665 | |
| 666 | foreach ($values as $key => $val) |
| 667 | { |
| 668 | $ids[] = $val[$index]; |
| 669 | |
| 670 | foreach (array_keys($val) as $field) |
| 671 | { |
| 672 | if ($field != $index) |
| 673 | { |
| 674 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | $sql = "UPDATE ".$table." SET "; |
| 680 | $cases = ''; |
| 681 | |
| 682 | foreach ($final as $k => $v) |
| 683 | { |
| 684 | $cases .= $k.' = CASE '."\n"; |
| 685 | foreach ($v as $row) |
| 686 | { |
| 687 | $cases .= $row."\n"; |
| 688 | } |
| 689 | |
| 690 | $cases .= 'ELSE '.$k.' END, '; |
| 691 | } |
| 692 | |
| 693 | $sql .= substr($cases, 0, -2); |
| 694 | |
| 695 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 696 | |
| 697 | return $sql; |
| 698 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 699 | |
| 700 | |
| 701 | // -------------------------------------------------------------------- |
| 702 | |
| 703 | /** |
| 704 | * Truncate statement |
| 705 | * |
| 706 | * Generates a platform-specific truncate string from the supplied data |
| 707 | * If the database does not support the truncate() command |
| 708 | * This function maps to "DELETE FROM table" |
| 709 | * |
| 710 | * @access public |
| 711 | * @param string the table name |
| 712 | * @return string |
| 713 | */ |
| 714 | function _truncate($table) |
| 715 | { |
| 716 | return $this->_delete($table); |
| 717 | } |
| 718 | |
| 719 | // -------------------------------------------------------------------- |
| 720 | |
| 721 | /** |
| 722 | * Delete statement |
| 723 | * |
| 724 | * Generates a platform-specific delete string from the supplied data |
| 725 | * |
| 726 | * @access public |
| 727 | * @param string the table name |
| 728 | * @param array the where clause |
| 729 | * @param string the limit clause |
| 730 | * @return string |
| 731 | */ |
| 732 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 733 | { |
| 734 | $conditions = ''; |
| 735 | |
| 736 | if (count($where) > 0 OR count($like) > 0) |
| 737 | { |
| 738 | $conditions = "\nWHERE "; |
| 739 | $conditions .= implode("\n", $this->ar_where); |
| 740 | |
| 741 | if (count($where) > 0 && count($like) > 0) |
| 742 | { |
| 743 | $conditions .= " AND "; |
| 744 | } |
| 745 | $conditions .= implode("\n", $like); |
| 746 | } |
| 747 | |
| 748 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 749 | |
| 750 | return "DELETE FROM ".$table.$conditions.$limit; |
| 751 | } |
| 752 | |
| 753 | // -------------------------------------------------------------------- |
| 754 | |
| 755 | /** |
| 756 | * Limit string |
| 757 | * |
| 758 | * Generates a platform-specific LIMIT clause |
| 759 | * |
| 760 | * @access public |
| 761 | * @param string the sql query string |
| 762 | * @param integer the number of rows to limit the query to |
| 763 | * @param integer the offset value |
| 764 | * @return string |
| 765 | */ |
| 766 | function _limit($sql, $limit, $offset) |
| 767 | { |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 768 | if (strpos($this->hostname, 'cubrid') !== FALSE || strpos($this->hostname, 'sqlite') !== FALSE) |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 769 | { |
| 770 | if ($offset == 0) |
| 771 | { |
| 772 | $offset = ''; |
| 773 | } |
| 774 | else |
| 775 | { |
| 776 | $offset .= ", "; |
| 777 | } |
| 778 | |
| 779 | return $sql."LIMIT ".$offset.$limit; |
| 780 | } |
| 781 | else |
| 782 | { |
| 783 | $sql .= "LIMIT ".$limit; |
| 784 | |
| 785 | if ($offset > 0) |
| 786 | { |
| 787 | $sql .= " OFFSET ".$offset; |
| 788 | } |
| 789 | |
| 790 | return $sql; |
| 791 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | // -------------------------------------------------------------------- |
| 795 | |
| 796 | /** |
| 797 | * Close DB Connection |
| 798 | * |
| 799 | * @access public |
| 800 | * @param resource |
| 801 | * @return void |
| 802 | */ |
| 803 | function _close($conn_id) |
| 804 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 805 | $this->conn_id = null; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | |
| 809 | } |
| 810 | |
| 811 | |
| 812 | |
| 813 | /* End of file pdo_driver.php */ |
| 814 | /* Location: ./system/database/drivers/pdo/pdo_driver.php */ |