blob: 8738a2e87d6756ad5c0f6a1918a432282e9f1cbb [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 *
Andrey Andreevea3eec92012-03-01 19:16:23 +0200365 * @return int
Esen Sagynov2e087942011-08-09 23:35:01 -0700366 */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200367 public function affected_rows()
Esen Sagynov2e087942011-08-09 23:35:01 -0700368 {
Andrey Andreevea3eec92012-03-01 19:16:23 +0200369 return @cubrid_affected_rows();
Esen Sagynov2e087942011-08-09 23:35:01 -0700370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Insert ID
376 *
377 * @access public
378 * @return integer
379 */
380 function insert_id()
381 {
382 return @cubrid_insert_id($this->conn_id);
383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * "Count All" query
389 *
390 * Generates a platform-specific query string that counts all records in
391 * the specified table
392 *
393 * @access public
394 * @param string
395 * @return string
396 */
397 function count_all($table = '')
398 {
399 if ($table == '')
400 {
401 return 0;
402 }
403
404 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
405
406 if ($query->num_rows() == 0)
407 {
408 return 0;
409 }
410
411 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500412 $this->_reset_select();
Esen Sagynov2e087942011-08-09 23:35:01 -0700413 return (int) $row->numrows;
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * List table query
420 *
421 * Generates a platform-specific query string so that the table names can be fetched
422 *
423 * @access private
424 * @param boolean
425 * @return string
426 */
427 function _list_tables($prefix_limit = FALSE)
428 {
429 $sql = "SHOW TABLES";
430
431 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
432 {
433 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
434 }
435
436 return $sql;
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Show column query
443 *
444 * Generates a platform-specific query string so that the column names can be fetched
445 *
446 * @access public
447 * @param string the table name
448 * @return string
449 */
450 function _list_columns($table = '')
451 {
452 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Field data query
459 *
460 * Generates a platform-specific query so that the column data can be retrieved
461 *
462 * @access public
463 * @param string the table name
464 * @return object
465 */
466 function _field_data($table)
467 {
468 return "SELECT * FROM ".$table." LIMIT 1";
469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * The error message string
475 *
476 * @access private
477 * @return string
478 */
479 function _error_message()
480 {
481 return cubrid_error($this->conn_id);
482 }
483
484 // --------------------------------------------------------------------
485
486 /**
487 * The error message number
488 *
489 * @access private
490 * @return integer
491 */
492 function _error_number()
493 {
494 return cubrid_errno($this->conn_id);
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Escape the SQL Identifiers
501 *
502 * This function escapes column and table names
503 *
504 * @access private
505 * @param string
506 * @return string
507 */
508 function _escape_identifiers($item)
509 {
510 if ($this->_escape_char == '')
511 {
512 return $item;
513 }
514
515 foreach ($this->_reserved_identifiers as $id)
516 {
517 if (strpos($item, '.'.$id) !== FALSE)
518 {
519 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
520
521 // remove duplicates if the user already included the escape
522 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
523 }
524 }
525
526 if (strpos($item, '.') !== FALSE)
527 {
528 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
529 }
530 else
531 {
532 $str = $this->_escape_char.$item.$this->_escape_char;
533 }
534
535 // remove duplicates if the user already included the escape
536 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * From Tables
543 *
544 * This function implicitly groups FROM tables so there is no confusion
545 * about operator precedence in harmony with SQL standards
546 *
547 * @access public
548 * @param type
549 * @return type
550 */
551 function _from_tables($tables)
552 {
553 if ( ! is_array($tables))
554 {
555 $tables = array($tables);
556 }
557
558 return '('.implode(', ', $tables).')';
559 }
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Insert statement
565 *
566 * Generates a platform-specific insert string from the supplied data
567 *
568 * @access public
569 * @param string the table name
570 * @param array the insert keys
571 * @param array the insert values
572 * @return string
573 */
574 function _insert($table, $keys, $values)
575 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700576 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700577 }
578
579 // --------------------------------------------------------------------
580
581
582 /**
583 * Replace statement
584 *
585 * Generates a platform-specific replace string from the supplied data
586 *
587 * @access public
588 * @param string the table name
589 * @param array the insert keys
590 * @param array the insert values
591 * @return string
592 */
593 function _replace($table, $keys, $values)
594 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700595 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Insert_batch statement
602 *
603 * Generates a platform-specific insert string from the supplied data
604 *
605 * @access public
606 * @param string the table name
607 * @param array the insert keys
608 * @param array the insert values
609 * @return string
610 */
611 function _insert_batch($table, $keys, $values)
612 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700613 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
Esen Sagynov2e087942011-08-09 23:35:01 -0700614 }
615
616 // --------------------------------------------------------------------
617
618
619 /**
620 * Update statement
621 *
622 * Generates a platform-specific update string from the supplied data
623 *
624 * @access public
625 * @param string the table name
626 * @param array the update data
627 * @param array the where clause
628 * @param array the orderby clause
629 * @param array the limit clause
630 * @return string
631 */
632 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
633 {
634 foreach ($values as $key => $val)
635 {
Esen Sagynovee3e5942011-08-10 03:22:58 -0700636 $valstr[] = sprintf('"%s" = %s', $key, $val);
Esen Sagynov2e087942011-08-09 23:35:01 -0700637 }
638
639 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
640
641 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
642
643 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
644
645 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
646
647 $sql .= $orderby.$limit;
648
649 return $sql;
650 }
651
652 // --------------------------------------------------------------------
653
654
655 /**
656 * Update_Batch statement
657 *
658 * Generates a platform-specific batch update string from the supplied data
659 *
660 * @access public
661 * @param string the table name
662 * @param array the update data
663 * @param array the where clause
664 * @return string
665 */
666 function _update_batch($table, $values, $index, $where = NULL)
667 {
668 $ids = array();
669 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
670
671 foreach ($values as $key => $val)
672 {
673 $ids[] = $val[$index];
674
675 foreach (array_keys($val) as $field)
676 {
677 if ($field != $index)
678 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700679 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Esen Sagynov2e087942011-08-09 23:35:01 -0700680 }
681 }
682 }
683
684 $sql = "UPDATE ".$table." SET ";
685 $cases = '';
686
687 foreach ($final as $k => $v)
688 {
689 $cases .= $k.' = CASE '."\n";
690 foreach ($v as $row)
691 {
692 $cases .= $row."\n";
693 }
694
695 $cases .= 'ELSE '.$k.' END, ';
696 }
697
698 $sql .= substr($cases, 0, -2);
699
700 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
701
702 return $sql;
703 }
704
705 // --------------------------------------------------------------------
706
707
708 /**
709 * Truncate statement
710 *
711 * Generates a platform-specific truncate string from the supplied data
712 * If the database does not support the truncate() command
713 * This function maps to "DELETE FROM table"
714 *
715 * @access public
716 * @param string the table name
717 * @return string
718 */
719 function _truncate($table)
720 {
721 return "TRUNCATE ".$table;
722 }
723
724 // --------------------------------------------------------------------
725
726 /**
727 * Delete statement
728 *
729 * Generates a platform-specific delete string from the supplied data
730 *
731 * @access public
732 * @param string the table name
733 * @param array the where clause
734 * @param string the limit clause
735 * @return string
736 */
737 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
738 {
739 $conditions = '';
740
741 if (count($where) > 0 OR count($like) > 0)
742 {
743 $conditions = "\nWHERE ";
744 $conditions .= implode("\n", $this->ar_where);
745
746 if (count($where) > 0 && count($like) > 0)
747 {
748 $conditions .= " AND ";
749 }
750 $conditions .= implode("\n", $like);
751 }
752
753 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
754
755 return "DELETE FROM ".$table.$conditions.$limit;
756 }
757
758 // --------------------------------------------------------------------
759
760 /**
761 * Limit string
762 *
763 * Generates a platform-specific LIMIT clause
764 *
765 * @access public
766 * @param string the sql query string
767 * @param integer the number of rows to limit the query to
768 * @param integer the offset value
769 * @return string
770 */
771 function _limit($sql, $limit, $offset)
772 {
773 if ($offset == 0)
774 {
775 $offset = '';
776 }
777 else
778 {
779 $offset .= ", ";
780 }
781
782 return $sql."LIMIT ".$offset.$limit;
783 }
784
785 // --------------------------------------------------------------------
786
787 /**
788 * Close DB Connection
789 *
790 * @access public
791 * @param resource
792 * @return void
793 */
794 function _close($conn_id)
795 {
796 @cubrid_close($conn_id);
797 }
798
799}
800
801
802/* End of file cubrid_driver.php */
Andrey Andreevea3eec92012-03-01 19:16:23 +0200803/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */