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