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 | |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame^] | 60 | // CUBRID-specific properties |
| 61 | public $auto_commit = TRUE; |
| 62 | |
| 63 | public function __construct($params) |
| 64 | { |
| 65 | parent::__construct($params); |
| 66 | |
| 67 | if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) |
| 68 | { |
| 69 | if (stripos($matches[2], 'autocommit=off') !== FALSE) |
| 70 | { |
| 71 | $this->auto_commit = FALSE; |
| 72 | } |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | // If no port is defined by the user, use the default value |
| 77 | $this->port == '' OR $this->port = 33000; |
| 78 | } |
| 79 | } |
| 80 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Non-persistent database connection |
| 83 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 84 | * @return resource |
| 85 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 86 | public function db_connect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 87 | { |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [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 | 592f4ec | 2012-03-20 15:10:08 +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 | a00e504 | 2012-03-26 12:23:13 +0300 | [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 | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 104 | public function db_pconnect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 105 | { |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [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 | { |
| 125 | $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url'; |
| 126 | $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '') |
| 127 | ? $_temp($this->dsn, $this->username, $this->password) |
| 128 | : $_temp($this->dsn); |
| 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 | |
| 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 | 592f4ec | 2012-03-20 15:10:08 +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 | 592f4ec | 2012-03-20 15:10:08 +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 | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 197 | protected function _execute($sql) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 198 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 199 | return @cubrid_query($sql, $this->conn_id); |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 205 | * Begin Transaction |
| 206 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 207 | * @return bool |
| 208 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 209 | public function trans_begin($test_mode = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 210 | { |
| 211 | if ( ! $this->trans_enabled) |
| 212 | { |
| 213 | return TRUE; |
| 214 | } |
| 215 | |
| 216 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 217 | if ($this->_trans_depth > 0) |
| 218 | { |
| 219 | return TRUE; |
| 220 | } |
| 221 | |
| 222 | // Reset the transaction failure flag. |
| 223 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 224 | // even if the queries produce a successful result. |
| 225 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 226 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 227 | if (cubrid_get_autocommit($this->conn_id)) |
| 228 | { |
| 229 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE); |
| 230 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 231 | |
| 232 | return TRUE; |
| 233 | } |
| 234 | |
| 235 | // -------------------------------------------------------------------- |
| 236 | |
| 237 | /** |
| 238 | * Commit Transaction |
| 239 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 240 | * @return bool |
| 241 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 242 | public function trans_commit() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 243 | { |
| 244 | if ( ! $this->trans_enabled) |
| 245 | { |
| 246 | return TRUE; |
| 247 | } |
| 248 | |
| 249 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 250 | if ($this->_trans_depth > 0) |
| 251 | { |
| 252 | return TRUE; |
| 253 | } |
| 254 | |
| 255 | cubrid_commit($this->conn_id); |
| 256 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 257 | if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id)) |
| 258 | { |
| 259 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE); |
| 260 | } |
| 261 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 262 | return TRUE; |
| 263 | } |
| 264 | |
| 265 | // -------------------------------------------------------------------- |
| 266 | |
| 267 | /** |
| 268 | * Rollback Transaction |
| 269 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 270 | * @return bool |
| 271 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 272 | public function trans_rollback() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 273 | { |
| 274 | if ( ! $this->trans_enabled) |
| 275 | { |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
| 279 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 280 | if ($this->_trans_depth > 0) |
| 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 | 592f4ec | 2012-03-20 15:10:08 +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 | |
| 316 | if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id)) |
| 317 | { |
| 318 | $str = cubrid_real_escape_string($str, $this->conn_id); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | $str = addslashes($str); |
| 323 | } |
| 324 | |
| 325 | // escape LIKE condition wildcards |
| 326 | if ($like === TRUE) |
| 327 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 328 | $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); |
| 329 | } |
| 330 | |
| 331 | return $str; |
| 332 | } |
| 333 | |
| 334 | // -------------------------------------------------------------------- |
| 335 | |
| 336 | /** |
| 337 | * Affected Rows |
| 338 | * |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 339 | * @return int |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 340 | */ |
Timothy Warren | 175e289 | 2012-03-19 19:08:39 -0400 | [diff] [blame] | 341 | public function affected_rows() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 342 | { |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 343 | return @cubrid_affected_rows(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // -------------------------------------------------------------------- |
| 347 | |
| 348 | /** |
| 349 | * Insert ID |
| 350 | * |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 351 | * @return int |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 352 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 353 | public function insert_id() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 354 | { |
| 355 | return @cubrid_insert_id($this->conn_id); |
| 356 | } |
| 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * "Count All" query |
| 362 | * |
| 363 | * Generates a platform-specific query string that counts all records in |
| 364 | * the specified table |
| 365 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 366 | * @param string |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 367 | * @return int |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 368 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 369 | public function count_all($table = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 370 | { |
| 371 | if ($table == '') |
| 372 | { |
| 373 | return 0; |
| 374 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 375 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 376 | $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] | 377 | if ($query->num_rows() == 0) |
| 378 | { |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 383 | $this->_reset_select(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 384 | return (int) $row->numrows; |
| 385 | } |
| 386 | |
| 387 | // -------------------------------------------------------------------- |
| 388 | |
| 389 | /** |
| 390 | * List table query |
| 391 | * |
| 392 | * Generates a platform-specific query string so that the table names can be fetched |
| 393 | * |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 394 | * @param bool |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 395 | * @return string |
| 396 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 397 | protected function _list_tables($prefix_limit = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 398 | { |
| 399 | $sql = "SHOW TABLES"; |
| 400 | |
| 401 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 402 | { |
| 403 | $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
| 404 | } |
| 405 | |
| 406 | return $sql; |
| 407 | } |
| 408 | |
| 409 | // -------------------------------------------------------------------- |
| 410 | |
| 411 | /** |
| 412 | * Show column query |
| 413 | * |
| 414 | * Generates a platform-specific query string so that the column names can be fetched |
| 415 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 416 | * @param string the table name |
| 417 | * @return string |
| 418 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 419 | protected function _list_columns($table = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 420 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 421 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // -------------------------------------------------------------------- |
| 425 | |
| 426 | /** |
| 427 | * Field data query |
| 428 | * |
| 429 | * Generates a platform-specific query so that the column data can be retrieved |
| 430 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 431 | * @param string the table name |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 432 | * @return string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 433 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 434 | protected function _field_data($table) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 435 | { |
| 436 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 437 | } |
| 438 | |
| 439 | // -------------------------------------------------------------------- |
| 440 | |
| 441 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 442 | * Error |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 443 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 444 | * Returns an array containing code and message of the last |
| 445 | * database error that has occured. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 446 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 447 | * @return array |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 448 | */ |
Timothy Warren | 175e289 | 2012-03-19 19:08:39 -0400 | [diff] [blame] | 449 | public function error() |
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 | 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] | 452 | } |
| 453 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 454 | /** |
| 455 | * Escape the SQL Identifiers |
| 456 | * |
Timothy Warren | 175e289 | 2012-03-19 19:08:39 -0400 | [diff] [blame] | 457 | * This function escapes column and table names |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 458 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 459 | * @param string |
| 460 | * @return string |
| 461 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 462 | public function _escape_identifiers($item) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 463 | { |
| 464 | if ($this->_escape_char == '') |
| 465 | { |
| 466 | return $item; |
| 467 | } |
| 468 | |
| 469 | foreach ($this->_reserved_identifiers as $id) |
| 470 | { |
| 471 | if (strpos($item, '.'.$id) !== FALSE) |
| 472 | { |
| 473 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 474 | |
| 475 | // remove duplicates if the user already included the escape |
| 476 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if (strpos($item, '.') !== FALSE) |
| 481 | { |
| 482 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 487 | } |
| 488 | |
| 489 | // remove duplicates if the user already included the escape |
| 490 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 491 | } |
| 492 | |
| 493 | // -------------------------------------------------------------------- |
| 494 | |
| 495 | /** |
| 496 | * From Tables |
| 497 | * |
Timothy Warren | 175e289 | 2012-03-19 19:08:39 -0400 | [diff] [blame] | 498 | * This function implicitly groups FROM tables so there is no confusion |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 499 | * about operator precedence in harmony with SQL standards |
| 500 | * |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 501 | * @param array |
| 502 | * @return string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 503 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 504 | protected function _from_tables($tables) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 505 | { |
| 506 | if ( ! is_array($tables)) |
| 507 | { |
| 508 | $tables = array($tables); |
| 509 | } |
| 510 | |
| 511 | return '('.implode(', ', $tables).')'; |
| 512 | } |
| 513 | |
| 514 | // -------------------------------------------------------------------- |
| 515 | |
| 516 | /** |
| 517 | * Insert statement |
| 518 | * |
| 519 | * Generates a platform-specific insert string from the supplied data |
| 520 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 521 | * @param string the table name |
| 522 | * @param array the insert keys |
| 523 | * @param array the insert values |
| 524 | * @return string |
| 525 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 526 | protected function _insert($table, $keys, $values) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 527 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 528 | return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // -------------------------------------------------------------------- |
| 532 | |
| 533 | |
| 534 | /** |
| 535 | * Replace statement |
| 536 | * |
| 537 | * Generates a platform-specific replace string from the supplied data |
| 538 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 539 | * @param string the table name |
| 540 | * @param array the insert keys |
| 541 | * @param array the insert values |
| 542 | * @return string |
| 543 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 544 | protected function _replace($table, $keys, $values) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 545 | { |
Esen Sagynov | ee3e594 | 2011-08-10 03:22:58 -0700 | [diff] [blame] | 546 | return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | // -------------------------------------------------------------------- |
| 550 | |
| 551 | /** |
| 552 | * Insert_batch statement |
| 553 | * |
| 554 | * Generates a platform-specific insert string from the supplied data |
| 555 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 556 | * @param string the table name |
| 557 | * @param array the insert keys |
| 558 | * @param array the insert values |
| 559 | * @return string |
| 560 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 561 | protected function _insert_batch($table, $keys, $values) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 562 | { |
Esen Sagynov | ee3e594 | 2011-08-10 03:22:58 -0700 | [diff] [blame] | 563 | return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // -------------------------------------------------------------------- |
| 567 | |
| 568 | |
| 569 | /** |
| 570 | * Update statement |
| 571 | * |
| 572 | * Generates a platform-specific update string from the supplied data |
| 573 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 574 | * @param string the table name |
| 575 | * @param array the update data |
| 576 | * @param array the where clause |
| 577 | * @param array the orderby clause |
| 578 | * @param array the limit clause |
| 579 | * @return string |
| 580 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 581 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 582 | { |
| 583 | foreach ($values as $key => $val) |
| 584 | { |
Esen Sagynov | ee3e594 | 2011-08-10 03:22:58 -0700 | [diff] [blame] | 585 | $valstr[] = sprintf('"%s" = %s', $key, $val); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 589 | |
| 590 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
| 591 | |
| 592 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 593 | |
| 594 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 595 | |
| 596 | $sql .= $orderby.$limit; |
| 597 | |
| 598 | return $sql; |
| 599 | } |
| 600 | |
| 601 | // -------------------------------------------------------------------- |
| 602 | |
| 603 | |
| 604 | /** |
| 605 | * Update_Batch statement |
| 606 | * |
| 607 | * Generates a platform-specific batch update string from the supplied data |
| 608 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 609 | * @param string the table name |
| 610 | * @param array the update data |
| 611 | * @param array the where clause |
| 612 | * @return string |
| 613 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 614 | protected function _update_batch($table, $values, $index, $where = NULL) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 615 | { |
| 616 | $ids = array(); |
| 617 | $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
| 618 | |
| 619 | foreach ($values as $key => $val) |
| 620 | { |
| 621 | $ids[] = $val[$index]; |
| 622 | |
| 623 | foreach (array_keys($val) as $field) |
| 624 | { |
| 625 | if ($field != $index) |
| 626 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 627 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | $sql = "UPDATE ".$table." SET "; |
| 633 | $cases = ''; |
| 634 | |
| 635 | foreach ($final as $k => $v) |
| 636 | { |
| 637 | $cases .= $k.' = CASE '."\n"; |
| 638 | foreach ($v as $row) |
| 639 | { |
| 640 | $cases .= $row."\n"; |
| 641 | } |
| 642 | |
| 643 | $cases .= 'ELSE '.$k.' END, '; |
| 644 | } |
| 645 | |
| 646 | $sql .= substr($cases, 0, -2); |
| 647 | |
| 648 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 649 | |
| 650 | return $sql; |
| 651 | } |
| 652 | |
| 653 | // -------------------------------------------------------------------- |
| 654 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 655 | /** |
| 656 | * Truncate statement |
| 657 | * |
| 658 | * Generates a platform-specific truncate string from the supplied data |
| 659 | * If the database does not support the truncate() command |
Timothy Warren | 175e289 | 2012-03-19 19:08:39 -0400 | [diff] [blame] | 660 | * This function maps to "DELETE FROM table" |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 661 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 662 | * @param string the table name |
| 663 | * @return string |
| 664 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 665 | protected function _truncate($table) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 666 | { |
| 667 | return "TRUNCATE ".$table; |
| 668 | } |
| 669 | |
| 670 | // -------------------------------------------------------------------- |
| 671 | |
| 672 | /** |
| 673 | * Delete statement |
| 674 | * |
| 675 | * Generates a platform-specific delete string from the supplied data |
| 676 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 677 | * @param string the table name |
| 678 | * @param array the where clause |
| 679 | * @param string the limit clause |
| 680 | * @return string |
| 681 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 682 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 683 | { |
| 684 | $conditions = ''; |
| 685 | |
| 686 | if (count($where) > 0 OR count($like) > 0) |
| 687 | { |
| 688 | $conditions = "\nWHERE "; |
| 689 | $conditions .= implode("\n", $this->ar_where); |
| 690 | |
| 691 | if (count($where) > 0 && count($like) > 0) |
| 692 | { |
| 693 | $conditions .= " AND "; |
| 694 | } |
| 695 | $conditions .= implode("\n", $like); |
| 696 | } |
| 697 | |
| 698 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 699 | |
| 700 | return "DELETE FROM ".$table.$conditions.$limit; |
| 701 | } |
| 702 | |
| 703 | // -------------------------------------------------------------------- |
| 704 | |
| 705 | /** |
| 706 | * Limit string |
| 707 | * |
| 708 | * Generates a platform-specific LIMIT clause |
| 709 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 710 | * @param string the sql query string |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 711 | * @param int the number of rows to limit the query to |
| 712 | * @param int the offset value |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 713 | * @return string |
| 714 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 715 | protected function _limit($sql, $limit, $offset) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 716 | { |
| 717 | if ($offset == 0) |
| 718 | { |
| 719 | $offset = ''; |
| 720 | } |
| 721 | else |
| 722 | { |
| 723 | $offset .= ", "; |
| 724 | } |
| 725 | |
| 726 | return $sql."LIMIT ".$offset.$limit; |
| 727 | } |
| 728 | |
| 729 | // -------------------------------------------------------------------- |
| 730 | |
| 731 | /** |
| 732 | * Close DB Connection |
| 733 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 734 | * @param resource |
| 735 | * @return void |
| 736 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 737 | protected function _close($conn_id) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 738 | { |
| 739 | @cubrid_close($conn_id); |
| 740 | } |
| 741 | |
| 742 | } |
| 743 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 744 | /* End of file cubrid_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 745 | /* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ |