blob: a54ed3d15cef60a5306967b9960598bd928d3a97 [file] [log] [blame]
Andrey Andreev7f55d612012-01-26 13:44:28 +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 Andreev7f55d612012-01-26 13:44:28 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7f55d612012-01-26 13:44:28 +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
32 * creates dynamically based on whether the active record
33 * 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 Andreev7f55d612012-01-26 13:44:28 +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 Andreev7f55d612012-01-26 13:44:28 +020049 protected $_like_escape_str = '';
50 protected $_like_escape_chr = '';
Esen Sagynov2e087942011-08-09 23:35:01 -070051
52 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev7f55d612012-01-26 13:44:28 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' RAND()'; // database specific random keyword
Esen Sagynov2e087942011-08-09 23:35:01 -070059
Andrey Andreev2e430a32012-02-12 23:37:58 +020060 // CUBRID-specific properties
61 public $auto_commit = TRUE;
62
Andrey Andreevca7e5f12012-01-26 17:58:28 +020063 public function __construct($params)
Esen Sagynov2e087942011-08-09 23:35:01 -070064 {
Andrey Andreevca7e5f12012-01-26 17:58:28 +020065 parent::__construct($params);
66
Andrey Andreev2e430a32012-02-12 23:37:58 +020067 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
Esen Sagynov2e087942011-08-09 23:35:01 -070068 {
Andrey Andreevcac407d2012-02-14 11:45:36 +020069 if (stripos($matches[2], 'autocommit=off') !== FALSE)
Andrey Andreev36cd5312012-02-13 01:40:55 +020070 {
71 $this->auto_commit = FALSE;
72 }
Andrey Andreev2e430a32012-02-12 23:37:58 +020073 }
74 else
75 {
76 // If no port is defined by the user, use the default value
77 $this->port == '' OR $this->port = 33000;
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070078 }
Andrey Andreev7f55d612012-01-26 13:44:28 +020079 }
Esen Sagynov2e087942011-08-09 23:35:01 -070080
Andrey Andreev7f55d612012-01-26 13:44:28 +020081 /**
82 * Non-persistent database connection
83 *
84 * @return resource
85 */
86 public function db_connect()
87 {
Andrey Andreev2e430a32012-02-12 23:37:58 +020088 return $this->_cubrid_connect();
Esen Sagynov2e087942011-08-09 23:35:01 -070089 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Persistent database connection
Andrey Andreev2e430a32012-02-12 23:37:58 +020095 *
Esen Sagynov2e087942011-08-09 23:35:01 -070096 * In CUBRID persistent DB connection is supported natively in CUBRID
97 * engine which can be configured in the CUBRID Broker configuration
98 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
99 * connections established between the client application and the
Andrey Andreev2e430a32012-02-12 23:37:58 +0200100 * server will become persistent.
Esen Sagynov2e087942011-08-09 23:35:01 -0700101 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700102 * @return resource
103 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200104 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700105 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200106 return $this->_cubrid_connect(TRUE);
107 }
108
109 // --------------------------------------------------------------------
110
111 /**
112 * CUBRID connection
113 *
114 * A CUBRID-specific method to create a connection to the database.
115 * Except for determining if a persistent connection should be used,
116 * the rest of the logic is the same for db_connect() and db_pconnect().
117 *
118 * @param bool
119 * @return resource
120 */
121 protected function _cubrid_connect($persistent = FALSE)
122 {
123 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
124 {
Andrey Andreevfe4b4e92012-02-13 05:40:25 +0200125 $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
Andrey Andreev2e430a32012-02-12 23:37:58 +0200126 $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
Andrey Andreevfe4b4e92012-02-13 05:40:25 +0200127 ? $_temp($this->dsn, $this->username, $this->password)
128 : $_temp($this->dsn);
Andrey Andreev2e430a32012-02-12 23:37:58 +0200129 }
130 else
131 {
132 $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
133 $conn_id = ($this->username !== '')
134 ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password)
135 : $_temp($this->hostname, $this->port, $this->database);
136 }
137
Andrey Andreev2e430a32012-02-12 23:37:58 +0200138 return $conn_id;
Esen Sagynov2e087942011-08-09 23:35:01 -0700139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Reconnect
145 *
146 * Keep / reestablish the db connection if no queries have been
147 * sent for a length of time exceeding the server's idle timeout
148 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700149 * @return void
150 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200151 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700152 {
153 if (cubrid_ping($this->conn_id) === FALSE)
154 {
155 $this->conn_id = FALSE;
156 }
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Select the database
163 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700164 * @return resource
165 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200166 public function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700167 {
168 // In CUBRID there is no need to select a database as the database
169 // is chosen at the connection time.
170 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700171 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 return cubrid_ping($this->conn_id);
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200178 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700179 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700180 * @return string
181 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200182 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200184 return isset($this->data_cache['version'])
185 ? $this->data_cache['version']
186 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Execute the query
193 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700194 * @param string an SQL query
195 * @return resource
196 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200197 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700198 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200199 return @cubrid_query($this->_prep_query($sql), $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Prep the query
206 *
207 * If needed, each database adapter can prep the query string
208 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700209 * @param string an SQL query
210 * @return string
211 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200212 protected function _prep_query($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700213 {
214 return $sql;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Begin Transaction
221 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700222 * @return bool
223 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200224 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700225 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700226 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200227 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700228 {
229 return TRUE;
230 }
231
232 // Reset the transaction failure flag.
233 // If the $test_mode flag is set to TRUE transactions will be rolled back
234 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200235 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700236
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700237 if (cubrid_get_autocommit($this->conn_id))
238 {
239 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
240 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700241
242 return TRUE;
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * Commit Transaction
249 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700250 * @return bool
251 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200252 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700253 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700254 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200255 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700256 {
257 return TRUE;
258 }
259
260 cubrid_commit($this->conn_id);
261
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700262 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
263 {
264 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
265 }
266
Esen Sagynov2e087942011-08-09 23:35:01 -0700267 return TRUE;
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Rollback Transaction
274 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700275 * @return bool
276 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200277 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700278 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700279 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200280 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700281 {
282 return TRUE;
283 }
284
285 cubrid_rollback($this->conn_id);
286
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700287 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
288 {
289 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
290 }
291
Esen Sagynov2e087942011-08-09 23:35:01 -0700292 return TRUE;
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
298 * Escape String
299 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700300 * @param string
301 * @param bool whether or not the string will be used in a LIKE condition
302 * @return string
303 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200304 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700305 {
306 if (is_array($str))
307 {
308 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700309 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700310 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700311 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700312
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700313 return $str;
314 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700315
Andrey Andreev394a3f12012-02-15 14:35:11 +0200316 if (function_exists('cubrid_real_escape_string') &&
317 (is_resource($this->conn_id)
318 OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id)))))
Esen Sagynov2e087942011-08-09 23:35:01 -0700319 {
320 $str = cubrid_real_escape_string($str, $this->conn_id);
321 }
322 else
323 {
324 $str = addslashes($str);
325 }
326
327 // escape LIKE condition wildcards
328 if ($like === TRUE)
329 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200330 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700331 }
332
333 return $str;
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Affected Rows
340 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200341 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700342 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200343 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700344 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200345 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Insert ID
352 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200353 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700354 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200355 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700356 {
357 return @cubrid_insert_id($this->conn_id);
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * "Count All" query
364 *
365 * Generates a platform-specific query string that counts all records in
366 * the specified table
367 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700368 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200369 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700370 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200371 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700372 {
373 if ($table == '')
374 {
375 return 0;
376 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700377
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200378 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Esen Sagynov2e087942011-08-09 23:35:01 -0700379 if ($query->num_rows() == 0)
380 {
381 return 0;
382 }
383
Andrey Andreev7f55d612012-01-26 13:44:28 +0200384 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500385 $this->_reset_select();
Andrey Andreev7f55d612012-01-26 13:44:28 +0200386 return (int) $query->numrows;
Esen Sagynov2e087942011-08-09 23:35:01 -0700387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * List table query
393 *
394 * Generates a platform-specific query string so that the table names can be fetched
395 *
Andrey Andreev2e430a32012-02-12 23:37:58 +0200396 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700397 * @return string
398 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200399 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700400 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200401 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700402
Andrey Andreev7f55d612012-01-26 13:44:28 +0200403 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700404 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200405 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700406 }
407
408 return $sql;
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Show column query
415 *
416 * Generates a platform-specific query string so that the column names can be fetched
417 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700418 * @param string the table name
419 * @return string
420 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200421 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700422 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200423 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700424 }
425
426 // --------------------------------------------------------------------
427
428 /**
429 * Field data query
430 *
431 * Generates a platform-specific query so that the column data can be retrieved
432 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700433 * @param string the table name
Andrey Andreev7f55d612012-01-26 13:44:28 +0200434 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700435 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200436 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700437 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200438 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700439 }
440
441 // --------------------------------------------------------------------
442
443 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200444 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700445 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200446 * Returns an array containing code and message of the last
447 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700448 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200449 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700450 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700452 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200453 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700454 }
455
Esen Sagynov2e087942011-08-09 23:35:01 -0700456 /**
457 * Escape the SQL Identifiers
458 *
459 * This function escapes column and table names
460 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700461 * @param string
462 * @return string
463 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200464 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700465 {
466 if ($this->_escape_char == '')
467 {
468 return $item;
469 }
470
471 foreach ($this->_reserved_identifiers as $id)
472 {
473 if (strpos($item, '.'.$id) !== FALSE)
474 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200475 $item = str_replace('.', $this->_escape_char.'.', $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700476
477 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200478 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700479 }
480 }
481
482 if (strpos($item, '.') !== FALSE)
483 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200484 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700485 }
486
487 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200488 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Esen Sagynov2e087942011-08-09 23:35:01 -0700489 }
490
491 // --------------------------------------------------------------------
492
493 /**
494 * From Tables
495 *
496 * This function implicitly groups FROM tables so there is no confusion
497 * about operator precedence in harmony with SQL standards
498 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200499 * @param array
Andrey Andreev7f55d612012-01-26 13:44:28 +0200500 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700501 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200502 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700503 {
504 if ( ! is_array($tables))
505 {
506 $tables = array($tables);
507 }
508
509 return '('.implode(', ', $tables).')';
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
515 * Insert statement
516 *
517 * Generates a platform-specific insert string from the supplied data
518 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700519 * @param string the table name
520 * @param array the insert keys
521 * @param array the insert values
522 * @return string
523 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200524 protected function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700525 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200526 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700527 }
528
529 // --------------------------------------------------------------------
530
531
532 /**
533 * Replace statement
534 *
535 * Generates a platform-specific replace string from the supplied data
536 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700537 * @param string the table name
538 * @param array the insert keys
539 * @param array the insert values
540 * @return string
541 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200542 protected function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700543 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200544 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700545 }
546
547 // --------------------------------------------------------------------
548
549 /**
550 * Insert_batch statement
551 *
552 * Generates a platform-specific insert string from the supplied data
553 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700554 * @param string the table name
555 * @param array the insert keys
556 * @param array the insert values
557 * @return string
558 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200559 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700560 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200561 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700562 }
563
564 // --------------------------------------------------------------------
565
Esen Sagynov2e087942011-08-09 23:35:01 -0700566 /**
567 * Update statement
568 *
569 * Generates a platform-specific update string from the supplied data
570 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700571 * @param string the table name
572 * @param array the update data
573 * @param array the where clause
574 * @param array the orderby clause
575 * @param array the limit clause
576 * @return string
577 */
Andrey Andreev394a3f12012-02-15 14:35:11 +0200578 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Esen Sagynov2e087942011-08-09 23:35:01 -0700579 {
580 foreach ($values as $key => $val)
581 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200582 $valstr[] = $key.' = '.$val;
Esen Sagynov2e087942011-08-09 23:35:01 -0700583 }
584
Andrey Andreev394a3f12012-02-15 14:35:11 +0200585 $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
586 if (count($like) > 0)
587 {
588 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
589 }
590
591 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where
Andrey Andreev7f55d612012-01-26 13:44:28 +0200592 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
593 .( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700594 }
595
596 // --------------------------------------------------------------------
597
598
599 /**
600 * Update_Batch statement
601 *
602 * Generates a platform-specific batch update string from the supplied data
603 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700604 * @param string the table name
605 * @param array the update data
606 * @param array the where clause
607 * @return string
608 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200609 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700610 {
611 $ids = array();
Esen Sagynov2e087942011-08-09 23:35:01 -0700612 foreach ($values as $key => $val)
613 {
614 $ids[] = $val[$index];
615
616 foreach (array_keys($val) as $field)
617 {
618 if ($field != $index)
619 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700620 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700621 }
622 }
623 }
624
Esen Sagynov2e087942011-08-09 23:35:01 -0700625 $cases = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700626 foreach ($final as $k => $v)
627 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200628 $cases .= $k." = CASE \n"
629 .implode("\n", $v)
630 .'ELSE '.$k.' END, ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700631 }
632
Andrey Andreev2e430a32012-02-12 23:37:58 +0200633 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Andrey Andreev7f55d612012-01-26 13:44:28 +0200634 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
635 .$index.' IN ('.implode(',', $ids).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700636 }
637
638 // --------------------------------------------------------------------
639
Esen Sagynov2e087942011-08-09 23:35:01 -0700640 /**
641 * Truncate statement
642 *
643 * Generates a platform-specific truncate string from the supplied data
644 * If the database does not support the truncate() command
645 * This function maps to "DELETE FROM table"
646 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700647 * @param string the table name
648 * @return string
649 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200650 protected function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700651 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200652 return 'TRUNCATE '.$table;
Esen Sagynov2e087942011-08-09 23:35:01 -0700653 }
654
655 // --------------------------------------------------------------------
656
657 /**
658 * Delete statement
659 *
660 * Generates a platform-specific delete string from the supplied data
661 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700662 * @param string the table name
663 * @param array the where clause
664 * @param string the limit clause
665 * @return string
666 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200667 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700668 {
669 $conditions = '';
670
671 if (count($where) > 0 OR count($like) > 0)
672 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200673 $conditions = "\nWHERE ".implode("\n", $where)
674 .((count($where) > 0 && count($like) > 0) ? ' AND ' : '')
675 .implode("\n", $like);
Esen Sagynov2e087942011-08-09 23:35:01 -0700676 }
677
Andrey Andreev7f55d612012-01-26 13:44:28 +0200678 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Limit string
685 *
686 * Generates a platform-specific LIMIT clause
687 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700688 * @param string the sql query string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200689 * @param int the number of rows to limit the query to
690 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700691 * @return string
692 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200693 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700694 {
Andrey Andreevc6953f42012-01-26 14:43:16 +0200695 return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Esen Sagynov2e087942011-08-09 23:35:01 -0700696 }
697
698 // --------------------------------------------------------------------
699
700 /**
701 * Close DB Connection
702 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700703 * @param resource
704 * @return void
705 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200706 protected function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700707 {
708 @cubrid_close($conn_id);
709 }
710
711}
712
Esen Sagynov2e087942011-08-09 23:35:01 -0700713/* End of file cubrid_driver.php */
Andrey Andreevde85d022012-03-20 15:19:19 +0200714/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */