summaryrefslogtreecommitdiffstats
path: root/system/database/drivers/cubrid/cubrid_driver.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/database/drivers/cubrid/cubrid_driver.php')
-rw-r--r--system/database/drivers/cubrid/cubrid_driver.php227
1 files changed, 103 insertions, 124 deletions
diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php
index 7b468bc5c..a54ed3d15 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
*/
@@ -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 = '';
@@ -57,38 +57,35 @@ class CI_DB_cubrid_driver extends CI_DB {
protected $_count_string = 'SELECT COUNT(*) AS ';
protected $_random_keyword = ' RAND()'; // database specific random keyword
- /**
- * Non-persistent database connection
- *
- * @return resource
- */
- public function db_connect()
- {
- // If no port is defined by the user, use the default value
- if ($this->port == '')
- {
- // Default CUBRID Broker port
- $this->port = 33000;
- }
+ // CUBRID-specific properties
+ public $auto_commit = TRUE;
- $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password);
+ public function __construct($params)
+ {
+ parent::__construct($params);
- if ($conn)
+ if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
{
- // 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)
+ if (stripos($matches[2], 'autocommit=off') !== FALSE)
{
- cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE);
- }
- else
- {
- cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE);
- $this->auto_commit = TRUE;
+ $this->auto_commit = FALSE;
}
}
+ else
+ {
+ // If no port is defined by the user, use the default value
+ $this->port == '' OR $this->port = 33000;
+ }
+ }
- return $conn;
+ /**
+ * Non-persistent database connection
+ *
+ * @return resource
+ */
+ public function db_connect()
+ {
+ return $this->_cubrid_connect();
}
// --------------------------------------------------------------------
@@ -100,15 +97,45 @@ class CI_DB_cubrid_driver extends CI_DB {
* 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.
*
* @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))
+ {
+ $_temp = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
+ $conn_id = ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
+ ? $_temp($this->dsn, $this->username, $this->password)
+ : $_temp($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);
+ }
+
+ return $conn_id;
}
// --------------------------------------------------------------------
@@ -169,8 +196,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);
}
// --------------------------------------------------------------------
@@ -185,7 +211,6 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
protected function _prep_query($sql)
{
- // No need to prepare
return $sql;
}
@@ -198,13 +223,8 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
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;
}
@@ -212,7 +232,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))
{
@@ -231,13 +251,8 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
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;
}
@@ -261,13 +276,8 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
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;
}
@@ -303,7 +313,9 @@ 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)
+ 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);
}
@@ -315,7 +327,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;
@@ -369,9 +381,9 @@ class CI_DB_cubrid_driver extends CI_DB {
return 0;
}
- $row = $query->row();
+ $query = $query->row();
$this->_reset_select();
- return (int) $row->numrows;
+ return (int) $query->numrows;
}
// --------------------------------------------------------------------
@@ -386,11 +398,11 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
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;
@@ -423,7 +435,7 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
protected function _field_data($table)
{
- return "SELECT * FROM ".$table." LIMIT 1";
+ return 'SELECT * FROM '.$table.' LIMIT 1';
}
// --------------------------------------------------------------------
@@ -460,24 +472,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);
}
// --------------------------------------------------------------------
@@ -515,7 +523,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).')';
}
// --------------------------------------------------------------------
@@ -533,7 +541,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).')';
}
// --------------------------------------------------------------------
@@ -550,12 +558,11 @@ 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);
}
// --------------------------------------------------------------------
-
/**
* Update statement
*
@@ -568,24 +575,22 @@ 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;
}
- $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;
+ $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
+ if (count($like) > 0)
+ {
+ $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
+ }
- return $sql;
+ return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where
+ .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
+ .( ! $limit ? '' : ' LIMIT '.$limit);
}
// --------------------------------------------------------------------
@@ -604,8 +609,6 @@ class CI_DB_cubrid_driver extends CI_DB {
protected function _update_batch($table, $values, $index, $where = NULL)
{
$ids = array();
- $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
-
foreach ($values as $key => $val)
{
$ids[] = $val[$index];
@@ -619,25 +622,17 @@ class CI_DB_cubrid_driver extends CI_DB {
}
}
- $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 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
+ .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
+ .$index.' IN ('.implode(',', $ids).')';
}
// --------------------------------------------------------------------
@@ -654,7 +649,7 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
protected function _truncate($table)
{
- return "TRUNCATE ".$table;
+ return 'TRUNCATE '.$table;
}
// --------------------------------------------------------------------
@@ -675,19 +670,12 @@ class CI_DB_cubrid_driver extends CI_DB {
if (count($where) > 0 OR count($like) > 0)
{
- $conditions = "\nWHERE ";
- $conditions .= 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);
}
- $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
-
- return "DELETE FROM ".$table.$conditions.$limit;
+ return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
}
// --------------------------------------------------------------------
@@ -704,16 +692,7 @@ class CI_DB_cubrid_driver extends CI_DB {
*/
protected function _limit($sql, $limit, $offset)
{
- if ($offset == 0)
- {
- $offset = '';
- }
- else
- {
- $offset .= ", ";
- }
-
- return $sql."LIMIT ".$offset.$limit;
+ return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
}
// --------------------------------------------------------------------
@@ -732,4 +711,4 @@ class CI_DB_cubrid_driver extends CI_DB {
}
/* End of file cubrid_driver.php */
-/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */
+/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */ \ No newline at end of file