Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [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 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [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/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 2.0.2 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CUBRID Database Adapter Class |
| 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 |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 40 | * @author Esen Sagynov |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 43 | class CI_DB_cubrid_driver extends CI_DB { |
| 44 | |
| 45 | // Default CUBRID Broker port. Will be used unless user |
| 46 | // explicitly specifies another one. |
| 47 | const DEFAULT_PORT = 33000; |
| 48 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 49 | var $dbdriver = 'cubrid'; |
| 50 | |
| 51 | // The character used for escaping - no need in CUBRID |
| 52 | var $_escape_char = ''; |
| 53 | |
| 54 | // clause and character used for LIKE escape sequences - not used in CUBRID |
| 55 | var $_like_escape_str = ''; |
| 56 | var $_like_escape_chr = ''; |
| 57 | |
| 58 | /** |
| 59 | * The syntax to count rows is slightly different across different |
| 60 | * database engines, so this string appears in each driver and is |
| 61 | * used for the count_all() and count_all_results() functions. |
| 62 | */ |
| 63 | var $_count_string = 'SELECT COUNT(*) AS '; |
| 64 | var $_random_keyword = ' RAND()'; // database specific random keyword |
| 65 | |
| 66 | /** |
| 67 | * Non-persistent database connection |
| 68 | * |
| 69 | * @access private called by the base class |
| 70 | * @return resource |
| 71 | */ |
| 72 | function db_connect() |
| 73 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 74 | // If no port is defined by the user, use the default value |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 75 | if ($this->port == '') |
| 76 | { |
| 77 | $this->port = self::DEFAULT_PORT; |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 78 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 79 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 80 | $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 81 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 82 | if ($conn) |
| 83 | { |
| 84 | // Check if a user wants to run queries in dry, i.e. run the |
| 85 | // queries but not commit them. |
| 86 | if (isset($this->auto_commit) && ! $this->auto_commit) |
| 87 | { |
| 88 | cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE); |
| 93 | $this->auto_commit = TRUE; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $conn; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // -------------------------------------------------------------------- |
| 101 | |
| 102 | /** |
| 103 | * Persistent database connection |
| 104 | * In CUBRID persistent DB connection is supported natively in CUBRID |
| 105 | * engine which can be configured in the CUBRID Broker configuration |
| 106 | * file by setting the CCI_PCONNECT parameter to ON. In that case, all |
| 107 | * connections established between the client application and the |
| 108 | * server will become persistent. This is calling the same |
| 109 | * @cubrid_connect function will establish persisten connection |
| 110 | * considering that the CCI_PCONNECT is ON. |
| 111 | * |
| 112 | * @access private called by the base class |
| 113 | * @return resource |
| 114 | */ |
| 115 | function db_pconnect() |
| 116 | { |
| 117 | return $this->db_connect(); |
| 118 | } |
| 119 | |
| 120 | // -------------------------------------------------------------------- |
| 121 | |
| 122 | /** |
| 123 | * Reconnect |
| 124 | * |
| 125 | * Keep / reestablish the db connection if no queries have been |
| 126 | * sent for a length of time exceeding the server's idle timeout |
| 127 | * |
| 128 | * @access public |
| 129 | * @return void |
| 130 | */ |
| 131 | function reconnect() |
| 132 | { |
| 133 | if (cubrid_ping($this->conn_id) === FALSE) |
| 134 | { |
| 135 | $this->conn_id = FALSE; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // -------------------------------------------------------------------- |
| 140 | |
| 141 | /** |
| 142 | * Select the database |
| 143 | * |
| 144 | * @access private called by the base class |
| 145 | * @return resource |
| 146 | */ |
| 147 | function db_select() |
| 148 | { |
| 149 | // In CUBRID there is no need to select a database as the database |
| 150 | // is chosen at the connection time. |
| 151 | // So, to determine if the database is "selected", all we have to |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 152 | // do is ping the server and return that value. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 153 | return cubrid_ping($this->conn_id); |
| 154 | } |
| 155 | |
| 156 | // -------------------------------------------------------------------- |
| 157 | |
| 158 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 159 | * Database version number |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 160 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 161 | * @return string |
| 162 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 163 | public function version() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 164 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 165 | return isset($this->data_cache['version']) |
| 166 | ? $this->data_cache['version'] |
| 167 | : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
| 172 | /** |
| 173 | * Execute the query |
| 174 | * |
| 175 | * @access private called by the base class |
| 176 | * @param string an SQL query |
| 177 | * @return resource |
| 178 | */ |
| 179 | function _execute($sql) |
| 180 | { |
| 181 | $sql = $this->_prep_query($sql); |
| 182 | return @cubrid_query($sql, $this->conn_id); |
| 183 | } |
| 184 | |
| 185 | // -------------------------------------------------------------------- |
| 186 | |
| 187 | /** |
| 188 | * Prep the query |
| 189 | * |
| 190 | * If needed, each database adapter can prep the query string |
| 191 | * |
| 192 | * @access private called by execute() |
| 193 | * @param string an SQL query |
| 194 | * @return string |
| 195 | */ |
| 196 | function _prep_query($sql) |
| 197 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 198 | // No need to prepare |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 199 | return $sql; |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Begin Transaction |
| 206 | * |
| 207 | * @access public |
| 208 | * @return bool |
| 209 | */ |
| 210 | function trans_begin($test_mode = FALSE) |
| 211 | { |
| 212 | if ( ! $this->trans_enabled) |
| 213 | { |
| 214 | return TRUE; |
| 215 | } |
| 216 | |
| 217 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 218 | if ($this->_trans_depth > 0) |
| 219 | { |
| 220 | return TRUE; |
| 221 | } |
| 222 | |
| 223 | // Reset the transaction failure flag. |
| 224 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 225 | // even if the queries produce a successful result. |
| 226 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 227 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 228 | if (cubrid_get_autocommit($this->conn_id)) |
| 229 | { |
| 230 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE); |
| 231 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 232 | |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Commit Transaction |
| 240 | * |
| 241 | * @access public |
| 242 | * @return bool |
| 243 | */ |
| 244 | function trans_commit() |
| 245 | { |
| 246 | if ( ! $this->trans_enabled) |
| 247 | { |
| 248 | return TRUE; |
| 249 | } |
| 250 | |
| 251 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 252 | if ($this->_trans_depth > 0) |
| 253 | { |
| 254 | return TRUE; |
| 255 | } |
| 256 | |
| 257 | cubrid_commit($this->conn_id); |
| 258 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 259 | if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id)) |
| 260 | { |
| 261 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE); |
| 262 | } |
| 263 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 264 | return TRUE; |
| 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
| 270 | * Rollback Transaction |
| 271 | * |
| 272 | * @access public |
| 273 | * @return bool |
| 274 | */ |
| 275 | function trans_rollback() |
| 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 | |
| 288 | cubrid_rollback($this->conn_id); |
| 289 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 290 | if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id)) |
| 291 | { |
| 292 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE); |
| 293 | } |
| 294 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 295 | return TRUE; |
| 296 | } |
| 297 | |
| 298 | // -------------------------------------------------------------------- |
| 299 | |
| 300 | /** |
| 301 | * Escape String |
| 302 | * |
| 303 | * @access public |
| 304 | * @param string |
| 305 | * @param bool whether or not the string will be used in a LIKE condition |
| 306 | * @return string |
| 307 | */ |
| 308 | function escape_str($str, $like = FALSE) |
| 309 | { |
| 310 | if (is_array($str)) |
| 311 | { |
| 312 | foreach ($str as $key => $val) |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 313 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 314 | $str[$key] = $this->escape_str($val, $like); |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 315 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 316 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 317 | return $str; |
| 318 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 319 | |
| 320 | if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id)) |
| 321 | { |
| 322 | $str = cubrid_real_escape_string($str, $this->conn_id); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | $str = addslashes($str); |
| 327 | } |
| 328 | |
| 329 | // escape LIKE condition wildcards |
| 330 | if ($like === TRUE) |
| 331 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 332 | $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); |
| 333 | } |
| 334 | |
| 335 | return $str; |
| 336 | } |
| 337 | |
| 338 | // -------------------------------------------------------------------- |
| 339 | |
| 340 | /** |
| 341 | * Affected Rows |
| 342 | * |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 343 | * @return int |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 344 | */ |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 345 | public function affected_rows() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 346 | { |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 347 | return @cubrid_affected_rows(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // -------------------------------------------------------------------- |
| 351 | |
| 352 | /** |
| 353 | * Insert ID |
| 354 | * |
| 355 | * @access public |
| 356 | * @return integer |
| 357 | */ |
| 358 | function insert_id() |
| 359 | { |
| 360 | return @cubrid_insert_id($this->conn_id); |
| 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
| 366 | * "Count All" query |
| 367 | * |
| 368 | * Generates a platform-specific query string that counts all records in |
| 369 | * the specified table |
| 370 | * |
| 371 | * @access public |
| 372 | * @param string |
| 373 | * @return string |
| 374 | */ |
| 375 | function count_all($table = '') |
| 376 | { |
| 377 | if ($table == '') |
| 378 | { |
| 379 | return 0; |
| 380 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 381 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame^] | 382 | $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 383 | if ($query->num_rows() == 0) |
| 384 | { |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 389 | $this->_reset_select(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 390 | return (int) $row->numrows; |
| 391 | } |
| 392 | |
| 393 | // -------------------------------------------------------------------- |
| 394 | |
| 395 | /** |
| 396 | * List table query |
| 397 | * |
| 398 | * Generates a platform-specific query string so that the table names can be fetched |
| 399 | * |
| 400 | * @access private |
| 401 | * @param boolean |
| 402 | * @return string |
| 403 | */ |
| 404 | function _list_tables($prefix_limit = FALSE) |
| 405 | { |
| 406 | $sql = "SHOW TABLES"; |
| 407 | |
| 408 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 409 | { |
| 410 | $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
| 411 | } |
| 412 | |
| 413 | return $sql; |
| 414 | } |
| 415 | |
| 416 | // -------------------------------------------------------------------- |
| 417 | |
| 418 | /** |
| 419 | * Show column query |
| 420 | * |
| 421 | * Generates a platform-specific query string so that the column names can be fetched |
| 422 | * |
| 423 | * @access public |
| 424 | * @param string the table name |
| 425 | * @return string |
| 426 | */ |
| 427 | function _list_columns($table = '') |
| 428 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame^] | 429 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // -------------------------------------------------------------------- |
| 433 | |
| 434 | /** |
| 435 | * Field data query |
| 436 | * |
| 437 | * Generates a platform-specific query so that the column data can be retrieved |
| 438 | * |
| 439 | * @access public |
| 440 | * @param string the table name |
| 441 | * @return object |
| 442 | */ |
| 443 | function _field_data($table) |
| 444 | { |
| 445 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 446 | } |
| 447 | |
| 448 | // -------------------------------------------------------------------- |
| 449 | |
| 450 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 451 | * Error |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 452 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 453 | * Returns an array containing code and message of the last |
| 454 | * database error that has occured. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 455 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 456 | * @return array |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 457 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 458 | public function error() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 459 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 460 | return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id)); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 463 | /** |
| 464 | * Escape the SQL Identifiers |
| 465 | * |
| 466 | * This function escapes column and table names |
| 467 | * |
| 468 | * @access private |
| 469 | * @param string |
| 470 | * @return string |
| 471 | */ |
| 472 | function _escape_identifiers($item) |
| 473 | { |
| 474 | if ($this->_escape_char == '') |
| 475 | { |
| 476 | return $item; |
| 477 | } |
| 478 | |
| 479 | foreach ($this->_reserved_identifiers as $id) |
| 480 | { |
| 481 | if (strpos($item, '.'.$id) !== FALSE) |
| 482 | { |
| 483 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 484 | |
| 485 | // remove duplicates if the user already included the escape |
| 486 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (strpos($item, '.') !== FALSE) |
| 491 | { |
| 492 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 497 | } |
| 498 | |
| 499 | // remove duplicates if the user already included the escape |
| 500 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 501 | } |
| 502 | |
| 503 | // -------------------------------------------------------------------- |
| 504 | |
| 505 | /** |
| 506 | * From Tables |
| 507 | * |
| 508 | * This function implicitly groups FROM tables so there is no confusion |
| 509 | * about operator precedence in harmony with SQL standards |
| 510 | * |
| 511 | * @access public |
| 512 | * @param type |
| 513 | * @return type |
| 514 | */ |
| 515 | function _from_tables($tables) |
| 516 | { |
| 517 | if ( ! is_array($tables)) |
| 518 | { |
| 519 | $tables = array($tables); |
| 520 | } |
| 521 | |
| 522 | return '('.implode(', ', $tables).')'; |
| 523 | } |
| 524 | |
| 525 | // -------------------------------------------------------------------- |
| 526 | |
| 527 | /** |
| 528 | * Insert statement |
| 529 | * |
| 530 | * Generates a platform-specific insert string from the supplied data |
| 531 | * |
| 532 | * @access public |
| 533 | * @param string the table name |
| 534 | * @param array the insert keys |
| 535 | * @param array the insert values |
| 536 | * @return string |
| 537 | */ |
| 538 | function _insert($table, $keys, $values) |
| 539 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 540 | return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // -------------------------------------------------------------------- |
| 544 | |
| 545 | |
| 546 | /** |
| 547 | * Replace statement |
| 548 | * |
| 549 | * Generates a platform-specific replace string from the supplied data |
| 550 | * |
| 551 | * @access public |
| 552 | * @param string the table name |
| 553 | * @param array the insert keys |
| 554 | * @param array the insert values |
| 555 | * @return string |
| 556 | */ |
| 557 | function _replace($table, $keys, $values) |
| 558 | { |
Esen Sagynov | ee3e594 | 2011-08-10 03:22:58 -0700 | [diff] [blame] | 559 | return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | // -------------------------------------------------------------------- |
| 563 | |
| 564 | /** |
| 565 | * Insert_batch statement |
| 566 | * |
| 567 | * Generates a platform-specific insert string from the supplied data |
| 568 | * |
| 569 | * @access public |
| 570 | * @param string the table name |
| 571 | * @param array the insert keys |
| 572 | * @param array the insert values |
| 573 | * @return string |
| 574 | */ |
| 575 | function _insert_batch($table, $keys, $values) |
| 576 | { |
Esen Sagynov | ee3e594 | 2011-08-10 03:22:58 -0700 | [diff] [blame] | 577 | return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // -------------------------------------------------------------------- |
| 581 | |
| 582 | |
| 583 | /** |
| 584 | * Update statement |
| 585 | * |
| 586 | * Generates a platform-specific update string from the supplied data |
| 587 | * |
| 588 | * @access public |
| 589 | * @param string the table name |
| 590 | * @param array the update data |
| 591 | * @param array the where clause |
| 592 | * @param array the orderby clause |
| 593 | * @param array the limit clause |
| 594 | * @return string |
| 595 | */ |
| 596 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 597 | { |
| 598 | foreach ($values as $key => $val) |
| 599 | { |
Esen Sagynov | ee3e594 | 2011-08-10 03:22:58 -0700 | [diff] [blame] | 600 | $valstr[] = sprintf('"%s" = %s', $key, $val); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 604 | |
| 605 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
| 606 | |
| 607 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 608 | |
| 609 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 610 | |
| 611 | $sql .= $orderby.$limit; |
| 612 | |
| 613 | return $sql; |
| 614 | } |
| 615 | |
| 616 | // -------------------------------------------------------------------- |
| 617 | |
| 618 | |
| 619 | /** |
| 620 | * Update_Batch statement |
| 621 | * |
| 622 | * Generates a platform-specific batch 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 | * @return string |
| 629 | */ |
| 630 | function _update_batch($table, $values, $index, $where = NULL) |
| 631 | { |
| 632 | $ids = array(); |
| 633 | $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
| 634 | |
| 635 | foreach ($values as $key => $val) |
| 636 | { |
| 637 | $ids[] = $val[$index]; |
| 638 | |
| 639 | foreach (array_keys($val) as $field) |
| 640 | { |
| 641 | if ($field != $index) |
| 642 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 643 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | $sql = "UPDATE ".$table." SET "; |
| 649 | $cases = ''; |
| 650 | |
| 651 | foreach ($final as $k => $v) |
| 652 | { |
| 653 | $cases .= $k.' = CASE '."\n"; |
| 654 | foreach ($v as $row) |
| 655 | { |
| 656 | $cases .= $row."\n"; |
| 657 | } |
| 658 | |
| 659 | $cases .= 'ELSE '.$k.' END, '; |
| 660 | } |
| 661 | |
| 662 | $sql .= substr($cases, 0, -2); |
| 663 | |
| 664 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 665 | |
| 666 | return $sql; |
| 667 | } |
| 668 | |
| 669 | // -------------------------------------------------------------------- |
| 670 | |
| 671 | |
| 672 | /** |
| 673 | * Truncate statement |
| 674 | * |
| 675 | * Generates a platform-specific truncate string from the supplied data |
| 676 | * If the database does not support the truncate() command |
| 677 | * This function maps to "DELETE FROM table" |
| 678 | * |
| 679 | * @access public |
| 680 | * @param string the table name |
| 681 | * @return string |
| 682 | */ |
| 683 | function _truncate($table) |
| 684 | { |
| 685 | return "TRUNCATE ".$table; |
| 686 | } |
| 687 | |
| 688 | // -------------------------------------------------------------------- |
| 689 | |
| 690 | /** |
| 691 | * Delete statement |
| 692 | * |
| 693 | * Generates a platform-specific delete string from the supplied data |
| 694 | * |
| 695 | * @access public |
| 696 | * @param string the table name |
| 697 | * @param array the where clause |
| 698 | * @param string the limit clause |
| 699 | * @return string |
| 700 | */ |
| 701 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 702 | { |
| 703 | $conditions = ''; |
| 704 | |
| 705 | if (count($where) > 0 OR count($like) > 0) |
| 706 | { |
| 707 | $conditions = "\nWHERE "; |
| 708 | $conditions .= implode("\n", $this->ar_where); |
| 709 | |
| 710 | if (count($where) > 0 && count($like) > 0) |
| 711 | { |
| 712 | $conditions .= " AND "; |
| 713 | } |
| 714 | $conditions .= implode("\n", $like); |
| 715 | } |
| 716 | |
| 717 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 718 | |
| 719 | return "DELETE FROM ".$table.$conditions.$limit; |
| 720 | } |
| 721 | |
| 722 | // -------------------------------------------------------------------- |
| 723 | |
| 724 | /** |
| 725 | * Limit string |
| 726 | * |
| 727 | * Generates a platform-specific LIMIT clause |
| 728 | * |
| 729 | * @access public |
| 730 | * @param string the sql query string |
| 731 | * @param integer the number of rows to limit the query to |
| 732 | * @param integer the offset value |
| 733 | * @return string |
| 734 | */ |
| 735 | function _limit($sql, $limit, $offset) |
| 736 | { |
| 737 | if ($offset == 0) |
| 738 | { |
| 739 | $offset = ''; |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | $offset .= ", "; |
| 744 | } |
| 745 | |
| 746 | return $sql."LIMIT ".$offset.$limit; |
| 747 | } |
| 748 | |
| 749 | // -------------------------------------------------------------------- |
| 750 | |
| 751 | /** |
| 752 | * Close DB Connection |
| 753 | * |
| 754 | * @access public |
| 755 | * @param resource |
| 756 | * @return void |
| 757 | */ |
| 758 | function _close($conn_id) |
| 759 | { |
| 760 | @cubrid_close($conn_id); |
| 761 | } |
| 762 | |
| 763 | } |
| 764 | |
| 765 | |
| 766 | /* End of file cubrid_driver.php */ |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 767 | /* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ |