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