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

View file

@ -55,15 +55,6 @@ class DBCubrid extends DB
$this->_connect(); $this->_connect();
} }
/**
* Create an instance of this class
* @return DBCubrid return DBCubrid object instance
*/
function create()
{
return new DBCubrid;
}
/** /**
* DB Connect * DB Connect
* this method is private * this method is private
@ -73,7 +64,7 @@ class DBCubrid extends DB
function __connect($connection) function __connect($connection)
{ {
// attempts to connect // 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 // check connections
if(!$result) if(!$result)

View file

@ -48,15 +48,6 @@ class DBMssql extends DB
$this->_connect(); $this->_connect();
} }
/**
* Create an instance of this class
* @return DBMssql return DBMssql object instance
*/
function create()
{
return new DBMssql;
}
/** /**
* DB Connect * DB Connect
* this method is private * this method is private
@ -68,7 +59,11 @@ class DBMssql extends DB
//sqlsrv_configure( 'WarningsReturnAsErrors', 0 ); //sqlsrv_configure( 'WarningsReturnAsErrors', 0 );
//sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL ); //sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
//sqlsrv_configure( 'LogSubsystems', SQLSRV_LOG_SYSTEM_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) if(!$result)
{ {

View file

@ -58,13 +58,13 @@ class DBMysql extends DB
function __connect($connection) function __connect($connection)
{ {
// Ignore if no DB information exists // 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 // 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) if(!$result)
{ {
exit('Unable to connect to DB.'); exit('Unable to connect to DB.');
@ -84,11 +84,11 @@ class DBMysql extends DB
} }
// Set charset // 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); mysql_set_charset($this->charset, $result);
// select db // select db
@mysql_select_db($connection["db_database"], $result); @mysql_select_db($connection['database'], $result);
if(mysql_error()) if(mysql_error())
{ {
$this->setError(mysql_errno(), mysql_error()); $this->setError(mysql_errno(), mysql_error());

View file

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

View file

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

View file

@ -100,6 +100,17 @@ class Config
$data = $value; $data = $value;
} }
/**
* Set all system configuration.
*
* @param array $config
* @return void
*/
public static function setAll($config)
{
self::$_config = $config;
}
/** /**
* Convert previous configuration files to the current format and return it. * Convert previous configuration files to the current format and return it.
* *
@ -345,8 +356,11 @@ class Config
*/ */
public static function save($config = null) public static function save($config = null)
{ {
$config = ($config === null) ? self::$_config : $config; if ($config)
$buff = '<?php' . "\n" . '// Rhymix System Configuration' . "\n" . 'return ' . self::serialize($config) . ';' . "\n"; {
self::setAll($config);
}
$buff = '<?php' . "\n" . '// Rhymix System Configuration' . "\n" . 'return ' . self::serialize(self::$_config) . ';' . "\n";
return \FileHandler::writeFile(RX_BASEDIR . 'files/config/config.php', $buff) ? true : false; return \FileHandler::writeFile(RX_BASEDIR . 'files/config/config.php', $buff) ? true : false;
} }

View file

@ -33,20 +33,15 @@ class installController extends install
$config = Context::gets('db_type', 'db_host', 'db_port', 'db_user', 'db_pass', 'db_database', 'db_prefix'); $config = Context::gets('db_type', 'db_host', 'db_port', 'db_user', 'db_pass', 'db_database', 'db_prefix');
// Create a temporary setting object. // Create a temporary setting object.
$db_info = new stdClass(); Rhymix\Framework\Config::set('db.master', array(
$db_info->master_db = array(); 'type' => $config->db_type,
$db_info->master_db['db_type'] = $config->db_type; 'host' => $config->db_host,
$db_info->master_db['db_hostname'] = $config->db_host; 'port' => $config->db_port,
$db_info->master_db['db_port'] = $config->db_port; 'user' => $config->db_user,
$db_info->master_db['db_userid'] = $config->db_user; 'pass' => $config->db_pass,
$db_info->master_db['db_password'] = $config->db_pass; 'database' => $config->db_database,
$db_info->master_db['db_database'] = $config->db_database; 'prefix' => $config->db_prefix,
$db_info->master_db['db_table_prefix'] = $config->db_prefix; ));
$db_info->slave_db = array($db_info->master_db);
$db_info->default_url = Context::getRequestUri();
$db_info->lang_type = Context::getLangType();
$db_info->use_mobile_view = 'Y';
Context::setDBInfo($db_info);
// Check connection to the DB. // Check connection to the DB.
$oDB = DB::getInstance(); $oDB = DB::getInstance();
@ -180,6 +175,7 @@ class installController extends install
$config['url']['default'] = Context::getRequestUri(); $config['url']['default'] = Context::getRequestUri();
// Load the new configuration. // Load the new configuration.
Rhymix\Framework\Config::setAll($config);
Context::loadDBInfo($config); Context::loadDBInfo($config);
// Check DB. // Check DB.
@ -223,7 +219,7 @@ class installController extends install
} }
// Save the new configuration. // Save the new configuration.
Rhymix\Framework\Config::save($config); Rhymix\Framework\Config::save();
// Unset temporary session variables. // Unset temporary session variables.
unset($_SESSION['use_rewrite']); unset($_SESSION['use_rewrite']);
@ -231,12 +227,9 @@ class installController extends install
// Redirect to the home page. // Redirect to the home page.
$this->setMessage('msg_install_completed'); $this->setMessage('msg_install_completed');
if(!in_array(Context::getRequestMethod(),array('XMLRPC','JSON')))
{
$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('');
$this->setRedirectUrl($returnUrl);
}
$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : RX_BASEURL;
$this->setRedirectUrl($returnUrl);
return new Object(); return new Object();
} }

View file

@ -57,8 +57,8 @@ class installView extends install
} }
else else
{ {
header("location: ./"); $this->setRedirectUrl(RX_BASEURL);
exit; return new Object();
} }
} }
} }