blob: bed3d86859eb87a293c8713d1dca28a6c97662fa [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
Andrey Andreeva00e5042012-03-26 12:23:13 +030060 // CUBRID-specific properties
61 public $auto_commit = TRUE;
62
63 public function __construct($params)
64 {
65 parent::__construct($params);
66
67 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
68 {
69 if (stripos($matches[2], 'autocommit=off') !== FALSE)
70 {
71 $this->auto_commit = FALSE;
72 }
73 }
74 else
75 {
76 // If no port is defined by the user, use the default value
77 $this->port == '' OR $this->port = 33000;
78 }
79 }
80
Esen Sagynov2e087942011-08-09 23:35:01 -070081 /**
82 * Non-persistent database connection
83 *
Esen Sagynov2e087942011-08-09 23:35:01 -070084 * @return resource
85 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +020086 public function db_connect()
Esen Sagynov2e087942011-08-09 23:35:01 -070087 {
Andrey Andreeva00e5042012-03-26 12:23:13 +030088 return $this->_cubrid_connect();
Esen Sagynov2e087942011-08-09 23:35:01 -070089 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Persistent database connection
Andrey Andreev592f4ec2012-03-20 15:10:08 +020095 *
Esen Sagynov2e087942011-08-09 23:35:01 -070096 * In CUBRID persistent DB connection is supported natively in CUBRID
97 * engine which can be configured in the CUBRID Broker configuration
98 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
99 * connections established between the client application and the
Andrey Andreeva00e5042012-03-26 12:23:13 +0300100 * server will become persistent.
Esen Sagynov2e087942011-08-09 23:35:01 -0700101 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700102 * @return resource
103 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200104 public function db_pconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700105 {
Andrey Andreeva00e5042012-03-26 12:23:13 +0300106 return $this->_cubrid_connect(TRUE);
107 }
108
109 // --------------------------------------------------------------------
110
111 /**
112 * CUBRID connection
113 *
114 * A CUBRID-specific method to create a connection to the database.
115 * Except for determining if a persistent connection should be used,
116 * the rest of the logic is the same for db_connect() and db_pconnect().
117 *
118 * @param bool
119 * @return resource
120 */
121 protected function _cubrid_connect($persistent = FALSE)
122 {
123 if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
124 {
125 $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
126 $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
127 ? $_temp($this->dsn, $this->username, $this->password)
128 : $_temp($this->dsn);
129 }
130 else
131 {
132 $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
133 $conn_id = ($this->username !== '')
134 ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password)
135 : $_temp($this->hostname, $this->port, $this->database);
136 }
137
138 return $conn_id;
Esen Sagynov2e087942011-08-09 23:35:01 -0700139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Reconnect
145 *
146 * Keep / reestablish the db connection if no queries have been
147 * sent for a length of time exceeding the server's idle timeout
148 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700149 * @return void
150 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200151 public function reconnect()
Esen Sagynov2e087942011-08-09 23:35:01 -0700152 {
153 if (cubrid_ping($this->conn_id) === FALSE)
154 {
155 $this->conn_id = FALSE;
156 }
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Select the database
163 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700164 * @return resource
165 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200166 public function db_select()
Esen Sagynov2e087942011-08-09 23:35:01 -0700167 {
168 // In CUBRID there is no need to select a database as the database
169 // is chosen at the connection time.
170 // So, to determine if the database is "selected", all we have to
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700171 // do is ping the server and return that value.
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 return cubrid_ping($this->conn_id);
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200178 * Database version number
Esen Sagynov2e087942011-08-09 23:35:01 -0700179 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700180 * @return string
181 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200182 public function version()
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200184 return isset($this->data_cache['version'])
185 ? $this->data_cache['version']
186 : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id);
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Execute the query
193 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700194 * @param string an SQL query
195 * @return resource
196 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200197 protected function _execute($sql)
Esen Sagynov2e087942011-08-09 23:35:01 -0700198 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700199 return @cubrid_query($sql, $this->conn_id);
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700205 * Begin Transaction
206 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700207 * @return bool
208 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200209 public function trans_begin($test_mode = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700210 {
211 if ( ! $this->trans_enabled)
212 {
213 return TRUE;
214 }
215
216 // When transactions are nested we only begin/commit/rollback the outermost ones
217 if ($this->_trans_depth > 0)
218 {
219 return TRUE;
220 }
221
222 // Reset the transaction failure flag.
223 // If the $test_mode flag is set to TRUE transactions will be rolled back
224 // even if the queries produce a successful result.
225 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
226
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700227 if (cubrid_get_autocommit($this->conn_id))
228 {
229 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
230 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700231
232 return TRUE;
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Commit Transaction
239 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700240 * @return bool
241 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200242 public function trans_commit()
Esen Sagynov2e087942011-08-09 23:35:01 -0700243 {
244 if ( ! $this->trans_enabled)
245 {
246 return TRUE;
247 }
248
249 // When transactions are nested we only begin/commit/rollback the outermost ones
250 if ($this->_trans_depth > 0)
251 {
252 return TRUE;
253 }
254
255 cubrid_commit($this->conn_id);
256
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700257 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
258 {
259 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
260 }
261
Esen Sagynov2e087942011-08-09 23:35:01 -0700262 return TRUE;
263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Rollback Transaction
269 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700270 * @return bool
271 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200272 public function trans_rollback()
Esen Sagynov2e087942011-08-09 23:35:01 -0700273 {
274 if ( ! $this->trans_enabled)
275 {
276 return TRUE;
277 }
278
279 // When transactions are nested we only begin/commit/rollback the outermost ones
280 if ($this->_trans_depth > 0)
281 {
282 return TRUE;
283 }
284
285 cubrid_rollback($this->conn_id);
286
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700287 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
288 {
289 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
290 }
291
Esen Sagynov2e087942011-08-09 23:35:01 -0700292 return TRUE;
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
298 * Escape String
299 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700300 * @param string
301 * @param bool whether or not the string will be used in a LIKE condition
302 * @return string
303 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200304 public function escape_str($str, $like = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700305 {
306 if (is_array($str))
307 {
308 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700309 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700310 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700311 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700312
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700313 return $str;
314 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700315
316 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
317 {
318 $str = cubrid_real_escape_string($str, $this->conn_id);
319 }
320 else
321 {
322 $str = addslashes($str);
323 }
324
325 // escape LIKE condition wildcards
326 if ($like === TRUE)
327 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700328 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
329 }
330
331 return $str;
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Affected Rows
338 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200339 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700340 */
Timothy Warren175e2892012-03-19 19:08:39 -0400341 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700342 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200343 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700344 }
345
346 // --------------------------------------------------------------------
347
348 /**
349 * Insert ID
350 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200351 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700352 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200353 public function insert_id()
Esen Sagynov2e087942011-08-09 23:35:01 -0700354 {
355 return @cubrid_insert_id($this->conn_id);
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * "Count All" query
362 *
363 * Generates a platform-specific query string that counts all records in
364 * the specified table
365 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700366 * @param string
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200367 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700368 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200369 public function count_all($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700370 {
371 if ($table == '')
372 {
373 return 0;
374 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700375
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200376 $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 -0700377 if ($query->num_rows() == 0)
378 {
379 return 0;
380 }
381
382 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500383 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700384 return (int) $row->numrows;
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * List table query
391 *
392 * Generates a platform-specific query string so that the table names can be fetched
393 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200394 * @param bool
Esen Sagynov2e087942011-08-09 23:35:01 -0700395 * @return string
396 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200397 protected function _list_tables($prefix_limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700398 {
399 $sql = "SHOW TABLES";
400
401 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
402 {
403 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
404 }
405
406 return $sql;
407 }
408
409 // --------------------------------------------------------------------
410
411 /**
412 * Show column query
413 *
414 * Generates a platform-specific query string so that the column names can be fetched
415 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700416 * @param string the table name
417 * @return string
418 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200419 protected function _list_columns($table = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700420 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200421 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Esen Sagynov2e087942011-08-09 23:35:01 -0700422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Field data query
428 *
429 * Generates a platform-specific query so that the column data can be retrieved
430 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700431 * @param string the table name
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200432 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700433 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200434 protected function _field_data($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700435 {
436 return "SELECT * FROM ".$table." LIMIT 1";
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200442 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700443 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200444 * Returns an array containing code and message of the last
445 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700446 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200447 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700448 */
Timothy Warren175e2892012-03-19 19:08:39 -0400449 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700450 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700452 }
453
Esen Sagynov2e087942011-08-09 23:35:01 -0700454 /**
455 * Escape the SQL Identifiers
456 *
Timothy Warren175e2892012-03-19 19:08:39 -0400457 * This function escapes column and table names
Esen Sagynov2e087942011-08-09 23:35:01 -0700458 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700459 * @param string
460 * @return string
461 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200462 public function _escape_identifiers($item)
Esen Sagynov2e087942011-08-09 23:35:01 -0700463 {
464 if ($this->_escape_char == '')
465 {
466 return $item;
467 }
468
469 foreach ($this->_reserved_identifiers as $id)
470 {
471 if (strpos($item, '.'.$id) !== FALSE)
472 {
473 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
474
475 // remove duplicates if the user already included the escape
476 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
477 }
478 }
479
480 if (strpos($item, '.') !== FALSE)
481 {
482 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
483 }
484 else
485 {
486 $str = $this->_escape_char.$item.$this->_escape_char;
487 }
488
489 // remove duplicates if the user already included the escape
490 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * From Tables
497 *
Timothy Warren175e2892012-03-19 19:08:39 -0400498 * This function implicitly groups FROM tables so there is no confusion
Esen Sagynov2e087942011-08-09 23:35:01 -0700499 * about operator precedence in harmony with SQL standards
500 *
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200501 * @param array
502 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700503 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200504 protected function _from_tables($tables)
Esen Sagynov2e087942011-08-09 23:35:01 -0700505 {
506 if ( ! is_array($tables))
507 {
508 $tables = array($tables);
509 }
510
511 return '('.implode(', ', $tables).')';
512 }
513
514 // --------------------------------------------------------------------
515
516 /**
517 * Insert statement
518 *
519 * Generates a platform-specific insert string from the supplied data
520 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700521 * @param string the table name
522 * @param array the insert keys
523 * @param array the insert values
524 * @return string
525 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200526 protected function _insert($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700527 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700528 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700529 }
530
531 // --------------------------------------------------------------------
532
533
534 /**
535 * Replace statement
536 *
537 * Generates a platform-specific replace string from the supplied data
538 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700539 * @param string the table name
540 * @param array the insert keys
541 * @param array the insert values
542 * @return string
543 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200544 protected function _replace($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700545 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700546 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700547 }
548
549 // --------------------------------------------------------------------
550
551 /**
552 * Insert_batch statement
553 *
554 * Generates a platform-specific insert string from the supplied data
555 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700556 * @param string the table name
557 * @param array the insert keys
558 * @param array the insert values
559 * @return string
560 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200561 protected function _insert_batch($table, $keys, $values)
Esen Sagynov2e087942011-08-09 23:35:01 -0700562 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700563 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700564 }
565
566 // --------------------------------------------------------------------
567
568
569 /**
570 * Update statement
571 *
572 * Generates a platform-specific update string from the supplied data
573 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700574 * @param string the table name
575 * @param array the update data
576 * @param array the where clause
577 * @param array the orderby clause
578 * @param array the limit clause
579 * @return string
580 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200581 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700582 {
583 foreach ($values as $key => $val)
584 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700585 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700586 }
587
588 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
589
590 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
591
592 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
593
594 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
595
596 $sql .= $orderby.$limit;
597
598 return $sql;
599 }
600
601 // --------------------------------------------------------------------
602
603
604 /**
605 * Update_Batch statement
606 *
607 * Generates a platform-specific batch update string from the supplied data
608 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700609 * @param string the table name
610 * @param array the update data
611 * @param array the where clause
612 * @return string
613 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200614 protected function _update_batch($table, $values, $index, $where = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700615 {
616 $ids = array();
617 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
618
619 foreach ($values as $key => $val)
620 {
621 $ids[] = $val[$index];
622
623 foreach (array_keys($val) as $field)
624 {
625 if ($field != $index)
626 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700627 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700628 }
629 }
630 }
631
632 $sql = "UPDATE ".$table." SET ";
633 $cases = '';
634
635 foreach ($final as $k => $v)
636 {
637 $cases .= $k.' = CASE '."\n";
638 foreach ($v as $row)
639 {
640 $cases .= $row."\n";
641 }
642
643 $cases .= 'ELSE '.$k.' END, ';
644 }
645
646 $sql .= substr($cases, 0, -2);
647
648 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
649
650 return $sql;
651 }
652
653 // --------------------------------------------------------------------
654
Esen Sagynov2e087942011-08-09 23:35:01 -0700655 /**
656 * Truncate statement
657 *
658 * Generates a platform-specific truncate string from the supplied data
659 * If the database does not support the truncate() command
Timothy Warren175e2892012-03-19 19:08:39 -0400660 * This function maps to "DELETE FROM table"
Esen Sagynov2e087942011-08-09 23:35:01 -0700661 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700662 * @param string the table name
663 * @return string
664 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200665 protected 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 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700677 * @param string the table name
678 * @param array the where clause
679 * @param string the limit clause
680 * @return string
681 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200682 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700683 {
684 $conditions = '';
685
686 if (count($where) > 0 OR count($like) > 0)
687 {
688 $conditions = "\nWHERE ";
689 $conditions .= implode("\n", $this->ar_where);
690
691 if (count($where) > 0 && count($like) > 0)
692 {
693 $conditions .= " AND ";
694 }
695 $conditions .= implode("\n", $like);
696 }
697
698 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
699
700 return "DELETE FROM ".$table.$conditions.$limit;
701 }
702
703 // --------------------------------------------------------------------
704
705 /**
706 * Limit string
707 *
708 * Generates a platform-specific LIMIT clause
709 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700710 * @param string the sql query string
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200711 * @param int the number of rows to limit the query to
712 * @param int the offset value
Esen Sagynov2e087942011-08-09 23:35:01 -0700713 * @return string
714 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200715 protected function _limit($sql, $limit, $offset)
Esen Sagynov2e087942011-08-09 23:35:01 -0700716 {
717 if ($offset == 0)
718 {
719 $offset = '';
720 }
721 else
722 {
723 $offset .= ", ";
724 }
725
726 return $sql."LIMIT ".$offset.$limit;
727 }
728
729 // --------------------------------------------------------------------
730
731 /**
732 * Close DB Connection
733 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700734 * @param resource
735 * @return void
736 */
Andrey Andreev592f4ec2012-03-20 15:10:08 +0200737 protected function _close($conn_id)
Esen Sagynov2e087942011-08-09 23:35:01 -0700738 {
739 @cubrid_close($conn_id);
740 }
741
742}
743
Esen Sagynov2e087942011-08-09 23:35:01 -0700744/* End of file cubrid_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400745/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */