Clean up some methods in DB classes

This commit is contained in:
Kijin Sung 2016-02-11 10:33:15 +09:00
parent 80f2dd7a8a
commit d5a5364814
5 changed files with 84 additions and 247 deletions

View file

@ -22,24 +22,18 @@ class DB
* priority of DBMS * priority of DBMS
* @var array * @var array
*/ */
var $priority_dbms = array( protected static $priority_dbms = array(
'mysqli' => 6, 'mysqli' => 6,
'mysql' => 4, 'mysql' => 4,
'cubrid' => 2, 'cubrid' => 2,
'mssql' => 1 'mssql' => 1
); );
/**
* count cache path
* @var string
*/
var $count_cache_path = 'files/cache/db';
/** /**
* operations for condition * operations for condition
* @var array * @var array
*/ */
var $cond_operation = array( protected static $cond_operation = array(
'equal' => '=', 'equal' => '=',
'more' => '>=', 'more' => '>=',
'excess' => '>', 'excess' => '>',
@ -54,83 +48,83 @@ class DB
* master database connection string * master database connection string
* @var array * @var array
*/ */
var $master_db = NULL; protected $master_db = NULL;
/** /**
* array of slave databases connection strings * array of slave databases connection strings
* @var array * @var array
*/ */
var $slave_db = NULL; protected $slave_db = NULL;
var $result = NULL; protected $result = NULL;
/** /**
* error code (0 means no error) * error code (0 means no error)
* @var int * @var int
*/ */
var $errno = 0; protected $errno = 0;
/** /**
* error message * error message
* @var string * @var string
*/ */
var $errstr = ''; protected $errstr = '';
/** /**
* query string of latest executed query * query string of latest executed query
* @var string * @var string
*/ */
var $query = ''; protected $query = '';
var $connection = ''; protected $connection = '';
/** /**
* elapsed time of latest executed query * elapsed time of latest executed query
* @var int * @var int
*/ */
var $elapsed_time = 0; protected $elapsed_time = 0;
/** /**
* elapsed time of latest executed DB class * elapsed time of latest executed DB class
* @var int * @var int
*/ */
var $elapsed_dbclass_time = 0; protected $elapsed_dbclass_time = 0;
/** /**
* transaction flag * transaction flag
* @var boolean * @var boolean
*/ */
var $transaction_started = FALSE; protected $transaction_started = FALSE;
var $is_connected = FALSE; protected $is_connected = FALSE;
/** /**
* returns enable list in supported dbms list * returns enable list in supported dbms list
* will be written by classes/DB/DB***.class.php * will be written by classes/DB/DB***.class.php
* @var array * @var array
*/ */
var $supported_list = array(); protected static $supported_list = array();
/** /**
* location of query cache * location of query cache
* @var string * @var string
*/ */
var $cache_file = 'files/cache/queries/'; protected $cache_file = 'files/cache/queries/';
/** /**
* stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set * stores database type: 'mysql','cubrid','mssql' etc. or 'db' when database is not yet set
* @var string * @var string
*/ */
var $db_type; public $db_type;
/** /**
* flag to decide if class prepared statements or not (when supported); can be changed from db.config.info * flag to decide if class prepared statements or not (when supported); can be changed from db.config.info
* @var string * @var string
*/ */
var $use_prepared_statements; public $use_prepared_statements;
/** /**
* leve of transaction * leve of transaction
* @var unknown * @var unknown
*/ */
private $transactionNestedLevel = 0; protected $transactionNestedLevel = 0;
/** /**
* returns instance of certain db type * returns instance of certain db type
@ -189,7 +183,6 @@ class DB
*/ */
public function __construct() public function __construct()
{ {
$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;
} }
@ -199,10 +192,9 @@ class DB
* check by instance can creatable * check by instance can creatable
* @return array return supported DBMS list * @return array return supported DBMS list
*/ */
function getSupportedList() public static function getSupportedList()
{ {
$oDB = new DB(); return self::_getSupportedList();
return $oDB->_getSupportedList();
} }
/** /**
@ -210,20 +202,18 @@ class DB
* this list return by child class * this list return by child class
* @return array return enable DBMS list in supported dbms list * @return array return enable DBMS list in supported dbms list
*/ */
function getEnableList() public static function getEnableList()
{ {
is_a($this, 'DB') ? $self = $this : $self = self::getInstance(); if(!self::$supported_list)
if(!$self->supported_list)
{ {
$oDB = new DB(); $oDB = new DB();
$self->supported_list = $oDB->_getSupportedList(); self::$supported_list = self::_getSupportedList();
} }
$enableList = array(); $enableList = array();
if(is_array($self->supported_list)) if(is_array(self::$supported_list))
{ {
foreach($self->supported_list AS $key => $value) foreach(self::$supported_list AS $key => $value)
{ {
if($value->enable) if($value->enable)
{ {
@ -239,20 +229,18 @@ class DB
* this list return by child class * this list return by child class
* @return array return disable DBMS list in supported dbms list * @return array return disable DBMS list in supported dbms list
*/ */
function getDisableList() public static function getDisableList()
{ {
is_a($this, 'DB') ? $self = $this : $self = self::getInstance(); if(!self::$supported_list)
if(!$self->supported_list)
{ {
$oDB = new DB(); $oDB = new DB();
$self->supported_list = $oDB->_getSupportedList(); self::$supported_list = self::_getSupportedList();
} }
$disableList = array(); $disableList = array();
if(is_array($self->supported_list)) if(is_array(self::$supported_list))
{ {
foreach($self->supported_list AS $key => $value) foreach(self::$supported_list AS $key => $value)
{ {
if(!$value->enable) if(!$value->enable)
{ {
@ -265,17 +253,16 @@ class DB
/** /**
* returns list of supported dbms list * returns list of supported dbms list
* this method is private *
* @return array return supported DBMS list * @return array return supported DBMS list
*/ */
function _getSupportedList() protected static function _getSupportedList()
{ {
static $get_supported_list = ''; if(self::$supported_list)
if(is_array($get_supported_list))
{ {
$this->supported_list = $get_supported_list; return self::$supported_list;
return $this->supported_list;
} }
$get_supported_list = array(); $get_supported_list = array();
$db_classes_path = _XE_PATH_ . "classes/db/"; $db_classes_path = _XE_PATH_ . "classes/db/";
$filter = "/^DB([^\.]+)\.class\.php/i"; $filter = "/^DB([^\.]+)\.class\.php/i";
@ -303,41 +290,13 @@ class DB
} }
// sort // sort
@usort($get_supported_list, array($this, '_sortDBMS')); usort($get_supported_list, function($a, $b) {
$priority_a = isset(self::$priority_dbms[$a->db_type]) ? self::$priority_dbms[$a->db_type] : 0;
$priority_b = isset(self::$priority_dbms[$b->db_type]) ? self::$priority_dbms[$b->db_type] : 0;
return $a - $b;
});
$this->supported_list = $get_supported_list; return self::$supported_list = $get_supported_list;
return $this->supported_list;
}
/**
* sort dbms as priority
*/
function _sortDBMS($a, $b)
{
if(!isset($this->priority_dbms[$a->db_type]))
{
$priority_a = 0;
}
else
{
$priority_a = $this->priority_dbms[$a->db_type];
}
if(!isset($this->priority_dbms[$b->db_type]))
{
$priority_b = 0;
}
else
{
$priority_b = $this->priority_dbms[$b->db_type];
}
if($priority_a == $priority_b)
{
return 0;
}
return ($priority_a > $priority_b) ? -1 : 1;
} }
/** /**
@ -345,7 +304,7 @@ class DB
* The value is set in the child class * The value is set in the child class
* @return boolean true: is supported, false: is not supported * @return boolean true: is supported, false: is not supported
*/ */
function isSupported() public function isSupported()
{ {
return self::$isSupported; return self::$isSupported;
} }
@ -356,7 +315,7 @@ class DB
* @param int $indx key of server list * @param int $indx key of server list
* @return boolean true: connected, false: not connected * @return boolean true: connected, false: not connected
*/ */
function isConnected($type = 'master', $indx = 0) public function isConnected($type = 'master', $indx = 0)
{ {
if($type == 'master') if($type == 'master')
{ {
@ -373,7 +332,7 @@ class DB
* @param string $query query string * @param string $query query string
* @return void * @return void
*/ */
function actStart($query) public function actStart($query)
{ {
$this->setError(0, 'success'); $this->setError(0, 'success');
$this->query = $query; $this->query = $query;
@ -385,7 +344,7 @@ class DB
* finish recording log * finish recording log
* @return void * @return void
*/ */
function actFinish() public function actFinish()
{ {
if(!$this->query) if(!$this->query)
{ {
@ -461,7 +420,7 @@ class DB
* @param array $log values set query debug * @param array $log values set query debug
* @return void * @return void
*/ */
function setQueryLog($log) public function setQueryLog($log)
{ {
$GLOBALS['__db_queries__'][] = $log; $GLOBALS['__db_queries__'][] = $log;
} }
@ -472,7 +431,7 @@ class DB
* @param string $errstr error message * @param string $errstr error message
* @return void * @return void
*/ */
function setError($errno = 0, $errstr = 'success') public function setError($errno = 0, $errstr = 'success')
{ {
$this->errno = $errno; $this->errno = $errno;
$this->errstr = $errstr; $this->errstr = $errstr;
@ -482,7 +441,7 @@ class DB
* Return error status * Return error status
* @return boolean true: error, false: no error * @return boolean true: error, false: no error
*/ */
function isError() public function isError()
{ {
return ($this->errno !== 0); return ($this->errno !== 0);
} }
@ -491,7 +450,7 @@ class DB
* Returns object of error info * Returns object of error info
* @return object object of error * @return object object of error
*/ */
function getError() public function getError()
{ {
$this->errstr = Context::convertEncodingStr($this->errstr); $this->errstr = Context::convertEncodingStr($this->errstr);
return new Object($this->errno, $this->errstr); return new Object($this->errno, $this->errstr);
@ -505,7 +464,7 @@ class DB
* @param array $arg_columns column list. if you want get specific colums from executed result, add column list to $arg_columns * @param array $arg_columns column list. if you want get specific colums from executed result, add column list to $arg_columns
* @return object result of query * @return object result of query
*/ */
function executeQuery($query_id, $args = NULL, $arg_columns = NULL, $type = NULL) public function executeQuery($query_id, $args = NULL, $arg_columns = NULL, $type = NULL)
{ {
static $cache_file = array(); static $cache_file = array();
@ -572,7 +531,7 @@ class DB
* @param string $xml_file original xml query file * @param string $xml_file original xml query file
* @return string cache file * @return string cache file
*/ */
function checkQueryCacheFile($query_id, $xml_file) public function checkQueryCacheFile($query_id, $xml_file)
{ {
// first try finding cache file // first try finding cache file
$cache_file = sprintf('%s%s%s.%s.%s.cache.php', _XE_PATH_, $this->cache_file, $query_id, __ZBXE_VERSION__, $this->db_type); $cache_file = sprintf('%s%s%s.%s.%s.cache.php', _XE_PATH_, $this->cache_file, $query_id, __ZBXE_VERSION__, $this->db_type);
@ -601,7 +560,7 @@ class DB
* @param array $arg_columns column list. if you want get specific colums from executed result, add column list to $arg_columns * @param array $arg_columns column list. if you want get specific colums from executed result, add column list to $arg_columns
* @return object result of query * @return object result of query
*/ */
function _executeQuery($cache_file, $source_args, $query_id, $arg_columns, $type) public function _executeQuery($cache_file, $source_args, $query_id, $arg_columns, $type)
{ {
global $lang; global $lang;
@ -668,57 +627,9 @@ class DB
* @param string $condition condition to get data * @param string $condition condition to get data
* @return int count of cache data * @return int count of cache data
*/ */
function getCountCache($tables, $condition) public function getCountCache($tables, $condition)
{ {
return FALSE; return FALSE;
/*
if(!$tables)
{
return FALSE;
}
if(!is_dir($this->count_cache_path))
{
return FileHandler::makeDir($this->count_cache_path);
}
$condition = md5($condition);
if(!is_array($tables))
{
$tables_str = $tables;
}
else
{
$tables_str = implode('.', $tables);
}
$cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str);
FileHandler::makeDir($cache_path);
$cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
if(!file_exists($cache_filename))
{
return FALSE;
}
$cache_mtime = filemtime($cache_filename);
if(!is_array($tables))
{
$tables = array($tables);
}
foreach($tables as $alias => $table)
{
$table_filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table);
if(!file_exists($table_filename) || filemtime($table_filename) > $cache_mtime)
{
return FALSE;
}
}
$count = (int) FileHandler::readFile($cache_filename);
return $count;
*/
} }
/** /**
@ -728,37 +639,9 @@ class DB
* @param int $count count of cache data to save * @param int $count count of cache data to save
* @return void * @return void
*/ */
function putCountCache($tables, $condition, $count = 0) public function putCountCache($tables, $condition, $count = 0)
{ {
return FALSE; return FALSE;
/*
if(!$tables)
{
return FALSE;
}
if(!is_dir($this->count_cache_path))
{
return FileHandler::makeDir($this->count_cache_path);
}
$condition = md5($condition);
if(!is_array($tables))
{
$tables_str = $tables;
}
else
{
$tables_str = implode('.', $tables);
}
$cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str);
FileHandler::makeDir($cache_path);
$cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
FileHandler::writeFile($cache_filename, $count);
*/
} }
/** /**
@ -766,29 +649,9 @@ class DB
* @param array|string $tables tables to reset cache data * @param array|string $tables tables to reset cache data
* @return boolean true: success, false: failed * @return boolean true: success, false: failed
*/ */
function resetCountCache($tables) public function resetCountCache($tables)
{ {
return FALSE; return FALSE;
/*
if(!$tables)
{
return FALSE;
}
return FileHandler::makeDir($this->count_cache_path);
if(!is_array($tables))
{
$tables = array($tables);
}
foreach($tables as $alias => $table)
{
$filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table);
FileHandler::removeFile($filename);
FileHandler::writeFile($filename, '');
}
return TRUE;
*/
} }
/** /**
@ -796,7 +659,7 @@ class DB
* @param string $table_name * @param string $table_name
* @return void * @return void
*/ */
function dropTable($table_name) public function dropTable($table_name)
{ {
if(!$table_name) if(!$table_name)
{ {
@ -812,7 +675,7 @@ class DB
* @param boolean $with_values * @param boolean $with_values
* @return string * @return string
*/ */
function getSelectSql($query, $with_values = TRUE) public function getSelectSql($query, $with_values = TRUE)
{ {
$select = $query->getSelectString($with_values); $select = $query->getSelectString($with_values);
if($select == '') if($select == '')
@ -881,7 +744,7 @@ class DB
* *
* @param $queryObject * @param $queryObject
*/ */
function getClickCountQuery($queryObject) public function getClickCountQuery($queryObject)
{ {
$new_update_columns = array(); $new_update_columns = array();
$click_count_columns = $queryObject->getClickCountColumns(); $click_count_columns = $queryObject->getClickCountColumns();
@ -907,7 +770,7 @@ class DB
* @param boolean $with_priority * @param boolean $with_priority
* @return string * @return string
*/ */
function getDeleteSql($query, $with_values = TRUE, $with_priority = FALSE) public function getDeleteSql($query, $with_values = TRUE, $with_priority = FALSE)
{ {
$sql = 'DELETE '; $sql = 'DELETE ';
@ -939,7 +802,7 @@ class DB
* @param boolean $with_priority * @param boolean $with_priority
* @return string * @return string
*/ */
function getUpdateSql($query, $with_values = TRUE, $with_priority = FALSE) public function getUpdateSql($query, $with_values = TRUE, $with_priority = FALSE)
{ {
$columnsList = $query->getUpdateString($with_values); $columnsList = $query->getUpdateString($with_values);
if($columnsList == '') if($columnsList == '')
@ -971,7 +834,7 @@ class DB
* @param boolean $with_priority * @param boolean $with_priority
* @return string * @return string
*/ */
function getInsertSql($query, $with_values = TRUE, $with_priority = FALSE) public function getInsertSql($query, $with_values = TRUE, $with_priority = FALSE)
{ {
$tableName = $query->getFirstTableName(); $tableName = $query->getFirstTableName();
$values = $query->getInsertString($with_values); $values = $query->getInsertString($with_values);
@ -984,7 +847,7 @@ class DB
* Return index from slave server list * Return index from slave server list
* @return int * @return int
*/ */
function _getSlaveConnectionStringIndex() public function _getSlaveConnectionStringIndex()
{ {
$max = count($this->slave_db); $max = count($this->slave_db);
$indx = rand(0, $max - 1); $indx = rand(0, $max - 1);
@ -997,7 +860,7 @@ class DB
* @param int $indx if indx value is NULL, return rand number in slave server list * @param int $indx if indx value is NULL, return rand number in slave server list
* @return resource * @return resource
*/ */
function _getConnection($type = 'master', $indx = NULL) public function _getConnection($type = 'master', $indx = NULL)
{ {
if($type == 'master' || $this->transactionNestedLevel) if($type == 'master' || $this->transactionNestedLevel)
{ {
@ -1036,26 +899,18 @@ class DB
* check db information exists * check db information exists
* @return boolean * @return boolean
*/ */
function _dbInfoExists() public function _dbInfoExists()
{ {
if(!$this->master_db) return ($this->master_db && count($this->slave_db));
{
return FALSE;
}
if(count($this->slave_db) === 0)
{
return FALSE;
}
return TRUE;
} }
/** /**
* DB disconnection * DB disconnection
* this method is protected *
* @param resource $connection * @param resource $connection
* @return void * @return void
*/ */
function _close($connection) protected function _close($connection)
{ {
} }
@ -1066,7 +921,7 @@ class DB
* @param int $indx number in slave dbms server list * @param int $indx number in slave dbms server list
* @return void * @return void
*/ */
function close($type = 'master', $indx = 0) public function close($type = 'master', $indx = 0)
{ {
if(!$this->isConnected($type, $indx)) if(!$this->isConnected($type, $indx))
{ {
@ -1093,7 +948,7 @@ class DB
* this method is protected * this method is protected
* @return boolean * @return boolean
*/ */
function _begin($transactionLevel = 0) protected function _begin($transactionLevel = 0)
{ {
return TRUE; return TRUE;
} }
@ -1102,7 +957,7 @@ class DB
* DB transaction start * DB transaction start
* @return void * @return void
*/ */
function begin() public function begin()
{ {
if(!$this->isConnected()) if(!$this->isConnected())
{ {
@ -1121,7 +976,7 @@ class DB
* this method is protected * this method is protected
* @return boolean * @return boolean
*/ */
function _rollback($transactionLevel = 0) protected function _rollback($transactionLevel = 0)
{ {
return TRUE; return TRUE;
} }
@ -1130,7 +985,7 @@ class DB
* DB transaction rollback * DB transaction rollback
* @return void * @return void
*/ */
function rollback() public function rollback()
{ {
if(!$this->isConnected() || !$this->transaction_started) if(!$this->isConnected() || !$this->transaction_started)
{ {
@ -1152,7 +1007,7 @@ class DB
* this method is protected * this method is protected
* @return boolean * @return boolean
*/ */
function _commit() protected function _commit()
{ {
return TRUE; return TRUE;
} }
@ -1162,7 +1017,7 @@ class DB
* @param boolean $force regardless transaction start status or connect status, forced to commit * @param boolean $force regardless transaction start status or connect status, forced to commit
* @return void * @return void
*/ */
function commit($force = FALSE) public function commit($force = FALSE)
{ {
if(!$force && (!$this->isConnected() || !$this->transaction_started)) if(!$force && (!$this->isConnected() || !$this->transaction_started))
{ {
@ -1186,19 +1041,19 @@ class DB
* @param resource $connection * @param resource $connection
* @return void * @return void
*/ */
function __query($query, $connection) protected function __query($query, $connection)
{ {
} }
/** /**
* Execute the query * Execute the query
* this method is protected *
* @param string $query * @param string $query
* @param resource $connection * @param resource $connection
* @return resource * @return resource
*/ */
function _query($query, $connection = NULL) public function _query($query, $connection = NULL)
{ {
if($connection == NULL) if($connection == NULL)
{ {
@ -1221,7 +1076,7 @@ class DB
* this method is protected * this method is protected
* @return void * @return void
*/ */
function _setDBInfo() protected function _setDBInfo()
{ {
$db_info = config('db'); $db_info = config('db');
$this->master_db = $db_info['master']; $this->master_db = $db_info['master'];
@ -1236,7 +1091,7 @@ class DB
* @param array $connection * @param array $connection
* @return void * @return void
*/ */
function __connect($connection) protected function __connect($connection)
{ {
} }
@ -1247,7 +1102,7 @@ class DB
* @param resource $connection * @param resource $connection
* @return void * @return void
*/ */
function _afterConnect($connection) protected function _afterConnect($connection)
{ {
} }
@ -1259,7 +1114,7 @@ class DB
* @param int $indx number in slave dbms server list * @param int $indx number in slave dbms server list
* @return void * @return void
*/ */
function _connect($type = 'master', $indx = 0) protected function _connect($type = 'master', $indx = 0)
{ {
if($this->isConnected($type, $indx)) if($this->isConnected($type, $indx))
{ {
@ -1305,7 +1160,7 @@ class DB
* Start recording DBClass log * Start recording DBClass log
* @return void * @return void
*/ */
function actDBClassStart() public function actDBClassStart()
{ {
$this->setError(0, 'success'); $this->setError(0, 'success');
$this->act_dbclass_start = microtime(true); $this->act_dbclass_start = microtime(true);
@ -1316,7 +1171,7 @@ class DB
* Finish recording DBClass log * Finish recording DBClass log
* @return void * @return void
*/ */
function actDBClassFinish() public function actDBClassFinish()
{ {
if(!$this->query) if(!$this->query)
{ {
@ -1338,7 +1193,7 @@ class DB
* @param boolean $force force load DBParser instance * @param boolean $force force load DBParser instance
* @return DBParser * @return DBParser
*/ */
function getParser($force = FALSE) public function getParser($force = FALSE)
{ {
static $dbParser = NULL; static $dbParser = NULL;
if(!$dbParser || $force) if(!$dbParser || $force)

View file

@ -107,12 +107,6 @@ class DBCubrid extends DB
*/ */
function addQuotes($string) function addQuotes($string)
{ {
if(version_compare(PHP_VERSION, "5.4.0", "<") &&
get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
if(!is_numeric($string)) if(!is_numeric($string))
{ {
/* /*

View file

@ -94,10 +94,6 @@ class DBMssql extends DB
*/ */
function addQuotes($string) function addQuotes($string)
{ {
if(version_compare(PHP_VERSION, "5.4.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
//if(!is_numeric($string)) $string = str_replace("'","''",$string); //if(!is_numeric($string)) $string = str_replace("'","''",$string);
return $string; return $string;

View file

@ -116,10 +116,6 @@ class DBMysql extends DB
*/ */
function addQuotes($string) function addQuotes($string)
{ {
if(version_compare(PHP_VERSION, "5.4.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
if(!is_numeric($string)) if(!is_numeric($string))
{ {
$string = @mysql_real_escape_string($string); $string = @mysql_real_escape_string($string);

View file

@ -61,10 +61,6 @@ class DBMysqli extends DBMysql
*/ */
function addQuotes($string) function addQuotes($string)
{ {
if(version_compare(PHP_VERSION, "5.4.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
if(!is_numeric($string)) if(!is_numeric($string))
{ {
$connection = $this->_getConnection('master'); $connection = $this->_getConnection('master');