blob: 1cfaf3e42e20bf738e07d6695c65f4008c222aa4 [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 Andreev7f55d612012-01-26 13:44:28 +020060 public function __construct()
Esen Sagynov2e087942011-08-09 23:35:01 -070061 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070062 // If no port is defined by the user, use the default value
Esen Sagynov2e087942011-08-09 23:35:01 -070063 if ($this->port == '')
64 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020065 // Default CUBRID broker port
66 $this->port = 33000;
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070067 }
Andrey Andreev7f55d612012-01-26 13:44:28 +020068 }
Esen Sagynov2e087942011-08-09 23:35:01 -070069
Andrey Andreev7f55d612012-01-26 13:44:28 +020070 /**
71 * Non-persistent database connection
72 *
73 * @return resource
74 */
75 public function db_connect()
76 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070077 $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password);
Esen Sagynov2e087942011-08-09 23:35:01 -070078
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070079 if ($conn)
80 {
81 // Check if a user wants to run queries in dry, i.e. run the
82 // queries but not commit them.
83 if (isset($this->auto_commit) && ! $this->auto_commit)
84 {
85 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE);
86 }
87 else
88 {
89 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE);
90 $this->auto_commit = TRUE;
91 }
92 }
93
94 return $conn;
Esen Sagynov2e087942011-08-09 23:35:01 -070095 }
96
97 // --------------------------------------------------------------------
98
99 /**
100 * Persistent database connection
101 * In CUBRID persistent DB connection is supported natively in CUBRID
102 * engine which can be configured in the CUBRID Broker configuration
103 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
104 * connections established between the client application and the
105 * server will become persistent. This is calling the same
106 * @cubrid_connect function will establish persisten connection
107 * considering that the CCI_PCONNECT is ON.
108 *
109 * @access private called by the base class
110 * @return resource
111 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200112 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700113 {
114 return $this->db_connect();
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Reconnect
121 *
122 * Keep / reestablish the db connection if no queries have been
123 * sent for a length of time exceeding the server's idle timeout
124 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700125 * @return void
126 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200127 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700128 {
129 if (cubrid_ping($this->conn_id) === FALSE)
130 {
131 $this->conn_id = FALSE;
132 }
133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Select the database
139 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700140 * @return resource
141 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200142 public function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700143 {
144 // In CUBRID there is no need to select a database as the database
145 // is chosen at the connection time.
146 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700147 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700148 return cubrid_ping($this->conn_id);
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Set client character set
155 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700156 * @param string
157 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200158 * @return bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700159 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200160 public function db_set_charset($charset, $collation)
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 {
162 // In CUBRID, there is no need to set charset or collation.
163 // This is why returning true will allow the application continue
164 // its normal process.
165 return TRUE;
166 }
167
168 // --------------------------------------------------------------------
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700169
Esen Sagynov2e087942011-08-09 23:35:01 -0700170 /**
171 * Version number query string
172 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700173 * @return string
174 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200175 protected function _version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 {
177 return cubrid_get_server_info($this->conn_id);
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Execute the query
184 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700185 * @param string an SQL query
186 * @return resource
187 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200188 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 {
190 $sql = $this->_prep_query($sql);
191 return @cubrid_query($sql, $this->conn_id);
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Prep the query
198 *
199 * If needed, each database adapter can prep the query string
200 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700201 * @param string an SQL query
202 * @return string
203 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200204 protected function _prep_query($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700205 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700206 // No need to prepare
Esen Sagynov2e087942011-08-09 23:35:01 -0700207 return $sql;
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Begin Transaction
214 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700215 * @return bool
216 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200217 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700218 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700219 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200220 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700221 {
222 return TRUE;
223 }
224
225 // Reset the transaction failure flag.
226 // If the $test_mode flag is set to TRUE transactions will be rolled back
227 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200228 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700229
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700230 if (cubrid_get_autocommit($this->conn_id))
231 {
232 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
233 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700234
235 return TRUE;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Commit Transaction
242 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700243 * @return bool
244 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200245 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700246 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700247 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200248 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700249 {
250 return TRUE;
251 }
252
253 cubrid_commit($this->conn_id);
254
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700255 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
256 {
257 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
258 }
259
Esen Sagynov2e087942011-08-09 23:35:01 -0700260 return TRUE;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Rollback Transaction
267 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700268 * @return bool
269 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200270 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700271 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700272 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200273 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700274 {
275 return TRUE;
276 }
277
278 cubrid_rollback($this->conn_id);
279
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700280 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
281 {
282 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
283 }
284
Esen Sagynov2e087942011-08-09 23:35:01 -0700285 return TRUE;
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * Escape String
292 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700293 * @param string
294 * @param bool whether or not the string will be used in a LIKE condition
295 * @return string
296 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200297 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700298 {
299 if (is_array($str))
300 {
301 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700302 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700303 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700304 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700305
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700306 return $str;
307 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700308
Andrey Andreev7f55d612012-01-26 13:44:28 +0200309 if (function_exists('cubrid_real_escape_string') && is_resource($this->conn_id))
Esen Sagynov2e087942011-08-09 23:35:01 -0700310 {
311 $str = cubrid_real_escape_string($str, $this->conn_id);
312 }
313 else
314 {
315 $str = addslashes($str);
316 }
317
318 // escape LIKE condition wildcards
319 if ($like === TRUE)
320 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200321 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700322 }
323
324 return $str;
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Affected Rows
331 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200332 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700333 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200334 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700335 {
336 return @cubrid_affected_rows($this->conn_id);
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Insert ID
343 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200344 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700345 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200346 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700347 {
348 return @cubrid_insert_id($this->conn_id);
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * "Count All" query
355 *
356 * Generates a platform-specific query string that counts all records in
357 * the specified table
358 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700359 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200360 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700361 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200362 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700363 {
364 if ($table == '')
365 {
366 return 0;
367 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200368 $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 -0700369 if ($query->num_rows() == 0)
370 {
371 return 0;
372 }
373
Andrey Andreev7f55d612012-01-26 13:44:28 +0200374 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500375 $this->_reset_select();
Andrey Andreev7f55d612012-01-26 13:44:28 +0200376 return (int) $query->numrows;
Esen Sagynov2e087942011-08-09 23:35:01 -0700377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * List table query
383 *
384 * Generates a platform-specific query string so that the table names can be fetched
385 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700386 * @param boolean
387 * @return string
388 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200389 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700390 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200391 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700392
Andrey Andreev7f55d612012-01-26 13:44:28 +0200393 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700394 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200395 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700396 }
397
398 return $sql;
399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * Show column query
405 *
406 * Generates a platform-specific query string so that the column names can be fetched
407 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700408 * @param string the table name
409 * @return string
410 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200411 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700412 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200413 return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Field data query
420 *
421 * Generates a platform-specific query so that the column data can be retrieved
422 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700423 * @param string the table name
Andrey Andreev7f55d612012-01-26 13:44:28 +0200424 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700425 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200426 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700427 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200428 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * The error message string
435 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700436 * @return string
437 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200438 protected function _error_message()
Esen Sagynov2e087942011-08-09 23:35:01 -0700439 {
440 return cubrid_error($this->conn_id);
441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * The error message number
447 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200448 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700449 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200450 protected function _error_number()
Esen Sagynov2e087942011-08-09 23:35:01 -0700451 {
452 return cubrid_errno($this->conn_id);
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Escape the SQL Identifiers
459 *
460 * This function escapes column and table names
461 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700462 * @param string
463 * @return string
464 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200465 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700466 {
467 if ($this->_escape_char == '')
468 {
469 return $item;
470 }
471
472 foreach ($this->_reserved_identifiers as $id)
473 {
474 if (strpos($item, '.'.$id) !== FALSE)
475 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200476 $item = str_replace('.', $this->_escape_char.'.', $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700477
478 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200479 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700480 }
481 }
482
483 if (strpos($item, '.') !== FALSE)
484 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200485 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700486 }
487
488 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200489 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Esen Sagynov2e087942011-08-09 23:35:01 -0700490 }
491
492 // --------------------------------------------------------------------
493
494 /**
495 * From Tables
496 *
497 * This function implicitly groups FROM tables so there is no confusion
498 * about operator precedence in harmony with SQL standards
499 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200500 * @param string the table name
501 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700502 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200503 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700504 {
505 if ( ! is_array($tables))
506 {
507 $tables = array($tables);
508 }
509
510 return '('.implode(', ', $tables).')';
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Insert statement
517 *
518 * Generates a platform-specific insert string from the supplied data
519 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700520 * @param string the table name
521 * @param array the insert keys
522 * @param array the insert values
523 * @return string
524 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200525 protected function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700526 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200527 return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700528 }
529
530 // --------------------------------------------------------------------
531
532
533 /**
534 * Replace statement
535 *
536 * Generates a platform-specific replace string from the supplied data
537 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700538 * @param string the table name
539 * @param array the insert keys
540 * @param array the insert values
541 * @return string
542 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200543 protected function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700544 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200545 return 'REPLACE INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700546 }
547
548 // --------------------------------------------------------------------
549
550 /**
551 * Insert_batch statement
552 *
553 * Generates a platform-specific insert string from the supplied data
554 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700555 * @param string the table name
556 * @param array the insert keys
557 * @param array the insert values
558 * @return string
559 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200560 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700561 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200562 return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES '.implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700563 }
564
565 // --------------------------------------------------------------------
566
Esen Sagynov2e087942011-08-09 23:35:01 -0700567 /**
568 * Update statement
569 *
570 * Generates a platform-specific update string from the supplied data
571 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700572 * @param string the table name
573 * @param array the update data
574 * @param array the where clause
575 * @param array the orderby clause
576 * @param array the limit clause
577 * @return string
578 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200579 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700580 {
581 foreach ($values as $key => $val)
582 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700583 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700584 }
585
Andrey Andreev7f55d612012-01-26 13:44:28 +0200586 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
587 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
588 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
589 .( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700590 }
591
592 // --------------------------------------------------------------------
593
594
595 /**
596 * Update_Batch statement
597 *
598 * Generates a platform-specific batch update string from the supplied data
599 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700600 * @param string the table name
601 * @param array the update data
602 * @param array the where clause
603 * @return string
604 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200605 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700606 {
607 $ids = array();
Andrey Andreev7f55d612012-01-26 13:44:28 +0200608 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700609
610 foreach ($values as $key => $val)
611 {
612 $ids[] = $val[$index];
613
614 foreach (array_keys($val) as $field)
615 {
616 if ($field != $index)
617 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700618 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700619 }
620 }
621 }
622
Andrey Andreev7f55d612012-01-26 13:44:28 +0200623 $sql = 'UPDATE '.$table.' SET ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700624 $cases = '';
625
626 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 Andreev7f55d612012-01-26 13:44:28 +0200633 return $sql.substr($cases, 0, -2)
634 .' 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 Andreev7f55d612012-01-26 13:44:28 +0200673 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Esen Sagynov2e087942011-08-09 23:35:01 -0700674
675 if (count($where) > 0 && count($like) > 0)
676 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200677 $conditions .= ' AND ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700678 }
679 $conditions .= implode("\n", $like);
680 }
681
Andrey Andreev7f55d612012-01-26 13:44:28 +0200682 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700683 }
684
685 // --------------------------------------------------------------------
686
687 /**
688 * Limit string
689 *
690 * Generates a platform-specific LIMIT clause
691 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700692 * @param string the sql query string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200693 * @param int the number of rows to limit the query to
694 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700695 * @return string
696 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200697 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700698 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200699 return $sql.'LIMIT '.($offset == 0 ? '' : ', ').$limit;
Esen Sagynov2e087942011-08-09 23:35:01 -0700700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * Close DB Connection
706 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700707 * @param resource
708 * @return void
709 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200710 protected function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700711 {
712 @cubrid_close($conn_id);
713 }
714
715}
716
Esen Sagynov2e087942011-08-09 23:35:01 -0700717/* End of file cubrid_driver.php */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200718/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */