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