Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 |
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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 28 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 29 | /** |
| 30 | * CUBRID Database Adapter Class |
| 31 | * |
| 32 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 33 | * creates dynamically based on whether the query builder |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 34 | * class is being used or not. |
| 35 | * |
| 36 | * @package CodeIgniter |
| 37 | * @subpackage Drivers |
| 38 | * @category Database |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 39 | * @author Esen Sagynov |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | */ |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 42 | class CI_DB_cubrid_driver extends CI_DB { |
| 43 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Database driver |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 49 | public $dbdriver = 'cubrid'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 50 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 51 | /** |
| 52 | * Auto-commit flag |
| 53 | * |
| 54 | * @var bool |
| 55 | */ |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 56 | public $auto_commit = TRUE; |
| 57 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 58 | // -------------------------------------------------------------------- |
| 59 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 60 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 61 | * Identifier escape character |
| 62 | * |
| 63 | * @var string |
| 64 | */ |
| 65 | protected $_escape_char = '`'; |
| 66 | |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 67 | /** |
| 68 | * ORDER BY random keyword |
| 69 | * |
| 70 | * @var array |
| 71 | */ |
| 72 | protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)'); |
| 73 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 74 | // -------------------------------------------------------------------- |
| 75 | |
| 76 | /** |
| 77 | * Class constructor |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 78 | * |
| 79 | * @param array $params |
| 80 | * @return void |
| 81 | */ |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 82 | public function __construct($params) |
| 83 | { |
| 84 | parent::__construct($params); |
| 85 | |
| 86 | if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) |
| 87 | { |
| 88 | if (stripos($matches[2], 'autocommit=off') !== FALSE) |
| 89 | { |
| 90 | $this->auto_commit = FALSE; |
| 91 | } |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | // If no port is defined by the user, use the default value |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 96 | empty($this->port) OR $this->port = 33000; |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 97 | } |
| 98 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 99 | |
Andrey Andreev | 2ea33c3 | 2012-10-04 12:37:51 +0300 | [diff] [blame] | 100 | // -------------------------------------------------------------------- |
| 101 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Non-persistent database connection |
| 104 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 105 | * @return resource |
| 106 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 107 | public function db_connect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 108 | { |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 109 | return $this->_cubrid_connect(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // -------------------------------------------------------------------- |
| 113 | |
| 114 | /** |
| 115 | * Persistent database connection |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 116 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 117 | * In CUBRID persistent DB connection is supported natively in CUBRID |
| 118 | * engine which can be configured in the CUBRID Broker configuration |
| 119 | * file by setting the CCI_PCONNECT parameter to ON. In that case, all |
| 120 | * connections established between the client application and the |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 121 | * server will become persistent. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 122 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 123 | * @return resource |
| 124 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 125 | public function db_pconnect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 126 | { |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 127 | return $this->_cubrid_connect(TRUE); |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * CUBRID connection |
| 134 | * |
| 135 | * A CUBRID-specific method to create a connection to the database. |
| 136 | * Except for determining if a persistent connection should be used, |
| 137 | * the rest of the logic is the same for db_connect() and db_pconnect(). |
| 138 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 139 | * @param bool $persistent |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 140 | * @return resource |
| 141 | */ |
| 142 | protected function _cubrid_connect($persistent = FALSE) |
| 143 | { |
| 144 | if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches)) |
| 145 | { |
| 146 | $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url'; |
| 147 | $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '') |
| 148 | ? $_temp($this->dsn, $this->username, $this->password) |
| 149 | : $_temp($this->dsn); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect'; |
| 154 | $conn_id = ($this->username !== '') |
| 155 | ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password) |
| 156 | : $_temp($this->hostname, $this->port, $this->database); |
| 157 | } |
| 158 | |
| 159 | return $conn_id; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Reconnect |
| 166 | * |
| 167 | * Keep / reestablish the db connection if no queries have been |
| 168 | * sent for a length of time exceeding the server's idle timeout |
| 169 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 170 | * @return void |
| 171 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 172 | public function reconnect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 173 | { |
| 174 | if (cubrid_ping($this->conn_id) === FALSE) |
| 175 | { |
| 176 | $this->conn_id = FALSE; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 183 | * Database version number |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 184 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 185 | * @return string |
| 186 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 187 | public function version() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 188 | { |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame] | 189 | if (isset($this->data_cache['version'])) |
| 190 | { |
| 191 | return $this->data_cache['version']; |
| 192 | } |
| 193 | elseif ( ! $this->conn_id) |
| 194 | { |
| 195 | $this->initialize(); |
| 196 | } |
| 197 | |
| 198 | return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE) |
| 199 | ? FALSE |
| 200 | : $this->data_cache['version'] = $version; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // -------------------------------------------------------------------- |
| 204 | |
| 205 | /** |
| 206 | * Execute the query |
| 207 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 208 | * @param string $sql an SQL query |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 209 | * @return resource |
| 210 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 211 | protected function _execute($sql) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 212 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 213 | return @cubrid_query($sql, $this->conn_id); |
| 214 | } |
| 215 | |
| 216 | // -------------------------------------------------------------------- |
| 217 | |
| 218 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 219 | * Begin Transaction |
| 220 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 221 | * @param bool $test_mode |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 222 | * @return bool |
| 223 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +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 | 592f4ec | 2012-03-20 15:10:08 +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 | 592f4ec | 2012-03-20 15:10:08 +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 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 300 | * @param string $str |
| 301 | * @param bool $like Whether or not the string will be used in a LIKE condition |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 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 | |
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 | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 341 | * @return int |
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 | public function affected_rows() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 344 | { |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 345 | 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 | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 353 | * @return int |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 354 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +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 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 363 | * List table query |
| 364 | * |
| 365 | * Generates a platform-specific query string so that the table names can be fetched |
| 366 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 367 | * @param bool $prefix_limit |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 368 | * @return string |
| 369 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 370 | protected function _list_tables($prefix_limit = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 371 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 372 | $sql = 'SHOW TABLES'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 373 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 374 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 375 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 376 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return $sql; |
| 380 | } |
| 381 | |
| 382 | // -------------------------------------------------------------------- |
| 383 | |
| 384 | /** |
| 385 | * Show column query |
| 386 | * |
| 387 | * Generates a platform-specific query string so that the column names can be fetched |
| 388 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 389 | * @param string $table |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 390 | * @return string |
| 391 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 392 | protected function _list_columns($table = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 393 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 394 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // -------------------------------------------------------------------- |
| 398 | |
| 399 | /** |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame^] | 400 | * Returns an object with field data |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 401 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 402 | * @param string $table |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame^] | 403 | * @return array |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 404 | */ |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame^] | 405 | public function field_data($table = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 406 | { |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame^] | 407 | if ($table === '') |
| 408 | { |
| 409 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
| 410 | } |
| 411 | |
| 412 | if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE) |
| 413 | { |
| 414 | return FALSE; |
| 415 | } |
| 416 | $query = $query->result_object(); |
| 417 | |
| 418 | $retval = array(); |
| 419 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 420 | { |
| 421 | $retval[$i] = new stdClass(); |
| 422 | $retval[$i]->name = $query[$i]->Field; |
| 423 | |
| 424 | sscanf($query[$i]->Type, '%[a-z](%d)', |
| 425 | $retval[$i]->type, |
| 426 | $retval[$i]->max_length |
| 427 | ); |
| 428 | |
| 429 | $retval[$i]->default = $query[$i]->Default; |
| 430 | $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); |
| 431 | } |
| 432 | |
| 433 | return $retval; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // -------------------------------------------------------------------- |
| 437 | |
| 438 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 439 | * 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 | * Returns an array containing code and message of the last |
| 442 | * database error that has occured. |
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 | * @return array |
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 | public function error() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 447 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 448 | 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] | 449 | } |
| 450 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 451 | // -------------------------------------------------------------------- |
| 452 | |
| 453 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 454 | * Update_Batch statement |
| 455 | * |
| 456 | * Generates a platform-specific batch update string from the supplied data |
| 457 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 458 | * @param string $table Table name |
| 459 | * @param array $values Update data |
| 460 | * @param string $index WHERE key |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 461 | * @return string |
| 462 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 463 | protected function _update_batch($table, $values, $index) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 464 | { |
| 465 | $ids = array(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 466 | foreach ($values as $key => $val) |
| 467 | { |
| 468 | $ids[] = $val[$index]; |
| 469 | |
| 470 | foreach (array_keys($val) as $field) |
| 471 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 472 | if ($field !== $index) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 473 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 474 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 479 | $cases = ''; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 480 | foreach ($final as $k => $v) |
| 481 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 482 | $cases .= $k." = CASE \n" |
| 483 | .implode("\n", $v) |
| 484 | .'ELSE '.$k.' END, '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 487 | $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); |
| 488 | |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 489 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // -------------------------------------------------------------------- |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 493 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 494 | /** |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 495 | * FROM tables |
| 496 | * |
| 497 | * Groups tables in FROM clauses if needed, so there is no confusion |
| 498 | * about operator precedence. |
| 499 | * |
| 500 | * @return string |
| 501 | */ |
| 502 | protected function _from_tables() |
| 503 | { |
Andrey Andreev | fce9abe | 2012-10-09 11:37:00 +0300 | [diff] [blame] | 504 | if ( ! empty($this->qb_join) && count($this->qb_from) > 1) |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 505 | { |
| 506 | return '('.implode(', ', $this->qb_from).')'; |
| 507 | } |
| 508 | |
| 509 | return implode(', ', $this->qb_from); |
| 510 | } |
| 511 | |
| 512 | // -------------------------------------------------------------------- |
| 513 | |
| 514 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 515 | * Close DB Connection |
| 516 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 517 | * @return void |
| 518 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 519 | protected function _close() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 520 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 521 | @cubrid_close($this->conn_id); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | } |
| 525 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 526 | /* End of file cubrid_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 527 | /* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ |