blob: 67571d3a03c2b77f5a62ffd749f6e71ae4329316 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Esen Sagynov2e087942011-08-09 23:35:01 -07002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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 Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @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 }
166 elseif ( ! $this->conn_id)
167 {
168 $this->initialize();
169 }
170
171 return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE)
172 ? FALSE
173 : $this->data_cache['version'] = $version;
Esen Sagynov2e087942011-08-09 23:35:01 -0700174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Execute the query
180 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200181 * @param string $sql an SQL query
Esen Sagynov2e087942011-08-09 23:35:01 -0700182 * @return resource
183 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200184 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700185 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300186 return cubrid_query($sql, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 }
188
189 // --------------------------------------------------------------------
190
191 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700192 * Begin Transaction
193 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200194 * @param bool $test_mode
Esen Sagynov2e087942011-08-09 23:35:01 -0700195 * @return bool
196 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200197 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700198 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700199 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200200 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700201 {
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.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200208 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700209
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700210 if (cubrid_get_autocommit($this->conn_id))
211 {
212 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
213 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700214
215 return TRUE;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Commit Transaction
222 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700223 * @return bool
224 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200225 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700226 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700227 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200228 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700229 {
230 return TRUE;
231 }
232
233 cubrid_commit($this->conn_id);
234
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700235 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
236 {
237 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
238 }
239
Esen Sagynov2e087942011-08-09 23:35:01 -0700240 return TRUE;
241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Rollback Transaction
247 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700248 * @return bool
249 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200250 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700251 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700252 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200253 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700254 {
255 return TRUE;
256 }
257
258 cubrid_rollback($this->conn_id);
259
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700260 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
261 {
262 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
263 }
264
Esen Sagynov2e087942011-08-09 23:35:01 -0700265 return TRUE;
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200271 * Platform-dependant string escape
Esen Sagynov2e087942011-08-09 23:35:01 -0700272 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200273 * @param string
Esen Sagynov2e087942011-08-09 23:35:01 -0700274 * @return string
275 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200276 protected function _escape_str($str)
Esen Sagynov2e087942011-08-09 23:35:01 -0700277 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300278 return cubrid_real_escape_string($str, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Affected Rows
285 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200286 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700287 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200288 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700289 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300290 return cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Insert ID
297 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200298 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700299 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200300 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700301 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300302 return cubrid_insert_id($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700303 }
304
305 // --------------------------------------------------------------------
306
307 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700308 * List table query
309 *
310 * Generates a platform-specific query string so that the table names can be fetched
311 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200312 * @param bool $prefix_limit
Esen Sagynov2e087942011-08-09 23:35:01 -0700313 * @return string
314 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200315 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700316 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200317 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700318
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100319 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700320 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200321 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700322 }
323
324 return $sql;
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Show column query
331 *
332 * Generates a platform-specific query string so that the column names can be fetched
333 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200334 * @param string $table
Esen Sagynov2e087942011-08-09 23:35:01 -0700335 * @return string
336 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200337 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700338 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200339 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700340 }
341
342 // --------------------------------------------------------------------
343
344 /**
Andrey Andreev10ecc842012-11-16 01:06:20 +0200345 * Returns an object with field data
Esen Sagynov2e087942011-08-09 23:35:01 -0700346 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200347 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200348 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700349 */
Andrey Andreev10ecc842012-11-16 01:06:20 +0200350 public function field_data($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700351 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200352 if ($table === '')
353 {
354 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
355 }
356
357 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
358 {
359 return FALSE;
360 }
361 $query = $query->result_object();
362
363 $retval = array();
364 for ($i = 0, $c = count($query); $i < $c; $i++)
365 {
366 $retval[$i] = new stdClass();
367 $retval[$i]->name = $query[$i]->Field;
368
369 sscanf($query[$i]->Type, '%[a-z](%d)',
370 $retval[$i]->type,
371 $retval[$i]->max_length
372 );
373
374 $retval[$i]->default = $query[$i]->Default;
375 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
376 }
377
378 return $retval;
Esen Sagynov2e087942011-08-09 23:35:01 -0700379 }
380
381 // --------------------------------------------------------------------
382
383 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200384 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700385 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200386 * Returns an array containing code and message of the last
387 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700388 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200389 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700390 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200391 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700392 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200393 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700394 }
395
Esen Sagynov2e087942011-08-09 23:35:01 -0700396 // --------------------------------------------------------------------
397
398 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300399 * FROM tables
400 *
401 * Groups tables in FROM clauses if needed, so there is no confusion
402 * about operator precedence.
403 *
404 * @return string
405 */
406 protected function _from_tables()
407 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300408 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300409 {
410 return '('.implode(', ', $this->qb_from).')';
411 }
412
413 return implode(', ', $this->qb_from);
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700419 * Close DB Connection
420 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700421 * @return void
422 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300423 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700424 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300425 cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700426 }
427
428}
429
Esen Sagynov2e087942011-08-09 23:35:01 -0700430/* End of file cubrid_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300431/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */