blob: 2e79faa39a4f569cdcc47467832e3bf0c18fdae0 [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 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700175 return @cubrid_query($sql, $this->conn_id);
176 }
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 Andreev394a3f12012-02-15 14:35:11 +0200267 if (function_exists('cubrid_real_escape_string') &&
268 (is_resource($this->conn_id)
269 OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id)))))
Esen Sagynov2e087942011-08-09 23:35:01 -0700270 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200271 return cubrid_real_escape_string($str, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700272 }
273
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200274 return addslashes($str);
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 Andreevea3eec92012-03-01 19:16:23 +0200286 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 {
298 return @cubrid_insert_id($this->conn_id);
299 }
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 Andreev10ecc842012-11-16 01:06:20 +0200346 public function field_data($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700347 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200348 if ($table === '')
349 {
350 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
351 }
352
353 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
354 {
355 return FALSE;
356 }
357 $query = $query->result_object();
358
359 $retval = array();
360 for ($i = 0, $c = count($query); $i < $c; $i++)
361 {
362 $retval[$i] = new stdClass();
363 $retval[$i]->name = $query[$i]->Field;
364
365 sscanf($query[$i]->Type, '%[a-z](%d)',
366 $retval[$i]->type,
367 $retval[$i]->max_length
368 );
369
370 $retval[$i]->default = $query[$i]->Default;
371 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
372 }
373
374 return $retval;
Esen Sagynov2e087942011-08-09 23:35:01 -0700375 }
376
377 // --------------------------------------------------------------------
378
379 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200380 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700381 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200382 * Returns an array containing code and message of the last
383 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700384 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700386 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200387 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700388 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200389 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700390 }
391
Esen Sagynov2e087942011-08-09 23:35:01 -0700392 // --------------------------------------------------------------------
393
394 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300395 * FROM tables
396 *
397 * Groups tables in FROM clauses if needed, so there is no confusion
398 * about operator precedence.
399 *
400 * @return string
401 */
402 protected function _from_tables()
403 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300404 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300405 {
406 return '('.implode(', ', $this->qb_from).')';
407 }
408
409 return implode(', ', $this->qb_from);
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700415 * Close DB Connection
416 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700417 * @return void
418 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300419 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700420 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300421 @cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700422 }
423
424}
425
Esen Sagynov2e087942011-08-09 23:35:01 -0700426/* End of file cubrid_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300427/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */