402 lines
8.8 KiB
PHP
402 lines
8.8 KiB
PHP
<?php
|
|
/**
|
|
* FuzeWorks.
|
|
*
|
|
* The FuzeWorks MVC PHP FrameWork
|
|
*
|
|
* Copyright (C) 2015 TechFuze
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* @author TechFuze
|
|
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
|
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
|
*
|
|
* @link http://techfuze.net/fuzeworks
|
|
* @since Version 0.0.1
|
|
*
|
|
* @version Version 1.0.0
|
|
*/
|
|
|
|
/**
|
|
* CUBRID Database Adapter Class
|
|
*
|
|
* Note: _DB is an extender class that the app controller
|
|
* creates dynamically based on whether the query builder
|
|
* class is being used or not.
|
|
*
|
|
* Converted from CodeIgniter.
|
|
*
|
|
* @package FuzeWorks
|
|
* @subpackage Drivers
|
|
* @category Database
|
|
* @author Esen Sagynov
|
|
* @link https://codeigniter.com/user_guide/database/
|
|
* @license http://opensource.org/licenses/MIT MIT License
|
|
*/
|
|
class FW_DB_cubrid_driver extends FW_DB {
|
|
|
|
/**
|
|
* Database driver
|
|
*
|
|
* @var string
|
|
*/
|
|
public $dbdriver = 'cubrid';
|
|
|
|
/**
|
|
* Auto-commit flag
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $auto_commit = TRUE;
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Identifier escape character
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $_escape_char = '`';
|
|
|
|
/**
|
|
* ORDER BY random keyword
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Class constructor
|
|
*
|
|
* @param array $params
|
|
* @return void
|
|
*/
|
|
public function __construct($params)
|
|
{
|
|
parent::__construct($params);
|
|
|
|
if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
|
|
{
|
|
if (stripos($matches[2], 'autocommit=off') !== FALSE)
|
|
{
|
|
$this->auto_commit = FALSE;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// If no port is defined by the user, use the default value
|
|
empty($this->port) OR $this->port = 33000;
|
|
}
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Non-persistent database connection
|
|
*
|
|
* @param bool $persistent
|
|
* @return resource
|
|
*/
|
|
public function db_connect($persistent = FALSE)
|
|
{
|
|
if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
|
|
{
|
|
$func = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
|
|
return ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
|
|
? $func($this->dsn, $this->username, $this->password)
|
|
: $func($this->dsn);
|
|
}
|
|
|
|
$func = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
|
|
return ($this->username !== '')
|
|
? $func($this->hostname, $this->port, $this->database, $this->username, $this->password)
|
|
: $func($this->hostname, $this->port, $this->database);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Reconnect
|
|
*
|
|
* Keep / reestablish the db connection if no queries have been
|
|
* sent for a length of time exceeding the server's idle timeout
|
|
*
|
|
* @return void
|
|
*/
|
|
public function reconnect()
|
|
{
|
|
if (cubrid_ping($this->conn_id) === FALSE)
|
|
{
|
|
$this->conn_id = FALSE;
|
|
}
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Database version number
|
|
*
|
|
* @return string
|
|
*/
|
|
public function version()
|
|
{
|
|
if (isset($this->data_cache['version']))
|
|
{
|
|
return $this->data_cache['version'];
|
|
}
|
|
|
|
return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE)
|
|
? FALSE
|
|
: $this->data_cache['version'] = $version;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Execute the query
|
|
*
|
|
* @param string $sql an SQL query
|
|
* @return resource
|
|
*/
|
|
protected function _execute($sql)
|
|
{
|
|
return cubrid_query($sql, $this->conn_id);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Begin Transaction
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function _trans_begin()
|
|
{
|
|
if (($autocommit = cubrid_get_autocommit($this->conn_id)) === NULL)
|
|
{
|
|
return FALSE;
|
|
}
|
|
elseif ($autocommit === TRUE)
|
|
{
|
|
return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Commit Transaction
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function _trans_commit()
|
|
{
|
|
if ( ! cubrid_commit($this->conn_id))
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
|
|
{
|
|
return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Rollback Transaction
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function _trans_rollback()
|
|
{
|
|
if ( ! cubrid_rollback($this->conn_id))
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
|
|
{
|
|
cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Platform-dependant string escape
|
|
*
|
|
* @param string
|
|
* @return string
|
|
*/
|
|
protected function _escape_str($str)
|
|
{
|
|
return cubrid_real_escape_string($str, $this->conn_id);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Affected Rows
|
|
*
|
|
* @return int
|
|
*/
|
|
public function affected_rows()
|
|
{
|
|
return cubrid_affected_rows();
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Insert ID
|
|
*
|
|
* @return int
|
|
*/
|
|
public function insert_id()
|
|
{
|
|
return cubrid_insert_id($this->conn_id);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* List table query
|
|
*
|
|
* Generates a platform-specific query string so that the table names can be fetched
|
|
*
|
|
* @param bool $prefix_limit
|
|
* @return string
|
|
*/
|
|
protected function _list_tables($prefix_limit = FALSE)
|
|
{
|
|
$sql = 'SHOW TABLES';
|
|
|
|
if ($prefix_limit !== FALSE && $this->dbprefix !== '')
|
|
{
|
|
return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
|
|
}
|
|
|
|
return $sql;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Show column query
|
|
*
|
|
* Generates a platform-specific query string so that the column names can be fetched
|
|
*
|
|
* @param string $table
|
|
* @return string
|
|
*/
|
|
protected function _list_columns($table = '')
|
|
{
|
|
return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Returns an object with field data
|
|
*
|
|
* @param string $table
|
|
* @return array
|
|
*/
|
|
public function field_data($table)
|
|
{
|
|
if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
|
|
{
|
|
return FALSE;
|
|
}
|
|
$query = $query->result_object();
|
|
|
|
$retval = array();
|
|
for ($i = 0, $c = count($query); $i < $c; $i++)
|
|
{
|
|
$retval[$i] = new stdClass();
|
|
$retval[$i]->name = $query[$i]->Field;
|
|
|
|
sscanf($query[$i]->Type, '%[a-z](%d)',
|
|
$retval[$i]->type,
|
|
$retval[$i]->max_length
|
|
);
|
|
|
|
$retval[$i]->default = $query[$i]->Default;
|
|
$retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
|
|
}
|
|
|
|
return $retval;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Error
|
|
*
|
|
* Returns an array containing code and message of the last
|
|
* database error that has occured.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function error()
|
|
{
|
|
return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* FROM tables
|
|
*
|
|
* Groups tables in FROM clauses if needed, so there is no confusion
|
|
* about operator precedence.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function _from_tables()
|
|
{
|
|
if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
|
|
{
|
|
return '('.implode(', ', $this->qb_from).')';
|
|
}
|
|
|
|
return implode(', ', $this->qb_from);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/**
|
|
* Close DB Connection
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function _close()
|
|
{
|
|
cubrid_close($this->conn_id);
|
|
}
|
|
|
|
}
|