[1] | 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 | * |
---|
| 7 | * @package CodeIgniter |
---|
| 8 | * @author Esen Sagynov |
---|
| 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
---|
| 10 | * @license http://codeigniter.com/user_guide/license.html |
---|
| 11 | * @link http://codeigniter.com |
---|
| 12 | * @since Version 2.0.2 |
---|
| 13 | * @filesource |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | // ------------------------------------------------------------------------ |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * CUBRID Database Adapter Class |
---|
| 20 | * |
---|
| 21 | * Note: _DB is an extender class that the app controller |
---|
| 22 | * creates dynamically based on whether the active record |
---|
| 23 | * class is being used or not. |
---|
| 24 | * |
---|
| 25 | * @package CodeIgniter |
---|
| 26 | * @subpackage Drivers |
---|
| 27 | * @category Database |
---|
| 28 | * @author Esen Sagynov |
---|
| 29 | * @link http://codeigniter.com/user_guide/database/ |
---|
| 30 | */ |
---|
| 31 | class CI_DB_cubrid_driver extends CI_DB { |
---|
| 32 | |
---|
| 33 | // Default CUBRID Broker port. Will be used unless user |
---|
| 34 | // explicitly specifies another one. |
---|
| 35 | const DEFAULT_PORT = 33000; |
---|
| 36 | |
---|
| 37 | var $dbdriver = 'cubrid'; |
---|
| 38 | |
---|
| 39 | // The character used for escaping - no need in CUBRID |
---|
| 40 | var $_escape_char = ''; |
---|
| 41 | |
---|
| 42 | // clause and character used for LIKE escape sequences - not used in CUBRID |
---|
| 43 | var $_like_escape_str = ''; |
---|
| 44 | var $_like_escape_chr = ''; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * The syntax to count rows is slightly different across different |
---|
| 48 | * database engines, so this string appears in each driver and is |
---|
| 49 | * used for the count_all() and count_all_results() functions. |
---|
| 50 | */ |
---|
| 51 | var $_count_string = 'SELECT COUNT(*) AS '; |
---|
| 52 | var $_random_keyword = ' RAND()'; // database specific random keyword |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * Non-persistent database connection |
---|
| 56 | * |
---|
| 57 | * @access private called by the base class |
---|
| 58 | * @return resource |
---|
| 59 | */ |
---|
| 60 | function db_connect() |
---|
| 61 | { |
---|
| 62 | // If no port is defined by the user, use the default value |
---|
| 63 | if ($this->port == '') |
---|
| 64 | { |
---|
| 65 | $this->port = self::DEFAULT_PORT; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password); |
---|
| 69 | |
---|
| 70 | if ($conn) |
---|
| 71 | { |
---|
| 72 | // Check if a user wants to run queries in dry, i.e. run the |
---|
| 73 | // queries but not commit them. |
---|
| 74 | if (isset($this->auto_commit) && ! $this->auto_commit) |
---|
| 75 | { |
---|
| 76 | cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE); |
---|
| 77 | } |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE); |
---|
| 81 | $this->auto_commit = TRUE; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | return $conn; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // -------------------------------------------------------------------- |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Persistent database connection |
---|
| 92 | * In CUBRID persistent DB connection is supported natively in CUBRID |
---|
| 93 | * engine which can be configured in the CUBRID Broker configuration |
---|
| 94 | * file by setting the CCI_PCONNECT parameter to ON. In that case, all |
---|
| 95 | * connections established between the client application and the |
---|
| 96 | * server will become persistent. This is calling the same |
---|
| 97 | * @cubrid_connect function will establish persisten connection |
---|
| 98 | * considering that the CCI_PCONNECT is ON. |
---|
| 99 | * |
---|
| 100 | * @access private called by the base class |
---|
| 101 | * @return resource |
---|
| 102 | */ |
---|
| 103 | function db_pconnect() |
---|
| 104 | { |
---|
| 105 | return $this->db_connect(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // -------------------------------------------------------------------- |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * Reconnect |
---|
| 112 | * |
---|
| 113 | * Keep / reestablish the db connection if no queries have been |
---|
| 114 | * sent for a length of time exceeding the server's idle timeout |
---|
| 115 | * |
---|
| 116 | * @access public |
---|
| 117 | * @return void |
---|
| 118 | */ |
---|
| 119 | function reconnect() |
---|
| 120 | { |
---|
| 121 | if (cubrid_ping($this->conn_id) === FALSE) |
---|
| 122 | { |
---|
| 123 | $this->conn_id = FALSE; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // -------------------------------------------------------------------- |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * Select the database |
---|
| 131 | * |
---|
| 132 | * @access private called by the base class |
---|
| 133 | * @return resource |
---|
| 134 | */ |
---|
| 135 | function db_select() |
---|
| 136 | { |
---|
| 137 | // In CUBRID there is no need to select a database as the database |
---|
| 138 | // is chosen at the connection time. |
---|
| 139 | // So, to determine if the database is "selected", all we have to |
---|
| 140 | // do is ping the server and return that value. |
---|
| 141 | return cubrid_ping($this->conn_id); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | // -------------------------------------------------------------------- |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * Set client character set |
---|
| 148 | * |
---|
| 149 | * @access public |
---|
| 150 | * @param string |
---|
| 151 | * @param string |
---|
| 152 | * @return resource |
---|
| 153 | */ |
---|
| 154 | function db_set_charset($charset, $collation) |
---|
| 155 | { |
---|
| 156 | // In CUBRID, there is no need to set charset or collation. |
---|
| 157 | // This is why returning true will allow the application continue |
---|
| 158 | // its normal process. |
---|
| 159 | return TRUE; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | // -------------------------------------------------------------------- |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * Version number query string |
---|
| 166 | * |
---|
| 167 | * @access public |
---|
| 168 | * @return string |
---|
| 169 | */ |
---|
| 170 | function _version() |
---|
| 171 | { |
---|
| 172 | // To obtain the CUBRID Server version, no need to run the SQL query. |
---|
| 173 | // CUBRID PHP API provides a function to determin this value. |
---|
| 174 | // This is why we also need to add 'cubrid' value to the list of |
---|
| 175 | // $driver_version_exceptions array in DB_driver class in |
---|
| 176 | // version() function. |
---|
| 177 | return cubrid_get_server_info($this->conn_id); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | // -------------------------------------------------------------------- |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * Execute the query |
---|
| 184 | * |
---|
| 185 | * @access private called by the base class |
---|
| 186 | * @param string an SQL query |
---|
| 187 | * @return resource |
---|
| 188 | */ |
---|
| 189 | function _execute($sql) |
---|
| 190 | { |
---|
| 191 | $sql = $this->_prep_query($sql); |
---|
| 192 | return @cubrid_query($sql, $this->conn_id); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | // -------------------------------------------------------------------- |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * Prep the query |
---|
| 199 | * |
---|
| 200 | * If needed, each database adapter can prep the query string |
---|
| 201 | * |
---|
| 202 | * @access private called by execute() |
---|
| 203 | * @param string an SQL query |
---|
| 204 | * @return string |
---|
| 205 | */ |
---|
| 206 | function _prep_query($sql) |
---|
| 207 | { |
---|
| 208 | // No need to prepare |
---|
| 209 | return $sql; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // -------------------------------------------------------------------- |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * Begin Transaction |
---|
| 216 | * |
---|
| 217 | * @access public |
---|
| 218 | * @return bool |
---|
| 219 | */ |
---|
| 220 | function trans_begin($test_mode = FALSE) |
---|
| 221 | { |
---|
| 222 | if ( ! $this->trans_enabled) |
---|
| 223 | { |
---|
| 224 | return TRUE; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | // When transactions are nested we only begin/commit/rollback the outermost ones |
---|
| 228 | if ($this->_trans_depth > 0) |
---|
| 229 | { |
---|
| 230 | return TRUE; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // Reset the transaction failure flag. |
---|
| 234 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
---|
| 235 | // even if the queries produce a successful result. |
---|
| 236 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
---|
| 237 | |
---|
| 238 | if (cubrid_get_autocommit($this->conn_id)) |
---|
| 239 | { |
---|
| 240 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | return TRUE; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | // -------------------------------------------------------------------- |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * Commit Transaction |
---|
| 250 | * |
---|
| 251 | * @access public |
---|
| 252 | * @return bool |
---|
| 253 | */ |
---|
| 254 | function trans_commit() |
---|
| 255 | { |
---|
| 256 | if ( ! $this->trans_enabled) |
---|
| 257 | { |
---|
| 258 | return TRUE; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | // When transactions are nested we only begin/commit/rollback the outermost ones |
---|
| 262 | if ($this->_trans_depth > 0) |
---|
| 263 | { |
---|
| 264 | return TRUE; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | cubrid_commit($this->conn_id); |
---|
| 268 | |
---|
| 269 | if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id)) |
---|
| 270 | { |
---|
| 271 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | return TRUE; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | // -------------------------------------------------------------------- |
---|
| 278 | |
---|
| 279 | /** |
---|
| 280 | * Rollback Transaction |
---|
| 281 | * |
---|
| 282 | * @access public |
---|
| 283 | * @return bool |
---|
| 284 | */ |
---|
| 285 | function trans_rollback() |
---|
| 286 | { |
---|
| 287 | if ( ! $this->trans_enabled) |
---|
| 288 | { |
---|
| 289 | return TRUE; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | // When transactions are nested we only begin/commit/rollback the outermost ones |
---|
| 293 | if ($this->_trans_depth > 0) |
---|
| 294 | { |
---|
| 295 | return TRUE; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | cubrid_rollback($this->conn_id); |
---|
| 299 | |
---|
| 300 | if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id)) |
---|
| 301 | { |
---|
| 302 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | return TRUE; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | // -------------------------------------------------------------------- |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | * Escape String |
---|
| 312 | * |
---|
| 313 | * @access public |
---|
| 314 | * @param string |
---|
| 315 | * @param bool whether or not the string will be used in a LIKE condition |
---|
| 316 | * @return string |
---|
| 317 | */ |
---|
| 318 | function escape_str($str, $like = FALSE) |
---|
| 319 | { |
---|
| 320 | if (is_array($str)) |
---|
| 321 | { |
---|
| 322 | foreach ($str as $key => $val) |
---|
| 323 | { |
---|
| 324 | $str[$key] = $this->escape_str($val, $like); |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | return $str; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id)) |
---|
| 331 | { |
---|
| 332 | $str = cubrid_real_escape_string($str, $this->conn_id); |
---|
| 333 | } |
---|
| 334 | else |
---|
| 335 | { |
---|
| 336 | $str = addslashes($str); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | // escape LIKE condition wildcards |
---|
| 340 | if ($like === TRUE) |
---|
| 341 | { |
---|
| 342 | $str = str_replace(array('%', '_'), array('\\%', '\\_'), $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 | { |
---|
| 358 | return @cubrid_affected_rows($this->conn_id); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | // -------------------------------------------------------------------- |
---|
| 362 | |
---|
| 363 | /** |
---|
| 364 | * Insert ID |
---|
| 365 | * |
---|
| 366 | * @access public |
---|
| 367 | * @return integer |
---|
| 368 | */ |
---|
| 369 | function insert_id() |
---|
| 370 | { |
---|
| 371 | return @cubrid_insert_id($this->conn_id); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | // -------------------------------------------------------------------- |
---|
| 375 | |
---|
| 376 | /** |
---|
| 377 | * "Count All" query |
---|
| 378 | * |
---|
| 379 | * Generates a platform-specific query string that counts all records in |
---|
| 380 | * the specified table |
---|
| 381 | * |
---|
| 382 | * @access public |
---|
| 383 | * @param string |
---|
| 384 | * @return string |
---|
| 385 | */ |
---|
| 386 | function count_all($table = '') |
---|
| 387 | { |
---|
| 388 | if ($table == '') |
---|
| 389 | { |
---|
| 390 | return 0; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
---|
| 394 | |
---|
| 395 | if ($query->num_rows() == 0) |
---|
| 396 | { |
---|
| 397 | return 0; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | $row = $query->row(); |
---|
| 401 | $this->_reset_select(); |
---|
| 402 | return (int) $row->numrows; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | // -------------------------------------------------------------------- |
---|
| 406 | |
---|
| 407 | /** |
---|
| 408 | * List table query |
---|
| 409 | * |
---|
| 410 | * Generates a platform-specific query string so that the table names can be fetched |
---|
| 411 | * |
---|
| 412 | * @access private |
---|
| 413 | * @param boolean |
---|
| 414 | * @return string |
---|
| 415 | */ |
---|
| 416 | function _list_tables($prefix_limit = FALSE) |
---|
| 417 | { |
---|
| 418 | $sql = "SHOW TABLES"; |
---|
| 419 | |
---|
| 420 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
---|
| 421 | { |
---|
| 422 | $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | return $sql; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | // -------------------------------------------------------------------- |
---|
| 429 | |
---|
| 430 | /** |
---|
| 431 | * Show column query |
---|
| 432 | * |
---|
| 433 | * Generates a platform-specific query string so that the column names can be fetched |
---|
| 434 | * |
---|
| 435 | * @access public |
---|
| 436 | * @param string the table name |
---|
| 437 | * @return string |
---|
| 438 | */ |
---|
| 439 | function _list_columns($table = '') |
---|
| 440 | { |
---|
| 441 | return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | // -------------------------------------------------------------------- |
---|
| 445 | |
---|
| 446 | /** |
---|
| 447 | * Field data query |
---|
| 448 | * |
---|
| 449 | * Generates a platform-specific query so that the column data can be retrieved |
---|
| 450 | * |
---|
| 451 | * @access public |
---|
| 452 | * @param string the table name |
---|
| 453 | * @return object |
---|
| 454 | */ |
---|
| 455 | function _field_data($table) |
---|
| 456 | { |
---|
| 457 | return "SELECT * FROM ".$table." LIMIT 1"; |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | // -------------------------------------------------------------------- |
---|
| 461 | |
---|
| 462 | /** |
---|
| 463 | * The error message string |
---|
| 464 | * |
---|
| 465 | * @access private |
---|
| 466 | * @return string |
---|
| 467 | */ |
---|
| 468 | function _error_message() |
---|
| 469 | { |
---|
| 470 | return cubrid_error($this->conn_id); |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | // -------------------------------------------------------------------- |
---|
| 474 | |
---|
| 475 | /** |
---|
| 476 | * The error message number |
---|
| 477 | * |
---|
| 478 | * @access private |
---|
| 479 | * @return integer |
---|
| 480 | */ |
---|
| 481 | function _error_number() |
---|
| 482 | { |
---|
| 483 | return cubrid_errno($this->conn_id); |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | // -------------------------------------------------------------------- |
---|
| 487 | |
---|
| 488 | /** |
---|
| 489 | * Escape the SQL Identifiers |
---|
| 490 | * |
---|
| 491 | * This function escapes column and table names |
---|
| 492 | * |
---|
| 493 | * @access private |
---|
| 494 | * @param string |
---|
| 495 | * @return string |
---|
| 496 | */ |
---|
| 497 | function _escape_identifiers($item) |
---|
| 498 | { |
---|
| 499 | if ($this->_escape_char == '') |
---|
| 500 | { |
---|
| 501 | return $item; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | foreach ($this->_reserved_identifiers as $id) |
---|
| 505 | { |
---|
| 506 | if (strpos($item, '.'.$id) !== FALSE) |
---|
| 507 | { |
---|
| 508 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
---|
| 509 | |
---|
| 510 | // remove duplicates if the user already included the escape |
---|
| 511 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | if (strpos($item, '.') !== FALSE) |
---|
| 516 | { |
---|
| 517 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
---|
| 518 | } |
---|
| 519 | else |
---|
| 520 | { |
---|
| 521 | $str = $this->_escape_char.$item.$this->_escape_char; |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | // remove duplicates if the user already included the escape |
---|
| 525 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | // -------------------------------------------------------------------- |
---|
| 529 | |
---|
| 530 | /** |
---|
| 531 | * From Tables |
---|
| 532 | * |
---|
| 533 | * This function implicitly groups FROM tables so there is no confusion |
---|
| 534 | * about operator precedence in harmony with SQL standards |
---|
| 535 | * |
---|
| 536 | * @access public |
---|
| 537 | * @param type |
---|
| 538 | * @return type |
---|
| 539 | */ |
---|
| 540 | function _from_tables($tables) |
---|
| 541 | { |
---|
| 542 | if ( ! is_array($tables)) |
---|
| 543 | { |
---|
| 544 | $tables = array($tables); |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | return '('.implode(', ', $tables).')'; |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | // -------------------------------------------------------------------- |
---|
| 551 | |
---|
| 552 | /** |
---|
| 553 | * Insert statement |
---|
| 554 | * |
---|
| 555 | * Generates a platform-specific insert string from the supplied data |
---|
| 556 | * |
---|
| 557 | * @access public |
---|
| 558 | * @param string the table name |
---|
| 559 | * @param array the insert keys |
---|
| 560 | * @param array the insert values |
---|
| 561 | * @return string |
---|
| 562 | */ |
---|
| 563 | function _insert($table, $keys, $values) |
---|
| 564 | { |
---|
| 565 | return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | // -------------------------------------------------------------------- |
---|
| 569 | |
---|
| 570 | |
---|
| 571 | /** |
---|
| 572 | * Replace statement |
---|
| 573 | * |
---|
| 574 | * Generates a platform-specific replace string from the supplied data |
---|
| 575 | * |
---|
| 576 | * @access public |
---|
| 577 | * @param string the table name |
---|
| 578 | * @param array the insert keys |
---|
| 579 | * @param array the insert values |
---|
| 580 | * @return string |
---|
| 581 | */ |
---|
| 582 | function _replace($table, $keys, $values) |
---|
| 583 | { |
---|
| 584 | return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | // -------------------------------------------------------------------- |
---|
| 588 | |
---|
| 589 | /** |
---|
| 590 | * Insert_batch statement |
---|
| 591 | * |
---|
| 592 | * Generates a platform-specific insert string from the supplied data |
---|
| 593 | * |
---|
| 594 | * @access public |
---|
| 595 | * @param string the table name |
---|
| 596 | * @param array the insert keys |
---|
| 597 | * @param array the insert values |
---|
| 598 | * @return string |
---|
| 599 | */ |
---|
| 600 | function _insert_batch($table, $keys, $values) |
---|
| 601 | { |
---|
| 602 | return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | // -------------------------------------------------------------------- |
---|
| 606 | |
---|
| 607 | |
---|
| 608 | /** |
---|
| 609 | * Update statement |
---|
| 610 | * |
---|
| 611 | * Generates a platform-specific update string from the supplied data |
---|
| 612 | * |
---|
| 613 | * @access public |
---|
| 614 | * @param string the table name |
---|
| 615 | * @param array the update data |
---|
| 616 | * @param array the where clause |
---|
| 617 | * @param array the orderby clause |
---|
| 618 | * @param array the limit clause |
---|
| 619 | * @return string |
---|
| 620 | */ |
---|
| 621 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
---|
| 622 | { |
---|
| 623 | foreach ($values as $key => $val) |
---|
| 624 | { |
---|
| 625 | $valstr[] = sprintf('"%s" = %s', $key, $val); |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
---|
| 629 | |
---|
| 630 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
---|
| 631 | |
---|
| 632 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
---|
| 633 | |
---|
| 634 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
---|
| 635 | |
---|
| 636 | $sql .= $orderby.$limit; |
---|
| 637 | |
---|
| 638 | return $sql; |
---|
| 639 | } |
---|
| 640 | |
---|
| 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 | } |
---|
| 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 "TRUNCATE ".$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 | { |
---|
| 762 | if ($offset == 0) |
---|
| 763 | { |
---|
| 764 | $offset = ''; |
---|
| 765 | } |
---|
| 766 | else |
---|
| 767 | { |
---|
| 768 | $offset .= ", "; |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | return $sql."LIMIT ".$offset.$limit; |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | // -------------------------------------------------------------------- |
---|
| 775 | |
---|
| 776 | /** |
---|
| 777 | * Close DB Connection |
---|
| 778 | * |
---|
| 779 | * @access public |
---|
| 780 | * @param resource |
---|
| 781 | * @return void |
---|
| 782 | */ |
---|
| 783 | function _close($conn_id) |
---|
| 784 | { |
---|
| 785 | @cubrid_close($conn_id); |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | } |
---|
| 789 | |
---|
| 790 | |
---|
| 791 | /* End of file cubrid_driver.php */ |
---|
| 792 | /* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ |
---|