blob: a3d0287f5c24ccfb347a9881091f735aa9c03cc0 [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
48 // clause and character used for LIKE escape sequences - not used in CUBRID
Andrey Andreev592f4ec2012-03-20 15:10:08 +020049 protected $_like_escape_str = '';
50 protected $_like_escape_chr = '';
Esen Sagynov2e087942011-08-09 23:35:01 -070051
Andrey Andreev592f4ec2012-03-20 15:10:08 +020052 protected $_random_keyword = ' RAND()'; // database specific random keyword
Esen Sagynov2e087942011-08-09 23:35:01 -070053
Andrey Andreeva00e5042012-03-26 12:23:13 +030054 // CUBRID-specific properties
55 public $auto_commit = TRUE;
56
57 public function __construct($params)
58 {
59 parent::__construct($params);
60
61 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
62 {
63 if (stripos($matches[2], 'autocommit=off') !== FALSE)
64 {
65 $this->auto_commit = FALSE;
66 }
67 }
68 else
69 {
70 // If no port is defined by the user, use the default value
Andrey Andreeve4c30192012-06-04 15:08:24 +030071 empty($this->port) OR $this->port = 33000;
Andrey Andreeva00e5042012-03-26 12:23:13 +030072 }
73 }
Esen Sagynov2e087942011-08-09 23:35:01 -070074
75 /**
76 * Non-persistent database connection
77 *
Esen Sagynov2e087942011-08-09 23:35:01 -070078 * @return resource
79 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020080 public function db_connect()
Esen Sagynov2e087942011-08-09 23:35:01 -070081 {
Andrey Andreeva00e5042012-03-26 12:23:13 +030082 return $this->_cubrid_connect();
Esen Sagynov2e087942011-08-09 23:35:01 -070083 }
84
85 // --------------------------------------------------------------------
86
87 /**
88 * Persistent database connection
Andrey Andreev592f4ec2012-03-20 15:10:08 +020089 *
Esen Sagynov2e087942011-08-09 23:35:01 -070090 * In CUBRID persistent DB connection is supported natively in CUBRID
91 * engine which can be configured in the CUBRID Broker configuration
92 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
93 * connections established between the client application and the
Andrey Andreeva00e5042012-03-26 12:23:13 +030094 * server will become persistent.
Esen Sagynov2e087942011-08-09 23:35:01 -070095 *
Esen Sagynov2e087942011-08-09 23:35:01 -070096 * @return resource
97 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020098 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -070099 {
Andrey Andreeva00e5042012-03-26 12:23:13 +0300100 return $this->_cubrid_connect(TRUE);
101 }
102
103 // --------------------------------------------------------------------
104
105 /**
106 * CUBRID connection
107 *
108 * A CUBRID-specific method to create a connection to the database.
109 * Except for determining if a persistent connection should be used,
110 * the rest of the logic is the same for db_connect() and db_pconnect().
111 *
112 * @param bool
113 * @return resource
114 */
115 protected function _cubrid_connect($persistent = FALSE)
116 {
117 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
118 {
119 $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
120 $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
121 ? $_temp($this->dsn, $this->username, $this->password)
122 : $_temp($this->dsn);
123 }
124 else
125 {
126 $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
127 $conn_id = ($this->username !== '')
128 ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password)
129 : $_temp($this->hostname, $this->port, $this->database);
130 }
131
132 return $conn_id;
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 Andreev08856b82012-03-03 03:19:28 +0200162 return isset($this->data_cache['version'])
163 ? $this->data_cache['version']
164 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Execute the query
171 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 * @param string an SQL query
173 * @return resource
174 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200175 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700177 return @cubrid_query($sql, $this->conn_id);
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 * Begin Transaction
184 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700185 * @return bool
186 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200187 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700188 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200190 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700191 {
192 return TRUE;
193 }
194
195 // Reset the transaction failure flag.
196 // If the $test_mode flag is set to TRUE transactions will be rolled back
197 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200198 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700199
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700200 if (cubrid_get_autocommit($this->conn_id))
201 {
202 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
203 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700204
205 return TRUE;
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Commit Transaction
212 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700213 * @return bool
214 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200215 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700216 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700217 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200218 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700219 {
220 return TRUE;
221 }
222
223 cubrid_commit($this->conn_id);
224
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700225 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
226 {
227 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
228 }
229
Esen Sagynov2e087942011-08-09 23:35:01 -0700230 return TRUE;
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Rollback Transaction
237 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700238 * @return bool
239 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200240 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700241 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700242 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200243 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700244 {
245 return TRUE;
246 }
247
248 cubrid_rollback($this->conn_id);
249
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700250 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
251 {
252 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
253 }
254
Esen Sagynov2e087942011-08-09 23:35:01 -0700255 return TRUE;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Escape String
262 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700263 * @param string
264 * @param bool whether or not the string will be used in a LIKE condition
265 * @return string
266 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200267 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700268 {
269 if (is_array($str))
270 {
271 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700272 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700273 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700274 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700275
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700276 return $str;
277 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700278
Andrey Andreev394a3f12012-02-15 14:35:11 +0200279 if (function_exists('cubrid_real_escape_string') &&
280 (is_resource($this->conn_id)
281 OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id)))))
Esen Sagynov2e087942011-08-09 23:35:01 -0700282 {
283 $str = cubrid_real_escape_string($str, $this->conn_id);
284 }
285 else
286 {
287 $str = addslashes($str);
288 }
289
290 // escape LIKE condition wildcards
291 if ($like === TRUE)
292 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200293 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700294 }
295
296 return $str;
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Affected Rows
303 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200304 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700305 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200306 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700307 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200308 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * Insert ID
315 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200316 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700317 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200318 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700319 {
320 return @cubrid_insert_id($this->conn_id);
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700326 * List table query
327 *
328 * Generates a platform-specific query string so that the table names can be fetched
329 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200330 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700331 * @return string
332 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200333 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700334 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200335 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700336
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100337 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700338 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200339 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700340 }
341
342 return $sql;
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Show column query
349 *
350 * Generates a platform-specific query string so that the column names can be fetched
351 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700352 * @param string the table name
353 * @return string
354 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200355 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700356 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200357 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Field data query
364 *
365 * Generates a platform-specific query so that the column data can be retrieved
366 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700367 * @param string the table name
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200368 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700369 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200370 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700371 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200372 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700373 }
374
375 // --------------------------------------------------------------------
376
377 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200378 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700379 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200380 * Returns an array containing code and message of the last
381 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700382 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200383 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700384 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700386 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200387 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700388 }
389
Esen Sagynov2e087942011-08-09 23:35:01 -0700390 // --------------------------------------------------------------------
391
392 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700393 * Update_Batch statement
394 *
395 * Generates a platform-specific batch update string from the supplied data
396 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700397 * @param string the table name
398 * @param array the update data
399 * @param array the where clause
400 * @return string
401 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200402 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700403 {
404 $ids = array();
Esen Sagynov2e087942011-08-09 23:35:01 -0700405 foreach ($values as $key => $val)
406 {
407 $ids[] = $val[$index];
408
409 foreach (array_keys($val) as $field)
410 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100411 if ($field !== $index)
Esen Sagynov2e087942011-08-09 23:35:01 -0700412 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700413 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700414 }
415 }
416 }
417
Esen Sagynov2e087942011-08-09 23:35:01 -0700418 $cases = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700419 foreach ($final as $k => $v)
420 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200421 $cases .= $k." = CASE \n"
422 .implode("\n", $v)
423 .'ELSE '.$k.' END, ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700424 }
425
Andrey Andreev2e430a32012-02-12 23:37:58 +0200426 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100427 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev7f55d612012-01-26 13:44:28 +0200428 .$index.' IN ('.implode(',', $ids).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700429 }
430
431 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300432
Esen Sagynov2e087942011-08-09 23:35:01 -0700433 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700434 * Close DB Connection
435 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700436 * @return void
437 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300438 protected function _close()
Esen Sagynov2e087942011-08-09 23:35:01 -0700439 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300440 @cubrid_close($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700441 }
442
443}
444
Esen Sagynov2e087942011-08-09 23:35:01 -0700445/* End of file cubrid_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300446/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */