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