blob: afdaef3518f1d64cb80c712cf27e176ece0e0948 [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
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 *
69 * @access private called by the base class
70 * @return resource
71 */
72 function db_connect()
73 {
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
109 * @cubrid_connect function will establish persisten connection
110 * considering that the CCI_PCONNECT is ON.
111 *
112 * @access private called by the base class
113 * @return resource
114 */
115 function db_pconnect()
116 {
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 *
128 * @access public
129 * @return void
130 */
131 function reconnect()
132 {
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 *
144 * @access private called by the base class
145 * @return resource
146 */
147 function db_select()
148 {
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 *
175 * @access private called by the base class
176 * @param string an SQL query
177 * @return resource
178 */
179 function _execute($sql)
180 {
181 $sql = $this->_prep_query($sql);
182 return @cubrid_query($sql, $this->conn_id);
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Prep the query
189 *
190 * If needed, each database adapter can prep the query string
191 *
192 * @access private called by execute()
193 * @param string an SQL query
194 * @return string
195 */
196 function _prep_query($sql)
197 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700198 // No need to prepare
Esen Sagynov2e087942011-08-09 23:35:01 -0700199 return $sql;
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Begin Transaction
206 *
207 * @access public
208 * @return bool
209 */
210 function trans_begin($test_mode = FALSE)
211 {
212 if ( ! $this->trans_enabled)
213 {
214 return TRUE;
215 }
216
217 // When transactions are nested we only begin/commit/rollback the outermost ones
218 if ($this->_trans_depth > 0)
219 {
220 return TRUE;
221 }
222
223 // Reset the transaction failure flag.
224 // If the $test_mode flag is set to TRUE transactions will be rolled back
225 // even if the queries produce a successful result.
226 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
227
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700228 if (cubrid_get_autocommit($this->conn_id))
229 {
230 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
231 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700232
233 return TRUE;
234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Commit Transaction
240 *
241 * @access public
242 * @return bool
243 */
244 function trans_commit()
245 {
246 if ( ! $this->trans_enabled)
247 {
248 return TRUE;
249 }
250
251 // When transactions are nested we only begin/commit/rollback the outermost ones
252 if ($this->_trans_depth > 0)
253 {
254 return TRUE;
255 }
256
257 cubrid_commit($this->conn_id);
258
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700259 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
260 {
261 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
262 }
263
Esen Sagynov2e087942011-08-09 23:35:01 -0700264 return TRUE;
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Rollback Transaction
271 *
272 * @access public
273 * @return bool
274 */
275 function trans_rollback()
276 {
277 if ( ! $this->trans_enabled)
278 {
279 return TRUE;
280 }
281
282 // When transactions are nested we only begin/commit/rollback the outermost ones
283 if ($this->_trans_depth > 0)
284 {
285 return TRUE;
286 }
287
288 cubrid_rollback($this->conn_id);
289
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700290 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
291 {
292 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
293 }
294
Esen Sagynov2e087942011-08-09 23:35:01 -0700295 return TRUE;
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Escape String
302 *
303 * @access public
304 * @param string
305 * @param bool whether or not the string will be used in a LIKE condition
306 * @return string
307 */
308 function escape_str($str, $like = FALSE)
309 {
310 if (is_array($str))
311 {
312 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700313 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700314 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700315 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700316
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700317 return $str;
318 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700319
320 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
321 {
322 $str = cubrid_real_escape_string($str, $this->conn_id);
323 }
324 else
325 {
326 $str = addslashes($str);
327 }
328
329 // escape LIKE condition wildcards
330 if ($like === TRUE)
331 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700332 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
333 }
334
335 return $str;
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Affected Rows
342 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200343 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700344 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200345 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700346 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200347 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Insert ID
354 *
355 * @access public
356 * @return integer
357 */
358 function insert_id()
359 {
360 return @cubrid_insert_id($this->conn_id);
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * "Count All" query
367 *
368 * Generates a platform-specific query string that counts all records in
369 * the specified table
370 *
371 * @access public
372 * @param string
373 * @return string
374 */
375 function count_all($table = '')
376 {
377 if ($table == '')
378 {
379 return 0;
380 }
381
382 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
383
384 if ($query->num_rows() == 0)
385 {
386 return 0;
387 }
388
389 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500390 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700391 return (int) $row->numrows;
392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * List table query
398 *
399 * Generates a platform-specific query string so that the table names can be fetched
400 *
401 * @access private
402 * @param boolean
403 * @return string
404 */
405 function _list_tables($prefix_limit = FALSE)
406 {
407 $sql = "SHOW TABLES";
408
409 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
410 {
411 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
412 }
413
414 return $sql;
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Show column query
421 *
422 * Generates a platform-specific query string so that the column names can be fetched
423 *
424 * @access public
425 * @param string the table name
426 * @return string
427 */
428 function _list_columns($table = '')
429 {
430 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Field data query
437 *
438 * Generates a platform-specific query so that the column data can be retrieved
439 *
440 * @access public
441 * @param string the table name
442 * @return object
443 */
444 function _field_data($table)
445 {
446 return "SELECT * FROM ".$table." LIMIT 1";
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200452 * Error
Esen Sagynov2e087942011-08-09 23:35:01 -0700453 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200454 * Returns an array containing code and message of the last
455 * database error that has occured.
Esen Sagynov2e087942011-08-09 23:35:01 -0700456 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200457 * @return array
Esen Sagynov2e087942011-08-09 23:35:01 -0700458 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200459 public function error()
Esen Sagynov2e087942011-08-09 23:35:01 -0700460 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200461 return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
Esen Sagynov2e087942011-08-09 23:35:01 -0700462 }
463
Esen Sagynov2e087942011-08-09 23:35:01 -0700464 /**
465 * Escape the SQL Identifiers
466 *
467 * This function escapes column and table names
468 *
469 * @access private
470 * @param string
471 * @return string
472 */
473 function _escape_identifiers($item)
474 {
475 if ($this->_escape_char == '')
476 {
477 return $item;
478 }
479
480 foreach ($this->_reserved_identifiers as $id)
481 {
482 if (strpos($item, '.'.$id) !== FALSE)
483 {
484 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
485
486 // remove duplicates if the user already included the escape
487 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
488 }
489 }
490
491 if (strpos($item, '.') !== FALSE)
492 {
493 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
494 }
495 else
496 {
497 $str = $this->_escape_char.$item.$this->_escape_char;
498 }
499
500 // remove duplicates if the user already included the escape
501 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * From Tables
508 *
509 * This function implicitly groups FROM tables so there is no confusion
510 * about operator precedence in harmony with SQL standards
511 *
512 * @access public
513 * @param type
514 * @return type
515 */
516 function _from_tables($tables)
517 {
518 if ( ! is_array($tables))
519 {
520 $tables = array($tables);
521 }
522
523 return '('.implode(', ', $tables).')';
524 }
525
526 // --------------------------------------------------------------------
527
528 /**
529 * Insert statement
530 *
531 * Generates a platform-specific insert string from the supplied data
532 *
533 * @access public
534 * @param string the table name
535 * @param array the insert keys
536 * @param array the insert values
537 * @return string
538 */
539 function _insert($table, $keys, $values)
540 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700541 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700542 }
543
544 // --------------------------------------------------------------------
545
546
547 /**
548 * Replace statement
549 *
550 * Generates a platform-specific replace string from the supplied data
551 *
552 * @access public
553 * @param string the table name
554 * @param array the insert keys
555 * @param array the insert values
556 * @return string
557 */
558 function _replace($table, $keys, $values)
559 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700560 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700561 }
562
563 // --------------------------------------------------------------------
564
565 /**
566 * Insert_batch statement
567 *
568 * Generates a platform-specific insert string from the supplied data
569 *
570 * @access public
571 * @param string the table name
572 * @param array the insert keys
573 * @param array the insert values
574 * @return string
575 */
576 function _insert_batch($table, $keys, $values)
577 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700578 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700579 }
580
581 // --------------------------------------------------------------------
582
583
584 /**
585 * Update statement
586 *
587 * Generates a platform-specific update string from the supplied data
588 *
589 * @access public
590 * @param string the table name
591 * @param array the update data
592 * @param array the where clause
593 * @param array the orderby clause
594 * @param array the limit clause
595 * @return string
596 */
597 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
598 {
599 foreach ($values as $key => $val)
600 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700601 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700602 }
603
604 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
605
606 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
607
608 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
609
610 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
611
612 $sql .= $orderby.$limit;
613
614 return $sql;
615 }
616
617 // --------------------------------------------------------------------
618
619
620 /**
621 * Update_Batch statement
622 *
623 * Generates a platform-specific batch update string from the supplied data
624 *
625 * @access public
626 * @param string the table name
627 * @param array the update data
628 * @param array the where clause
629 * @return string
630 */
631 function _update_batch($table, $values, $index, $where = NULL)
632 {
633 $ids = array();
634 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
635
636 foreach ($values as $key => $val)
637 {
638 $ids[] = $val[$index];
639
640 foreach (array_keys($val) as $field)
641 {
642 if ($field != $index)
643 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700644 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700645 }
646 }
647 }
648
649 $sql = "UPDATE ".$table." SET ";
650 $cases = '';
651
652 foreach ($final as $k => $v)
653 {
654 $cases .= $k.' = CASE '."\n";
655 foreach ($v as $row)
656 {
657 $cases .= $row."\n";
658 }
659
660 $cases .= 'ELSE '.$k.' END, ';
661 }
662
663 $sql .= substr($cases, 0, -2);
664
665 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
666
667 return $sql;
668 }
669
670 // --------------------------------------------------------------------
671
672
673 /**
674 * Truncate statement
675 *
676 * Generates a platform-specific truncate string from the supplied data
677 * If the database does not support the truncate() command
678 * This function maps to "DELETE FROM table"
679 *
680 * @access public
681 * @param string the table name
682 * @return string
683 */
684 function _truncate($table)
685 {
686 return "TRUNCATE ".$table;
687 }
688
689 // --------------------------------------------------------------------
690
691 /**
692 * Delete statement
693 *
694 * Generates a platform-specific delete string from the supplied data
695 *
696 * @access public
697 * @param string the table name
698 * @param array the where clause
699 * @param string the limit clause
700 * @return string
701 */
702 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
703 {
704 $conditions = '';
705
706 if (count($where) > 0 OR count($like) > 0)
707 {
708 $conditions = "\nWHERE ";
709 $conditions .= implode("\n", $this->ar_where);
710
711 if (count($where) > 0 && count($like) > 0)
712 {
713 $conditions .= " AND ";
714 }
715 $conditions .= implode("\n", $like);
716 }
717
718 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
719
720 return "DELETE FROM ".$table.$conditions.$limit;
721 }
722
723 // --------------------------------------------------------------------
724
725 /**
726 * Limit string
727 *
728 * Generates a platform-specific LIMIT clause
729 *
730 * @access public
731 * @param string the sql query string
732 * @param integer the number of rows to limit the query to
733 * @param integer the offset value
734 * @return string
735 */
736 function _limit($sql, $limit, $offset)
737 {
738 if ($offset == 0)
739 {
740 $offset = '';
741 }
742 else
743 {
744 $offset .= ", ";
745 }
746
747 return $sql."LIMIT ".$offset.$limit;
748 }
749
750 // --------------------------------------------------------------------
751
752 /**
753 * Close DB Connection
754 *
755 * @access public
756 * @param resource
757 * @return void
758 */
759 function _close($conn_id)
760 {
761 @cubrid_close($conn_id);
762 }
763
764}
765
766
767/* End of file cubrid_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200768/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */