blob: 9d6afea6d358f3c914b27062dda80eb368ecec12 [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 Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Esen Sagynov2e087942011-08-09 23:35:01 -070052 */
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 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700190 * @return bool
191 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300192 protected function _trans_begin()
Esen Sagynov2e087942011-08-09 23:35:01 -0700193 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300194 if (($autocommit = cubrid_get_autocommit($this->conn_id)) === NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700195 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300196 return FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -0700197 }
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300198 elseif ($autocommit === TRUE)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700199 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300200 return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700201 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700202
203 return TRUE;
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * Commit Transaction
210 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700211 * @return bool
212 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300213 protected function _trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700214 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300215 if ( ! cubrid_commit($this->conn_id))
Esen Sagynov2e087942011-08-09 23:35:01 -0700216 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300217 return FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -0700218 }
219
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700220 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
221 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300222 return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700223 }
224
Esen Sagynov2e087942011-08-09 23:35:01 -0700225 return TRUE;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Rollback Transaction
232 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700233 * @return bool
234 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300235 protected function _trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700236 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300237 if ( ! cubrid_rollback($this->conn_id))
Esen Sagynov2e087942011-08-09 23:35:01 -0700238 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300239 return FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -0700240 }
241
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700242 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
243 {
244 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
245 }
246
Esen Sagynov2e087942011-08-09 23:35:01 -0700247 return TRUE;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
Andrey Andreev71d8f722017-01-17 12:01:00 +0200253 * Platform-dependent string escape
Esen Sagynov2e087942011-08-09 23:35:01 -0700254 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200255 * @param string
Esen Sagynov2e087942011-08-09 23:35:01 -0700256 * @return string
257 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200258 protected function _escape_str($str)
Esen Sagynov2e087942011-08-09 23:35:01 -0700259 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300260 return cubrid_real_escape_string($str, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Affected Rows
267 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200268 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700269 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200270 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700271 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300272 return cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Insert ID
279 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200280 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700281 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200282 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700283 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300284 return cubrid_insert_id($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700285 }
286
287 // --------------------------------------------------------------------
288
289 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700290 * List table query
291 *
292 * Generates a platform-specific query string so that the table names can be fetched
293 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200294 * @param bool $prefix_limit
Esen Sagynov2e087942011-08-09 23:35:01 -0700295 * @return string
296 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200297 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700298 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200299 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700300
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100301 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700302 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200303 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700304 }
305
306 return $sql;
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Show column query
313 *
314 * Generates a platform-specific query string so that the column names can be fetched
315 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200316 * @param string $table
Esen Sagynov2e087942011-08-09 23:35:01 -0700317 * @return string
318 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200319 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700320 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200321 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700322 }
323
324 // --------------------------------------------------------------------
325
326 /**
Andrey Andreev10ecc842012-11-16 01:06:20 +0200327 * Returns an object with field data
Esen Sagynov2e087942011-08-09 23:35:01 -0700328 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200329 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200330 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700331 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200332 public function field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700333 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200334 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
335 {
336 return FALSE;
337 }
338 $query = $query->result_object();
339
340 $retval = array();
341 for ($i = 0, $c = count($query); $i < $c; $i++)
342 {
343 $retval[$i] = new stdClass();
344 $retval[$i]->name = $query[$i]->Field;
345
346 sscanf($query[$i]->Type, '%[a-z](%d)',
347 $retval[$i]->type,
348 $retval[$i]->max_length
349 );
350
351 $retval[$i]->default = $query[$i]->Default;
352 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
353 }
354
355 return $retval;
Esen Sagynov2e087942011-08-09 23:35:01 -0700356 }
357
358 // --------------------------------------------------------------------
359
360 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200361 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700362 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200363 * Returns an array containing code and message of the last
Andrey Andreev71d8f722017-01-17 12:01:00 +0200364 * database error that has occurred.
Esen Sagynov2e087942011-08-09 23:35:01 -0700365 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200366 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700367 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200368 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700369 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200370 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700371 }
372
Esen Sagynov2e087942011-08-09 23:35:01 -0700373 // --------------------------------------------------------------------
374
375 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300376 * FROM tables
377 *
378 * Groups tables in FROM clauses if needed, so there is no confusion
379 * about operator precedence.
380 *
381 * @return string
382 */
383 protected function _from_tables()
384 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300385 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300386 {
387 return '('.implode(', ', $this->qb_from).')';
388 }
389
390 return implode(', ', $this->qb_from);
391 }
392
393 // --------------------------------------------------------------------
394
395 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700396 * Close DB Connection
397 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700398 * @return void
399 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300400 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700401 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300402 cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700403 }
404
405}