blob: 32bd8a8b253145ba60bf161bdfb50de297cec2a6 [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 *
Timothy Warren175e2892012-03-19 19:08:39 -040069 * @access private called by the base class
Esen Sagynov2e087942011-08-09 23:35:01 -070070 * @return resource
71 */
Timothy Warren175e2892012-03-19 19:08:39 -040072 function db_connect()
Esen Sagynov2e087942011-08-09 23:35:01 -070073 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070074 // If no port is defined by the user, use the default value
Esen Sagynov2e087942011-08-09 23:35:01 -070075 if ($this->port == '')
76 {
77 $this->port = self::DEFAULT_PORT;
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070078 }
Esen Sagynov2e087942011-08-09 23:35:01 -070079
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070080 $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password);
Esen Sagynov2e087942011-08-09 23:35:01 -070081
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070082 if ($conn)
83 {
84 // Check if a user wants to run queries in dry, i.e. run the
85 // queries but not commit them.
86 if (isset($this->auto_commit) && ! $this->auto_commit)
87 {
88 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE);
89 }
90 else
91 {
92 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE);
93 $this->auto_commit = TRUE;
94 }
95 }
96
97 return $conn;
Esen Sagynov2e087942011-08-09 23:35:01 -070098 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Persistent database connection
104 * In CUBRID persistent DB connection is supported natively in CUBRID
105 * engine which can be configured in the CUBRID Broker configuration
106 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
107 * connections established between the client application and the
108 * server will become persistent. This is calling the same
Timothy Warren175e2892012-03-19 19:08:39 -0400109 * @cubrid_connect function will establish persisten connection
Esen Sagynov2e087942011-08-09 23:35:01 -0700110 * considering that the CCI_PCONNECT is ON.
111 *
Timothy Warren175e2892012-03-19 19:08:39 -0400112 * @access private called by the base class
Esen Sagynov2e087942011-08-09 23:35:01 -0700113 * @return resource
114 */
Timothy Warren175e2892012-03-19 19:08:39 -0400115 function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700116 {
117 return $this->db_connect();
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Reconnect
124 *
125 * Keep / reestablish the db connection if no queries have been
126 * sent for a length of time exceeding the server's idle timeout
127 *
Timothy Warren175e2892012-03-19 19:08:39 -0400128 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700129 * @return void
130 */
Timothy Warren175e2892012-03-19 19:08:39 -0400131 function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700132 {
133 if (cubrid_ping($this->conn_id) === FALSE)
134 {
135 $this->conn_id = FALSE;
136 }
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Select the database
143 *
Timothy Warren175e2892012-03-19 19:08:39 -0400144 * @access private called by the base class
Esen Sagynov2e087942011-08-09 23:35:01 -0700145 * @return resource
146 */
Timothy Warren175e2892012-03-19 19:08:39 -0400147 function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700148 {
149 // In CUBRID there is no need to select a database as the database
150 // is chosen at the connection time.
151 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700152 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700153 return cubrid_ping($this->conn_id);
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200159 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700160 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 * @return string
162 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200163 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700164 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200165 return isset($this->data_cache['version'])
166 ? $this->data_cache['version']
167 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Execute the query
174 *
Timothy Warren175e2892012-03-19 19:08:39 -0400175 * @access private called by the base class
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 * @param string an SQL query
177 * @return resource
178 */
Timothy Warren175e2892012-03-19 19:08:39 -0400179 function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700180 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700181 return @cubrid_query($sql, $this->conn_id);
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 * Begin Transaction
188 *
Timothy Warren175e2892012-03-19 19:08:39 -0400189 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700190 * @return bool
191 */
Timothy Warren175e2892012-03-19 19:08:39 -0400192 function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700193 {
194 if ( ! $this->trans_enabled)
195 {
196 return TRUE;
197 }
198
199 // When transactions are nested we only begin/commit/rollback the outermost ones
200 if ($this->_trans_depth > 0)
201 {
202 return TRUE;
203 }
204
205 // Reset the transaction failure flag.
206 // If the $test_mode flag is set to TRUE transactions will be rolled back
207 // even if the queries produce a successful result.
208 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
209
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700210 if (cubrid_get_autocommit($this->conn_id))
211 {
212 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
213 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700214
215 return TRUE;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Commit Transaction
222 *
Timothy Warren175e2892012-03-19 19:08:39 -0400223 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700224 * @return bool
225 */
Timothy Warren175e2892012-03-19 19:08:39 -0400226 function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700227 {
228 if ( ! $this->trans_enabled)
229 {
230 return TRUE;
231 }
232
233 // When transactions are nested we only begin/commit/rollback the outermost ones
234 if ($this->_trans_depth > 0)
235 {
236 return TRUE;
237 }
238
239 cubrid_commit($this->conn_id);
240
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700241 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
242 {
243 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
244 }
245
Esen Sagynov2e087942011-08-09 23:35:01 -0700246 return TRUE;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Rollback Transaction
253 *
Timothy Warren175e2892012-03-19 19:08:39 -0400254 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700255 * @return bool
256 */
Timothy Warren175e2892012-03-19 19:08:39 -0400257 function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700258 {
259 if ( ! $this->trans_enabled)
260 {
261 return TRUE;
262 }
263
264 // When transactions are nested we only begin/commit/rollback the outermost ones
265 if ($this->_trans_depth > 0)
266 {
267 return TRUE;
268 }
269
270 cubrid_rollback($this->conn_id);
271
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700272 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
273 {
274 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
275 }
276
Esen Sagynov2e087942011-08-09 23:35:01 -0700277 return TRUE;
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Escape String
284 *
Timothy Warren175e2892012-03-19 19:08:39 -0400285 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700286 * @param string
287 * @param bool whether or not the string will be used in a LIKE condition
288 * @return string
289 */
Timothy Warren175e2892012-03-19 19:08:39 -0400290 function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700291 {
292 if (is_array($str))
293 {
294 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700295 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700296 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700297 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700298
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700299 return $str;
300 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700301
302 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
303 {
304 $str = cubrid_real_escape_string($str, $this->conn_id);
305 }
306 else
307 {
308 $str = addslashes($str);
309 }
310
311 // escape LIKE condition wildcards
312 if ($like === TRUE)
313 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700314 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
315 }
316
317 return $str;
318 }
319
320 // --------------------------------------------------------------------
321
322 /**
323 * Affected Rows
324 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200325 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700326 */
Timothy Warren175e2892012-03-19 19:08:39 -0400327 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700328 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200329 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Insert ID
336 *
Timothy Warren175e2892012-03-19 19:08:39 -0400337 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700338 * @return integer
339 */
Timothy Warren175e2892012-03-19 19:08:39 -0400340 function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700341 {
342 return @cubrid_insert_id($this->conn_id);
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * "Count All" query
349 *
350 * Generates a platform-specific query string that counts all records in
351 * the specified table
352 *
Timothy Warren175e2892012-03-19 19:08:39 -0400353 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700354 * @param string
355 * @return string
356 */
Timothy Warren175e2892012-03-19 19:08:39 -0400357 function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700358 {
359 if ($table == '')
360 {
361 return 0;
362 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700363
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200364 $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 -0700365 if ($query->num_rows() == 0)
366 {
367 return 0;
368 }
369
370 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500371 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700372 return (int) $row->numrows;
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * List table query
379 *
380 * Generates a platform-specific query string so that the table names can be fetched
381 *
Timothy Warren175e2892012-03-19 19:08:39 -0400382 * @access private
Esen Sagynov2e087942011-08-09 23:35:01 -0700383 * @param boolean
384 * @return string
385 */
Timothy Warren175e2892012-03-19 19:08:39 -0400386 function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700387 {
388 $sql = "SHOW TABLES";
389
390 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
391 {
392 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
393 }
394
395 return $sql;
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Show column query
402 *
403 * Generates a platform-specific query string so that the column names can be fetched
404 *
Timothy Warren175e2892012-03-19 19:08:39 -0400405 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700406 * @param string the table name
407 * @return string
408 */
Timothy Warren175e2892012-03-19 19:08:39 -0400409 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 *
Timothy Warren175e2892012-03-19 19:08:39 -0400421 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700422 * @param string the table name
423 * @return object
424 */
Timothy Warren175e2892012-03-19 19:08:39 -0400425 function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700426 {
427 return "SELECT * FROM ".$table." LIMIT 1";
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200433 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700434 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200435 * Returns an array containing code and message of the last
436 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700437 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200438 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700439 */
Timothy Warren175e2892012-03-19 19:08:39 -0400440 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700441 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200442 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700443 }
444
Esen Sagynov2e087942011-08-09 23:35:01 -0700445 /**
446 * Escape the SQL Identifiers
447 *
Timothy Warren175e2892012-03-19 19:08:39 -0400448 * This function escapes column and table names
Esen Sagynov2e087942011-08-09 23:35:01 -0700449 *
Timothy Warren175e2892012-03-19 19:08:39 -0400450 * @access private
Esen Sagynov2e087942011-08-09 23:35:01 -0700451 * @param string
452 * @return string
453 */
Timothy Warren175e2892012-03-19 19:08:39 -0400454 function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700455 {
456 if ($this->_escape_char == '')
457 {
458 return $item;
459 }
460
461 foreach ($this->_reserved_identifiers as $id)
462 {
463 if (strpos($item, '.'.$id) !== FALSE)
464 {
465 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
466
467 // remove duplicates if the user already included the escape
468 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
469 }
470 }
471
472 if (strpos($item, '.') !== FALSE)
473 {
474 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
475 }
476 else
477 {
478 $str = $this->_escape_char.$item.$this->_escape_char;
479 }
480
481 // remove duplicates if the user already included the escape
482 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * From Tables
489 *
Timothy Warren175e2892012-03-19 19:08:39 -0400490 * This function implicitly groups FROM tables so there is no confusion
Esen Sagynov2e087942011-08-09 23:35:01 -0700491 * about operator precedence in harmony with SQL standards
492 *
Timothy Warren175e2892012-03-19 19:08:39 -0400493 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700494 * @param type
495 * @return type
496 */
Timothy Warren175e2892012-03-19 19:08:39 -0400497 function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700498 {
499 if ( ! is_array($tables))
500 {
501 $tables = array($tables);
502 }
503
504 return '('.implode(', ', $tables).')';
505 }
506
507 // --------------------------------------------------------------------
508
509 /**
510 * Insert statement
511 *
512 * Generates a platform-specific insert string from the supplied data
513 *
Timothy Warren175e2892012-03-19 19:08:39 -0400514 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700515 * @param string the table name
516 * @param array the insert keys
517 * @param array the insert values
518 * @return string
519 */
Timothy Warren175e2892012-03-19 19:08:39 -0400520 function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700521 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700522 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700523 }
524
525 // --------------------------------------------------------------------
526
527
528 /**
529 * Replace statement
530 *
531 * Generates a platform-specific replace string from the supplied data
532 *
Timothy Warren175e2892012-03-19 19:08:39 -0400533 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700534 * @param string the table name
535 * @param array the insert keys
536 * @param array the insert values
537 * @return string
538 */
Timothy Warren175e2892012-03-19 19:08:39 -0400539 function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700540 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700541 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700542 }
543
544 // --------------------------------------------------------------------
545
546 /**
547 * Insert_batch statement
548 *
549 * Generates a platform-specific insert string from the supplied data
550 *
Timothy Warren175e2892012-03-19 19:08:39 -0400551 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700552 * @param string the table name
553 * @param array the insert keys
554 * @param array the insert values
555 * @return string
556 */
Timothy Warren175e2892012-03-19 19:08:39 -0400557 function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700558 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700559 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700560 }
561
562 // --------------------------------------------------------------------
563
564
565 /**
566 * Update statement
567 *
568 * Generates a platform-specific update string from the supplied data
569 *
Timothy Warren175e2892012-03-19 19:08:39 -0400570 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700571 * @param string the table name
572 * @param array the update data
573 * @param array the where clause
574 * @param array the orderby clause
575 * @param array the limit clause
576 * @return string
577 */
Timothy Warren175e2892012-03-19 19:08:39 -0400578 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700579 {
580 foreach ($values as $key => $val)
581 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700582 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700583 }
584
585 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
586
587 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
588
589 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
590
591 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
592
593 $sql .= $orderby.$limit;
594
595 return $sql;
596 }
597
598 // --------------------------------------------------------------------
599
600
601 /**
602 * Update_Batch statement
603 *
604 * Generates a platform-specific batch update string from the supplied data
605 *
Timothy Warren175e2892012-03-19 19:08:39 -0400606 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700607 * @param string the table name
608 * @param array the update data
609 * @param array the where clause
610 * @return string
611 */
Timothy Warren175e2892012-03-19 19:08:39 -0400612 function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700613 {
614 $ids = array();
615 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
616
617 foreach ($values as $key => $val)
618 {
619 $ids[] = $val[$index];
620
621 foreach (array_keys($val) as $field)
622 {
623 if ($field != $index)
624 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700625 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700626 }
627 }
628 }
629
630 $sql = "UPDATE ".$table." SET ";
631 $cases = '';
632
633 foreach ($final as $k => $v)
634 {
635 $cases .= $k.' = CASE '."\n";
636 foreach ($v as $row)
637 {
638 $cases .= $row."\n";
639 }
640
641 $cases .= 'ELSE '.$k.' END, ';
642 }
643
644 $sql .= substr($cases, 0, -2);
645
646 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
647
648 return $sql;
649 }
650
651 // --------------------------------------------------------------------
652
653
654 /**
655 * Truncate statement
656 *
657 * Generates a platform-specific truncate string from the supplied data
658 * If the database does not support the truncate() command
Timothy Warren175e2892012-03-19 19:08:39 -0400659 * This function maps to "DELETE FROM table"
Esen Sagynov2e087942011-08-09 23:35:01 -0700660 *
Timothy Warren175e2892012-03-19 19:08:39 -0400661 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700662 * @param string the table name
663 * @return string
664 */
Timothy Warren175e2892012-03-19 19:08:39 -0400665 function _truncate($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700666 {
667 return "TRUNCATE ".$table;
668 }
669
670 // --------------------------------------------------------------------
671
672 /**
673 * Delete statement
674 *
675 * Generates a platform-specific delete string from the supplied data
676 *
Timothy Warren175e2892012-03-19 19:08:39 -0400677 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700678 * @param string the table name
679 * @param array the where clause
680 * @param string the limit clause
681 * @return string
682 */
Timothy Warren175e2892012-03-19 19:08:39 -0400683 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700684 {
685 $conditions = '';
686
687 if (count($where) > 0 OR count($like) > 0)
688 {
689 $conditions = "\nWHERE ";
690 $conditions .= implode("\n", $this->ar_where);
691
692 if (count($where) > 0 && count($like) > 0)
693 {
694 $conditions .= " AND ";
695 }
696 $conditions .= implode("\n", $like);
697 }
698
699 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
700
701 return "DELETE FROM ".$table.$conditions.$limit;
702 }
703
704 // --------------------------------------------------------------------
705
706 /**
707 * Limit string
708 *
709 * Generates a platform-specific LIMIT clause
710 *
Timothy Warren175e2892012-03-19 19:08:39 -0400711 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700712 * @param string the sql query string
713 * @param integer the number of rows to limit the query to
714 * @param integer the offset value
715 * @return string
716 */
Timothy Warren175e2892012-03-19 19:08:39 -0400717 function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700718 {
719 if ($offset == 0)
720 {
721 $offset = '';
722 }
723 else
724 {
725 $offset .= ", ";
726 }
727
728 return $sql."LIMIT ".$offset.$limit;
729 }
730
731 // --------------------------------------------------------------------
732
733 /**
734 * Close DB Connection
735 *
Timothy Warren175e2892012-03-19 19:08:39 -0400736 * @access public
Esen Sagynov2e087942011-08-09 23:35:01 -0700737 * @param resource
738 * @return void
739 */
Timothy Warren175e2892012-03-19 19:08:39 -0400740 function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700741 {
742 @cubrid_close($conn_id);
743 }
744
745}
746
747
748/* End of file cubrid_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200749/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */