blob: 7b468bc5cbbd36f9ddb2d10fd0363a36b0da0e74 [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
104 * @cubrid_connect function will establish persisten connection
105 * 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 {
172 $sql = $this->_prep_query($sql);
173 return @cubrid_query($sql, $this->conn_id);
174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Prep the query
180 *
181 * If needed, each database adapter can prep the query string
182 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 * @param string an SQL query
184 * @return string
185 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200186 protected function _prep_query($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700188 // No need to prepare
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 return $sql;
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Begin Transaction
196 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700197 * @return bool
198 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200199 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700200 {
201 if ( ! $this->trans_enabled)
202 {
203 return TRUE;
204 }
205
206 // When transactions are nested we only begin/commit/rollback the outermost ones
207 if ($this->_trans_depth > 0)
208 {
209 return TRUE;
210 }
211
212 // Reset the transaction failure flag.
213 // If the $test_mode flag is set to TRUE transactions will be rolled back
214 // even if the queries produce a successful result.
215 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
216
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700217 if (cubrid_get_autocommit($this->conn_id))
218 {
219 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
220 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700221
222 return TRUE;
223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Commit Transaction
229 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700230 * @return bool
231 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200232 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700233 {
234 if ( ! $this->trans_enabled)
235 {
236 return TRUE;
237 }
238
239 // When transactions are nested we only begin/commit/rollback the outermost ones
240 if ($this->_trans_depth > 0)
241 {
242 return TRUE;
243 }
244
245 cubrid_commit($this->conn_id);
246
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700247 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
248 {
249 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
250 }
251
Esen Sagynov2e087942011-08-09 23:35:01 -0700252 return TRUE;
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Rollback Transaction
259 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700260 * @return bool
261 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200262 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700263 {
264 if ( ! $this->trans_enabled)
265 {
266 return TRUE;
267 }
268
269 // When transactions are nested we only begin/commit/rollback the outermost ones
270 if ($this->_trans_depth > 0)
271 {
272 return TRUE;
273 }
274
275 cubrid_rollback($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 * Escape String
289 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700290 * @param string
291 * @param bool whether or not the string will be used in a LIKE condition
292 * @return string
293 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200294 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700295 {
296 if (is_array($str))
297 {
298 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700299 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700300 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700301 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700302
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700303 return $str;
304 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700305
306 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
307 {
308 $str = cubrid_real_escape_string($str, $this->conn_id);
309 }
310 else
311 {
312 $str = addslashes($str);
313 }
314
315 // escape LIKE condition wildcards
316 if ($like === TRUE)
317 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700318 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
319 }
320
321 return $str;
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * Affected Rows
328 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200329 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700330 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200331 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700332 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200333 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Insert ID
340 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200341 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700342 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200343 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700344 {
345 return @cubrid_insert_id($this->conn_id);
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * "Count All" query
352 *
353 * Generates a platform-specific query string that counts all records in
354 * the specified table
355 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700356 * @param string
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200357 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700358 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200359 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700360 {
361 if ($table == '')
362 {
363 return 0;
364 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700365
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200366 $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 -0700367 if ($query->num_rows() == 0)
368 {
369 return 0;
370 }
371
372 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500373 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700374 return (int) $row->numrows;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * List table query
381 *
382 * Generates a platform-specific query string so that the table names can be fetched
383 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200384 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700385 * @return string
386 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200387 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700388 {
389 $sql = "SHOW TABLES";
390
391 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
392 {
393 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
394 }
395
396 return $sql;
397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Show column query
403 *
404 * Generates a platform-specific query string so that the column names can be fetched
405 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700406 * @param string the table name
407 * @return string
408 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200409 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700410 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200411 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700412 }
413
414 // --------------------------------------------------------------------
415
416 /**
417 * Field data query
418 *
419 * Generates a platform-specific query so that the column data can be retrieved
420 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700421 * @param string the table name
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200422 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700423 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200424 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700425 {
426 return "SELECT * FROM ".$table." LIMIT 1";
427 }
428
429 // --------------------------------------------------------------------
430
431 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200432 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700433 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200434 * Returns an array containing code and message of the last
435 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700436 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200437 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700438 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200439 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700440 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200441 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700442 }
443
Esen Sagynov2e087942011-08-09 23:35:01 -0700444 /**
445 * Escape the SQL Identifiers
446 *
447 * This function escapes column and table names
448 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700449 * @param string
450 * @return string
451 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200452 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700453 {
454 if ($this->_escape_char == '')
455 {
456 return $item;
457 }
458
459 foreach ($this->_reserved_identifiers as $id)
460 {
461 if (strpos($item, '.'.$id) !== FALSE)
462 {
463 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
464
465 // remove duplicates if the user already included the escape
466 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
467 }
468 }
469
470 if (strpos($item, '.') !== FALSE)
471 {
472 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
473 }
474 else
475 {
476 $str = $this->_escape_char.$item.$this->_escape_char;
477 }
478
479 // remove duplicates if the user already included the escape
480 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
481 }
482
483 // --------------------------------------------------------------------
484
485 /**
486 * From Tables
487 *
488 * This function implicitly groups FROM tables so there is no confusion
489 * about operator precedence in harmony with SQL standards
490 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200491 * @param array
492 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700493 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200494 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700495 {
496 if ( ! is_array($tables))
497 {
498 $tables = array($tables);
499 }
500
501 return '('.implode(', ', $tables).')';
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Insert statement
508 *
509 * Generates a platform-specific insert string from the supplied data
510 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700511 * @param string the table name
512 * @param array the insert keys
513 * @param array the insert values
514 * @return string
515 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200516 protected function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700517 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700518 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700519 }
520
521 // --------------------------------------------------------------------
522
523
524 /**
525 * Replace statement
526 *
527 * Generates a platform-specific replace 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 _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700535 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700536 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Insert_batch statement
543 *
544 * Generates a platform-specific insert string from the supplied data
545 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700546 * @param string the table name
547 * @param array the insert keys
548 * @param array the insert values
549 * @return string
550 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200551 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700552 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700553 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700554 }
555
556 // --------------------------------------------------------------------
557
558
559 /**
560 * Update statement
561 *
562 * Generates a platform-specific update string from the supplied data
563 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700564 * @param string the table name
565 * @param array the update data
566 * @param array the where clause
567 * @param array the orderby clause
568 * @param array the limit clause
569 * @return string
570 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200571 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700572 {
573 foreach ($values as $key => $val)
574 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700575 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700576 }
577
578 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
579
580 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
581
582 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
583
584 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
585
586 $sql .= $orderby.$limit;
587
588 return $sql;
589 }
590
591 // --------------------------------------------------------------------
592
593
594 /**
595 * Update_Batch statement
596 *
597 * Generates a platform-specific batch update string from the supplied data
598 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700599 * @param string the table name
600 * @param array the update data
601 * @param array the where clause
602 * @return string
603 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200604 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700605 {
606 $ids = array();
607 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
608
609 foreach ($values as $key => $val)
610 {
611 $ids[] = $val[$index];
612
613 foreach (array_keys($val) as $field)
614 {
615 if ($field != $index)
616 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700617 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700618 }
619 }
620 }
621
622 $sql = "UPDATE ".$table." SET ";
623 $cases = '';
624
625 foreach ($final as $k => $v)
626 {
627 $cases .= $k.' = CASE '."\n";
628 foreach ($v as $row)
629 {
630 $cases .= $row."\n";
631 }
632
633 $cases .= 'ELSE '.$k.' END, ';
634 }
635
636 $sql .= substr($cases, 0, -2);
637
638 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
639
640 return $sql;
641 }
642
643 // --------------------------------------------------------------------
644
Esen Sagynov2e087942011-08-09 23:35:01 -0700645 /**
646 * Truncate statement
647 *
648 * Generates a platform-specific truncate string from the supplied data
649 * If the database does not support the truncate() command
650 * This function maps to "DELETE FROM table"
651 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700652 * @param string the table name
653 * @return string
654 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200655 protected function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700656 {
657 return "TRUNCATE ".$table;
658 }
659
660 // --------------------------------------------------------------------
661
662 /**
663 * Delete statement
664 *
665 * Generates a platform-specific delete string from the supplied data
666 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700667 * @param string the table name
668 * @param array the where clause
669 * @param string the limit clause
670 * @return string
671 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200672 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700673 {
674 $conditions = '';
675
676 if (count($where) > 0 OR count($like) > 0)
677 {
678 $conditions = "\nWHERE ";
679 $conditions .= implode("\n", $this->ar_where);
680
681 if (count($where) > 0 && count($like) > 0)
682 {
683 $conditions .= " AND ";
684 }
685 $conditions .= implode("\n", $like);
686 }
687
688 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
689
690 return "DELETE FROM ".$table.$conditions.$limit;
691 }
692
693 // --------------------------------------------------------------------
694
695 /**
696 * Limit string
697 *
698 * Generates a platform-specific LIMIT clause
699 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700700 * @param string the sql query string
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200701 * @param int the number of rows to limit the query to
702 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700703 * @return string
704 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200705 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700706 {
707 if ($offset == 0)
708 {
709 $offset = '';
710 }
711 else
712 {
713 $offset .= ", ";
714 }
715
716 return $sql."LIMIT ".$offset.$limit;
717 }
718
719 // --------------------------------------------------------------------
720
721 /**
722 * Close DB Connection
723 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700724 * @param resource
725 * @return void
726 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200727 protected function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700728 {
729 @cubrid_close($conn_id);
730 }
731
732}
733
Esen Sagynov2e087942011-08-09 23:35:01 -0700734/* End of file cubrid_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200735/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */