blob: cde719eae08cc8fd9409de3e5b04acd857e3e2f1 [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 /**
159 * Set client character set
160 *
161 * @access public
162 * @param string
163 * @param string
164 * @return resource
165 */
166 function db_set_charset($charset, $collation)
167 {
168 // In CUBRID, there is no need to set charset or collation.
169 // This is why returning true will allow the application continue
170 // its normal process.
171 return TRUE;
172 }
173
174 // --------------------------------------------------------------------
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700175
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 /**
177 * Version number query string
178 *
179 * @access public
180 * @return string
181 */
182 function _version()
183 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700184 // To obtain the CUBRID Server version, no need to run the SQL query.
185 // CUBRID PHP API provides a function to determin this value.
186 // This is why we also need to add 'cubrid' value to the list of
187 // $driver_version_exceptions array in DB_driver class in
188 // version() function.
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 return cubrid_get_server_info($this->conn_id);
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Execute the query
196 *
197 * @access private called by the base class
198 * @param string an SQL query
199 * @return resource
200 */
201 function _execute($sql)
202 {
203 $sql = $this->_prep_query($sql);
204 return @cubrid_query($sql, $this->conn_id);
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Prep the query
211 *
212 * If needed, each database adapter can prep the query string
213 *
214 * @access private called by execute()
215 * @param string an SQL query
216 * @return string
217 */
218 function _prep_query($sql)
219 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700220 // No need to prepare
Esen Sagynov2e087942011-08-09 23:35:01 -0700221 return $sql;
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Begin Transaction
228 *
229 * @access public
230 * @return bool
231 */
232 function trans_begin($test_mode = FALSE)
233 {
234 if ( ! $this->trans_enabled)
235 {
236 return TRUE;
237 }
238
239 // When transactions are nested we only begin/commit/rollback the outermost ones
240 if ($this->_trans_depth > 0)
241 {
242 return TRUE;
243 }
244
245 // Reset the transaction failure flag.
246 // If the $test_mode flag is set to TRUE transactions will be rolled back
247 // even if the queries produce a successful result.
248 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
249
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700250 if (cubrid_get_autocommit($this->conn_id))
251 {
252 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
253 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700254
255 return TRUE;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Commit Transaction
262 *
263 * @access public
264 * @return bool
265 */
266 function trans_commit()
267 {
268 if ( ! $this->trans_enabled)
269 {
270 return TRUE;
271 }
272
273 // When transactions are nested we only begin/commit/rollback the outermost ones
274 if ($this->_trans_depth > 0)
275 {
276 return TRUE;
277 }
278
279 cubrid_commit($this->conn_id);
280
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700281 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
282 {
283 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
284 }
285
Esen Sagynov2e087942011-08-09 23:35:01 -0700286 return TRUE;
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Rollback Transaction
293 *
294 * @access public
295 * @return bool
296 */
297 function trans_rollback()
298 {
299 if ( ! $this->trans_enabled)
300 {
301 return TRUE;
302 }
303
304 // When transactions are nested we only begin/commit/rollback the outermost ones
305 if ($this->_trans_depth > 0)
306 {
307 return TRUE;
308 }
309
310 cubrid_rollback($this->conn_id);
311
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700312 if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
313 {
314 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
315 }
316
Esen Sagynov2e087942011-08-09 23:35:01 -0700317 return TRUE;
318 }
319
320 // --------------------------------------------------------------------
321
322 /**
323 * Escape String
324 *
325 * @access public
326 * @param string
327 * @param bool whether or not the string will be used in a LIKE condition
328 * @return string
329 */
330 function escape_str($str, $like = FALSE)
331 {
332 if (is_array($str))
333 {
334 foreach ($str as $key => $val)
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700335 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700336 $str[$key] = $this->escape_str($val, $like);
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700337 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700338
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700339 return $str;
340 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700341
342 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
343 {
344 $str = cubrid_real_escape_string($str, $this->conn_id);
345 }
346 else
347 {
348 $str = addslashes($str);
349 }
350
351 // escape LIKE condition wildcards
352 if ($like === TRUE)
353 {
Esen Sagynov2e087942011-08-09 23:35:01 -0700354 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
355 }
356
357 return $str;
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Affected Rows
364 *
365 * @access public
366 * @return integer
367 */
368 function affected_rows()
369 {
370 return @cubrid_affected_rows($this->conn_id);
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * Insert ID
377 *
378 * @access public
379 * @return integer
380 */
381 function insert_id()
382 {
383 return @cubrid_insert_id($this->conn_id);
384 }
385
386 // --------------------------------------------------------------------
387
388 /**
389 * "Count All" query
390 *
391 * Generates a platform-specific query string that counts all records in
392 * the specified table
393 *
394 * @access public
395 * @param string
396 * @return string
397 */
398 function count_all($table = '')
399 {
400 if ($table == '')
401 {
402 return 0;
403 }
404
405 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
406
407 if ($query->num_rows() == 0)
408 {
409 return 0;
410 }
411
412 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500413 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700414 return (int) $row->numrows;
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * List table query
421 *
422 * Generates a platform-specific query string so that the table names can be fetched
423 *
424 * @access private
425 * @param boolean
426 * @return string
427 */
428 function _list_tables($prefix_limit = FALSE)
429 {
430 $sql = "SHOW TABLES";
431
432 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
433 {
434 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
435 }
436
437 return $sql;
438 }
439
440 // --------------------------------------------------------------------
441
442 /**
443 * Show column query
444 *
445 * Generates a platform-specific query string so that the column names can be fetched
446 *
447 * @access public
448 * @param string the table name
449 * @return string
450 */
451 function _list_columns($table = '')
452 {
453 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Field data query
460 *
461 * Generates a platform-specific query so that the column data can be retrieved
462 *
463 * @access public
464 * @param string the table name
465 * @return object
466 */
467 function _field_data($table)
468 {
469 return "SELECT * FROM ".$table." LIMIT 1";
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * The error message string
476 *
477 * @access private
478 * @return string
479 */
480 function _error_message()
481 {
482 return cubrid_error($this->conn_id);
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * The error message number
489 *
490 * @access private
491 * @return integer
492 */
493 function _error_number()
494 {
495 return cubrid_errno($this->conn_id);
496 }
497
498 // --------------------------------------------------------------------
499
500 /**
501 * Escape the SQL Identifiers
502 *
503 * This function escapes column and table names
504 *
505 * @access private
506 * @param string
507 * @return string
508 */
509 function _escape_identifiers($item)
510 {
511 if ($this->_escape_char == '')
512 {
513 return $item;
514 }
515
516 foreach ($this->_reserved_identifiers as $id)
517 {
518 if (strpos($item, '.'.$id) !== FALSE)
519 {
520 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
521
522 // remove duplicates if the user already included the escape
523 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
524 }
525 }
526
527 if (strpos($item, '.') !== FALSE)
528 {
529 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
530 }
531 else
532 {
533 $str = $this->_escape_char.$item.$this->_escape_char;
534 }
535
536 // remove duplicates if the user already included the escape
537 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * From Tables
544 *
545 * This function implicitly groups FROM tables so there is no confusion
546 * about operator precedence in harmony with SQL standards
547 *
548 * @access public
549 * @param type
550 * @return type
551 */
552 function _from_tables($tables)
553 {
554 if ( ! is_array($tables))
555 {
556 $tables = array($tables);
557 }
558
559 return '('.implode(', ', $tables).')';
560 }
561
562 // --------------------------------------------------------------------
563
564 /**
565 * Insert statement
566 *
567 * Generates a platform-specific insert string from the supplied data
568 *
569 * @access public
570 * @param string the table name
571 * @param array the insert keys
572 * @param array the insert values
573 * @return string
574 */
575 function _insert($table, $keys, $values)
576 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700577 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700578 }
579
580 // --------------------------------------------------------------------
581
582
583 /**
584 * Replace statement
585 *
586 * Generates a platform-specific replace string from the supplied data
587 *
588 * @access public
589 * @param string the table name
590 * @param array the insert keys
591 * @param array the insert values
592 * @return string
593 */
594 function _replace($table, $keys, $values)
595 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700596 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Insert_batch statement
603 *
604 * Generates a platform-specific insert string from the supplied data
605 *
606 * @access public
607 * @param string the table name
608 * @param array the insert keys
609 * @param array the insert values
610 * @return string
611 */
612 function _insert_batch($table, $keys, $values)
613 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700614 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700615 }
616
617 // --------------------------------------------------------------------
618
619
620 /**
621 * Update statement
622 *
623 * Generates a platform-specific 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 * @param array the orderby clause
630 * @param array the limit clause
631 * @return string
632 */
633 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
634 {
635 foreach ($values as $key => $val)
636 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700637 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700638 }
639
640 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
641
642 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
643
644 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
645
646 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
647
648 $sql .= $orderby.$limit;
649
650 return $sql;
651 }
652
653 // --------------------------------------------------------------------
654
655
656 /**
657 * Update_Batch statement
658 *
659 * Generates a platform-specific batch update string from the supplied data
660 *
661 * @access public
662 * @param string the table name
663 * @param array the update data
664 * @param array the where clause
665 * @return string
666 */
667 function _update_batch($table, $values, $index, $where = NULL)
668 {
669 $ids = array();
670 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
671
672 foreach ($values as $key => $val)
673 {
674 $ids[] = $val[$index];
675
676 foreach (array_keys($val) as $field)
677 {
678 if ($field != $index)
679 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700680 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700681 }
682 }
683 }
684
685 $sql = "UPDATE ".$table." SET ";
686 $cases = '';
687
688 foreach ($final as $k => $v)
689 {
690 $cases .= $k.' = CASE '."\n";
691 foreach ($v as $row)
692 {
693 $cases .= $row."\n";
694 }
695
696 $cases .= 'ELSE '.$k.' END, ';
697 }
698
699 $sql .= substr($cases, 0, -2);
700
701 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
702
703 return $sql;
704 }
705
706 // --------------------------------------------------------------------
707
708
709 /**
710 * Truncate statement
711 *
712 * Generates a platform-specific truncate string from the supplied data
713 * If the database does not support the truncate() command
714 * This function maps to "DELETE FROM table"
715 *
716 * @access public
717 * @param string the table name
718 * @return string
719 */
720 function _truncate($table)
721 {
722 return "TRUNCATE ".$table;
723 }
724
725 // --------------------------------------------------------------------
726
727 /**
728 * Delete statement
729 *
730 * Generates a platform-specific delete string from the supplied data
731 *
732 * @access public
733 * @param string the table name
734 * @param array the where clause
735 * @param string the limit clause
736 * @return string
737 */
738 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
739 {
740 $conditions = '';
741
742 if (count($where) > 0 OR count($like) > 0)
743 {
744 $conditions = "\nWHERE ";
745 $conditions .= implode("\n", $this->ar_where);
746
747 if (count($where) > 0 && count($like) > 0)
748 {
749 $conditions .= " AND ";
750 }
751 $conditions .= implode("\n", $like);
752 }
753
754 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
755
756 return "DELETE FROM ".$table.$conditions.$limit;
757 }
758
759 // --------------------------------------------------------------------
760
761 /**
762 * Limit string
763 *
764 * Generates a platform-specific LIMIT clause
765 *
766 * @access public
767 * @param string the sql query string
768 * @param integer the number of rows to limit the query to
769 * @param integer the offset value
770 * @return string
771 */
772 function _limit($sql, $limit, $offset)
773 {
774 if ($offset == 0)
775 {
776 $offset = '';
777 }
778 else
779 {
780 $offset .= ", ";
781 }
782
783 return $sql."LIMIT ".$offset.$limit;
784 }
785
786 // --------------------------------------------------------------------
787
788 /**
789 * Close DB Connection
790 *
791 * @access public
792 * @param resource
793 * @return void
794 */
795 function _close($conn_id)
796 {
797 @cubrid_close($conn_id);
798 }
799
800}
801
802
803/* End of file cubrid_driver.php */
804/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */