From 7f55d6133b70346a428ae481d1fe57bf4d4d2320 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 13:44:28 +0200 Subject: Improve the CUBRID database driver --- system/database/drivers/cubrid/cubrid_driver.php | 284 ++++++++--------------- 1 file changed, 99 insertions(+), 185 deletions(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index cde719eae..1cfaf3e42 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -1,13 +1,13 @@ -port == '') { - $this->port = self::DEFAULT_PORT; + // Default CUBRID broker port + $this->port = 33000; } + } + /** + * Non-persistent database connection + * + * @return resource + */ + public function db_connect() + { $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password); if ($conn) @@ -112,7 +109,7 @@ class CI_DB_cubrid_driver extends CI_DB { * @access private called by the base class * @return resource */ - function db_pconnect() + public function db_pconnect() { return $this->db_connect(); } @@ -125,10 +122,9 @@ class CI_DB_cubrid_driver extends CI_DB { * Keep / reestablish the db connection if no queries have been * sent for a length of time exceeding the server's idle timeout * - * @access public * @return void */ - function reconnect() + public function reconnect() { if (cubrid_ping($this->conn_id) === FALSE) { @@ -141,10 +137,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Select the database * - * @access private called by the base class * @return resource */ - function db_select() + public function db_select() { // In CUBRID there is no need to select a database as the database // is chosen at the connection time. @@ -158,12 +153,11 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Set client character set * - * @access public * @param string * @param string - * @return resource + * @return bool */ - function db_set_charset($charset, $collation) + public function db_set_charset($charset, $collation) { // In CUBRID, there is no need to set charset or collation. // This is why returning true will allow the application continue @@ -176,16 +170,10 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Version number query string * - * @access public * @return string */ - function _version() + protected function _version() { - // To obtain the CUBRID Server version, no need to run the SQL query. - // CUBRID PHP API provides a function to determin this value. - // This is why we also need to add 'cubrid' value to the list of - // $driver_version_exceptions array in DB_driver class in - // version() function. return cubrid_get_server_info($this->conn_id); } @@ -194,11 +182,10 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Execute the query * - * @access private called by the base class * @param string an SQL query * @return resource */ - function _execute($sql) + protected function _execute($sql) { $sql = $this->_prep_query($sql); return @cubrid_query($sql, $this->conn_id); @@ -211,11 +198,10 @@ class CI_DB_cubrid_driver extends CI_DB { * * If needed, each database adapter can prep the query string * - * @access private called by execute() * @param string an SQL query * @return string */ - function _prep_query($sql) + protected function _prep_query($sql) { // No need to prepare return $sql; @@ -226,18 +212,12 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Begin Transaction * - * @access public * @return bool */ - function trans_begin($test_mode = FALSE) + public function trans_begin($test_mode = FALSE) { - if ( ! $this->trans_enabled) - { - return TRUE; - } - // When transactions are nested we only begin/commit/rollback the outermost ones - if ($this->_trans_depth > 0) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } @@ -245,7 +225,7 @@ class CI_DB_cubrid_driver extends CI_DB { // Reset the transaction failure flag. // If the $test_mode flag is set to TRUE transactions will be rolled back // even if the queries produce a successful result. - $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; + $this->_trans_failure = ($test_mode === TRUE); if (cubrid_get_autocommit($this->conn_id)) { @@ -260,18 +240,12 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Commit Transaction * - * @access public * @return bool */ - function trans_commit() + public function trans_commit() { - if ( ! $this->trans_enabled) - { - return TRUE; - } - // When transactions are nested we only begin/commit/rollback the outermost ones - if ($this->_trans_depth > 0) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } @@ -291,18 +265,12 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Rollback Transaction * - * @access public * @return bool */ - function trans_rollback() + public function trans_rollback() { - if ( ! $this->trans_enabled) - { - return TRUE; - } - // When transactions are nested we only begin/commit/rollback the outermost ones - if ($this->_trans_depth > 0) + if ( ! $this->trans_enabled OR $this->_trans_depth > 0) { return TRUE; } @@ -322,12 +290,11 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Escape String * - * @access public * @param string * @param bool whether or not the string will be used in a LIKE condition * @return string */ - function escape_str($str, $like = FALSE) + public function escape_str($str, $like = FALSE) { if (is_array($str)) { @@ -339,7 +306,7 @@ class CI_DB_cubrid_driver extends CI_DB { return $str; } - if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id)) + if (function_exists('cubrid_real_escape_string') && is_resource($this->conn_id)) { $str = cubrid_real_escape_string($str, $this->conn_id); } @@ -351,7 +318,7 @@ class CI_DB_cubrid_driver extends CI_DB { // escape LIKE condition wildcards if ($like === TRUE) { - $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); + return str_replace(array('%', '_'), array('\\%', '\\_'), $str); } return $str; @@ -362,10 +329,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Affected Rows * - * @access public - * @return integer + * @return int */ - function affected_rows() + public function affected_rows() { return @cubrid_affected_rows($this->conn_id); } @@ -375,10 +341,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Insert ID * - * @access public - * @return integer + * @return int */ - function insert_id() + public function insert_id() { return @cubrid_insert_id($this->conn_id); } @@ -391,27 +356,24 @@ class CI_DB_cubrid_driver extends CI_DB { * Generates a platform-specific query string that counts all records in * the specified table * - * @access public * @param string - * @return string + * @return int */ - function count_all($table = '') + public function count_all($table = '') { if ($table == '') { return 0; } - - $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); - + $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); if ($query->num_rows() == 0) { return 0; } - $row = $query->row(); + $query = $query->row(); $this->_reset_select(); - return (int) $row->numrows; + return (int) $query->numrows; } // -------------------------------------------------------------------- @@ -421,17 +383,16 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific query string so that the table names can be fetched * - * @access private * @param boolean * @return string */ - function _list_tables($prefix_limit = FALSE) + protected function _list_tables($prefix_limit = FALSE) { - $sql = "SHOW TABLES"; + $sql = 'SHOW TABLES'; - if ($prefix_limit !== FALSE AND $this->dbprefix != '') + if ($prefix_limit !== FALSE && $this->dbprefix != '') { - $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; + return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; } return $sql; @@ -444,13 +405,12 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific query string so that the column names can be fetched * - * @access public * @param string the table name * @return string */ - function _list_columns($table = '') + protected function _list_columns($table = '') { - return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE); + return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE); } // -------------------------------------------------------------------- @@ -460,13 +420,12 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific query so that the column data can be retrieved * - * @access public * @param string the table name - * @return object + * @return string */ - function _field_data($table) + protected function _field_data($table) { - return "SELECT * FROM ".$table." LIMIT 1"; + return 'SELECT * FROM '.$table.' LIMIT 1'; } // -------------------------------------------------------------------- @@ -474,10 +433,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * The error message string * - * @access private * @return string */ - function _error_message() + protected function _error_message() { return cubrid_error($this->conn_id); } @@ -487,10 +445,9 @@ class CI_DB_cubrid_driver extends CI_DB { /** * The error message number * - * @access private - * @return integer + * @return int */ - function _error_number() + protected function _error_number() { return cubrid_errno($this->conn_id); } @@ -502,11 +459,10 @@ class CI_DB_cubrid_driver extends CI_DB { * * This function escapes column and table names * - * @access private * @param string * @return string */ - function _escape_identifiers($item) + public function _escape_identifiers($item) { if ($this->_escape_char == '') { @@ -517,24 +473,20 @@ class CI_DB_cubrid_driver extends CI_DB { { if (strpos($item, '.'.$id) !== FALSE) { - $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); + $item = str_replace('.', $this->_escape_char.'.', $item); // remove duplicates if the user already included the escape - return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); } } if (strpos($item, '.') !== FALSE) { - $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; - } - else - { - $str = $this->_escape_char.$item.$this->_escape_char; + $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); } // remove duplicates if the user already included the escape - return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); } // -------------------------------------------------------------------- @@ -545,11 +497,10 @@ class CI_DB_cubrid_driver extends CI_DB { * This function implicitly groups FROM tables so there is no confusion * about operator precedence in harmony with SQL standards * - * @access public - * @param type - * @return type + * @param string the table name + * @return string */ - function _from_tables($tables) + protected function _from_tables($tables) { if ( ! is_array($tables)) { @@ -566,15 +517,14 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific insert string from the supplied data * - * @access public * @param string the table name * @param array the insert keys * @param array the insert values * @return string */ - function _insert($table, $keys, $values) + protected function _insert($table, $keys, $values) { - return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; + return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -585,15 +535,14 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific replace string from the supplied data * - * @access public * @param string the table name * @param array the insert keys * @param array the insert values * @return string */ - function _replace($table, $keys, $values) + protected function _replace($table, $keys, $values) { - return "REPLACE INTO ".$table." (\"".implode('", "', $keys)."\") VALUES (".implode(', ', $values).")"; + return 'REPLACE INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -603,26 +552,23 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific insert string from the supplied data * - * @access public * @param string the table name * @param array the insert keys * @param array the insert values * @return string */ - function _insert_batch($table, $keys, $values) + protected function _insert_batch($table, $keys, $values) { - return "INSERT INTO ".$table." (\"".implode('", "', $keys)."\") VALUES ".implode(', ', $values); + return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES '.implode(', ', $values); } // -------------------------------------------------------------------- - /** * Update statement * * Generates a platform-specific update string from the supplied data * - * @access public * @param string the table name * @param array the update data * @param array the where clause @@ -630,24 +576,17 @@ class CI_DB_cubrid_driver extends CI_DB { * @param array the limit clause * @return string */ - function _update($table, $values, $where, $orderby = array(), $limit = FALSE) + protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) { foreach ($values as $key => $val) { $valstr[] = sprintf('"%s" = %s', $key, $val); } - $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - - $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; - - $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); - - $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; - - $sql .= $orderby.$limit; - - return $sql; + return 'UPDATE '.$table.' SET '.implode(', ', $valstr) + .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') + .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') + .( ! $limit ? '' : ' LIMIT '.$limit); } // -------------------------------------------------------------------- @@ -658,16 +597,15 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific batch update string from the supplied data * - * @access public * @param string the table name * @param array the update data * @param array the where clause * @return string */ - function _update_batch($table, $values, $index, $where = NULL) + protected function _update_batch($table, $values, $index, $where = NULL) { $ids = array(); - $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; + $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : ''; foreach ($values as $key => $val) { @@ -682,30 +620,23 @@ class CI_DB_cubrid_driver extends CI_DB { } } - $sql = "UPDATE ".$table." SET "; + $sql = 'UPDATE '.$table.' SET '; $cases = ''; foreach ($final as $k => $v) { - $cases .= $k.' = CASE '."\n"; - foreach ($v as $row) - { - $cases .= $row."\n"; - } - - $cases .= 'ELSE '.$k.' END, '; + $cases .= $k." = CASE \n" + .implode("\n", $v) + .'ELSE '.$k.' END, '; } - $sql .= substr($cases, 0, -2); - - $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; - - return $sql; + return $sql.substr($cases, 0, -2) + .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') + .$index.' IN ('.implode(',', $ids).')'; } // -------------------------------------------------------------------- - /** * Truncate statement * @@ -713,13 +644,12 @@ class CI_DB_cubrid_driver extends CI_DB { * If the database does not support the truncate() command * This function maps to "DELETE FROM table" * - * @access public * @param string the table name * @return string */ - function _truncate($table) + protected function _truncate($table) { - return "TRUNCATE ".$table; + return 'TRUNCATE '.$table; } // -------------------------------------------------------------------- @@ -729,31 +659,27 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific delete string from the supplied data * - * @access public * @param string the table name * @param array the where clause * @param string the limit clause * @return string */ - function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) { $conditions = ''; if (count($where) > 0 OR count($like) > 0) { - $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions = "\nWHERE ".implode("\n", $this->ar_where); if (count($where) > 0 && count($like) > 0) { - $conditions .= " AND "; + $conditions .= ' AND '; } $conditions .= implode("\n", $like); } - $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - - return "DELETE FROM ".$table.$conditions.$limit; + return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); } // -------------------------------------------------------------------- @@ -763,24 +689,14 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific LIMIT clause * - * @access public * @param string the sql query string - * @param integer the number of rows to limit the query to - * @param integer the offset value + * @param int the number of rows to limit the query to + * @param int the offset value * @return string */ - function _limit($sql, $limit, $offset) + protected function _limit($sql, $limit, $offset) { - if ($offset == 0) - { - $offset = ''; - } - else - { - $offset .= ", "; - } - - return $sql."LIMIT ".$offset.$limit; + return $sql.'LIMIT '.($offset == 0 ? '' : ', ').$limit; } // -------------------------------------------------------------------- @@ -788,17 +704,15 @@ class CI_DB_cubrid_driver extends CI_DB { /** * Close DB Connection * - * @access public * @param resource * @return void */ - function _close($conn_id) + protected function _close($conn_id) { @cubrid_close($conn_id); } } - /* End of file cubrid_driver.php */ -/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ \ No newline at end of file +/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ -- cgit v1.2.3-24-g4f1b From c6953f4077fe3ee394abb37eaa0575527bd013cc Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 14:43:16 +0200 Subject: Fix _limit() --- system/database/drivers/cubrid/cubrid_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index 1cfaf3e42..b89746b7d 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -696,7 +696,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _limit($sql, $limit, $offset) { - return $sql.'LIMIT '.($offset == 0 ? '' : ', ').$limit; + return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From ca7e5f1eb15408b9d71e55d9a0e54b96f5ddc6aa Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 17:58:28 +0200 Subject: Call parent::__construct() in our constructor --- system/database/drivers/cubrid/cubrid_driver.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index b89746b7d..d1fc5f0fa 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -57,8 +57,10 @@ class CI_DB_cubrid_driver extends CI_DB { protected $_count_string = 'SELECT COUNT(*) AS '; protected $_random_keyword = ' RAND()'; // database specific random keyword - public function __construct() + public function __construct($params) { + parent::__construct($params); + // If no port is defined by the user, use the default value if ($this->port == '') { -- cgit v1.2.3-24-g4f1b From 2e430a3f3b160641c36037fbaf92457e0b3bb51e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 12 Feb 2012 23:37:58 +0200 Subject: Added CUBRID DSN string & persistent connections support --- system/database/drivers/cubrid/cubrid_driver.php | 104 +++++++++++++---------- 1 file changed, 58 insertions(+), 46 deletions(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index d1fc5f0fa..f7dec72de 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -57,15 +57,21 @@ class CI_DB_cubrid_driver extends CI_DB { protected $_count_string = 'SELECT COUNT(*) AS '; protected $_random_keyword = ' RAND()'; // database specific random keyword + // CUBRID-specific properties + public $auto_commit = TRUE; + public function __construct($params) { parent::__construct($params); - // If no port is defined by the user, use the default value - if ($this->port == '') + if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) { - // Default CUBRID broker port - $this->port = 33000; + preg_match('/autocommit=on/', $matches[2], $matches) OR $this->auto_commit = FALSE; + } + else + { + // If no port is defined by the user, use the default value + $this->port == '' OR $this->port = 33000; } } @@ -76,44 +82,62 @@ class CI_DB_cubrid_driver extends CI_DB { */ public function db_connect() { - $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password); - - if ($conn) - { - // Check if a user wants to run queries in dry, i.e. run the - // queries but not commit them. - if (isset($this->auto_commit) && ! $this->auto_commit) - { - cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE); - } - else - { - cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE); - $this->auto_commit = TRUE; - } - } - - return $conn; + return $this->_cubrid_connect(); } // -------------------------------------------------------------------- /** * Persistent database connection + * * In CUBRID persistent DB connection is supported natively in CUBRID * engine which can be configured in the CUBRID Broker configuration * file by setting the CCI_PCONNECT parameter to ON. In that case, all * connections established between the client application and the - * server will become persistent. This is calling the same - * @cubrid_connect function will establish persisten connection - * considering that the CCI_PCONNECT is ON. + * server will become persistent. * - * @access private called by the base class * @return resource */ public function db_pconnect() { - return $this->db_connect(); + return $this->_cubrid_connect(TRUE); + } + + // -------------------------------------------------------------------- + + /** + * CUBRID connection + * + * A CUBRID-specific method to create a connection to the database. + * Except for determining if a persistent connection should be used, + * the rest of the logic is the same for db_connect() and db_pconnect(). + * + * @param bool + * @return resource + */ + protected function _cubrid_connect($persistent = FALSE) + { + if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches)) + { + $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '') + ? cubrid_connect_with_url($this->dsn, $this->username, $this->password) + : cubrid_connect_with_url($this->dsn); + } + else + { + $_temp = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect'; + $conn_id = ($this->username !== '') + ? $_temp($this->hostname, $this->port, $this->database, $this->username, $this->password) + : $_temp($this->hostname, $this->port, $this->database); + } + + if ($conn_id) + { + $_temp = ($this->auto_commit) ? CUBRID_AUTOCOMMIT_TRUE : CUBRID_AUTOCOMMIT_FALSE; + cubrid_set_autocommit($conn_id, $_temp); + } + + return $conn_id; } // -------------------------------------------------------------------- @@ -161,9 +185,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ public function db_set_charset($charset, $collation) { - // In CUBRID, there is no need to set charset or collation. - // This is why returning true will allow the application continue - // its normal process. + // Not supported in CUBRID return TRUE; } @@ -189,8 +211,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _execute($sql) { - $sql = $this->_prep_query($sql); - return @cubrid_query($sql, $this->conn_id); + return @cubrid_query($this->_prep_query($sql), $this->conn_id); } // -------------------------------------------------------------------- @@ -205,7 +226,6 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _prep_query($sql) { - // No need to prepare return $sql; } @@ -385,7 +405,7 @@ class CI_DB_cubrid_driver extends CI_DB { * * Generates a platform-specific query string so that the table names can be fetched * - * @param boolean + * @param bool * @return string */ protected function _list_tables($prefix_limit = FALSE) @@ -607,8 +627,6 @@ class CI_DB_cubrid_driver extends CI_DB { protected function _update_batch($table, $values, $index, $where = NULL) { $ids = array(); - $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : ''; - foreach ($values as $key => $val) { $ids[] = $val[$index]; @@ -622,9 +640,7 @@ class CI_DB_cubrid_driver extends CI_DB { } } - $sql = 'UPDATE '.$table.' SET '; $cases = ''; - foreach ($final as $k => $v) { $cases .= $k." = CASE \n" @@ -632,7 +648,7 @@ class CI_DB_cubrid_driver extends CI_DB { .'ELSE '.$k.' END, '; } - return $sql.substr($cases, 0, -2) + return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') .$index.' IN ('.implode(',', $ids).')'; } @@ -672,13 +688,9 @@ class CI_DB_cubrid_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { - $conditions = "\nWHERE ".implode("\n", $this->ar_where); - - if (count($where) > 0 && count($like) > 0) - { - $conditions .= ' AND '; - } - $conditions .= implode("\n", $like); + $conditions = "\nWHERE ".implode("\n", $where) + .((count($where) > 0 && count($like) > 0) ? ' AND ' : '') + .implode("\n", $like); } return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); -- cgit v1.2.3-24-g4f1b From 36cd531e9874d32a272088838055bdd56045c41b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 13 Feb 2012 01:40:55 +0200 Subject: Fix auto_commit initialization --- system/database/drivers/cubrid/cubrid_driver.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index f7dec72de..a0d874a8c 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -66,7 +66,10 @@ class CI_DB_cubrid_driver extends CI_DB { if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) { - preg_match('/autocommit=on/', $matches[2], $matches) OR $this->auto_commit = FALSE; + if (preg_match('/autocommit=off/', $matches[2], $matches)) + { + $this->auto_commit = FALSE; + } } else { -- cgit v1.2.3-24-g4f1b From fe4b4e9d5f5c19da77c8e675f33f030766019ac3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 13 Feb 2012 05:40:25 +0200 Subject: Fix pconnect with DSN --- system/database/drivers/cubrid/cubrid_driver.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index a0d874a8c..8d5344af9 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -122,9 +122,10 @@ class CI_DB_cubrid_driver extends CI_DB { { if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches)) { + $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url'; $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '') - ? cubrid_connect_with_url($this->dsn, $this->username, $this->password) - : cubrid_connect_with_url($this->dsn); + ? $_temp($this->dsn, $this->username, $this->password) + : $_temp($this->dsn); } else { -- cgit v1.2.3-24-g4f1b From cac407d6399e9325454b48418cf3fce873ce1b14 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 14 Feb 2012 11:45:36 +0200 Subject: Replace a preg_match() with stripos() --- system/database/drivers/cubrid/cubrid_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index 8d5344af9..147e463ce 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -66,7 +66,7 @@ class CI_DB_cubrid_driver extends CI_DB { if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches)) { - if (preg_match('/autocommit=off/', $matches[2], $matches)) + if (stripos($matches[2], 'autocommit=off') !== FALSE) { $this->auto_commit = FALSE; } -- cgit v1.2.3-24-g4f1b From 394a3f171c72176d3e31a2562e604a26a703f957 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 15 Feb 2012 14:35:11 +0200 Subject: Add improvements from @CUBRID's implementation --- system/database/drivers/cubrid/cubrid_driver.php | 33 ++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index 147e463ce..aa8e997ce 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -43,7 +43,7 @@ class CI_DB_cubrid_driver extends CI_DB { public $dbdriver = 'cubrid'; // The character used for escaping - no need in CUBRID - protected $_escape_char = ''; + protected $_escape_char = '`'; // clause and character used for LIKE escape sequences - not used in CUBRID protected $_like_escape_str = ''; @@ -135,12 +135,6 @@ class CI_DB_cubrid_driver extends CI_DB { : $_temp($this->hostname, $this->port, $this->database); } - if ($conn_id) - { - $_temp = ($this->auto_commit) ? CUBRID_AUTOCOMMIT_TRUE : CUBRID_AUTOCOMMIT_FALSE; - cubrid_set_autocommit($conn_id, $_temp); - } - return $conn_id; } @@ -332,7 +326,9 @@ class CI_DB_cubrid_driver extends CI_DB { return $str; } - if (function_exists('cubrid_real_escape_string') && is_resource($this->conn_id)) + if (function_exists('cubrid_real_escape_string') && + (is_resource($this->conn_id) + OR (get_resource_type($this->conn_id) === 'Unknown' && preg_match('/Resource id #/', strval($this->conn_id))))) { $str = cubrid_real_escape_string($str, $this->conn_id); } @@ -359,7 +355,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ public function affected_rows() { - return @cubrid_affected_rows($this->conn_id); + return @cubrid_affected_rows(); } // -------------------------------------------------------------------- @@ -550,7 +546,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _insert($table, $keys, $values) { - return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; + return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -568,7 +564,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _replace($table, $keys, $values) { - return 'REPLACE INTO '.$table.' ("'.implode('", "', $keys).'") VALUES ('.implode(', ', $values).')'; + return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; } // -------------------------------------------------------------------- @@ -585,7 +581,7 @@ class CI_DB_cubrid_driver extends CI_DB { */ protected function _insert_batch($table, $keys, $values) { - return 'INSERT INTO '.$table.' ("'.implode('", "', $keys).'") VALUES '.implode(', ', $values); + return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); } // -------------------------------------------------------------------- @@ -602,15 +598,20 @@ class CI_DB_cubrid_driver extends CI_DB { * @param array the limit clause * @return string */ - protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) + protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) { foreach ($values as $key => $val) { - $valstr[] = sprintf('"%s" = %s', $key, $val); + $valstr[] = $key.' = '.$val; + } + + $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : ''; + if (count($like) > 0) + { + $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); } - return 'UPDATE '.$table.' SET '.implode(', ', $valstr) - .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') + return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') .( ! $limit ? '' : ' LIMIT '.$limit); } -- cgit v1.2.3-24-g4f1b From cf63120a60da9485ab1f94bc58b5d62e6b05a0fd Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 16 Feb 2012 11:36:28 +0200 Subject: A change in the class descriptions --- system/database/drivers/cubrid/cubrid_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index aa8e997ce..1dc7a454b 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -21,7 +21,7 @@ * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com - * @since Version 2.0.2 + * @since Version 2.1 * @filesource */ -- cgit v1.2.3-24-g4f1b From 7efad20597ef7e06f8cf837a9f40918d2d3f2727 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Sun, 19 Feb 2012 12:37:00 +0000 Subject: Renaming Active Record to Query Builder across the system --- system/database/drivers/cubrid/cubrid_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/drivers/cubrid/cubrid_driver.php') diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index cde719eae..ccabb3b4b 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -31,7 +31,7 @@ * CUBRID Database Adapter Class * * Note: _DB is an extender class that the app controller - * creates dynamically based on whether the active record + * creates dynamically based on whether the query builder * class is being used or not. * * @package CodeIgniter @@ -742,7 +742,7 @@ class CI_DB_cubrid_driver extends CI_DB { if (count($where) > 0 OR count($like) > 0) { $conditions = "\nWHERE "; - $conditions .= implode("\n", $this->ar_where); + $conditions .= implode("\n", $this->qb_where); if (count($where) > 0 && count($like) > 0) { -- cgit v1.2.3-24-g4f1b