blob: c5cb7968328b61be94a75d2abb6d04a04a76ecf2 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev592f4ec2012-03-20 15:10:08 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev592f4ec2012-03-20 15:10:08 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Sagynov2e087942011-08-09 23:35:01 -070019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Esen Sagynov2e087942011-08-09 23:35:01 -070023 * @link http://codeigniter.com
Andrey Andreevcf631202012-02-16 11:36:28 +020024 * @since Version 2.1
Esen Sagynov2e087942011-08-09 23:35:01 -070025 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Esen Sagynov2e087942011-08-09 23:35:01 -070028
Esen Sagynov2e087942011-08-09 23:35:01 -070029/**
30 * CUBRID Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Esen Sagynov2e087942011-08-09 23:35:01 -070034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070039 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070040 * @link http://codeigniter.com/user_guide/database/
41 */
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070042class CI_DB_cubrid_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020049 public $dbdriver = 'cubrid';
Esen Sagynov2e087942011-08-09 23:35:01 -070050
Andrey Andreeva24e52e2012-11-02 03:54:12 +020051 /**
52 * Auto-commit flag
53 *
54 * @var bool
55 */
Andrey Andreeva00e5042012-03-26 12:23:13 +030056 public $auto_commit = TRUE;
57
Andrey Andreeva24e52e2012-11-02 03:54:12 +020058 // --------------------------------------------------------------------
59
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030060 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020061 * Identifier escape character
62 *
63 * @var string
64 */
65 protected $_escape_char = '`';
66
Andrey Andreev98e46cf2012-11-13 03:01:42 +020067 /**
68 * ORDER BY random keyword
69 *
70 * @var array
71 */
72 protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
73
Andrey Andreeva24e52e2012-11-02 03:54:12 +020074 // --------------------------------------------------------------------
75
76 /**
77 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030078 *
79 * @param array $params
80 * @return void
81 */
Andrey Andreeva00e5042012-03-26 12:23:13 +030082 public function __construct($params)
83 {
84 parent::__construct($params);
85
86 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
87 {
88 if (stripos($matches[2], 'autocommit=off') !== FALSE)
89 {
90 $this->auto_commit = FALSE;
91 }
92 }
93 else
94 {
95 // If no port is defined by the user, use the default value
Andrey Andreeve4c30192012-06-04 15:08:24 +030096 empty($this->port) OR $this->port = 33000;
Andrey Andreeva00e5042012-03-26 12:23:13 +030097 }
98 }
Esen Sagynov2e087942011-08-09 23:35:01 -070099
Andrey Andreev2ea33c32012-10-04 12:37:51 +0300100 // --------------------------------------------------------------------
101
Esen Sagynov2e087942011-08-09 23:35:01 -0700102 /**
103 * Non-persistent database connection
104 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200105 * @param bool $persistent
Andrey Andreeva00e5042012-03-26 12:23:13 +0300106 * @return resource
107 */
Andrey Andreev2e171022014-02-25 15:21:41 +0200108 public function db_connect($persistent = FALSE)
Andrey Andreeva00e5042012-03-26 12:23:13 +0300109 {
110 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
111 {
Andrey Andreev2e171022014-02-25 15:21:41 +0200112 $func = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
113 return ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
114 ? $func($this->dsn, $this->username, $this->password)
115 : $func($this->dsn);
Andrey Andreeva00e5042012-03-26 12:23:13 +0300116 }
117
Andrey Andreev2e171022014-02-25 15:21:41 +0200118 $func = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
119 return ($this->username !== '')
120 ? $func($this->hostname, $this->port, $this->database, $this->username, $this->password)
121 : $func($this->hostname, $this->port, $this->database);
Esen Sagynov2e087942011-08-09 23:35:01 -0700122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Reconnect
128 *
129 * Keep / reestablish the db connection if no queries have been
130 * sent for a length of time exceeding the server's idle timeout
131 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700132 * @return void
133 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200134 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700135 {
136 if (cubrid_ping($this->conn_id) === FALSE)
137 {
138 $this->conn_id = FALSE;
139 }
140 }
141
142 // --------------------------------------------------------------------
143
144 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200145 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700146 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700147 * @return string
148 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200149 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700150 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200151 if (isset($this->data_cache['version']))
152 {
153 return $this->data_cache['version'];
154 }
155 elseif ( ! $this->conn_id)
156 {
157 $this->initialize();
158 }
159
160 return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE)
161 ? FALSE
162 : $this->data_cache['version'] = $version;
Esen Sagynov2e087942011-08-09 23:35:01 -0700163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Execute the query
169 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200170 * @param string $sql an SQL query
Esen Sagynov2e087942011-08-09 23:35:01 -0700171 * @return resource
172 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200173 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700174 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300175 return cubrid_query($sql, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 }
177
178 // --------------------------------------------------------------------
179
180 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700181 * Begin Transaction
182 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200183 * @param bool $test_mode
Esen Sagynov2e087942011-08-09 23:35:01 -0700184 * @return bool
185 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200186 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700188 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200189 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700190 {
191 return TRUE;
192 }
193
194 // Reset the transaction failure flag.
195 // If the $test_mode flag is set to TRUE transactions will be rolled back
196 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200197 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700198
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700199 if (cubrid_get_autocommit($this->conn_id))
200 {
201 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
202 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700203
204 return TRUE;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Commit Transaction
211 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700212 * @return bool
213 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200214 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700215 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700216 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200217 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700218 {
219 return TRUE;
220 }
221
222 cubrid_commit($this->conn_id);
223
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700224 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
225 {
226 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
227 }
228
Esen Sagynov2e087942011-08-09 23:35:01 -0700229 return TRUE;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Rollback Transaction
236 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700237 * @return bool
238 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200239 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700240 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700241 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200242 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700243 {
244 return TRUE;
245 }
246
247 cubrid_rollback($this->conn_id);
248
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700249 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
250 {
251 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
252 }
253
Esen Sagynov2e087942011-08-09 23:35:01 -0700254 return TRUE;
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200260 * Platform-dependant string escape
Esen Sagynov2e087942011-08-09 23:35:01 -0700261 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200262 * @param string
Esen Sagynov2e087942011-08-09 23:35:01 -0700263 * @return string
264 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200265 protected function _escape_str($str)
Esen Sagynov2e087942011-08-09 23:35:01 -0700266 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300267 return cubrid_real_escape_string($str, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Affected Rows
274 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200275 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700276 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200277 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700278 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300279 return cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700280 }
281
282 // --------------------------------------------------------------------
283
284 /**
285 * Insert ID
286 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200287 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700288 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200289 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700290 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300291 return cubrid_insert_id($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700292 }
293
294 // --------------------------------------------------------------------
295
296 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700297 * List table query
298 *
299 * Generates a platform-specific query string so that the table names can be fetched
300 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200301 * @param bool $prefix_limit
Esen Sagynov2e087942011-08-09 23:35:01 -0700302 * @return string
303 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200304 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700305 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200306 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700307
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100308 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700309 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200310 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700311 }
312
313 return $sql;
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Show column query
320 *
321 * Generates a platform-specific query string so that the column names can be fetched
322 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200323 * @param string $table
Esen Sagynov2e087942011-08-09 23:35:01 -0700324 * @return string
325 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200326 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700327 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200328 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700329 }
330
331 // --------------------------------------------------------------------
332
333 /**
Andrey Andreev10ecc842012-11-16 01:06:20 +0200334 * Returns an object with field data
Esen Sagynov2e087942011-08-09 23:35:01 -0700335 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200336 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200337 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700338 */
Andrey Andreev10ecc842012-11-16 01:06:20 +0200339 public function field_data($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700340 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200341 if ($table === '')
342 {
343 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
344 }
345
346 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
347 {
348 return FALSE;
349 }
350 $query = $query->result_object();
351
352 $retval = array();
353 for ($i = 0, $c = count($query); $i < $c; $i++)
354 {
355 $retval[$i] = new stdClass();
356 $retval[$i]->name = $query[$i]->Field;
357
358 sscanf($query[$i]->Type, '%[a-z](%d)',
359 $retval[$i]->type,
360 $retval[$i]->max_length
361 );
362
363 $retval[$i]->default = $query[$i]->Default;
364 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
365 }
366
367 return $retval;
Esen Sagynov2e087942011-08-09 23:35:01 -0700368 }
369
370 // --------------------------------------------------------------------
371
372 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200373 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700374 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 * Returns an array containing code and message of the last
376 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700377 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200378 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700379 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200380 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700381 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200382 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700383 }
384
Esen Sagynov2e087942011-08-09 23:35:01 -0700385 // --------------------------------------------------------------------
386
387 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300388 * FROM tables
389 *
390 * Groups tables in FROM clauses if needed, so there is no confusion
391 * about operator precedence.
392 *
393 * @return string
394 */
395 protected function _from_tables()
396 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300397 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300398 {
399 return '('.implode(', ', $this->qb_from).')';
400 }
401
402 return implode(', ', $this->qb_from);
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700408 * Close DB Connection
409 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700410 * @return void
411 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300412 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700413 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300414 cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700415 }
416
417}
418
Esen Sagynov2e087942011-08-09 23:35:01 -0700419/* End of file cubrid_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300420/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */