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 |
Andrey Andreev | cf63120 | 2012-02-16 11:36:28 +0200 | [diff] [blame] | 24 | * @since Version 2.1 |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 28 | /** |
| 29 | * CUBRID Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 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 | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 46 | protected $_escape_char = '`'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 47 | |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 48 | protected $_random_keyword = ' RAND()'; // database specific random keyword |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 49 | |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 50 | // CUBRID-specific properties |
| 51 | public $auto_commit = TRUE; |
| 52 | |
| 53 | public function __construct($params) |
| 54 | { |
| 55 | parent::__construct($params); |
| 56 | |
| 57 | if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) |
| 58 | { |
| 59 | if (stripos($matches[2], 'autocommit=off') !== FALSE) |
| 60 | { |
| 61 | $this->auto_commit = FALSE; |
| 62 | } |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | // If no port is defined by the user, use the default value |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 67 | empty($this->port) OR $this->port = 33000; |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 68 | } |
| 69 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 70 | |
Andrey Andreev | 2ea33c3 | 2012-10-04 12:37:51 +0300 | [diff] [blame^] | 71 | // -------------------------------------------------------------------- |
| 72 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Non-persistent database connection |
| 75 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 76 | * @return resource |
| 77 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 78 | public function db_connect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 79 | { |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 80 | return $this->_cubrid_connect(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // -------------------------------------------------------------------- |
| 84 | |
| 85 | /** |
| 86 | * Persistent database connection |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 87 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 88 | * In CUBRID persistent DB connection is supported natively in CUBRID |
| 89 | * engine which can be configured in the CUBRID Broker configuration |
| 90 | * file by setting the CCI_PCONNECT parameter to ON. In that case, all |
| 91 | * connections established between the client application and the |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 92 | * server will become persistent. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 93 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 94 | * @return resource |
| 95 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 96 | public function db_pconnect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 97 | { |
Andrey Andreev | a00e504 | 2012-03-26 12:23:13 +0300 | [diff] [blame] | 98 | return $this->_cubrid_connect(TRUE); |
| 99 | } |
| 100 | |
| 101 | // -------------------------------------------------------------------- |
| 102 | |
| 103 | /** |
| 104 | * CUBRID connection |
| 105 | * |
| 106 | * A CUBRID-specific method to create a connection to the database. |
| 107 | * Except for determining if a persistent connection should be used, |
| 108 | * the rest of the logic is the same for db_connect() and db_pconnect(). |
| 109 | * |
| 110 | * @param bool |
| 111 | * @return resource |
| 112 | */ |
| 113 | protected function _cubrid_connect($persistent = FALSE) |
| 114 | { |
| 115 | if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches)) |
| 116 | { |
| 117 | $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url'; |
| 118 | $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '') |
| 119 | ? $_temp($this->dsn, $this->username, $this->password) |
| 120 | : $_temp($this->dsn); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect'; |
| 125 | $conn_id = ($this->username !== '') |
| 126 | ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password) |
| 127 | : $_temp($this->hostname, $this->port, $this->database); |
| 128 | } |
| 129 | |
| 130 | return $conn_id; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // -------------------------------------------------------------------- |
| 134 | |
| 135 | /** |
| 136 | * Reconnect |
| 137 | * |
| 138 | * Keep / reestablish the db connection if no queries have been |
| 139 | * sent for a length of time exceeding the server's idle timeout |
| 140 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 141 | * @return void |
| 142 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 143 | public function reconnect() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 144 | { |
| 145 | if (cubrid_ping($this->conn_id) === FALSE) |
| 146 | { |
| 147 | $this->conn_id = FALSE; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 154 | * Database version number |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 155 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 156 | * @return string |
| 157 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 158 | public function version() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 159 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 160 | return isset($this->data_cache['version']) |
| 161 | ? $this->data_cache['version'] |
| 162 | : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // -------------------------------------------------------------------- |
| 166 | |
| 167 | /** |
| 168 | * Execute the query |
| 169 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 170 | * @param string an SQL query |
| 171 | * @return resource |
| 172 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 173 | protected function _execute($sql) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 174 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 175 | return @cubrid_query($sql, $this->conn_id); |
| 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 181 | * Begin Transaction |
| 182 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 183 | * @return bool |
| 184 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 185 | public function trans_begin($test_mode = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 186 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 187 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 188 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 189 | { |
| 190 | return TRUE; |
| 191 | } |
| 192 | |
| 193 | // Reset the transaction failure flag. |
| 194 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 195 | // even if the queries produce a successful result. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 196 | $this->_trans_failure = ($test_mode === TRUE); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 197 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 198 | if (cubrid_get_autocommit($this->conn_id)) |
| 199 | { |
| 200 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE); |
| 201 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 202 | |
| 203 | return TRUE; |
| 204 | } |
| 205 | |
| 206 | // -------------------------------------------------------------------- |
| 207 | |
| 208 | /** |
| 209 | * Commit Transaction |
| 210 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 211 | * @return bool |
| 212 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 213 | public function trans_commit() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 214 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 215 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 216 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 217 | { |
| 218 | return TRUE; |
| 219 | } |
| 220 | |
| 221 | cubrid_commit($this->conn_id); |
| 222 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 223 | if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id)) |
| 224 | { |
| 225 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE); |
| 226 | } |
| 227 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 228 | return TRUE; |
| 229 | } |
| 230 | |
| 231 | // -------------------------------------------------------------------- |
| 232 | |
| 233 | /** |
| 234 | * Rollback Transaction |
| 235 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 236 | * @return bool |
| 237 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 238 | public function trans_rollback() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 239 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 240 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 241 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 242 | { |
| 243 | return TRUE; |
| 244 | } |
| 245 | |
| 246 | cubrid_rollback($this->conn_id); |
| 247 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 248 | if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id)) |
| 249 | { |
| 250 | cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE); |
| 251 | } |
| 252 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 253 | return TRUE; |
| 254 | } |
| 255 | |
| 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * Escape String |
| 260 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 261 | * @param string |
| 262 | * @param bool whether or not the string will be used in a LIKE condition |
| 263 | * @return string |
| 264 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 265 | public function escape_str($str, $like = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 266 | { |
| 267 | if (is_array($str)) |
| 268 | { |
| 269 | foreach ($str as $key => $val) |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 270 | { |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 271 | $str[$key] = $this->escape_str($val, $like); |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 272 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 273 | |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 274 | return $str; |
| 275 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 276 | |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 277 | if (function_exists('cubrid_real_escape_string') && |
| 278 | (is_resource($this->conn_id) |
| 279 | 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] | 280 | { |
| 281 | $str = cubrid_real_escape_string($str, $this->conn_id); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | $str = addslashes($str); |
| 286 | } |
| 287 | |
| 288 | // escape LIKE condition wildcards |
| 289 | if ($like === TRUE) |
| 290 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 291 | return str_replace(array('%', '_'), array('\\%', '\\_'), $str); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return $str; |
| 295 | } |
| 296 | |
| 297 | // -------------------------------------------------------------------- |
| 298 | |
| 299 | /** |
| 300 | * Affected Rows |
| 301 | * |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 302 | * @return int |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 303 | */ |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 304 | public function affected_rows() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 305 | { |
Andrey Andreev | ea3eec9 | 2012-03-01 19:16:23 +0200 | [diff] [blame] | 306 | return @cubrid_affected_rows(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------- |
| 310 | |
| 311 | /** |
| 312 | * Insert ID |
| 313 | * |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 314 | * @return int |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 315 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 316 | public function insert_id() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 317 | { |
| 318 | return @cubrid_insert_id($this->conn_id); |
| 319 | } |
| 320 | |
| 321 | // -------------------------------------------------------------------- |
| 322 | |
| 323 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 324 | * List table query |
| 325 | * |
| 326 | * Generates a platform-specific query string so that the table names can be fetched |
| 327 | * |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 328 | * @param bool |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 329 | * @return string |
| 330 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 331 | protected function _list_tables($prefix_limit = FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 332 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 333 | $sql = 'SHOW TABLES'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 334 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 335 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 336 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 337 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return $sql; |
| 341 | } |
| 342 | |
| 343 | // -------------------------------------------------------------------- |
| 344 | |
| 345 | /** |
| 346 | * Show column query |
| 347 | * |
| 348 | * Generates a platform-specific query string so that the column names can be fetched |
| 349 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 350 | * @param string the table name |
| 351 | * @return string |
| 352 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 353 | protected function _list_columns($table = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 354 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 355 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * Field data query |
| 362 | * |
| 363 | * Generates a platform-specific query so that the column data can be retrieved |
| 364 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 365 | * @param string the table name |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 366 | * @return string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 367 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 368 | protected function _field_data($table) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 369 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 370 | return 'SELECT * FROM '.$table.' LIMIT 1'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // -------------------------------------------------------------------- |
| 374 | |
| 375 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 376 | * Error |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 377 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 378 | * Returns an array containing code and message of the last |
| 379 | * database error that has occured. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 380 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 381 | * @return array |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 382 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 383 | public function error() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 384 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 385 | 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] | 386 | } |
| 387 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 388 | // -------------------------------------------------------------------- |
| 389 | |
| 390 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 391 | * Update_Batch statement |
| 392 | * |
| 393 | * Generates a platform-specific batch update string from the supplied data |
| 394 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 395 | * @param string the table name |
| 396 | * @param array the update data |
| 397 | * @param array the where clause |
| 398 | * @return string |
| 399 | */ |
Andrey Andreev | 592f4ec | 2012-03-20 15:10:08 +0200 | [diff] [blame] | 400 | protected function _update_batch($table, $values, $index, $where = NULL) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 401 | { |
| 402 | $ids = array(); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 403 | foreach ($values as $key => $val) |
| 404 | { |
| 405 | $ids[] = $val[$index]; |
| 406 | |
| 407 | foreach (array_keys($val) as $field) |
| 408 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 409 | if ($field !== $index) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 410 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 411 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 416 | $cases = ''; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 417 | foreach ($final as $k => $v) |
| 418 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 419 | $cases .= $k." = CASE \n" |
| 420 | .implode("\n", $v) |
| 421 | .'ELSE '.$k.' END, '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Andrey Andreev | 2e430a3 | 2012-02-12 23:37:58 +0200 | [diff] [blame] | 424 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 425 | .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 426 | .$index.' IN ('.implode(',', $ids).')'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // -------------------------------------------------------------------- |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 430 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 431 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 432 | * Close DB Connection |
| 433 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 434 | * @return void |
| 435 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 436 | protected function _close() |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 437 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 438 | @cubrid_close($this->conn_id); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | } |
| 442 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 443 | /* End of file cubrid_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 444 | /* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ |