blob: 28724e0e8612edfc0b573017209c46a6dfa21b96 [file] [log] [blame]
Andrey Andreev592f4ec2012-03-20 15:10:08 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Esen Sagynov2e087942011-08-09 23:35:01 -070028/**
29 * CUBRID Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Esen Sagynov2e087942011-08-09 23:35:01 -070033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070038 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070039 * @link http://codeigniter.com/user_guide/database/
40 */
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070041class CI_DB_cubrid_driver extends CI_DB {
42
Andrey Andreev592f4ec2012-03-20 15:10:08 +020043 public $dbdriver = 'cubrid';
Esen Sagynov2e087942011-08-09 23:35:01 -070044
45 // The character used for escaping - no need in CUBRID
Andrey Andreev394a3f12012-02-15 14:35:11 +020046 protected $_escape_char = '`';
Esen Sagynov2e087942011-08-09 23:35:01 -070047
Andrey Andreev592f4ec2012-03-20 15:10:08 +020048 protected $_random_keyword = ' RAND()'; // database specific random keyword
Esen Sagynov2e087942011-08-09 23:35:01 -070049
Andrey Andreeva00e5042012-03-26 12:23:13 +030050 // CUBRID-specific properties
51 public $auto_commit = TRUE;
52
53 public function __construct($params)
54 {
55 parent::__construct($params);
56
57 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
58 {
59 if (stripos($matches[2], 'autocommit=off') !== FALSE)
60 {
61 $this->auto_commit = FALSE;
62 }
63 }
64 else
65 {
66 // If no port is defined by the user, use the default value
Andrey Andreeve4c30192012-06-04 15:08:24 +030067 empty($this->port) OR $this->port = 33000;
Andrey Andreeva00e5042012-03-26 12:23:13 +030068 }
69 }
Esen Sagynov2e087942011-08-09 23:35:01 -070070
Andrey Andreev2ea33c32012-10-04 12:37:51 +030071 // --------------------------------------------------------------------
72
Esen Sagynov2e087942011-08-09 23:35:01 -070073 /**
74 * Non-persistent database connection
75 *
Esen Sagynov2e087942011-08-09 23:35:01 -070076 * @return resource
77 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020078 public function db_connect()
Esen Sagynov2e087942011-08-09 23:35:01 -070079 {
Andrey Andreeva00e5042012-03-26 12:23:13 +030080 return $this->_cubrid_connect();
Esen Sagynov2e087942011-08-09 23:35:01 -070081 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Persistent database connection
Andrey Andreev592f4ec2012-03-20 15:10:08 +020087 *
Esen Sagynov2e087942011-08-09 23:35:01 -070088 * In CUBRID persistent DB connection is supported natively in CUBRID
89 * engine which can be configured in the CUBRID Broker configuration
90 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
91 * connections established between the client application and the
Andrey Andreeva00e5042012-03-26 12:23:13 +030092 * server will become persistent.
Esen Sagynov2e087942011-08-09 23:35:01 -070093 *
Esen Sagynov2e087942011-08-09 23:35:01 -070094 * @return resource
95 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020096 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -070097 {
Andrey Andreeva00e5042012-03-26 12:23:13 +030098 return $this->_cubrid_connect(TRUE);
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * CUBRID connection
105 *
106 * A CUBRID-specific method to create a connection to the database.
107 * Except for determining if a persistent connection should be used,
108 * the rest of the logic is the same for db_connect() and db_pconnect().
109 *
110 * @param bool
111 * @return resource
112 */
113 protected function _cubrid_connect($persistent = FALSE)
114 {
115 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
116 {
117 $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
118 $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
119 ? $_temp($this->dsn, $this->username, $this->password)
120 : $_temp($this->dsn);
121 }
122 else
123 {
124 $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
125 $conn_id = ($this->username !== '')
126 ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password)
127 : $_temp($this->hostname, $this->port, $this->database);
128 }
129
130 return $conn_id;
Esen Sagynov2e087942011-08-09 23:35:01 -0700131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Reconnect
137 *
138 * Keep / reestablish the db connection if no queries have been
139 * sent for a length of time exceeding the server's idle timeout
140 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700141 * @return void
142 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200143 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700144 {
145 if (cubrid_ping($this->conn_id) === FALSE)
146 {
147 $this->conn_id = FALSE;
148 }
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200154 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700155 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700156 * @return string
157 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200158 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700159 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200160 return isset($this->data_cache['version'])
161 ? $this->data_cache['version']
162 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Execute the query
169 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700170 * @param string an SQL query
171 * @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 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 * @return bool
184 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200185 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700186 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200188 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 {
190 return TRUE;
191 }
192
193 // Reset the transaction failure flag.
194 // If the $test_mode flag is set to TRUE transactions will be rolled back
195 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200196 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700197
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700198 if (cubrid_get_autocommit($this->conn_id))
199 {
200 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
201 }
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 Andreev592f4ec2012-03-20 15:10:08 +0200213 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700214 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700215 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200216 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700217 {
218 return TRUE;
219 }
220
221 cubrid_commit($this->conn_id);
222
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700223 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
224 {
225 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
226 }
227
Esen Sagynov2e087942011-08-09 23:35:01 -0700228 return TRUE;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Rollback Transaction
235 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700236 * @return bool
237 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200238 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700239 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700240 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200241 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700242 {
243 return TRUE;
244 }
245
246 cubrid_rollback($this->conn_id);
247
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700248 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
249 {
250 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
251 }
252
Esen Sagynov2e087942011-08-09 23:35:01 -0700253 return TRUE;
254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Escape String
260 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700261 * @param string
262 * @param bool whether or not the string will be used in a LIKE condition
263 * @return string
264 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200265 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700266 {
267 if (is_array($str))
268 {
269 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700270 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700271 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700272 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700273
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700274 return $str;
275 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700276
Andrey Andreev394a3f12012-02-15 14:35:11 +0200277 if (function_exists('cubrid_real_escape_string') &&
278 (is_resource($this->conn_id)
279 OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id)))))
Esen Sagynov2e087942011-08-09 23:35:01 -0700280 {
281 $str = cubrid_real_escape_string($str, $this->conn_id);
282 }
283 else
284 {
285 $str = addslashes($str);
286 }
287
288 // escape LIKE condition wildcards
289 if ($like === TRUE)
290 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200291 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700292 }
293
294 return $str;
295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * Affected Rows
301 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200302 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700303 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200304 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700305 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200306 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Insert ID
313 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200314 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700315 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200316 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700317 {
318 return @cubrid_insert_id($this->conn_id);
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700324 * List table query
325 *
326 * Generates a platform-specific query string so that the table names can be fetched
327 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200328 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700329 * @return string
330 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200331 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700332 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200333 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700334
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100335 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700336 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200337 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700338 }
339
340 return $sql;
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Show column query
347 *
348 * Generates a platform-specific query string so that the column names can be fetched
349 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700350 * @param string the table name
351 * @return string
352 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200353 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700354 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200355 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Field data query
362 *
363 * Generates a platform-specific query so that the column data can be retrieved
364 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700365 * @param string the table name
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200366 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700367 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200368 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700369 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200370 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700371 }
372
373 // --------------------------------------------------------------------
374
375 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200376 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700377 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200378 * Returns an array containing code and message of the last
379 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700380 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200381 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700382 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200383 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700384 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700386 }
387
Esen Sagynov2e087942011-08-09 23:35:01 -0700388 // --------------------------------------------------------------------
389
390 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700391 * Update_Batch statement
392 *
393 * Generates a platform-specific batch update string from the supplied data
394 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700395 * @param string the table name
396 * @param array the update data
397 * @param array the where clause
398 * @return string
399 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200400 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700401 {
402 $ids = array();
Esen Sagynov2e087942011-08-09 23:35:01 -0700403 foreach ($values as $key => $val)
404 {
405 $ids[] = $val[$index];
406
407 foreach (array_keys($val) as $field)
408 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100409 if ($field !== $index)
Esen Sagynov2e087942011-08-09 23:35:01 -0700410 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700411 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700412 }
413 }
414 }
415
Esen Sagynov2e087942011-08-09 23:35:01 -0700416 $cases = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700417 foreach ($final as $k => $v)
418 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200419 $cases .= $k." = CASE \n"
420 .implode("\n", $v)
421 .'ELSE '.$k.' END, ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700422 }
423
Andrey Andreev2e430a32012-02-12 23:37:58 +0200424 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100425 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev7f55d612012-01-26 13:44:28 +0200426 .$index.' IN ('.implode(',', $ids).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700427 }
428
429 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300430
Esen Sagynov2e087942011-08-09 23:35:01 -0700431 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700432 * Close DB Connection
433 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700434 * @return void
435 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300436 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700437 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300438 @cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700439 }
440
441}
442
Esen Sagynov2e087942011-08-09 23:35:01 -0700443/* End of file cubrid_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300444/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */