blob: d1fc5f0fa7e6be216344dd6c41119607384bc073 [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 Andreevca7e5f12012-01-26 17:58:28 +020060 public function __construct($params)
Esen Sagynov2e087942011-08-09 23:35:01 -070061 {
Andrey Andreevca7e5f12012-01-26 17:58:28 +020062 parent::__construct($params);
63
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070064 // If no port is defined by the user, use the default value
Esen Sagynov2e087942011-08-09 23:35:01 -070065 if ($this->port == '')
66 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020067 // Default CUBRID broker port
68 $this->port = 33000;
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070069 }
Andrey Andreev7f55d612012-01-26 13:44:28 +020070 }
Esen Sagynov2e087942011-08-09 23:35:01 -070071
Andrey Andreev7f55d612012-01-26 13:44:28 +020072 /**
73 * Non-persistent database connection
74 *
75 * @return resource
76 */
77 public function db_connect()
78 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070079 $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password);
Esen Sagynov2e087942011-08-09 23:35:01 -070080
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070081 if ($conn)
82 {
83 // Check if a user wants to run queries in dry, i.e. run the
84 // queries but not commit them.
85 if (isset($this->auto_commit) && ! $this->auto_commit)
86 {
87 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE);
88 }
89 else
90 {
91 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE);
92 $this->auto_commit = TRUE;
93 }
94 }
95
96 return $conn;
Esen Sagynov2e087942011-08-09 23:35:01 -070097 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Persistent database connection
103 * In CUBRID persistent DB connection is supported natively in CUBRID
104 * engine which can be configured in the CUBRID Broker configuration
105 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
106 * connections established between the client application and the
107 * server will become persistent. This is calling the same
108 * @cubrid_connect function will establish persisten connection
109 * considering that the CCI_PCONNECT is ON.
110 *
111 * @access private called by the base class
112 * @return resource
113 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200114 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700115 {
116 return $this->db_connect();
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Reconnect
123 *
124 * Keep / reestablish the db connection if no queries have been
125 * sent for a length of time exceeding the server's idle timeout
126 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700127 * @return void
128 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200129 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700130 {
131 if (cubrid_ping($this->conn_id) === FALSE)
132 {
133 $this->conn_id = FALSE;
134 }
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Select the database
141 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700142 * @return resource
143 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200144 public function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700145 {
146 // In CUBRID there is no need to select a database as the database
147 // is chosen at the connection time.
148 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700149 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700150 return cubrid_ping($this->conn_id);
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Set client character set
157 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700158 * @param string
159 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200160 * @return bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200162 public function db_set_charset($charset, $collation)
Esen Sagynov2e087942011-08-09 23:35:01 -0700163 {
164 // In CUBRID, there is no need to set charset or collation.
165 // This is why returning true will allow the application continue
166 // its normal process.
167 return TRUE;
168 }
169
170 // --------------------------------------------------------------------
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700171
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 /**
173 * Version number query string
174 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700175 * @return string
176 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200177 protected function _version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700178 {
179 return cubrid_get_server_info($this->conn_id);
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Execute the query
186 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 * @param string an SQL query
188 * @return resource
189 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200190 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700191 {
192 $sql = $this->_prep_query($sql);
193 return @cubrid_query($sql, $this->conn_id);
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Prep the query
200 *
201 * If needed, each database adapter can prep the query string
202 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700203 * @param string an SQL query
204 * @return string
205 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200206 protected function _prep_query($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700207 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700208 // No need to prepare
Esen Sagynov2e087942011-08-09 23:35:01 -0700209 return $sql;
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Begin Transaction
216 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700217 * @return bool
218 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200219 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700220 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700221 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200222 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700223 {
224 return TRUE;
225 }
226
227 // Reset the transaction failure flag.
228 // If the $test_mode flag is set to TRUE transactions will be rolled back
229 // even if the queries produce a successful result.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200230 $this->_trans_failure = ($test_mode === TRUE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700231
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700232 if (cubrid_get_autocommit($this->conn_id))
233 {
234 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
235 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700236
237 return TRUE;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Commit Transaction
244 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700245 * @return bool
246 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200247 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700248 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700249 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200250 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700251 {
252 return TRUE;
253 }
254
255 cubrid_commit($this->conn_id);
256
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700257 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
258 {
259 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
260 }
261
Esen Sagynov2e087942011-08-09 23:35:01 -0700262 return TRUE;
263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Rollback Transaction
269 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700270 * @return bool
271 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200272 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700273 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700274 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev7f55d612012-01-26 13:44:28 +0200275 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Esen Sagynov2e087942011-08-09 23:35:01 -0700276 {
277 return TRUE;
278 }
279
280 cubrid_rollback($this->conn_id);
281
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700282 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
283 {
284 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
285 }
286
Esen Sagynov2e087942011-08-09 23:35:01 -0700287 return TRUE;
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Escape String
294 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700295 * @param string
296 * @param bool whether or not the string will be used in a LIKE condition
297 * @return string
298 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200299 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700300 {
301 if (is_array($str))
302 {
303 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700304 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700305 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700306 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700307
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700308 return $str;
309 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700310
Andrey Andreev7f55d612012-01-26 13:44:28 +0200311 if (function_exists('cubrid_real_escape_string') && is_resource($this->conn_id))
Esen Sagynov2e087942011-08-09 23:35:01 -0700312 {
313 $str = cubrid_real_escape_string($str, $this->conn_id);
314 }
315 else
316 {
317 $str = addslashes($str);
318 }
319
320 // escape LIKE condition wildcards
321 if ($like === TRUE)
322 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200323 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Esen Sagynov2e087942011-08-09 23:35:01 -0700324 }
325
326 return $str;
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Affected Rows
333 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200334 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700335 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200336 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700337 {
338 return @cubrid_affected_rows($this->conn_id);
339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * Insert ID
345 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200346 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700347 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200348 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700349 {
350 return @cubrid_insert_id($this->conn_id);
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * "Count All" query
357 *
358 * Generates a platform-specific query string that counts all records in
359 * the specified table
360 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700361 * @param string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200362 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700363 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200364 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700365 {
366 if ($table == '')
367 {
368 return 0;
369 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200370 $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 -0700371 if ($query->num_rows() == 0)
372 {
373 return 0;
374 }
375
Andrey Andreev7f55d612012-01-26 13:44:28 +0200376 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500377 $this->_reset_select();
Andrey Andreev7f55d612012-01-26 13:44:28 +0200378 return (int) $query->numrows;
Esen Sagynov2e087942011-08-09 23:35:01 -0700379 }
380
381 // --------------------------------------------------------------------
382
383 /**
384 * List table query
385 *
386 * Generates a platform-specific query string so that the table names can be fetched
387 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700388 * @param boolean
389 * @return string
390 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200391 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700392 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200393 $sql = 'SHOW TABLES';
Esen Sagynov2e087942011-08-09 23:35:01 -0700394
Andrey Andreev7f55d612012-01-26 13:44:28 +0200395 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700396 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200397 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Esen Sagynov2e087942011-08-09 23:35:01 -0700398 }
399
400 return $sql;
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Show column query
407 *
408 * Generates a platform-specific query string so that the column names can be fetched
409 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700410 * @param string the table name
411 * @return string
412 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200413 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700414 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200415 return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Field data query
422 *
423 * Generates a platform-specific query so that the column data can be retrieved
424 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700425 * @param string the table name
Andrey Andreev7f55d612012-01-26 13:44:28 +0200426 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700427 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200428 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700429 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200430 return 'SELECT * FROM '.$table.' LIMIT 1';
Esen Sagynov2e087942011-08-09 23:35:01 -0700431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * The error message string
437 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700438 * @return string
439 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200440 protected function _error_message()
Esen Sagynov2e087942011-08-09 23:35:01 -0700441 {
442 return cubrid_error($this->conn_id);
443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * The error message number
449 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200450 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700451 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200452 protected function _error_number()
Esen Sagynov2e087942011-08-09 23:35:01 -0700453 {
454 return cubrid_errno($this->conn_id);
455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Escape the SQL Identifiers
461 *
462 * This function escapes column and table names
463 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700464 * @param string
465 * @return string
466 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200467 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700468 {
469 if ($this->_escape_char == '')
470 {
471 return $item;
472 }
473
474 foreach ($this->_reserved_identifiers as $id)
475 {
476 if (strpos($item, '.'.$id) !== FALSE)
477 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200478 $item = str_replace('.', $this->_escape_char.'.', $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700479
480 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200481 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700482 }
483 }
484
485 if (strpos($item, '.') !== FALSE)
486 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200487 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Esen Sagynov2e087942011-08-09 23:35:01 -0700488 }
489
490 // remove duplicates if the user already included the escape
Andrey Andreev7f55d612012-01-26 13:44:28 +0200491 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Esen Sagynov2e087942011-08-09 23:35:01 -0700492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * From Tables
498 *
499 * This function implicitly groups FROM tables so there is no confusion
500 * about operator precedence in harmony with SQL standards
501 *
Andrey Andreev7f55d612012-01-26 13:44:28 +0200502 * @param string the table name
503 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700504 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200505 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700506 {
507 if ( ! is_array($tables))
508 {
509 $tables = array($tables);
510 }
511
512 return '('.implode(', ', $tables).')';
513 }
514
515 // --------------------------------------------------------------------
516
517 /**
518 * Insert statement
519 *
520 * Generates a platform-specific insert 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 _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700528 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200529 return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700530 }
531
532 // --------------------------------------------------------------------
533
534
535 /**
536 * Replace statement
537 *
538 * Generates a platform-specific replace string from the supplied data
539 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700540 * @param string the table name
541 * @param array the insert keys
542 * @param array the insert values
543 * @return string
544 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200545 protected function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700546 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200547 return 'REPLACE INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700548 }
549
550 // --------------------------------------------------------------------
551
552 /**
553 * Insert_batch statement
554 *
555 * Generates a platform-specific insert string from the supplied data
556 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700557 * @param string the table name
558 * @param array the insert keys
559 * @param array the insert values
560 * @return string
561 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200562 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700563 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200564 return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES '.implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700565 }
566
567 // --------------------------------------------------------------------
568
Esen Sagynov2e087942011-08-09 23:35:01 -0700569 /**
570 * Update statement
571 *
572 * Generates a platform-specific update string from the supplied data
573 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700574 * @param string the table name
575 * @param array the update data
576 * @param array the where clause
577 * @param array the orderby clause
578 * @param array the limit clause
579 * @return string
580 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200581 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700582 {
583 foreach ($values as $key => $val)
584 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700585 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700586 }
587
Andrey Andreev7f55d612012-01-26 13:44:28 +0200588 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
589 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
590 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
591 .( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700592 }
593
594 // --------------------------------------------------------------------
595
596
597 /**
598 * Update_Batch statement
599 *
600 * Generates a platform-specific batch update string from the supplied data
601 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700602 * @param string the table name
603 * @param array the update data
604 * @param array the where clause
605 * @return string
606 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200607 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700608 {
609 $ids = array();
Andrey Andreev7f55d612012-01-26 13:44:28 +0200610 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700611
612 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
Andrey Andreev7f55d612012-01-26 13:44:28 +0200625 $sql = 'UPDATE '.$table.' SET ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700626 $cases = '';
627
628 foreach ($final as $k => $v)
629 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200630 $cases .= $k." = CASE \n"
631 .implode("\n", $v)
632 .'ELSE '.$k.' END, ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700633 }
634
Andrey Andreev7f55d612012-01-26 13:44:28 +0200635 return $sql.substr($cases, 0, -2)
636 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
637 .$index.' IN ('.implode(',', $ids).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700638 }
639
640 // --------------------------------------------------------------------
641
Esen Sagynov2e087942011-08-09 23:35:01 -0700642 /**
643 * Truncate statement
644 *
645 * Generates a platform-specific truncate string from the supplied data
646 * If the database does not support the truncate() command
647 * This function maps to "DELETE FROM table"
648 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700649 * @param string the table name
650 * @return string
651 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200652 protected function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700653 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200654 return 'TRUNCATE '.$table;
Esen Sagynov2e087942011-08-09 23:35:01 -0700655 }
656
657 // --------------------------------------------------------------------
658
659 /**
660 * Delete statement
661 *
662 * Generates a platform-specific delete string from the supplied data
663 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700664 * @param string the table name
665 * @param array the where clause
666 * @param string the limit clause
667 * @return string
668 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200669 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700670 {
671 $conditions = '';
672
673 if (count($where) > 0 OR count($like) > 0)
674 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200675 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Esen Sagynov2e087942011-08-09 23:35:01 -0700676
677 if (count($where) > 0 && count($like) > 0)
678 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200679 $conditions .= ' AND ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700680 }
681 $conditions .= implode("\n", $like);
682 }
683
Andrey Andreev7f55d612012-01-26 13:44:28 +0200684 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Esen Sagynov2e087942011-08-09 23:35:01 -0700685 }
686
687 // --------------------------------------------------------------------
688
689 /**
690 * Limit string
691 *
692 * Generates a platform-specific LIMIT clause
693 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700694 * @param string the sql query string
Andrey Andreev7f55d612012-01-26 13:44:28 +0200695 * @param int the number of rows to limit the query to
696 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700697 * @return string
698 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200699 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700700 {
Andrey Andreevc6953f42012-01-26 14:43:16 +0200701 return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Esen Sagynov2e087942011-08-09 23:35:01 -0700702 }
703
704 // --------------------------------------------------------------------
705
706 /**
707 * Close DB Connection
708 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700709 * @param resource
710 * @return void
711 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200712 protected function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700713 {
714 @cubrid_close($conn_id);
715 }
716
717}
718
Esen Sagynov2e087942011-08-09 23:35:01 -0700719/* End of file cubrid_driver.php */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200720/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */