]>
jfr.im git - z_archive/KronOS.git/blob - video/system/database/drivers/cubrid/cubrid_driver.php
d011404120787311aae0b094b86fa01284ac9234
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 2.0.2
16 // ------------------------------------------------------------------------
19 * CUBRID Database Adapter Class
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
25 * @package CodeIgniter
28 * @author Esen Sagynov
29 * @link http://codeigniter.com/user_guide/database/
31 class CI_DB_cubrid_driver
extends CI_DB
{
33 // Default CUBRID Broker port. Will be used unless user
34 // explicitly specifies another one.
35 const DEFAULT_PORT
= 33000;
37 var $dbdriver = 'cubrid';
39 // The character used for escaping - no need in CUBRID
40 var $_escape_char = '';
42 // clause and character used for LIKE escape sequences - not used in CUBRID
43 var $_like_escape_str = '';
44 var $_like_escape_chr = '';
47 * The syntax to count rows is slightly different across different
48 * database engines, so this string appears in each driver and is
49 * used for the count_all() and count_all_results() functions.
51 var $_count_string = 'SELECT COUNT(*) AS ';
52 var $_random_keyword = ' RAND()'; // database specific random keyword
55 * Non-persistent database connection
57 * @access private called by the base class
62 // If no port is defined by the user, use the default value
63 if ($this->port
== '')
65 $this->port
= self
::DEFAULT_PORT
;
68 $conn = cubrid_connect($this->hostname
, $this->port
, $this->database
, $this->username
, $this->password
);
72 // Check if a user wants to run queries in dry, i.e. run the
73 // queries but not commit them.
74 if (isset($this->auto_commit
) && ! $this->auto_commit
)
76 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE
);
80 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE
);
81 $this->auto_commit
= TRUE;
88 // --------------------------------------------------------------------
91 * Persistent database connection
92 * In CUBRID persistent DB connection is supported natively in CUBRID
93 * engine which can be configured in the CUBRID Broker configuration
94 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
95 * connections established between the client application and the
96 * server will become persistent. This is calling the same
97 * @cubrid_connect function will establish persisten connection
98 * considering that the CCI_PCONNECT is ON.
100 * @access private called by the base class
103 function db_pconnect()
105 return $this->db_connect();
108 // --------------------------------------------------------------------
113 * Keep / reestablish the db connection if no queries have been
114 * sent for a length of time exceeding the server's idle timeout
121 if (cubrid_ping($this->conn_id
) === FALSE)
123 $this->conn_id
= FALSE;
127 // --------------------------------------------------------------------
130 * Select the database
132 * @access private called by the base class
137 // In CUBRID there is no need to select a database as the database
138 // is chosen at the connection time.
139 // So, to determine if the database is "selected", all we have to
140 // do is ping the server and return that value.
141 return cubrid_ping($this->conn_id
);
144 // --------------------------------------------------------------------
147 * Set client character set
154 function db_set_charset($charset, $collation)
156 // In CUBRID, there is no need to set charset or collation.
157 // This is why returning true will allow the application continue
158 // its normal process.
162 // --------------------------------------------------------------------
165 * Version number query string
172 // To obtain the CUBRID Server version, no need to run the SQL query.
173 // CUBRID PHP API provides a function to determin this value.
174 // This is why we also need to add 'cubrid' value to the list of
175 // $driver_version_exceptions array in DB_driver class in
176 // version() function.
177 return cubrid_get_server_info($this->conn_id
);
180 // --------------------------------------------------------------------
185 * @access private called by the base class
186 * @param string an SQL query
189 function _execute($sql)
191 $sql = $this->_prep_query($sql);
192 return @cubrid_query($sql, $this->conn_id
);
195 // --------------------------------------------------------------------
200 * If needed, each database adapter can prep the query string
202 * @access private called by execute()
203 * @param string an SQL query
206 function _prep_query($sql)
208 // No need to prepare
212 // --------------------------------------------------------------------
220 function trans_begin($test_mode = FALSE)
222 if ( ! $this->trans_enabled
)
227 // When transactions are nested we only begin/commit/rollback the outermost ones
228 if ($this->_trans_depth
> 0)
233 // Reset the transaction failure flag.
234 // If the $test_mode flag is set to TRUE transactions will be rolled back
235 // even if the queries produce a successful result.
236 $this->_trans_failure
= ($test_mode === TRUE) ? TRUE : FALSE;
238 if (cubrid_get_autocommit($this->conn_id
))
240 cubrid_set_autocommit($this->conn_id
, CUBRID_AUTOCOMMIT_FALSE
);
246 // --------------------------------------------------------------------
254 function trans_commit()
256 if ( ! $this->trans_enabled
)
261 // When transactions are nested we only begin/commit/rollback the outermost ones
262 if ($this->_trans_depth
> 0)
267 cubrid_commit($this->conn_id
);
269 if ($this->auto_commit
&& ! cubrid_get_autocommit($this->conn_id
))
271 cubrid_set_autocommit($this->conn_id
, CUBRID_AUTOCOMMIT_TRUE
);
277 // --------------------------------------------------------------------
280 * Rollback Transaction
285 function trans_rollback()
287 if ( ! $this->trans_enabled
)
292 // When transactions are nested we only begin/commit/rollback the outermost ones
293 if ($this->_trans_depth
> 0)
298 cubrid_rollback($this->conn_id
);
300 if ($this->auto_commit
&& ! cubrid_get_autocommit($this->conn_id
))
302 cubrid_set_autocommit($this->conn_id
, CUBRID_AUTOCOMMIT_TRUE
);
308 // --------------------------------------------------------------------
315 * @param bool whether or not the string will be used in a LIKE condition
318 function escape_str($str, $like = FALSE)
322 foreach ($str as $key => $val)
324 $str[$key] = $this->escape_str($val, $like);
330 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id
))
332 $str = cubrid_real_escape_string($str, $this->conn_id
);
336 $str = addslashes($str);
339 // escape LIKE condition wildcards
342 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
348 // --------------------------------------------------------------------
356 function affected_rows()
358 return @cubrid_affected_rows($this->conn_id
);
361 // --------------------------------------------------------------------
371 return @cubrid_insert_id($this->conn_id
);
374 // --------------------------------------------------------------------
379 * Generates a platform-specific query string that counts all records in
380 * the specified table
386 function count_all($table = '')
393 $query = $this->query($this->_count_string
. $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
395 if ($query->num_rows() == 0)
400 $row = $query->row();
401 $this->_reset_select();
402 return (int) $row->numrows
;
405 // --------------------------------------------------------------------
410 * Generates a platform-specific query string so that the table names can be fetched
416 function _list_tables($prefix_limit = FALSE)
418 $sql = "SHOW TABLES";
420 if ($prefix_limit !== FALSE AND $this->dbprefix
!= '')
422 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix
)."%'";
428 // --------------------------------------------------------------------
433 * Generates a platform-specific query string so that the column names can be fetched
436 * @param string the table name
439 function _list_columns($table = '')
441 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
444 // --------------------------------------------------------------------
449 * Generates a platform-specific query so that the column data can be retrieved
452 * @param string the table name
455 function _field_data($table)
457 return "SELECT * FROM ".$table." LIMIT 1";
460 // --------------------------------------------------------------------
463 * The error message string
468 function _error_message()
470 return cubrid_error($this->conn_id
);
473 // --------------------------------------------------------------------
476 * The error message number
481 function _error_number()
483 return cubrid_errno($this->conn_id
);
486 // --------------------------------------------------------------------
489 * Escape the SQL Identifiers
491 * This function escapes column and table names
497 function _escape_identifiers($item)
499 if ($this->_escape_char
== '')
504 foreach ($this->_reserved_identifiers
as $id)
506 if (strpos($item, '.'.$id) !== FALSE)
508 $str = $this->_escape_char
. str_replace('.', $this->_escape_char
.'.', $item);
510 // remove duplicates if the user already included the escape
511 return preg_replace('/['.$this->_escape_char
.']+/', $this->_escape_char
, $str);
515 if (strpos($item, '.') !== FALSE)
517 $str = $this->_escape_char
.str_replace('.', $this->_escape_char
.'.'.$this->_escape_char
, $item).$this->_escape_char
;
521 $str = $this->_escape_char
.$item.$this->_escape_char
;
524 // remove duplicates if the user already included the escape
525 return preg_replace('/['.$this->_escape_char
.']+/', $this->_escape_char
, $str);
528 // --------------------------------------------------------------------
533 * This function implicitly groups FROM tables so there is no confusion
534 * about operator precedence in harmony with SQL standards
540 function _from_tables($tables)
542 if ( ! is_array($tables))
544 $tables = array($tables);
547 return '('.implode(', ', $tables).')';
550 // --------------------------------------------------------------------
555 * Generates a platform-specific insert string from the supplied data
558 * @param string the table name
559 * @param array the insert keys
560 * @param array the insert values
563 function _insert($table, $keys, $values)
565 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
568 // --------------------------------------------------------------------
574 * Generates a platform-specific replace string from the supplied data
577 * @param string the table name
578 * @param array the insert keys
579 * @param array the insert values
582 function _replace($table, $keys, $values)
584 return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")";
587 // --------------------------------------------------------------------
590 * Insert_batch statement
592 * Generates a platform-specific insert string from the supplied data
595 * @param string the table name
596 * @param array the insert keys
597 * @param array the insert values
600 function _insert_batch($table, $keys, $values)
602 return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values);
605 // --------------------------------------------------------------------
611 * Generates a platform-specific update string from the supplied data
614 * @param string the table name
615 * @param array the update data
616 * @param array the where clause
617 * @param array the orderby clause
618 * @param array the limit clause
621 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
623 foreach ($values as $key => $val)
625 $valstr[] = sprintf('"%s" = %s', $key, $val);
628 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
630 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
632 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
634 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
636 $sql .= $orderby.$limit;
641 // --------------------------------------------------------------------
645 * Update_Batch statement
647 * Generates a platform-specific batch update string from the supplied data
650 * @param string the table name
651 * @param array the update data
652 * @param array the where clause
655 function _update_batch($table, $values, $index, $where = NULL)
658 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
660 foreach ($values as $key => $val)
662 $ids[] = $val[$index];
664 foreach (array_keys($val) as $field)
666 if ($field != $index)
668 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
673 $sql = "UPDATE ".$table." SET ";
676 foreach ($final as $k => $v)
678 $cases .= $k.' = CASE '."\n";
684 $cases .= 'ELSE '.$k.' END, ';
687 $sql .= substr($cases, 0, -2);
689 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
694 // --------------------------------------------------------------------
700 * Generates a platform-specific truncate string from the supplied data
701 * If the database does not support the truncate() command
702 * This function maps to "DELETE FROM table"
705 * @param string the table name
708 function _truncate($table)
710 return "TRUNCATE ".$table;
713 // --------------------------------------------------------------------
718 * Generates a platform-specific delete string from the supplied data
721 * @param string the table name
722 * @param array the where clause
723 * @param string the limit clause
726 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
730 if (count($where) > 0 OR count($like) > 0)
732 $conditions = "\nWHERE ";
733 $conditions .= implode("\n", $this->ar_where
);
735 if (count($where) > 0 && count($like) > 0)
737 $conditions .= " AND ";
739 $conditions .= implode("\n", $like);
742 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
744 return "DELETE FROM ".$table.$conditions.$limit;
747 // --------------------------------------------------------------------
752 * Generates a platform-specific LIMIT clause
755 * @param string the sql query string
756 * @param integer the number of rows to limit the query to
757 * @param integer the offset value
760 function _limit($sql, $limit, $offset)
771 return $sql."LIMIT ".$offset.$limit;
774 // --------------------------------------------------------------------
777 * Close DB Connection
783 function _close($conn_id)
785 @cubrid_close($conn_id);
791 /* End of file cubrid_driver.php */
792 /* Location: ./system/database/drivers/cubrid/cubrid_driver.php */