Update DB classes to use new config format

This commit is contained in:
Kijin Sung 2016-02-05 14:46:55 +09:00
parent 6876eac464
commit e28856bfbb
9 changed files with 70 additions and 88 deletions

View file

@ -139,11 +139,15 @@ class DB
* @param string $db_type type of db
* @return DB return DB object instance
*/
function getInstance($db_type = NULL)
public static function getInstance($db_type = NULL)
{
if(!$db_type)
{
$db_type = Context::getDBType();
$db_type = config('db.master.type');
if (config('db.master.engine') === 'innodb')
{
$db_type .= '_innodb';
}
}
if(!$db_type && Context::isInstalled())
{
@ -157,7 +161,7 @@ class DB
if(!isset($GLOBALS['__DB__'][$db_type]))
{
$class_name = 'DB' . ucfirst($db_type);
$class_file = _XE_PATH_ . "classes/db/$class_name.class.php";
$class_file = RX_BASEDIR . "classes/db/$class_name.class.php";
if(!file_exists($class_file))
{
return new Object(-1, 'msg_db_not_setted');
@ -165,7 +169,7 @@ class DB
// get a singletone instance of the database driver class
require_once($class_file);
$GLOBALS['__DB__'][$db_type] = call_user_func(array($class_name, 'create'));
$GLOBALS['__DB__'][$db_type] = new $class_name;
$GLOBALS['__DB__'][$db_type]->db_type = $db_type;
}
@ -176,7 +180,7 @@ class DB
* returns instance of db
* @return DB return DB object instance
*/
function create()
public static function create()
{
return new static();
}
@ -185,7 +189,7 @@ class DB
* constructor
* @return void
*/
function __construct()
public function __construct()
{
$this->count_cache_path = _XE_PATH_ . $this->count_cache_path;
$this->cache_file = _XE_PATH_ . $this->cache_file;
@ -1010,7 +1014,7 @@ class DB
{
$this->_connect($type);
}
$this->connection = 'Master ' . $this->master_db['db_hostname'];
$this->connection = 'Master ' . $this->master_db['host'];
return $this->master_db["resource"];
}
@ -1024,7 +1028,7 @@ class DB
$this->_connect($type, $indx);
}
$this->connection = 'Slave ' . $this->slave_db[$indx]['db_hostname'];
$this->connection = 'Slave ' . $this->slave_db[$indx]['host'];
return $this->slave_db[$indx]["resource"];
}
@ -1219,23 +1223,11 @@ class DB
*/
function _setDBInfo()
{
$db_info = Context::getDBInfo();
$this->master_db = $db_info->master_db;
if($db_info->master_db["db_hostname"] == $db_info->slave_db[0]["db_hostname"]
&& $db_info->master_db["db_port"] == $db_info->slave_db[0]["db_port"]
&& $db_info->master_db["db_userid"] == $db_info->slave_db[0]["db_userid"]
&& $db_info->master_db["db_password"] == $db_info->slave_db[0]["db_password"]
&& $db_info->master_db["db_database"] == $db_info->slave_db[0]["db_database"]
)
{
$this->slave_db[0] = &$this->master_db;
}
else
{
$this->slave_db = $db_info->slave_db;
}
$this->prefix = $db_info->master_db["db_table_prefix"];
$this->use_prepared_statements = $db_info->use_prepared_statements;
$db_info = config('db');
$this->master_db = $db_info['master'];
$this->slave_db = $db_info ? array_values($db_info) : null;
$this->prefix = $this->master_db['prefix'];
$this->use_prepared_statements = config('use_prepared_statements');
}
/**
@ -1301,7 +1293,7 @@ class DB
$connection["is_connected"] = TRUE;
// Save connection info for db logs
$this->connection = ucfirst($type) . ' ' . $connection["db_hostname"];
$this->connection = ucfirst($type) . ' ' . $connection['host'];
// regist $this->close callback
register_shutdown_function(array($this, "close"));

View file

@ -55,15 +55,6 @@ class DBCubrid extends DB
$this->_connect();
}
/**
* Create an instance of this class
* @return DBCubrid return DBCubrid object instance
*/
function create()
{
return new DBCubrid;
}
/**
* DB Connect
* this method is private
@ -73,7 +64,7 @@ class DBCubrid extends DB
function __connect($connection)
{
// attempts to connect
$result = @cubrid_connect($connection["db_hostname"], $connection["db_port"], $connection["db_database"], $connection["db_userid"], $connection["db_password"]);
$result = @cubrid_connect($connection['host'], $connection['port'], $connection['database'], $connection['user'], $connection['pass']);
// check connections
if(!$result)

View file

@ -48,15 +48,6 @@ class DBMssql extends DB
$this->_connect();
}
/**
* Create an instance of this class
* @return DBMssql return DBMssql object instance
*/
function create()
{
return new DBMssql;
}
/**
* DB Connect
* this method is private
@ -68,7 +59,11 @@ class DBMssql extends DB
//sqlsrv_configure( 'WarningsReturnAsErrors', 0 );
//sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
//sqlsrv_configure( 'LogSubsystems', SQLSRV_LOG_SYSTEM_ALL );
$result = @sqlsrv_connect($connection["db_hostname"], array('Database' => $connection["db_database"], 'UID' => $connection["db_userid"], 'PWD' => $connection["db_password"]));
$result = @sqlsrv_connect($connection['host'], array(
'Database' => $connection['database'],
'UID' => $connection['user'],
'PWD' => $connection['pass'],
));
if(!$result)
{

View file

@ -58,13 +58,13 @@ class DBMysql extends DB
function __connect($connection)
{
// Ignore if no DB information exists
if(strpos($connection["db_hostname"], ':') === false && $connection["db_port"])
if(strpos($connection['host'], ':') === false && $connection['port'])
{
$connection["db_hostname"] .= ':' . $connection["db_port"];
$connection['host'] .= ':' . $connection['port'];
}
// Attempt to connect
$result = @mysql_connect($connection["db_hostname"], $connection["db_userid"], $connection["db_password"]);
$result = @mysql_connect($connection['host'], $connection['user'], $connection['pass']);
if(!$result)
{
exit('Unable to connect to DB.');
@ -84,11 +84,11 @@ class DBMysql extends DB
}
// Set charset
$this->charset = isset($connection["db_charset"]) ? $connection["db_charset"] : 'utf8';
$this->charset = isset($connection['charset']) ? $connection['charset'] : 'utf8';
mysql_set_charset($this->charset, $result);
// select db
@mysql_select_db($connection["db_database"], $result);
@mysql_select_db($connection['database'], $result);
if(mysql_error())
{
$this->setError(mysql_errno(), mysql_error());

View file

@ -24,20 +24,13 @@ class DBMysqli extends DBMysql
function __connect($connection)
{
// Attempt to connect
if($connection["db_port"])
if($connection['port'])
{
$result = @mysqli_connect($connection["db_hostname"]
, $connection["db_userid"]
, $connection["db_password"]
, $connection["db_database"]
, $connection["db_port"]);
$result = @mysqli_connect($connection['host'], $connection['user'], $connection['pass'], $connection['database'], $connection['port']);
}
else
{
$result = @mysqli_connect($connection["db_hostname"]
, $connection["db_userid"]
, $connection["db_password"]
, $connection["db_database"]);
$result = @mysqli_connect($connection['host'], $connection['user'], $connection['pass'], $connection['database']);
}
$error = mysqli_connect_errno();
if($error)
@ -45,7 +38,7 @@ class DBMysqli extends DBMysql
$this->setError($error, mysqli_connect_error());
return;
}
$this->charset = isset($connection["db_charset"]) ? $connection["db_charset"] : 'utf8';
$this->charset = isset($connection['charset']) ? $connection['charset'] : 'utf8';
mysqli_set_charset($result, $this->charset);
return $result;
}
@ -89,6 +82,11 @@ class DBMysqli extends DBMysql
*/
function __query($query, $connection)
{
if ($connection === null)
{
debug_print_backtrace();
exit;
}
if($this->use_prepared_statements == 'Y')
{
// 1. Prepare query

View file

@ -49,8 +49,7 @@ class CubridTableWithHint extends Table
$result = '';
// Retrieve table prefix, to add it to index name
$db_info = Context::getDBInfo();
$prefix = $db_info->master_db["db_table_prefix"];
$prefix = config('db.master.prefix');
foreach($this->index_hints_list as $index_hint)
{