blob: f6b08daa0a54e65eea162406828c56721ddc9981 [file] [log] [blame]
Andrey Andreev592f4ec2012-03-20 15:10:08 +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 Andreev592f4ec2012-03-20 15:10:08 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev592f4ec2012-03-20 15:10:08 +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 Andreev592f4ec2012-03-20 15:10:08 +020043 public $dbdriver = 'cubrid';
Esen Sagynov2e087942011-08-09 23:35:01 -070044
45 // The character used for escaping - no need in CUBRID
Andrey Andreev592f4ec2012-03-20 15:10:08 +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 Andreev592f4ec2012-03-20 15:10:08 +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 Andreev592f4ec2012-03-20 15:10:08 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' RAND()'; // database specific random keyword
Esen Sagynov2e087942011-08-09 23:35:01 -070059
60 /**
61 * Non-persistent database connection
62 *
Esen Sagynov2e087942011-08-09 23:35:01 -070063 * @return resource
64 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020065 public function db_connect()
Esen Sagynov2e087942011-08-09 23:35:01 -070066 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070067 // If no port is defined by the user, use the default value
Esen Sagynov2e087942011-08-09 23:35:01 -070068 if ($this->port == '')
69 {
Andrey Andreev592f4ec2012-03-20 15:10:08 +020070 // Default CUBRID Broker port
71 $this->port = 33000;
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070072 }
Esen Sagynov2e087942011-08-09 23:35:01 -070073
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070074 $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password);
Esen Sagynov2e087942011-08-09 23:35:01 -070075
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070076 if ($conn)
77 {
78 // Check if a user wants to run queries in dry, i.e. run the
79 // queries but not commit them.
80 if (isset($this->auto_commit) && ! $this->auto_commit)
81 {
82 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE);
83 }
84 else
85 {
86 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE);
87 $this->auto_commit = TRUE;
88 }
89 }
90
91 return $conn;
Esen Sagynov2e087942011-08-09 23:35:01 -070092 }
93
94 // --------------------------------------------------------------------
95
96 /**
97 * Persistent database connection
Andrey Andreev592f4ec2012-03-20 15:10:08 +020098 *
Esen Sagynov2e087942011-08-09 23:35:01 -070099 * In CUBRID persistent DB connection is supported natively in CUBRID
100 * engine which can be configured in the CUBRID Broker configuration
101 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
102 * connections established between the client application and the
103 * server will become persistent. This is calling the same
Timothy Warren175e2892012-03-19 19:08:39 -0400104 * @cubrid_connect function will establish persisten connection
Esen Sagynov2e087942011-08-09 23:35:01 -0700105 * considering that the CCI_PCONNECT is ON.
106 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700107 * @return resource
108 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200109 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700110 {
111 return $this->db_connect();
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Reconnect
118 *
119 * Keep / reestablish the db connection if no queries have been
120 * sent for a length of time exceeding the server's idle timeout
121 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700122 * @return void
123 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200124 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700125 {
126 if (cubrid_ping($this->conn_id) === FALSE)
127 {
128 $this->conn_id = FALSE;
129 }
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Select the database
136 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700137 * @return resource
138 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200139 public function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700140 {
141 // In CUBRID there is no need to select a database as the database
142 // is chosen at the connection time.
143 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700144 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700145 return cubrid_ping($this->conn_id);
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200151 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700152 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700153 * @return string
154 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200155 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700156 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200157 return isset($this->data_cache['version'])
158 ? $this->data_cache['version']
159 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Execute the query
166 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700167 * @param string an SQL query
168 * @return resource
169 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200170 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700171 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 return @cubrid_query($sql, $this->conn_id);
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700178 * Begin Transaction
179 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700180 * @return bool
181 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200182 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 {
184 if ( ! $this->trans_enabled)
185 {
186 return TRUE;
187 }
188
189 // When transactions are nested we only begin/commit/rollback the outermost ones
190 if ($this->_trans_depth > 0)
191 {
192 return TRUE;
193 }
194
195 // Reset the transaction failure flag.
196 // If the $test_mode flag is set to TRUE transactions will be rolled back
197 // even if the queries produce a successful result.
198 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
199
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700200 if (cubrid_get_autocommit($this->conn_id))
201 {
202 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
203 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700204
205 return TRUE;
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Commit Transaction
212 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700213 * @return bool
214 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200215 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700216 {
217 if ( ! $this->trans_enabled)
218 {
219 return TRUE;
220 }
221
222 // When transactions are nested we only begin/commit/rollback the outermost ones
223 if ($this->_trans_depth > 0)
224 {
225 return TRUE;
226 }
227
228 cubrid_commit($this->conn_id);
229
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700230 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
231 {
232 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
233 }
234
Esen Sagynov2e087942011-08-09 23:35:01 -0700235 return TRUE;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Rollback Transaction
242 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700243 * @return bool
244 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200245 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700246 {
247 if ( ! $this->trans_enabled)
248 {
249 return TRUE;
250 }
251
252 // When transactions are nested we only begin/commit/rollback the outermost ones
253 if ($this->_trans_depth > 0)
254 {
255 return TRUE;
256 }
257
258 cubrid_rollback($this->conn_id);
259
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700260 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
261 {
262 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
263 }
264
Esen Sagynov2e087942011-08-09 23:35:01 -0700265 return TRUE;
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * Escape String
272 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700273 * @param string
274 * @param bool whether or not the string will be used in a LIKE condition
275 * @return string
276 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200277 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700278 {
279 if (is_array($str))
280 {
281 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700282 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700283 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700284 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700285
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700286 return $str;
287 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700288
289 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
290 {
291 $str = cubrid_real_escape_string($str, $this->conn_id);
292 }
293 else
294 {
295 $str = addslashes($str);
296 }
297
298 // escape LIKE condition wildcards
299 if ($like === TRUE)
300 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700301 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
302 }
303
304 return $str;
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Affected Rows
311 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200312 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700313 */
Timothy Warren175e2892012-03-19 19:08:39 -0400314 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700315 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200316 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * Insert ID
323 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200324 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700325 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200326 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700327 {
328 return @cubrid_insert_id($this->conn_id);
329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * "Count All" query
335 *
336 * Generates a platform-specific query string that counts all records in
337 * the specified table
338 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700339 * @param string
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200340 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700341 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200342 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700343 {
344 if ($table == '')
345 {
346 return 0;
347 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700348
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200349 $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 -0700350 if ($query->num_rows() == 0)
351 {
352 return 0;
353 }
354
355 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500356 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700357 return (int) $row->numrows;
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * List table query
364 *
365 * Generates a platform-specific query string so that the table names can be fetched
366 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200367 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700368 * @return string
369 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200370 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700371 {
372 $sql = "SHOW TABLES";
373
374 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
375 {
376 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
377 }
378
379 return $sql;
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Show column query
386 *
387 * Generates a platform-specific query string so that the column names can be fetched
388 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700389 * @param string the table name
390 * @return string
391 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200392 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700393 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200394 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700395 }
396
397 // --------------------------------------------------------------------
398
399 /**
400 * Field data query
401 *
402 * Generates a platform-specific query so that the column data can be retrieved
403 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700404 * @param string the table name
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200405 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700406 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200407 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700408 {
409 return "SELECT * FROM ".$table." LIMIT 1";
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200415 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700416 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200417 * Returns an array containing code and message of the last
418 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700419 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200420 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700421 */
Timothy Warren175e2892012-03-19 19:08:39 -0400422 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700423 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200424 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700425 }
426
Esen Sagynov2e087942011-08-09 23:35:01 -0700427 /**
428 * Escape the SQL Identifiers
429 *
Timothy Warren175e2892012-03-19 19:08:39 -0400430 * This function escapes column and table names
Esen Sagynov2e087942011-08-09 23:35:01 -0700431 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700432 * @param string
433 * @return string
434 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200435 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700436 {
437 if ($this->_escape_char == '')
438 {
439 return $item;
440 }
441
442 foreach ($this->_reserved_identifiers as $id)
443 {
444 if (strpos($item, '.'.$id) !== FALSE)
445 {
446 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
447
448 // remove duplicates if the user already included the escape
449 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
450 }
451 }
452
453 if (strpos($item, '.') !== FALSE)
454 {
455 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
456 }
457 else
458 {
459 $str = $this->_escape_char.$item.$this->_escape_char;
460 }
461
462 // remove duplicates if the user already included the escape
463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * From Tables
470 *
Timothy Warren175e2892012-03-19 19:08:39 -0400471 * This function implicitly groups FROM tables so there is no confusion
Esen Sagynov2e087942011-08-09 23:35:01 -0700472 * about operator precedence in harmony with SQL standards
473 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200474 * @param array
475 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700476 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200477 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700478 {
479 if ( ! is_array($tables))
480 {
481 $tables = array($tables);
482 }
483
484 return '('.implode(', ', $tables).')';
485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Insert statement
491 *
492 * Generates a platform-specific insert string from the supplied data
493 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700494 * @param string the table name
495 * @param array the insert keys
496 * @param array the insert values
497 * @return string
498 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200499 protected function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700500 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700501 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700502 }
503
504 // --------------------------------------------------------------------
505
506
507 /**
508 * Replace statement
509 *
510 * Generates a platform-specific replace string from the supplied data
511 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700512 * @param string the table name
513 * @param array the insert keys
514 * @param array the insert values
515 * @return string
516 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200517 protected function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700518 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700519 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * Insert_batch statement
526 *
527 * Generates a platform-specific insert string from the supplied data
528 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700529 * @param string the table name
530 * @param array the insert keys
531 * @param array the insert values
532 * @return string
533 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200534 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700535 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700536 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700537 }
538
539 // --------------------------------------------------------------------
540
541
542 /**
543 * Update statement
544 *
545 * Generates a platform-specific update string from the supplied data
546 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700547 * @param string the table name
548 * @param array the update data
549 * @param array the where clause
550 * @param array the orderby clause
551 * @param array the limit clause
552 * @return string
553 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200554 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700555 {
556 foreach ($values as $key => $val)
557 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700558 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700559 }
560
561 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
562
563 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
564
565 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
566
567 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
568
569 $sql .= $orderby.$limit;
570
571 return $sql;
572 }
573
574 // --------------------------------------------------------------------
575
576
577 /**
578 * Update_Batch statement
579 *
580 * Generates a platform-specific batch update string from the supplied data
581 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700582 * @param string the table name
583 * @param array the update data
584 * @param array the where clause
585 * @return string
586 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200587 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700588 {
589 $ids = array();
590 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
591
592 foreach ($values as $key => $val)
593 {
594 $ids[] = $val[$index];
595
596 foreach (array_keys($val) as $field)
597 {
598 if ($field != $index)
599 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700600 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700601 }
602 }
603 }
604
605 $sql = "UPDATE ".$table." SET ";
606 $cases = '';
607
608 foreach ($final as $k => $v)
609 {
610 $cases .= $k.' = CASE '."\n";
611 foreach ($v as $row)
612 {
613 $cases .= $row."\n";
614 }
615
616 $cases .= 'ELSE '.$k.' END, ';
617 }
618
619 $sql .= substr($cases, 0, -2);
620
621 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
622
623 return $sql;
624 }
625
626 // --------------------------------------------------------------------
627
Esen Sagynov2e087942011-08-09 23:35:01 -0700628 /**
629 * Truncate statement
630 *
631 * Generates a platform-specific truncate string from the supplied data
632 * If the database does not support the truncate() command
Timothy Warren175e2892012-03-19 19:08:39 -0400633 * This function maps to "DELETE FROM table"
Esen Sagynov2e087942011-08-09 23:35:01 -0700634 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700635 * @param string the table name
636 * @return string
637 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200638 protected function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700639 {
640 return "TRUNCATE ".$table;
641 }
642
643 // --------------------------------------------------------------------
644
645 /**
646 * Delete statement
647 *
648 * Generates a platform-specific delete string from the supplied data
649 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700650 * @param string the table name
651 * @param array the where clause
652 * @param string the limit clause
653 * @return string
654 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200655 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700656 {
657 $conditions = '';
658
659 if (count($where) > 0 OR count($like) > 0)
660 {
661 $conditions = "\nWHERE ";
662 $conditions .= implode("\n", $this->ar_where);
663
664 if (count($where) > 0 && count($like) > 0)
665 {
666 $conditions .= " AND ";
667 }
668 $conditions .= implode("\n", $like);
669 }
670
671 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
672
673 return "DELETE FROM ".$table.$conditions.$limit;
674 }
675
676 // --------------------------------------------------------------------
677
678 /**
679 * Limit string
680 *
681 * Generates a platform-specific LIMIT clause
682 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700683 * @param string the sql query string
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200684 * @param int the number of rows to limit the query to
685 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700686 * @return string
687 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200688 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700689 {
690 if ($offset == 0)
691 {
692 $offset = '';
693 }
694 else
695 {
696 $offset .= ", ";
697 }
698
699 return $sql."LIMIT ".$offset.$limit;
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * Close DB Connection
706 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700707 * @param resource
708 * @return void
709 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +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 Andreev063f5962012-02-27 12:20:52 +0200718/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */