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.php339
1 files changed, 143 insertions, 196 deletions
diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php
index 3c0850ad3..0a3a8725c 100644
--- a/system/database/drivers/cubrid/cubrid_driver.php
+++ b/system/database/drivers/cubrid/cubrid_driver.php
@@ -1,13 +1,13 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* NOTICE OF LICENSE
- *
+ *
* Licensed under the Open Software License version 3.0
- *
+ *
* This source file is subject to the Open Software License (OSL 3.0) that is
* bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
@@ -21,12 +21,10 @@
* @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
*/
-// ------------------------------------------------------------------------
-
/**
* CUBRID Database Adapter Class
*
@@ -42,79 +40,102 @@
*/
class CI_DB_cubrid_driver extends CI_DB {
- // Default CUBRID Broker port. Will be used unless user
- // explicitly specifies another one.
- const DEFAULT_PORT = 33000;
-
- var $dbdriver = 'cubrid';
+ public $dbdriver = 'cubrid';
// The character used for escaping - no need in CUBRID
- var $_escape_char = '';
+ protected $_escape_char = '`';
// clause and character used for LIKE escape sequences - not used in CUBRID
- var $_like_escape_str = '';
- var $_like_escape_chr = '';
+ protected $_like_escape_str = '';
+ protected $_like_escape_chr = '';
/**
* The syntax to count rows is slightly different across different
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = 'SELECT COUNT(*) AS ';
- var $_random_keyword = ' RAND()'; // database specific random keyword
+ protected $_count_string = 'SELECT COUNT(*) AS ';
+ protected $_random_keyword = ' RAND()'; // database specific random keyword
- /**
- * Non-persistent database connection
- *
- * @access private called by the base class
- * @return resource
- */
- function db_connect()
- {
- // If no port is defined by the user, use the default value
- if ($this->port == '')
- {
- $this->port = self::DEFAULT_PORT;
- }
+ // 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)
- {
- cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE);
- }
- else
+ if (stripos($matches[2], 'autocommit=off') !== FALSE)
{
- 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();
}
// --------------------------------------------------------------------
/**
* 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
*/
- function db_pconnect()
+ 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;
}
// --------------------------------------------------------------------
@@ -125,10 +146,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 +161,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.
@@ -172,14 +191,12 @@ 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);
+ return @cubrid_query($this->_prep_query($sql), $this->conn_id);
}
// --------------------------------------------------------------------
@@ -189,13 +206,11 @@ 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;
}
@@ -204,18 +219,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;
}
@@ -223,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))
{
@@ -238,18 +247,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;
}
@@ -269,18 +272,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;
}
@@ -300,12 +297,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))
{
@@ -317,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);
}
@@ -329,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;
@@ -352,10 +350,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);
}
@@ -368,11 +365,10 @@ 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 == '')
{
@@ -385,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;
}
// --------------------------------------------------------------------
@@ -397,17 +393,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
+ * @param bool
* @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;
@@ -420,11 +415,10 @@ 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);
}
@@ -436,13 +430,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';
}
// --------------------------------------------------------------------
@@ -465,11 +458,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 == '')
{
@@ -480,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);
}
// --------------------------------------------------------------------
@@ -508,11 +496,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))
{
@@ -529,15 +516,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).')';
}
// --------------------------------------------------------------------
@@ -548,15 +534,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).')';
}
// --------------------------------------------------------------------
@@ -566,26 +551,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
@@ -593,24 +575,22 @@ 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, $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);
}
// --------------------------------------------------------------------
@@ -621,17 +601,14 @@ 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 ' : '';
-
foreach ($values as $key => $val)
{
$ids[] = $val[$index];
@@ -645,30 +622,21 @@ 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).')';
}
// --------------------------------------------------------------------
-
/**
* Truncate statement
*
@@ -676,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;
}
// --------------------------------------------------------------------
@@ -692,31 +659,23 @@ 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);
-
- 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);
}
// --------------------------------------------------------------------
@@ -726,24 +685,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 ? '' : $offset.', ').$limit;
}
// --------------------------------------------------------------------
@@ -751,17 +700,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 */