blob: 0b79de0b8e4c64868166d0a551dbd017a3d36c6a [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 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700199 return @cubrid_query($sql, $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Begin Transaction
206 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700207 * @return bool
208 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200209 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700210 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700211 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200212 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700213 {
214 return TRUE;
215 }
216
217 // Reset the transaction failure flag.
218 // If the $test_mode flag is set to TRUE transactions will be rolled back
219 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200220 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700221
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700222 if (cubrid_get_autocommit($this->conn_id))
223 {
224 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
225 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700226
227 return TRUE;
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Commit Transaction
234 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700235 * @return bool
236 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200237 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700238 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700239 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200240 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700241 {
242 return TRUE;
243 }
244
245 cubrid_commit($this->conn_id);
246
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700247 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
248 {
249 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
250 }
251
Esen Sagynov2e087942011-08-09 23:35:01 -0700252 return TRUE;
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Rollback Transaction
259 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700260 * @return bool
261 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200262 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700263 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700264 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200265 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700266 {
267 return TRUE;
268 }
269
270 cubrid_rollback($this->conn_id);
271
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700272 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
273 {
274 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
275 }
276
Esen Sagynov2e087942011-08-09 23:35:01 -0700277 return TRUE;
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Escape String
284 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700285 * @param string
286 * @param bool whether or not the string will be used in a LIKE condition
287 * @return string
288 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200289 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700290 {
291 if (is_array($str))
292 {
293 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700294 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700295 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700296 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700297
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700298 return $str;
299 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700300
Andrey Andreev394a3f12012-02-15 14:35:11 +0200301 if (function_exists('cubrid_real_escape_string') &&
302 (is_resource($this->conn_id)
303 OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id)))))
Esen Sagynov2e087942011-08-09 23:35:01 -0700304 {
305 $str = cubrid_real_escape_string($str, $this->conn_id);
306 }
307 else
308 {
309 $str = addslashes($str);
310 }
311
312 // escape LIKE condition wildcards
313 if ($like === TRUE)
314 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200315 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700316 }
317
318 return $str;
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Affected Rows
325 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200326 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700327 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200328 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700329 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200330 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Insert ID
337 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200338 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700339 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200340 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700341 {
342 return @cubrid_insert_id($this->conn_id);
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * "Count All" query
349 *
350 * Generates a platform-specific query string that counts all records in
351 * the specified table
352 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700353 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200354 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700355 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200356 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700357 {
358 if ($table == '')
359 {
360 return 0;
361 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700362
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200363 $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 -0700364 if ($query->num_rows() == 0)
365 {
366 return 0;
367 }
368
Andrey Andreev7f55d612012-01-26 13:44:28 +0200369 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500370 $this->_reset_select();
Andrey Andreev7f55d612012-01-26 13:44:28 +0200371 return (int) $query->numrows;
Esen Sagynov2e087942011-08-09 23:35:01 -0700372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * List table query
378 *
379 * Generates a platform-specific query string so that the table names can be fetched
380 *
Andrey Andreev2e430a32012-02-12 23:37:58 +0200381 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700382 * @return string
383 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200384 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700385 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200386 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700387
Andrey Andreev7f55d612012-01-26 13:44:28 +0200388 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700389 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200390 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700391 }
392
393 return $sql;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Show column query
400 *
401 * Generates a platform-specific query string so that the column names can be fetched
402 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700403 * @param string the table name
404 * @return string
405 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200406 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700407 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200408 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Field data query
415 *
416 * Generates a platform-specific query so that the column data can be retrieved
417 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700418 * @param string the table name
Andrey Andreev7f55d612012-01-26 13:44:28 +0200419 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700420 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200421 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700422 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200423 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700424 }
425
426 // --------------------------------------------------------------------
427
428 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200429 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700430 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200431 * Returns an array containing code and message of the last
432 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700433 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200434 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700435 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200436 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700437 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200438 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700439 }
440
Esen Sagynov2e087942011-08-09 23:35:01 -0700441 /**
442 * Escape the SQL Identifiers
443 *
444 * This function escapes column and table names
445 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700446 * @param string
447 * @return string
448 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200449 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700450 {
451 if ($this->_escape_char == '')
452 {
453 return $item;
454 }
455
456 foreach ($this->_reserved_identifiers as $id)
457 {
458 if (strpos($item, '.'.$id) !== FALSE)
459 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200460 $item = str_replace('.', $this->_escape_char.'.', $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700461
462 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700464 }
465 }
466
467 if (strpos($item, '.') !== FALSE)
468 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200469 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700470 }
471
472 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200473 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Esen Sagynov2e087942011-08-09 23:35:01 -0700474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * From Tables
480 *
481 * This function implicitly groups FROM tables so there is no confusion
482 * about operator precedence in harmony with SQL standards
483 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200484 * @param array
Andrey Andreev7f55d612012-01-26 13:44:28 +0200485 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700486 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200487 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700488 {
489 if ( ! is_array($tables))
490 {
491 $tables = array($tables);
492 }
493
494 return '('.implode(', ', $tables).')';
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Insert statement
501 *
502 * Generates a platform-specific insert string from the supplied data
503 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700504 * @param string the table name
505 * @param array the insert keys
506 * @param array the insert values
507 * @return string
508 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200509 protected function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700510 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200511 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700512 }
513
514 // --------------------------------------------------------------------
515
516
517 /**
518 * Replace statement
519 *
520 * Generates a platform-specific replace string from the supplied data
521 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700522 * @param string the table name
523 * @param array the insert keys
524 * @param array the insert values
525 * @return string
526 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200527 protected function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700528 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200529 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Insert_batch statement
536 *
537 * Generates a platform-specific insert string from the supplied data
538 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700539 * @param string the table name
540 * @param array the insert keys
541 * @param array the insert values
542 * @return string
543 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200544 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700545 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200546 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700547 }
548
549 // --------------------------------------------------------------------
550
Esen Sagynov2e087942011-08-09 23:35:01 -0700551 /**
552 * Update statement
553 *
554 * Generates a platform-specific update string from the supplied data
555 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700556 * @param string the table name
557 * @param array the update data
558 * @param array the where clause
559 * @param array the orderby clause
560 * @param array the limit clause
561 * @return string
562 */
Andrey Andreev394a3f12012-02-15 14:35:11 +0200563 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Esen Sagynov2e087942011-08-09 23:35:01 -0700564 {
565 foreach ($values as $key => $val)
566 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200567 $valstr[] = $key.' = '.$val;
Esen Sagynov2e087942011-08-09 23:35:01 -0700568 }
569
Andrey Andreev394a3f12012-02-15 14:35:11 +0200570 $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
571 if (count($like) > 0)
572 {
573 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
574 }
575
576 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where
Andrey Andreev7f55d612012-01-26 13:44:28 +0200577 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
578 .( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700579 }
580
581 // --------------------------------------------------------------------
582
583
584 /**
585 * Update_Batch statement
586 *
587 * Generates a platform-specific batch update string from the supplied data
588 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700589 * @param string the table name
590 * @param array the update data
591 * @param array the where clause
592 * @return string
593 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200594 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700595 {
596 $ids = array();
Esen Sagynov2e087942011-08-09 23:35:01 -0700597 foreach ($values as $key => $val)
598 {
599 $ids[] = $val[$index];
600
601 foreach (array_keys($val) as $field)
602 {
603 if ($field != $index)
604 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700605 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700606 }
607 }
608 }
609
Esen Sagynov2e087942011-08-09 23:35:01 -0700610 $cases = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700611 foreach ($final as $k => $v)
612 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200613 $cases .= $k." = CASE \n"
614 .implode("\n", $v)
615 .'ELSE '.$k.' END, ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700616 }
617
Andrey Andreev2e430a32012-02-12 23:37:58 +0200618 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Andrey Andreev7f55d612012-01-26 13:44:28 +0200619 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
620 .$index.' IN ('.implode(',', $ids).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700621 }
622
623 // --------------------------------------------------------------------
624
Esen Sagynov2e087942011-08-09 23:35:01 -0700625 /**
626 * Truncate statement
627 *
628 * Generates a platform-specific truncate string from the supplied data
629 * If the database does not support the truncate() command
630 * This function maps to "DELETE FROM table"
631 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700632 * @param string the table name
633 * @return string
634 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200635 protected function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700636 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200637 return 'TRUNCATE '.$table;
Esen Sagynov2e087942011-08-09 23:35:01 -0700638 }
639
640 // --------------------------------------------------------------------
641
642 /**
643 * Delete statement
644 *
645 * Generates a platform-specific delete string from the supplied data
646 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700647 * @param string the table name
648 * @param array the where clause
649 * @param string the limit clause
650 * @return string
651 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200652 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700653 {
654 $conditions = '';
655
656 if (count($where) > 0 OR count($like) > 0)
657 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200658 $conditions = "\nWHERE ".implode("\n", $where)
659 .((count($where) > 0 && count($like) > 0) ? ' AND ' : '')
660 .implode("\n", $like);
Esen Sagynov2e087942011-08-09 23:35:01 -0700661 }
662
Andrey Andreev7f55d612012-01-26 13:44:28 +0200663 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Limit string
670 *
671 * Generates a platform-specific LIMIT clause
672 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700673 * @param string the sql query string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200674 * @param int the number of rows to limit the query to
675 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700676 * @return string
677 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200678 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700679 {
Andrey Andreevc6953f42012-01-26 14:43:16 +0200680 return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Esen Sagynov2e087942011-08-09 23:35:01 -0700681 }
682
683 // --------------------------------------------------------------------
684
685 /**
686 * Close DB Connection
687 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700688 * @param resource
689 * @return void
690 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200691 protected function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700692 {
693 @cubrid_close($conn_id);
694 }
695
696}
697
Esen Sagynov2e087942011-08-09 23:35:01 -0700698/* End of file cubrid_driver.php */
Andrey Andreevde85d022012-03-20 15:19:19 +0200699/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */