blob: f80b4db54b52390015c231e651ff526ab441d8ff [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Esen Sagynov2e087942011-08-09 23:35:01 -07002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Esen Sagynov2e087942011-08-09 23:35:01 -07006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev592f4ec2012-03-20 15:10:08 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev592f4ec2012-03-20 15:10:08 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 2.1.0
Esen Sagynov2e087942011-08-09 23:35:01 -070036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Esen Sagynov2e087942011-08-09 23:35:01 -070039
Esen Sagynov2e087942011-08-09 23:35:01 -070040/**
41 * CUBRID Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Esen Sagynov2e087942011-08-09 23:35:01 -070045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070050 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070051 * @link http://codeigniter.com/user_guide/database/
52 */
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070053class CI_DB_cubrid_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020060 public $dbdriver = 'cubrid';
Esen Sagynov2e087942011-08-09 23:35:01 -070061
Andrey Andreeva24e52e2012-11-02 03:54:12 +020062 /**
63 * Auto-commit flag
64 *
65 * @var bool
66 */
Andrey Andreeva00e5042012-03-26 12:23:13 +030067 public $auto_commit = TRUE;
68
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 // --------------------------------------------------------------------
70
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030071 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020072 * Identifier escape character
73 *
74 * @var string
75 */
76 protected $_escape_char = '`';
77
Andrey Andreev98e46cf2012-11-13 03:01:42 +020078 /**
79 * ORDER BY random keyword
80 *
81 * @var array
82 */
83 protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
84
Andrey Andreeva24e52e2012-11-02 03:54:12 +020085 // --------------------------------------------------------------------
86
87 /**
88 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030089 *
90 * @param array $params
91 * @return void
92 */
Andrey Andreeva00e5042012-03-26 12:23:13 +030093 public function __construct($params)
94 {
95 parent::__construct($params);
96
97 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
98 {
99 if (stripos($matches[2], 'autocommit=off') !== FALSE)
100 {
101 $this->auto_commit = FALSE;
102 }
103 }
104 else
105 {
106 // If no port is defined by the user, use the default value
Andrey Andreeve4c30192012-06-04 15:08:24 +0300107 empty($this->port) OR $this->port = 33000;
Andrey Andreeva00e5042012-03-26 12:23:13 +0300108 }
109 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700110
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300111 // --------------------------------------------------------------------
112
Esen Sagynov2e087942011-08-09 23:35:01 -0700113 /**
114 * Non-persistent database connection
115 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200116 * @param bool $persistent
Andrey Andreeva00e5042012-03-26 12:23:13 +0300117 * @return resource
118 */
Andrey Andreev2e171022014-02-25 15:21:41 +0200119 public function db_connect($persistent = FALSE)
Andrey Andreeva00e5042012-03-26 12:23:13 +0300120 {
121 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
122 {
Andrey Andreev2e171022014-02-25 15:21:41 +0200123 $func = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
124 return ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
125 ? $func($this->dsn, $this->username, $this->password)
126 : $func($this->dsn);
Andrey Andreeva00e5042012-03-26 12:23:13 +0300127 }
128
Andrey Andreev2e171022014-02-25 15:21:41 +0200129 $func = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
130 return ($this->username !== '')
131 ? $func($this->hostname, $this->port, $this->database, $this->username, $this->password)
132 : $func($this->hostname, $this->port, $this->database);
Esen Sagynov2e087942011-08-09 23:35:01 -0700133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Reconnect
139 *
140 * Keep / reestablish the db connection if no queries have been
141 * sent for a length of time exceeding the server's idle timeout
142 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700143 * @return void
144 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200145 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700146 {
147 if (cubrid_ping($this->conn_id) === FALSE)
148 {
149 $this->conn_id = FALSE;
150 }
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200156 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700157 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700158 * @return string
159 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200160 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200162 if (isset($this->data_cache['version']))
163 {
164 return $this->data_cache['version'];
165 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200166
167 return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE)
168 ? FALSE
169 : $this->data_cache['version'] = $version;
Esen Sagynov2e087942011-08-09 23:35:01 -0700170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Execute the query
176 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200177 * @param string $sql an SQL query
Esen Sagynov2e087942011-08-09 23:35:01 -0700178 * @return resource
179 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200180 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700181 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300182 return cubrid_query($sql, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 }
184
185 // --------------------------------------------------------------------
186
187 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700188 * Begin Transaction
189 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200190 * @param bool $test_mode
Esen Sagynov2e087942011-08-09 23:35:01 -0700191 * @return bool
192 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200193 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700194 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700195 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200196 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700197 {
198 return TRUE;
199 }
200
201 // Reset the transaction failure flag.
202 // If the $test_mode flag is set to TRUE transactions will be rolled back
203 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200204 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700205
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700206 if (cubrid_get_autocommit($this->conn_id))
207 {
208 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
209 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700210
211 return TRUE;
212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Commit Transaction
218 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700219 * @return bool
220 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200221 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700222 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700223 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200224 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700225 {
226 return TRUE;
227 }
228
229 cubrid_commit($this->conn_id);
230
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700231 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
232 {
233 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
234 }
235
Esen Sagynov2e087942011-08-09 23:35:01 -0700236 return TRUE;
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Rollback Transaction
243 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700244 * @return bool
245 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200246 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700247 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700248 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200249 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700250 {
251 return TRUE;
252 }
253
254 cubrid_rollback($this->conn_id);
255
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700256 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
257 {
258 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
259 }
260
Esen Sagynov2e087942011-08-09 23:35:01 -0700261 return TRUE;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200267 * Platform-dependant string escape
Esen Sagynov2e087942011-08-09 23:35:01 -0700268 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200269 * @param string
Esen Sagynov2e087942011-08-09 23:35:01 -0700270 * @return string
271 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200272 protected function _escape_str($str)
Esen Sagynov2e087942011-08-09 23:35:01 -0700273 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300274 return cubrid_real_escape_string($str, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Affected Rows
281 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200282 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700283 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200284 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700285 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300286 return cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Insert ID
293 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200294 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700295 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200296 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700297 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300298 return cubrid_insert_id($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700299 }
300
301 // --------------------------------------------------------------------
302
303 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700304 * List table query
305 *
306 * Generates a platform-specific query string so that the table names can be fetched
307 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200308 * @param bool $prefix_limit
Esen Sagynov2e087942011-08-09 23:35:01 -0700309 * @return string
310 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200311 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700312 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200313 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700314
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100315 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700316 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200317 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700318 }
319
320 return $sql;
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Show column query
327 *
328 * Generates a platform-specific query string so that the column names can be fetched
329 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200330 * @param string $table
Esen Sagynov2e087942011-08-09 23:35:01 -0700331 * @return string
332 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200333 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700334 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200335 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700336 }
337
338 // --------------------------------------------------------------------
339
340 /**
Andrey Andreev10ecc842012-11-16 01:06:20 +0200341 * Returns an object with field data
Esen Sagynov2e087942011-08-09 23:35:01 -0700342 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200343 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200344 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700345 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200346 public function field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700347 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200348 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
349 {
350 return FALSE;
351 }
352 $query = $query->result_object();
353
354 $retval = array();
355 for ($i = 0, $c = count($query); $i < $c; $i++)
356 {
357 $retval[$i] = new stdClass();
358 $retval[$i]->name = $query[$i]->Field;
359
360 sscanf($query[$i]->Type, '%[a-z](%d)',
361 $retval[$i]->type,
362 $retval[$i]->max_length
363 );
364
365 $retval[$i]->default = $query[$i]->Default;
366 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
367 }
368
369 return $retval;
Esen Sagynov2e087942011-08-09 23:35:01 -0700370 }
371
372 // --------------------------------------------------------------------
373
374 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700376 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200377 * Returns an array containing code and message of the last
378 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700379 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200380 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700381 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200382 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700383 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200384 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700385 }
386
Esen Sagynov2e087942011-08-09 23:35:01 -0700387 // --------------------------------------------------------------------
388
389 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300390 * FROM tables
391 *
392 * Groups tables in FROM clauses if needed, so there is no confusion
393 * about operator precedence.
394 *
395 * @return string
396 */
397 protected function _from_tables()
398 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300399 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300400 {
401 return '('.implode(', ', $this->qb_from).')';
402 }
403
404 return implode(', ', $this->qb_from);
405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700410 * Close DB Connection
411 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700412 * @return void
413 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300414 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700415 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300416 cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700417 }
418
419}