blob: 8e77d8396fbc71ff6d8e735c19bbe4b4abbfd5fd [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
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030053 /**
54 * Constructor
55 *
56 * @param array $params
57 * @return void
58 */
Andrey Andreeva00e5042012-03-26 12:23:13 +030059 public function __construct($params)
60 {
61 parent::__construct($params);
62
63 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
64 {
65 if (stripos($matches[2], 'autocommit=off') !== FALSE)
66 {
67 $this->auto_commit = FALSE;
68 }
69 }
70 else
71 {
72 // If no port is defined by the user, use the default value
Andrey Andreeve4c30192012-06-04 15:08:24 +030073 empty($this->port) OR $this->port = 33000;
Andrey Andreeva00e5042012-03-26 12:23:13 +030074 }
75 }
Esen Sagynov2e087942011-08-09 23:35:01 -070076
Andrey Andreev2ea33c32012-10-04 12:37:51 +030077 // --------------------------------------------------------------------
78
Esen Sagynov2e087942011-08-09 23:35:01 -070079 /**
80 * Non-persistent database connection
81 *
Esen Sagynov2e087942011-08-09 23:35:01 -070082 * @return resource
83 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020084 public function db_connect()
Esen Sagynov2e087942011-08-09 23:35:01 -070085 {
Andrey Andreeva00e5042012-03-26 12:23:13 +030086 return $this->_cubrid_connect();
Esen Sagynov2e087942011-08-09 23:35:01 -070087 }
88
89 // --------------------------------------------------------------------
90
91 /**
92 * Persistent database connection
Andrey Andreev592f4ec2012-03-20 15:10:08 +020093 *
Esen Sagynov2e087942011-08-09 23:35:01 -070094 * In CUBRID persistent DB connection is supported natively in CUBRID
95 * engine which can be configured in the CUBRID Broker configuration
96 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
97 * connections established between the client application and the
Andrey Andreeva00e5042012-03-26 12:23:13 +030098 * server will become persistent.
Esen Sagynov2e087942011-08-09 23:35:01 -070099 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700100 * @return resource
101 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200102 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700103 {
Andrey Andreeva00e5042012-03-26 12:23:13 +0300104 return $this->_cubrid_connect(TRUE);
105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * CUBRID connection
111 *
112 * A CUBRID-specific method to create a connection to the database.
113 * Except for determining if a persistent connection should be used,
114 * the rest of the logic is the same for db_connect() and db_pconnect().
115 *
116 * @param bool
117 * @return resource
118 */
119 protected function _cubrid_connect($persistent = FALSE)
120 {
121 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
122 {
123 $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
124 $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
125 ? $_temp($this->dsn, $this->username, $this->password)
126 : $_temp($this->dsn);
127 }
128 else
129 {
130 $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
131 $conn_id = ($this->username !== '')
132 ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password)
133 : $_temp($this->hostname, $this->port, $this->database);
134 }
135
136 return $conn_id;
Esen Sagynov2e087942011-08-09 23:35:01 -0700137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Reconnect
143 *
144 * Keep / reestablish the db connection if no queries have been
145 * sent for a length of time exceeding the server's idle timeout
146 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700147 * @return void
148 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200149 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700150 {
151 if (cubrid_ping($this->conn_id) === FALSE)
152 {
153 $this->conn_id = FALSE;
154 }
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200160 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700162 * @return string
163 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200164 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700165 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200166 return isset($this->data_cache['version'])
167 ? $this->data_cache['version']
168 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Execute the query
175 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 * @param string an SQL query
177 * @return resource
178 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200179 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700180 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700181 return @cubrid_query($sql, $this->conn_id);
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 * Begin Transaction
188 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300189 * @param bool $test_mode = FALSE
Esen Sagynov2e087942011-08-09 23:35:01 -0700190 * @return bool
191 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200192 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700193 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700194 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200195 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700196 {
197 return TRUE;
198 }
199
200 // Reset the transaction failure flag.
201 // If the $test_mode flag is set to TRUE transactions will be rolled back
202 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200203 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700204
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700205 if (cubrid_get_autocommit($this->conn_id))
206 {
207 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
208 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700209
210 return TRUE;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Commit Transaction
217 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700218 * @return bool
219 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200220 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700221 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700222 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200223 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700224 {
225 return TRUE;
226 }
227
228 cubrid_commit($this->conn_id);
229
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700230 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
231 {
232 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
233 }
234
Esen Sagynov2e087942011-08-09 23:35:01 -0700235 return TRUE;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Rollback Transaction
242 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700243 * @return bool
244 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200245 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700246 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700247 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200248 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700249 {
250 return TRUE;
251 }
252
253 cubrid_rollback($this->conn_id);
254
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700255 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
256 {
257 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
258 }
259
Esen Sagynov2e087942011-08-09 23:35:01 -0700260 return TRUE;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Escape String
267 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700268 * @param string
269 * @param bool whether or not the string will be used in a LIKE condition
270 * @return string
271 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200272 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700273 {
274 if (is_array($str))
275 {
276 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700277 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700278 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700279 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700280
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700281 return $str;
282 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700283
Andrey Andreev394a3f12012-02-15 14:35:11 +0200284 if (function_exists('cubrid_real_escape_string') &&
285 (is_resource($this->conn_id)
286 OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id)))))
Esen Sagynov2e087942011-08-09 23:35:01 -0700287 {
288 $str = cubrid_real_escape_string($str, $this->conn_id);
289 }
290 else
291 {
292 $str = addslashes($str);
293 }
294
295 // escape LIKE condition wildcards
296 if ($like === TRUE)
297 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200298 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700299 }
300
301 return $str;
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Affected Rows
308 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200309 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700310 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200311 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700312 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200313 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Insert ID
320 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200321 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700322 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200323 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700324 {
325 return @cubrid_insert_id($this->conn_id);
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700331 * List table query
332 *
333 * Generates a platform-specific query string so that the table names can be fetched
334 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200335 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700336 * @return string
337 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200338 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700339 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200340 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700341
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100342 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700343 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200344 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700345 }
346
347 return $sql;
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Show column query
354 *
355 * Generates a platform-specific query string so that the column names can be fetched
356 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700357 * @param string the table name
358 * @return string
359 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200360 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700361 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200362 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Field data query
369 *
370 * Generates a platform-specific query so that the column data can be retrieved
371 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700372 * @param string the table name
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200373 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700374 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200375 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700376 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200377 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700378 }
379
380 // --------------------------------------------------------------------
381
382 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200383 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700384 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 * Returns an array containing code and message of the last
386 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700387 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200388 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700389 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700391 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700393 }
394
Esen Sagynov2e087942011-08-09 23:35:01 -0700395 // --------------------------------------------------------------------
396
397 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700398 * Update_Batch statement
399 *
400 * Generates a platform-specific batch update string from the supplied data
401 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700402 * @param string the table name
403 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300404 * @param string the where key
Esen Sagynov2e087942011-08-09 23:35:01 -0700405 * @return string
406 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300407 protected function _update_batch($table, $values, $index)
Esen Sagynov2e087942011-08-09 23:35:01 -0700408 {
409 $ids = array();
Esen Sagynov2e087942011-08-09 23:35:01 -0700410 foreach ($values as $key => $val)
411 {
412 $ids[] = $val[$index];
413
414 foreach (array_keys($val) as $field)
415 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100416 if ($field !== $index)
Esen Sagynov2e087942011-08-09 23:35:01 -0700417 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700418 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700419 }
420 }
421 }
422
Esen Sagynov2e087942011-08-09 23:35:01 -0700423 $cases = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700424 foreach ($final as $k => $v)
425 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200426 $cases .= $k." = CASE \n"
427 .implode("\n", $v)
428 .'ELSE '.$k.' END, ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700429 }
430
Andrey Andreevb0478652012-07-18 15:34:46 +0300431 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
432
Andrey Andreevd40459d2012-07-18 16:46:39 +0300433 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Esen Sagynov2e087942011-08-09 23:35:01 -0700434 }
435
436 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300437
Esen Sagynov2e087942011-08-09 23:35:01 -0700438 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300439 * FROM tables
440 *
441 * Groups tables in FROM clauses if needed, so there is no confusion
442 * about operator precedence.
443 *
444 * @return string
445 */
446 protected function _from_tables()
447 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300448 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300449 {
450 return '('.implode(', ', $this->qb_from).')';
451 }
452
453 return implode(', ', $this->qb_from);
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700459 * Close DB Connection
460 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700461 * @return void
462 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300463 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700464 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300465 @cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700466 }
467
468}
469
Esen Sagynov2e087942011-08-09 23:35:01 -0700470/* End of file cubrid_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300471/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */