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