blob: cc9f23d9efb39946183fd420d75598167aee14f9 [file] [log] [blame]
Derek Jonesf4a4bd82011-10-20 12:18:42 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
30/**
31 * CUBRID Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070040 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070041 * @link http://codeigniter.com/user_guide/database/
42 */
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070043class CI_DB_cubrid_driver extends CI_DB {
44
45 // Default CUBRID Broker port. Will be used unless user
46 // explicitly specifies another one.
47 const DEFAULT_PORT = 33000;
48
Esen Sagynov2e087942011-08-09 23:35:01 -070049 var $dbdriver = 'cubrid';
50
51 // The character used for escaping - no need in CUBRID
52 var $_escape_char = '';
53
54 // clause and character used for LIKE escape sequences - not used in CUBRID
55 var $_like_escape_str = '';
56 var $_like_escape_chr = '';
57
58 /**
59 * The syntax to count rows is slightly different across different
60 * database engines, so this string appears in each driver and is
61 * used for the count_all() and count_all_results() functions.
62 */
63 var $_count_string = 'SELECT COUNT(*) AS ';
64 var $_random_keyword = ' RAND()'; // database specific random keyword
65
66 /**
67 * Non-persistent database connection
68 *
Esen Sagynov2e087942011-08-09 23:35:01 -070069 * @return resource
70 */
Timothy Warren70b21012012-03-19 17:33:05 -040071 protected function db_connect()
Esen Sagynov2e087942011-08-09 23:35:01 -070072 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070073 // If no port is defined by the user, use the default value
Esen Sagynov2e087942011-08-09 23:35:01 -070074 if ($this->port == '')
75 {
76 $this->port = self::DEFAULT_PORT;
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070077 }
Esen Sagynov2e087942011-08-09 23:35:01 -070078
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
Timothy Warren70b21012012-03-19 17:33:05 -0400108 * @cubrid_connect public function will establish persisten connection
Esen Sagynov2e087942011-08-09 23:35:01 -0700109 * considering that the CCI_PCONNECT is ON.
110 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700111 * @return resource
112 */
Timothy Warren70b21012012-03-19 17:33:05 -0400113 protected function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700114 {
115 return $this->db_connect();
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Reconnect
122 *
123 * Keep / reestablish the db connection if no queries have been
124 * sent for a length of time exceeding the server's idle timeout
125 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700126 * @return void
127 */
Timothy Warren70b21012012-03-19 17:33:05 -0400128 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700129 {
130 if (cubrid_ping($this->conn_id) === FALSE)
131 {
132 $this->conn_id = FALSE;
133 }
134 }
135
136 // --------------------------------------------------------------------
137
138 /**
139 * Select the database
140 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700141 * @return resource
142 */
Timothy Warren70b21012012-03-19 17:33:05 -0400143 public function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700144 {
145 // In CUBRID there is no need to select a database as the database
146 // is chosen at the connection time.
147 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700148 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700149 return cubrid_ping($this->conn_id);
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200155 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700156 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700157 * @return string
158 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200159 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700160 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200161 return isset($this->data_cache['version'])
162 ? $this->data_cache['version']
163 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Execute the query
170 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700171 * @param string an SQL query
172 * @return resource
173 */
Timothy Warren70b21012012-03-19 17:33:05 -0400174 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700175 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 return @cubrid_query($sql, $this->conn_id);
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700182 * Begin Transaction
183 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700184 * @return bool
185 */
Timothy Warren70b21012012-03-19 17:33:05 -0400186 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 {
188 if ( ! $this->trans_enabled)
189 {
190 return TRUE;
191 }
192
193 // When transactions are nested we only begin/commit/rollback the outermost ones
194 if ($this->_trans_depth > 0)
195 {
196 return TRUE;
197 }
198
199 // Reset the transaction failure flag.
200 // If the $test_mode flag is set to TRUE transactions will be rolled back
201 // even if the queries produce a successful result.
202 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
203
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700204 if (cubrid_get_autocommit($this->conn_id))
205 {
206 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
207 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700208
209 return TRUE;
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Commit Transaction
216 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700217 * @return bool
218 */
Timothy Warren70b21012012-03-19 17:33:05 -0400219 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700220 {
221 if ( ! $this->trans_enabled)
222 {
223 return TRUE;
224 }
225
226 // When transactions are nested we only begin/commit/rollback the outermost ones
227 if ($this->_trans_depth > 0)
228 {
229 return TRUE;
230 }
231
232 cubrid_commit($this->conn_id);
233
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700234 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
235 {
236 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
237 }
238
Esen Sagynov2e087942011-08-09 23:35:01 -0700239 return TRUE;
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Rollback Transaction
246 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700247 * @return bool
248 */
Timothy Warren70b21012012-03-19 17:33:05 -0400249 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700250 {
251 if ( ! $this->trans_enabled)
252 {
253 return TRUE;
254 }
255
256 // When transactions are nested we only begin/commit/rollback the outermost ones
257 if ($this->_trans_depth > 0)
258 {
259 return TRUE;
260 }
261
262 cubrid_rollback($this->conn_id);
263
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700264 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
265 {
266 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
267 }
268
Esen Sagynov2e087942011-08-09 23:35:01 -0700269 return TRUE;
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Escape String
276 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700277 * @param string
278 * @param bool whether or not the string will be used in a LIKE condition
279 * @return string
280 */
Timothy Warren70b21012012-03-19 17:33:05 -0400281 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700282 {
283 if (is_array($str))
284 {
285 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700286 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700287 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700288 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700289
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700290 return $str;
291 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700292
293 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
294 {
295 $str = cubrid_real_escape_string($str, $this->conn_id);
296 }
297 else
298 {
299 $str = addslashes($str);
300 }
301
302 // escape LIKE condition wildcards
303 if ($like === TRUE)
304 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700305 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
306 }
307
308 return $str;
309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * Affected Rows
315 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200316 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700317 */
Timothy Warren70b21012012-03-19 17:33:05 -0400318 public public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700319 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200320 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Insert ID
327 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700328 * @return integer
329 */
Timothy Warren70b21012012-03-19 17:33:05 -0400330 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700331 {
332 return @cubrid_insert_id($this->conn_id);
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * "Count All" query
339 *
340 * Generates a platform-specific query string that counts all records in
341 * the specified table
342 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700343 * @param string
344 * @return string
345 */
Timothy Warren70b21012012-03-19 17:33:05 -0400346 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700347 {
348 if ($table == '')
349 {
350 return 0;
351 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700352
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200353 $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 -0700354 if ($query->num_rows() == 0)
355 {
356 return 0;
357 }
358
359 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500360 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700361 return (int) $row->numrows;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * List table query
368 *
369 * Generates a platform-specific query string so that the table names can be fetched
370 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700371 * @param boolean
372 * @return string
373 */
Timothy Warren70b21012012-03-19 17:33:05 -0400374 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700375 {
376 $sql = "SHOW TABLES";
377
378 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
379 {
380 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
381 }
382
383 return $sql;
384 }
385
386 // --------------------------------------------------------------------
387
388 /**
389 * Show column query
390 *
391 * Generates a platform-specific query string so that the column names can be fetched
392 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700393 * @param string the table name
394 * @return string
395 */
Timothy Warren70b21012012-03-19 17:33:05 -0400396 public function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700397 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200398 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * Field data query
405 *
406 * Generates a platform-specific query so that the column data can be retrieved
407 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700408 * @param string the table name
409 * @return object
410 */
Timothy Warren70b21012012-03-19 17:33:05 -0400411 public function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700412 {
413 return "SELECT * FROM ".$table." LIMIT 1";
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200419 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700420 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200421 * Returns an array containing code and message of the last
422 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700423 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200424 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700425 */
Timothy Warren70b21012012-03-19 17:33:05 -0400426 public public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700427 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200428 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700429 }
430
Esen Sagynov2e087942011-08-09 23:35:01 -0700431 /**
432 * Escape the SQL Identifiers
433 *
Timothy Warren70b21012012-03-19 17:33:05 -0400434 * This public function escapes column and table names
Esen Sagynov2e087942011-08-09 23:35:01 -0700435 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700436 * @param string
437 * @return string
438 */
Timothy Warren70b21012012-03-19 17:33:05 -0400439 protected function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700440 {
441 if ($this->_escape_char == '')
442 {
443 return $item;
444 }
445
446 foreach ($this->_reserved_identifiers as $id)
447 {
448 if (strpos($item, '.'.$id) !== FALSE)
449 {
450 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
451
452 // remove duplicates if the user already included the escape
453 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
454 }
455 }
456
457 if (strpos($item, '.') !== FALSE)
458 {
459 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
460 }
461 else
462 {
463 $str = $this->_escape_char.$item.$this->_escape_char;
464 }
465
466 // remove duplicates if the user already included the escape
467 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
468 }
469
470 // --------------------------------------------------------------------
471
472 /**
473 * From Tables
474 *
Timothy Warren70b21012012-03-19 17:33:05 -0400475 * This public function implicitly groups FROM tables so there is no confusion
Esen Sagynov2e087942011-08-09 23:35:01 -0700476 * about operator precedence in harmony with SQL standards
477 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700478 * @param type
479 * @return type
480 */
Timothy Warren70b21012012-03-19 17:33:05 -0400481 public function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700482 {
483 if ( ! is_array($tables))
484 {
485 $tables = array($tables);
486 }
487
488 return '('.implode(', ', $tables).')';
489 }
490
491 // --------------------------------------------------------------------
492
493 /**
494 * Insert statement
495 *
496 * Generates a platform-specific insert string from the supplied data
497 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700498 * @param string the table name
499 * @param array the insert keys
500 * @param array the insert values
501 * @return string
502 */
Timothy Warren70b21012012-03-19 17:33:05 -0400503 public function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700504 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700505 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700506 }
507
508 // --------------------------------------------------------------------
509
510
511 /**
512 * Replace statement
513 *
514 * Generates a platform-specific replace string from the supplied data
515 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700516 * @param string the table name
517 * @param array the insert keys
518 * @param array the insert values
519 * @return string
520 */
Timothy Warren70b21012012-03-19 17:33:05 -0400521 public function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700522 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700523 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700524 }
525
526 // --------------------------------------------------------------------
527
528 /**
529 * Insert_batch statement
530 *
531 * Generates a platform-specific insert string from the supplied data
532 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700533 * @param string the table name
534 * @param array the insert keys
535 * @param array the insert values
536 * @return string
537 */
Timothy Warren70b21012012-03-19 17:33:05 -0400538 public function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700539 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700540 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700541 }
542
543 // --------------------------------------------------------------------
544
545
546 /**
547 * Update statement
548 *
549 * Generates a platform-specific update string from the supplied data
550 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700551 * @param string the table name
552 * @param array the update data
553 * @param array the where clause
554 * @param array the orderby clause
555 * @param array the limit clause
556 * @return string
557 */
Timothy Warren70b21012012-03-19 17:33:05 -0400558 public function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700559 {
560 foreach ($values as $key => $val)
561 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700562 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700563 }
564
565 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
566
567 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
568
569 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
570
571 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
572
573 $sql .= $orderby.$limit;
574
575 return $sql;
576 }
577
578 // --------------------------------------------------------------------
579
580
581 /**
582 * Update_Batch statement
583 *
584 * Generates a platform-specific batch update string from the supplied data
585 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700586 * @param string the table name
587 * @param array the update data
588 * @param array the where clause
589 * @return string
590 */
Timothy Warren70b21012012-03-19 17:33:05 -0400591 public function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700592 {
593 $ids = array();
594 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
595
596 foreach ($values as $key => $val)
597 {
598 $ids[] = $val[$index];
599
600 foreach (array_keys($val) as $field)
601 {
602 if ($field != $index)
603 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700604 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700605 }
606 }
607 }
608
609 $sql = "UPDATE ".$table." SET ";
610 $cases = '';
611
612 foreach ($final as $k => $v)
613 {
614 $cases .= $k.' = CASE '."\n";
615 foreach ($v as $row)
616 {
617 $cases .= $row."\n";
618 }
619
620 $cases .= 'ELSE '.$k.' END, ';
621 }
622
623 $sql .= substr($cases, 0, -2);
624
625 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
626
627 return $sql;
628 }
629
630 // --------------------------------------------------------------------
631
632
633 /**
634 * Truncate statement
635 *
636 * Generates a platform-specific truncate string from the supplied data
637 * If the database does not support the truncate() command
Timothy Warren70b21012012-03-19 17:33:05 -0400638 * This public function maps to "DELETE FROM table"
Esen Sagynov2e087942011-08-09 23:35:01 -0700639 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700640 * @param string the table name
641 * @return string
642 */
Timothy Warren70b21012012-03-19 17:33:05 -0400643 public function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700644 {
645 return "TRUNCATE ".$table;
646 }
647
648 // --------------------------------------------------------------------
649
650 /**
651 * Delete statement
652 *
653 * Generates a platform-specific delete string from the supplied data
654 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700655 * @param string the table name
656 * @param array the where clause
657 * @param string the limit clause
658 * @return string
659 */
Timothy Warren70b21012012-03-19 17:33:05 -0400660 public function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700661 {
662 $conditions = '';
663
664 if (count($where) > 0 OR count($like) > 0)
665 {
666 $conditions = "\nWHERE ";
667 $conditions .= implode("\n", $this->ar_where);
668
669 if (count($where) > 0 && count($like) > 0)
670 {
671 $conditions .= " AND ";
672 }
673 $conditions .= implode("\n", $like);
674 }
675
676 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
677
678 return "DELETE FROM ".$table.$conditions.$limit;
679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Limit string
685 *
686 * Generates a platform-specific LIMIT clause
687 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700688 * @param string the sql query string
689 * @param integer the number of rows to limit the query to
690 * @param integer the offset value
691 * @return string
692 */
Timothy Warren70b21012012-03-19 17:33:05 -0400693 public function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700694 {
695 if ($offset == 0)
696 {
697 $offset = '';
698 }
699 else
700 {
701 $offset .= ", ";
702 }
703
704 return $sql."LIMIT ".$offset.$limit;
705 }
706
707 // --------------------------------------------------------------------
708
709 /**
710 * Close DB Connection
711 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700712 * @param resource
713 * @return void
714 */
Timothy Warren70b21012012-03-19 17:33:05 -0400715 public function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700716 {
717 @cubrid_close($conn_id);
718 }
719
720}
721
722
723/* End of file cubrid_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200724/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */