blob: f7dec72de7a2d0a92039fa16564b393afd062996 [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
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
24 * @since Version 2.0.2
25 * @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 Andreev7f55d612012-01-26 13:44:28 +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 Andreev2e430a32012-02-12 23:37:58 +020069 preg_match('/autocommit=on/', $matches[2], $matches) OR $this->auto_commit = FALSE;
70 }
71 else
72 {
73 // If no port is defined by the user, use the default value
74 $this->port == '' OR $this->port = 33000;
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070075 }
Andrey Andreev7f55d612012-01-26 13:44:28 +020076 }
Esen Sagynov2e087942011-08-09 23:35:01 -070077
Andrey Andreev7f55d612012-01-26 13:44:28 +020078 /**
79 * Non-persistent database connection
80 *
81 * @return resource
82 */
83 public function db_connect()
84 {
Andrey Andreev2e430a32012-02-12 23:37:58 +020085 return $this->_cubrid_connect();
Esen Sagynov2e087942011-08-09 23:35:01 -070086 }
87
88 // --------------------------------------------------------------------
89
90 /**
91 * Persistent database connection
Andrey Andreev2e430a32012-02-12 23:37:58 +020092 *
Esen Sagynov2e087942011-08-09 23:35:01 -070093 * In CUBRID persistent DB connection is supported natively in CUBRID
94 * engine which can be configured in the CUBRID Broker configuration
95 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
96 * connections established between the client application and the
Andrey Andreev2e430a32012-02-12 23:37:58 +020097 * server will become persistent.
Esen Sagynov2e087942011-08-09 23:35:01 -070098 *
Esen Sagynov2e087942011-08-09 23:35:01 -070099 * @return resource
100 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200101 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700102 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200103 return $this->_cubrid_connect(TRUE);
104 }
105
106 // --------------------------------------------------------------------
107
108 /**
109 * CUBRID connection
110 *
111 * A CUBRID-specific method to create a connection to the database.
112 * Except for determining if a persistent connection should be used,
113 * the rest of the logic is the same for db_connect() and db_pconnect().
114 *
115 * @param bool
116 * @return resource
117 */
118 protected function _cubrid_connect($persistent = FALSE)
119 {
120 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
121 {
122 $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
123 ? cubrid_connect_with_url($this->dsn, $this->username, $this->password)
124 : cubrid_connect_with_url($this->dsn);
125 }
126 else
127 {
128 $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
129 $conn_id = ($this->username !== '')
130 ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password)
131 : $_temp($this->hostname, $this->port, $this->database);
132 }
133
134 if ($conn_id)
135 {
136 $_temp = ($this->auto_commit) ? CUBRID_AUTOCOMMIT_TRUE : CUBRID_AUTOCOMMIT_FALSE;
137 cubrid_set_autocommit($conn_id, $_temp);
138 }
139
140 return $conn_id;
Esen Sagynov2e087942011-08-09 23:35:01 -0700141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Reconnect
147 *
148 * Keep / reestablish the db connection if no queries have been
149 * sent for a length of time exceeding the server's idle timeout
150 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700151 * @return void
152 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200153 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700154 {
155 if (cubrid_ping($this->conn_id) === FALSE)
156 {
157 $this->conn_id = FALSE;
158 }
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Select the database
165 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700166 * @return resource
167 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200168 public function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700169 {
170 // In CUBRID there is no need to select a database as the database
171 // is chosen at the connection time.
172 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700173 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700174 return cubrid_ping($this->conn_id);
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Set client character set
181 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700182 * @param string
183 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200184 * @return bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700185 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200186 public function db_set_charset($charset, $collation)
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200188 // Not supported in CUBRID
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 return TRUE;
190 }
191
192 // --------------------------------------------------------------------
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700193
Esen Sagynov2e087942011-08-09 23:35:01 -0700194 /**
195 * Version number query string
196 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700197 * @return string
198 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200199 protected function _version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700200 {
201 return cubrid_get_server_info($this->conn_id);
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Execute the query
208 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700209 * @param string an SQL query
210 * @return resource
211 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200212 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700213 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200214 return @cubrid_query($this->_prep_query($sql), $this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Prep the query
221 *
222 * If needed, each database adapter can prep the query string
223 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700224 * @param string an SQL query
225 * @return string
226 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200227 protected function _prep_query($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700228 {
229 return $sql;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Begin Transaction
236 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700237 * @return bool
238 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200239 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700240 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700241 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200242 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700243 {
244 return TRUE;
245 }
246
247 // Reset the transaction failure flag.
248 // If the $test_mode flag is set to TRUE transactions will be rolled back
249 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200250 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700251
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700252 if (cubrid_get_autocommit($this->conn_id))
253 {
254 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
255 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700256
257 return TRUE;
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Commit Transaction
264 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700265 * @return bool
266 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200267 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700268 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700269 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200270 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700271 {
272 return TRUE;
273 }
274
275 cubrid_commit($this->conn_id);
276
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700277 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
278 {
279 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
280 }
281
Esen Sagynov2e087942011-08-09 23:35:01 -0700282 return TRUE;
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Rollback Transaction
289 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700290 * @return bool
291 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200292 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700293 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700294 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200295 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700296 {
297 return TRUE;
298 }
299
300 cubrid_rollback($this->conn_id);
301
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700302 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
303 {
304 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
305 }
306
Esen Sagynov2e087942011-08-09 23:35:01 -0700307 return TRUE;
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Escape String
314 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700315 * @param string
316 * @param bool whether or not the string will be used in a LIKE condition
317 * @return string
318 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200319 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700320 {
321 if (is_array($str))
322 {
323 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700324 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700325 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700326 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700327
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700328 return $str;
329 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700330
Andrey Andreev7f55d612012-01-26 13:44:28 +0200331 if (function_exists('cubrid_real_escape_string') && is_resource($this->conn_id))
Esen Sagynov2e087942011-08-09 23:35:01 -0700332 {
333 $str = cubrid_real_escape_string($str, $this->conn_id);
334 }
335 else
336 {
337 $str = addslashes($str);
338 }
339
340 // escape LIKE condition wildcards
341 if ($like === TRUE)
342 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200343 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700344 }
345
346 return $str;
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Affected Rows
353 *
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 affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700357 {
358 return @cubrid_affected_rows($this->conn_id);
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Insert ID
365 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200366 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700367 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200368 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700369 {
370 return @cubrid_insert_id($this->conn_id);
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * "Count All" query
377 *
378 * Generates a platform-specific query string that counts all records in
379 * the specified table
380 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700381 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200382 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700383 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200384 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700385 {
386 if ($table == '')
387 {
388 return 0;
389 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200390 $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 -0700391 if ($query->num_rows() == 0)
392 {
393 return 0;
394 }
395
Andrey Andreev7f55d612012-01-26 13:44:28 +0200396 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500397 $this->_reset_select();
Andrey Andreev7f55d612012-01-26 13:44:28 +0200398 return (int) $query->numrows;
Esen Sagynov2e087942011-08-09 23:35:01 -0700399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * List table query
405 *
406 * Generates a platform-specific query string so that the table names can be fetched
407 *
Andrey Andreev2e430a32012-02-12 23:37:58 +0200408 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700409 * @return string
410 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200411 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700412 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200413 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700414
Andrey Andreev7f55d612012-01-26 13:44:28 +0200415 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700416 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200417 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700418 }
419
420 return $sql;
421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Show column query
427 *
428 * Generates a platform-specific query string so that the column names can be fetched
429 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700430 * @param string the table name
431 * @return string
432 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200433 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700434 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200435 return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Field data query
442 *
443 * Generates a platform-specific query so that the column data can be retrieved
444 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700445 * @param string the table name
Andrey Andreev7f55d612012-01-26 13:44:28 +0200446 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700447 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200448 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700449 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200450 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * The error message string
457 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700458 * @return string
459 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200460 protected function _error_message()
Esen Sagynov2e087942011-08-09 23:35:01 -0700461 {
462 return cubrid_error($this->conn_id);
463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * The error message number
469 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200470 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700471 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200472 protected function _error_number()
Esen Sagynov2e087942011-08-09 23:35:01 -0700473 {
474 return cubrid_errno($this->conn_id);
475 }
476
477 // --------------------------------------------------------------------
478
479 /**
480 * Escape the SQL Identifiers
481 *
482 * This function escapes column and table names
483 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700484 * @param string
485 * @return string
486 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200487 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700488 {
489 if ($this->_escape_char == '')
490 {
491 return $item;
492 }
493
494 foreach ($this->_reserved_identifiers as $id)
495 {
496 if (strpos($item, '.'.$id) !== FALSE)
497 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200498 $item = str_replace('.', $this->_escape_char.'.', $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700499
500 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200501 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700502 }
503 }
504
505 if (strpos($item, '.') !== FALSE)
506 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200507 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700508 }
509
510 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200511 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Esen Sagynov2e087942011-08-09 23:35:01 -0700512 }
513
514 // --------------------------------------------------------------------
515
516 /**
517 * From Tables
518 *
519 * This function implicitly groups FROM tables so there is no confusion
520 * about operator precedence in harmony with SQL standards
521 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200522 * @param string the table name
523 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700524 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200525 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700526 {
527 if ( ! is_array($tables))
528 {
529 $tables = array($tables);
530 }
531
532 return '('.implode(', ', $tables).')';
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
538 * Insert statement
539 *
540 * Generates a platform-specific insert string from the supplied data
541 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700542 * @param string the table name
543 * @param array the insert keys
544 * @param array the insert values
545 * @return string
546 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200547 protected function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700548 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200549 return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700550 }
551
552 // --------------------------------------------------------------------
553
554
555 /**
556 * Replace statement
557 *
558 * Generates a platform-specific replace string from the supplied data
559 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700560 * @param string the table name
561 * @param array the insert keys
562 * @param array the insert values
563 * @return string
564 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200565 protected function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700566 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200567 return 'REPLACE INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Insert_batch statement
574 *
575 * Generates a platform-specific insert string from the supplied data
576 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700577 * @param string the table name
578 * @param array the insert keys
579 * @param array the insert values
580 * @return string
581 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200582 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700583 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200584 return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES '.implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700585 }
586
587 // --------------------------------------------------------------------
588
Esen Sagynov2e087942011-08-09 23:35:01 -0700589 /**
590 * Update statement
591 *
592 * Generates a platform-specific update string from the supplied data
593 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700594 * @param string the table name
595 * @param array the update data
596 * @param array the where clause
597 * @param array the orderby clause
598 * @param array the limit clause
599 * @return string
600 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200601 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700602 {
603 foreach ($values as $key => $val)
604 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700605 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700606 }
607
Andrey Andreev7f55d612012-01-26 13:44:28 +0200608 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
609 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
610 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
611 .( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700612 }
613
614 // --------------------------------------------------------------------
615
616
617 /**
618 * Update_Batch statement
619 *
620 * Generates a platform-specific batch update string from the supplied data
621 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700622 * @param string the table name
623 * @param array the update data
624 * @param array the where clause
625 * @return string
626 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200627 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700628 {
629 $ids = array();
Esen Sagynov2e087942011-08-09 23:35:01 -0700630 foreach ($values as $key => $val)
631 {
632 $ids[] = $val[$index];
633
634 foreach (array_keys($val) as $field)
635 {
636 if ($field != $index)
637 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700638 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700639 }
640 }
641 }
642
Esen Sagynov2e087942011-08-09 23:35:01 -0700643 $cases = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700644 foreach ($final as $k => $v)
645 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200646 $cases .= $k." = CASE \n"
647 .implode("\n", $v)
648 .'ELSE '.$k.' END, ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700649 }
650
Andrey Andreev2e430a32012-02-12 23:37:58 +0200651 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Andrey Andreev7f55d612012-01-26 13:44:28 +0200652 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
653 .$index.' IN ('.implode(',', $ids).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700654 }
655
656 // --------------------------------------------------------------------
657
Esen Sagynov2e087942011-08-09 23:35:01 -0700658 /**
659 * Truncate statement
660 *
661 * Generates a platform-specific truncate string from the supplied data
662 * If the database does not support the truncate() command
663 * This function maps to "DELETE FROM table"
664 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700665 * @param string the table name
666 * @return string
667 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200668 protected function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700669 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200670 return 'TRUNCATE '.$table;
Esen Sagynov2e087942011-08-09 23:35:01 -0700671 }
672
673 // --------------------------------------------------------------------
674
675 /**
676 * Delete statement
677 *
678 * Generates a platform-specific delete string from the supplied data
679 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700680 * @param string the table name
681 * @param array the where clause
682 * @param string the limit clause
683 * @return string
684 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200685 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700686 {
687 $conditions = '';
688
689 if (count($where) > 0 OR count($like) > 0)
690 {
Andrey Andreev2e430a32012-02-12 23:37:58 +0200691 $conditions = "\nWHERE ".implode("\n", $where)
692 .((count($where) > 0 && count($like) > 0) ? ' AND ' : '')
693 .implode("\n", $like);
Esen Sagynov2e087942011-08-09 23:35:01 -0700694 }
695
Andrey Andreev7f55d612012-01-26 13:44:28 +0200696 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700697 }
698
699 // --------------------------------------------------------------------
700
701 /**
702 * Limit string
703 *
704 * Generates a platform-specific LIMIT clause
705 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700706 * @param string the sql query string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200707 * @param int the number of rows to limit the query to
708 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700709 * @return string
710 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200711 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700712 {
Andrey Andreevc6953f42012-01-26 14:43:16 +0200713 return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Esen Sagynov2e087942011-08-09 23:35:01 -0700714 }
715
716 // --------------------------------------------------------------------
717
718 /**
719 * Close DB Connection
720 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700721 * @param resource
722 * @return void
723 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200724 protected function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700725 {
726 @cubrid_close($conn_id);
727 }
728
729}
730
Esen Sagynov2e087942011-08-09 23:35:01 -0700731/* End of file cubrid_driver.php */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200732/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */