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