mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-27 22:33:10 +09:00
issue 2119. supporting php 5.4. db classes.
git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12686 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
7fe03148f0
commit
41fdaf00c3
29 changed files with 1846 additions and 798 deletions
|
|
@ -1,36 +1,37 @@
|
|||
<?php
|
||||
|
||||
if(!defined('__XE_LOADED_DB_CLASS__'))
|
||||
{
|
||||
define('__XE_LOADED_DB_CLASS__', 1);
|
||||
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/DBParser.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/QueryParser.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/argument/Argument.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/argument/SortArgument.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/argument/ConditionArgument.class.php');
|
||||
require(_XE_PATH_ . 'classes/xml/xmlquery/DBParser.class.php');
|
||||
require(_XE_PATH_ . 'classes/xml/xmlquery/QueryParser.class.php');
|
||||
require(_XE_PATH_ . 'classes/xml/xmlquery/argument/Argument.class.php');
|
||||
require(_XE_PATH_ . 'classes/xml/xmlquery/argument/SortArgument.class.php');
|
||||
require(_XE_PATH_ . 'classes/xml/xmlquery/argument/ConditionArgument.class.php');
|
||||
|
||||
require(_XE_PATH_.'classes/db/queryparts/expression/Expression.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/expression/SelectExpression.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/expression/InsertExpression.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpression.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/expression/ClickCountExpression.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/table/Table.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/table/CubridTableWithHint.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/table/MysqlTableWithHint.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/table/MssqlTableWithHint.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/table/IndexHint.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/condition/ConditionGroup.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/condition/Condition.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/condition/ConditionWithArgument.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/condition/ConditionWithoutArgument.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/condition/ConditionSubquery.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/expression/StarExpression.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/order/OrderByColumn.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/limit/Limit.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/Query.class.php');
|
||||
require(_XE_PATH_.'classes/db/queryparts/Subquery.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/expression/Expression.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/expression/SelectExpression.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/expression/InsertExpression.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/expression/UpdateExpression.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/expression/ClickCountExpression.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/table/Table.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/table/JoinTable.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/table/CubridTableWithHint.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/table/MysqlTableWithHint.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/table/MssqlTableWithHint.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/table/IndexHint.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/condition/ConditionGroup.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/condition/Condition.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/condition/ConditionWithArgument.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/condition/ConditionWithoutArgument.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/condition/ConditionSubquery.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/expression/StarExpression.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/order/OrderByColumn.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/limit/Limit.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/Query.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/queryparts/Subquery.class.php');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -47,17 +48,18 @@ if(!defined('__XE_LOADED_DB_CLASS__'))
|
|||
*/
|
||||
class DB
|
||||
{
|
||||
|
||||
/**
|
||||
* priority of DBMS
|
||||
* @var array
|
||||
*/
|
||||
var $priority_dbms = array(
|
||||
'mysqli' => 5,
|
||||
'mysql' => 4,
|
||||
'mysql_innodb' => 3,
|
||||
'cubrid' => 2,
|
||||
'mssql' => 1
|
||||
);
|
||||
'mysqli' => 5,
|
||||
'mysql' => 4,
|
||||
'mysql_innodb' => 3,
|
||||
'cubrid' => 2,
|
||||
'mssql' => 1
|
||||
);
|
||||
|
||||
/**
|
||||
* count cache path
|
||||
|
|
@ -70,27 +72,27 @@ class DB
|
|||
* @var array
|
||||
*/
|
||||
var $cond_operation = array(
|
||||
'equal' => '=',
|
||||
'more' => '>=',
|
||||
'excess' => '>',
|
||||
'less' => '<=',
|
||||
'below' => '<',
|
||||
'notequal' => '<>',
|
||||
'notnull' => 'is not null',
|
||||
'null' => 'is null',
|
||||
);
|
||||
'equal' => '=',
|
||||
'more' => '>=',
|
||||
'excess' => '>',
|
||||
'less' => '<=',
|
||||
'below' => '<',
|
||||
'notequal' => '<>',
|
||||
'notnull' => 'is not null',
|
||||
'null' => 'is null',
|
||||
);
|
||||
|
||||
/**
|
||||
* master database connection string
|
||||
* @var array
|
||||
*/
|
||||
var $master_db = NULL;
|
||||
|
||||
/**
|
||||
* array of slave databases connection strings
|
||||
* @var array
|
||||
*/
|
||||
var $slave_db = NULL;
|
||||
|
||||
var $result = NULL;
|
||||
|
||||
/**
|
||||
|
|
@ -98,22 +100,26 @@ class DB
|
|||
* @var int
|
||||
*/
|
||||
var $errno = 0;
|
||||
|
||||
/**
|
||||
* error message
|
||||
* @var string
|
||||
*/
|
||||
var $errstr = '';
|
||||
|
||||
/**
|
||||
* query string of latest executed query
|
||||
* @var string
|
||||
*/
|
||||
var $query = '';
|
||||
var $connection = '';
|
||||
|
||||
/**
|
||||
* elapsed time of latest executed query
|
||||
* @var int
|
||||
*/
|
||||
var $elapsed_time = 0;
|
||||
|
||||
/**
|
||||
* elapsed time of latest executed DB class
|
||||
* @var int
|
||||
|
|
@ -125,7 +131,6 @@ class DB
|
|||
* @var boolean
|
||||
*/
|
||||
var $transaction_started = FALSE;
|
||||
|
||||
var $is_connected = FALSE;
|
||||
|
||||
/**
|
||||
|
|
@ -160,15 +165,27 @@ class DB
|
|||
*/
|
||||
function &getInstance($db_type = NULL)
|
||||
{
|
||||
if(!$db_type) $db_type = Context::getDBType();
|
||||
if(!$db_type && Context::isInstalled()) return new Object(-1, 'msg_db_not_setted');
|
||||
if(!$db_type)
|
||||
{
|
||||
$db_type = Context::getDBType();
|
||||
}
|
||||
if(!$db_type && Context::isInstalled())
|
||||
{
|
||||
return new Object(-1, 'msg_db_not_setted');
|
||||
}
|
||||
|
||||
if(!isset($GLOBALS['__DB__'])) $GLOBALS['__DB__'] = array();
|
||||
if(!isset($GLOBALS['__DB__']))
|
||||
{
|
||||
$GLOBALS['__DB__'] = array();
|
||||
}
|
||||
if(!isset($GLOBALS['__DB__'][$db_type]))
|
||||
{
|
||||
$class_name = 'DB'.ucfirst($db_type);
|
||||
$class_file = _XE_PATH_."classes/db/$class_name.class.php";
|
||||
if(!file_exists($class_file)) return new Object(-1, 'msg_db_not_setted');
|
||||
$class_name = 'DB' . ucfirst($db_type);
|
||||
$class_file = _XE_PATH_ . "classes/db/$class_name.class.php";
|
||||
if(!file_exists($class_file))
|
||||
{
|
||||
return new Object(-1, 'msg_db_not_setted');
|
||||
}
|
||||
|
||||
// get a singletone instance of the database driver class
|
||||
require_once($class_file);
|
||||
|
|
@ -194,8 +211,8 @@ class DB
|
|||
*/
|
||||
function DB()
|
||||
{
|
||||
$this->count_cache_path = _XE_PATH_.$this->count_cache_path;
|
||||
$this->cache_file = _XE_PATH_.$this->cache_file;
|
||||
$this->count_cache_path = _XE_PATH_ . $this->count_cache_path;
|
||||
$this->cache_file = _XE_PATH_ . $this->cache_file;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -226,8 +243,13 @@ class DB
|
|||
$enableList = array();
|
||||
if(is_array($this->supported_list))
|
||||
{
|
||||
foreach($this->supported_list AS $key=>$value)
|
||||
if($value->enable) array_push($enableList, $value);
|
||||
foreach($this->supported_list AS $key => $value)
|
||||
{
|
||||
if($value->enable)
|
||||
{
|
||||
array_push($enableList, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $enableList;
|
||||
}
|
||||
|
|
@ -248,8 +270,13 @@ class DB
|
|||
$disableList = array();
|
||||
if(is_array($this->supported_list))
|
||||
{
|
||||
foreach($this->supported_list AS $key=>$value)
|
||||
if(!$value->enable) array_push($disableList, $value);
|
||||
foreach($this->supported_list AS $key => $value)
|
||||
{
|
||||
if(!$value->enable)
|
||||
{
|
||||
array_push($disableList, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $disableList;
|
||||
}
|
||||
|
|
@ -268,7 +295,7 @@ class DB
|
|||
return $this->supported_list;
|
||||
}
|
||||
$get_supported_list = array();
|
||||
$db_classes_path = _XE_PATH_."classes/db/";
|
||||
$db_classes_path = _XE_PATH_ . "classes/db/";
|
||||
$filter = "/^DB([^\.]+)\.class\.php/i";
|
||||
$supported_list = FileHandler::readDir($db_classes_path, $filter, TRUE);
|
||||
|
||||
|
|
@ -277,18 +304,27 @@ class DB
|
|||
{
|
||||
$db_type = $supported_list[$i];
|
||||
|
||||
if(version_compare(phpversion(), '5.0') < 0 && preg_match('/pdo/i',$db_type)) continue;
|
||||
if(version_compare(phpversion(), '5.0') < 0 && preg_match('/pdo/i', $db_type))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$class_name = sprintf("DB%s%s", strtoupper(substr($db_type,0,1)), strtolower(substr($db_type,1)));
|
||||
$class_file = sprintf(_XE_PATH_."classes/db/%s.class.php", $class_name);
|
||||
if(!file_exists($class_file)) continue;
|
||||
$class_name = sprintf("DB%s%s", strtoupper(substr($db_type, 0, 1)), strtolower(substr($db_type, 1)));
|
||||
$class_file = sprintf(_XE_PATH_ . "classes/db/%s.class.php", $class_name);
|
||||
if(!file_exists($class_file))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($oDB);
|
||||
require_once($class_file);
|
||||
$tmp_fn = create_function('', "return new {$class_name}();");
|
||||
$oDB = $tmp_fn();
|
||||
$oDB = $tmp_fn();
|
||||
|
||||
if(!$oDB) continue;
|
||||
if(!$oDB)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$obj = NULL;
|
||||
$obj->db_type = $db_type;
|
||||
|
|
@ -353,8 +389,14 @@ class DB
|
|||
*/
|
||||
function isConnected($type = 'master', $indx = 0)
|
||||
{
|
||||
if($type == 'master') return $this->master_db["is_connected"] ? TRUE : FALSE;
|
||||
else return $this->slave_db[$indx]["is_connected"] ? TRUE : FALSE;
|
||||
if($type == 'master')
|
||||
{
|
||||
return $this->master_db["is_connected"] ? TRUE : FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->slave_db[$indx]["is_connected"] ? TRUE : FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -376,7 +418,10 @@ class DB
|
|||
*/
|
||||
function actFinish()
|
||||
{
|
||||
if(!$this->query) return;
|
||||
if(!$this->query)
|
||||
{
|
||||
return;
|
||||
}
|
||||
$this->act_finish = getMicroTime();
|
||||
$elapsed_time = $this->act_finish - $this->act_start;
|
||||
$this->elapsed_time = $elapsed_time;
|
||||
|
|
@ -400,13 +445,19 @@ class DB
|
|||
|
||||
if(__DEBUG_DB_OUTPUT__ == 1)
|
||||
{
|
||||
$debug_file = _XE_PATH_."files/_debug_db_query.php";
|
||||
$debug_file = _XE_PATH_ . "files/_debug_db_query.php";
|
||||
$buff = array();
|
||||
if(!file_exists($debug_file)) $buff[] = '<?php exit(); ?>';
|
||||
if(!file_exists($debug_file))
|
||||
{
|
||||
$buff[] = '<?php exit(); ?>';
|
||||
}
|
||||
$buff[] = print_r($log, TRUE);
|
||||
|
||||
if(@!$fp = fopen($debug_file, "a")) return;
|
||||
fwrite($fp, implode("\n", $buff)."\n\n");
|
||||
if(@!$fp = fopen($debug_file, "a"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
fwrite($fp, implode("\n", $buff) . "\n\n");
|
||||
fclose($fp);
|
||||
}
|
||||
}
|
||||
|
|
@ -420,10 +471,10 @@ class DB
|
|||
if(__LOG_SLOW_QUERY__ > 0 && $elapsed_time > __LOG_SLOW_QUERY__)
|
||||
{
|
||||
$buff = '';
|
||||
$log_file = _XE_PATH_.'files/_db_slow_query.php';
|
||||
$log_file = _XE_PATH_ . 'files/_db_slow_query.php';
|
||||
if(!file_exists($log_file))
|
||||
{
|
||||
$buff = '<?php exit();?>'."\n";
|
||||
$buff = '<?php exit();?>' . "\n";
|
||||
}
|
||||
|
||||
$buff .= sprintf("%s\t%s\n\t%0.6f sec\tquery_id:%s\n\n", date("Y-m-d H:i"), $this->query, $elapsed_time, $this->query_id);
|
||||
|
|
@ -479,8 +530,14 @@ class DB
|
|||
{
|
||||
static $cache_file = array();
|
||||
|
||||
if(!$query_id) return new Object(-1, 'msg_invalid_queryid');
|
||||
if(!$this->db_type) return;
|
||||
if(!$query_id)
|
||||
{
|
||||
return new Object(-1, 'msg_invalid_queryid');
|
||||
}
|
||||
if(!$this->db_type)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$this->actDBClassStart();
|
||||
|
||||
|
|
@ -498,7 +555,7 @@ class DB
|
|||
elseif(count($id_args) == 3)
|
||||
{
|
||||
$target = $id_args[0];
|
||||
$typeList = array('addons'=>1, 'widgets'=>1);
|
||||
$typeList = array('addons' => 1, 'widgets' => 1);
|
||||
if(!isset($typeList[$target]))
|
||||
{
|
||||
$this->actDBClassFinish();
|
||||
|
|
@ -530,25 +587,30 @@ class DB
|
|||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Look for query cache file
|
||||
* @param string $query_id query id for finding
|
||||
* @param string $xml_file original xml query file
|
||||
* @return string cache file
|
||||
*/
|
||||
function checkQueryCacheFile($query_id,$xml_file)
|
||||
function checkQueryCacheFile($query_id, $xml_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);
|
||||
|
||||
if(file_exists($cache_file)) $cache_time = filemtime($cache_file);
|
||||
else $cache_time = -1;
|
||||
if(file_exists($cache_file))
|
||||
{
|
||||
$cache_time = filemtime($cache_file);
|
||||
}
|
||||
else
|
||||
{
|
||||
$cache_time = -1;
|
||||
}
|
||||
|
||||
// if there is no cache file or is not new, find original xml query file and parse it
|
||||
if($cache_time < filemtime($xml_file) || $cache_time < filemtime(_XE_PATH_.'classes/db/DB.class.php') || $cache_time < filemtime(_XE_PATH_.'classes/xml/XmlQueryParser.150.class.php'))
|
||||
if($cache_time < filemtime($xml_file) || $cache_time < filemtime(_XE_PATH_ . 'classes/db/DB.class.php') || $cache_time < filemtime(_XE_PATH_ . 'classes/xml/XmlQueryParser.150.class.php'))
|
||||
{
|
||||
require_once(_XE_PATH_.'classes/xml/XmlQueryParser.150.class.php');
|
||||
require_once(_XE_PATH_ . 'classes/xml/XmlQueryParser.150.class.php');
|
||||
$oParser = new XmlQueryParser();
|
||||
$oParser->parse($query_id, $xml_file, $cache_file);
|
||||
}
|
||||
|
|
@ -556,7 +618,6 @@ class DB
|
|||
return $cache_file;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute query and return the result
|
||||
* @param string $cache_file cache file of query
|
||||
|
|
@ -569,13 +630,22 @@ class DB
|
|||
{
|
||||
global $lang;
|
||||
|
||||
if(!file_exists($cache_file)) return new Object(-1, 'msg_invalid_queryid');
|
||||
if(!file_exists($cache_file))
|
||||
{
|
||||
return new Object(-1, 'msg_invalid_queryid');
|
||||
}
|
||||
|
||||
if($source_args) $args = @clone($source_args);
|
||||
if($source_args)
|
||||
{
|
||||
$args = clone $source_args;
|
||||
}
|
||||
|
||||
$output = include($cache_file);
|
||||
|
||||
if( (is_a($output, 'Object') || is_subclass_of($output, 'Object')) && !$output->toBool()) return $output;
|
||||
if((is_a($output, 'Object') || is_subclass_of($output, 'Object')) && !$output->toBool())
|
||||
{
|
||||
return $output;
|
||||
}
|
||||
|
||||
// execute appropriate query
|
||||
switch($output->getAction())
|
||||
|
|
@ -594,22 +664,27 @@ class DB
|
|||
$output = $this->_executeDeleteAct($output);
|
||||
break;
|
||||
case 'select' :
|
||||
$arg_columns = is_array($arg_columns)?$arg_columns:array();
|
||||
$arg_columns = is_array($arg_columns) ? $arg_columns : array();
|
||||
$output->setColumnList($arg_columns);
|
||||
$connection = $this->_getConnection('slave');
|
||||
$output = $this->_executeSelectAct($output, $connection);
|
||||
break;
|
||||
}
|
||||
|
||||
if($this->isError()) $output = $this->getError();
|
||||
else if(!is_a($output, 'Object') && !is_subclass_of($output, 'Object')) $output = new Object();
|
||||
if($this->isError())
|
||||
{
|
||||
$output = $this->getError();
|
||||
}
|
||||
else if(!is_a($output, 'Object') && !is_subclass_of($output, 'Object'))
|
||||
{
|
||||
$output = new Object();
|
||||
}
|
||||
$output->add('_query', $this->query);
|
||||
$output->add('_elapsed_time', sprintf("%0.5f", $this->elapsed_time));
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns counter cache data
|
||||
* @param array|string $tables tables to get data
|
||||
|
|
@ -619,30 +694,54 @@ class DB
|
|||
function getCountCache($tables, $condition)
|
||||
{
|
||||
return FALSE;
|
||||
if(!$tables) return FALSE;
|
||||
if(!is_dir($this->count_cache_path)) return FileHandler::makeDir($this->count_cache_path);
|
||||
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);
|
||||
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);
|
||||
if(!is_dir($cache_path)) FileHandler::makeDir($cache_path);
|
||||
if(!is_dir($cache_path))
|
||||
{
|
||||
FileHandler::makeDir($cache_path);
|
||||
}
|
||||
|
||||
$cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
|
||||
if(!file_exists($cache_filename)) return FALSE;
|
||||
if(!file_exists($cache_filename))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$cache_mtime = filemtime($cache_filename);
|
||||
|
||||
if(!is_array($tables)) $tables = array($tables);
|
||||
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;
|
||||
$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);
|
||||
$count = (int) FileHandler::readFile($cache_filename);
|
||||
return $count;
|
||||
}
|
||||
|
||||
|
|
@ -656,16 +755,31 @@ class DB
|
|||
function putCountCache($tables, $condition, $count = 0)
|
||||
{
|
||||
return FALSE;
|
||||
if(!$tables) return FALSE;
|
||||
if(!is_dir($this->count_cache_path)) return FileHandler::makeDir($this->count_cache_path);
|
||||
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);
|
||||
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);
|
||||
if(!is_dir($cache_path)) FileHandler::makeDir($cache_path);
|
||||
if(!is_dir($cache_path))
|
||||
{
|
||||
FileHandler::makeDir($cache_path);
|
||||
}
|
||||
|
||||
$cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
|
||||
|
||||
|
|
@ -680,10 +794,19 @@ class DB
|
|||
function resetCountCache($tables)
|
||||
{
|
||||
return FALSE;
|
||||
if(!$tables) return FALSE;
|
||||
if(!is_dir($this->count_cache_path)) return FileHandler::makeDir($this->count_cache_path);
|
||||
if(!$tables)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if(!is_dir($this->count_cache_path))
|
||||
{
|
||||
return FileHandler::makeDir($this->count_cache_path);
|
||||
}
|
||||
|
||||
if(!is_array($tables)) $tables = array($tables);
|
||||
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);
|
||||
|
|
@ -702,13 +825,34 @@ class DB
|
|||
{
|
||||
$result = array();
|
||||
|
||||
if(function_exists('mysql_connect')) $result[] = 'MySQL';
|
||||
if(function_exists('cubrid_connect')) $result[] = 'Cubrid';
|
||||
if(function_exists('ibase_connect')) $result[] = 'FireBird';
|
||||
if(function_exists('pg_connect')) $result[] = 'Postgre';
|
||||
if(function_exists('sqlite_open')) $result[] = 'sqlite2';
|
||||
if(function_exists('mssql_connect')) $result[] = 'MSSQL';
|
||||
if(function_exists('PDO')) $result[] = 'sqlite3(PDO)';
|
||||
if(function_exists('mysql_connect'))
|
||||
{
|
||||
$result[] = 'MySQL';
|
||||
}
|
||||
if(function_exists('cubrid_connect'))
|
||||
{
|
||||
$result[] = 'Cubrid';
|
||||
}
|
||||
if(function_exists('ibase_connect'))
|
||||
{
|
||||
$result[] = 'FireBird';
|
||||
}
|
||||
if(function_exists('pg_connect'))
|
||||
{
|
||||
$result[] = 'Postgre';
|
||||
}
|
||||
if(function_exists('sqlite_open'))
|
||||
{
|
||||
$result[] = 'sqlite2';
|
||||
}
|
||||
if(function_exists('mssql_connect'))
|
||||
{
|
||||
$result[] = 'MSSQL';
|
||||
}
|
||||
if(function_exists('PDO'))
|
||||
{
|
||||
$result[] = 'sqlite3(PDO)';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
@ -720,7 +864,10 @@ class DB
|
|||
*/
|
||||
function dropTable($table_name)
|
||||
{
|
||||
if(!$table_name) return;
|
||||
if(!$table_name)
|
||||
{
|
||||
return;
|
||||
}
|
||||
$query = sprintf("drop table %s%s", $this->prefix, $table_name);
|
||||
$this->_query($query);
|
||||
}
|
||||
|
|
@ -734,35 +881,57 @@ class DB
|
|||
function getSelectSql($query, $with_values = TRUE)
|
||||
{
|
||||
$select = $query->getSelectString($with_values);
|
||||
if($select == '') return new Object(-1, "Invalid query");
|
||||
$select = 'SELECT ' .$select;
|
||||
if($select == '')
|
||||
{
|
||||
return new Object(-1, "Invalid query");
|
||||
}
|
||||
$select = 'SELECT ' . $select;
|
||||
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$from = ' FROM '.$from;
|
||||
if($from == '')
|
||||
{
|
||||
return new Object(-1, "Invalid query");
|
||||
}
|
||||
$from = ' FROM ' . $from;
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
if($where != '')
|
||||
{
|
||||
$where = ' WHERE ' . $where;
|
||||
}
|
||||
|
||||
$tableObjects = $query->getTables();
|
||||
$index_hint_list = '';
|
||||
foreach($tableObjects as $tableObject)
|
||||
{
|
||||
if(is_a($tableObject, 'CubridTableWithHint'))
|
||||
{
|
||||
$index_hint_list .= $tableObject->getIndexHintString() . ', ';
|
||||
}
|
||||
}
|
||||
$index_hint_list = substr($index_hint_list, 0, -2);
|
||||
if($index_hint_list != '')
|
||||
{
|
||||
$index_hint_list = 'USING INDEX ' . $index_hint_list;
|
||||
}
|
||||
|
||||
$groupBy = $query->getGroupByString();
|
||||
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
|
||||
if($groupBy != '')
|
||||
{
|
||||
$groupBy = ' GROUP BY ' . $groupBy;
|
||||
}
|
||||
|
||||
$orderBy = $query->getOrderByString();
|
||||
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
|
||||
if($orderBy != '')
|
||||
{
|
||||
$orderBy = ' ORDER BY ' . $orderBy;
|
||||
}
|
||||
|
||||
$limit = $query->getLimitString();
|
||||
if($limit != '') $limit = ' LIMIT ' . $limit;
|
||||
if($limit != '')
|
||||
{
|
||||
$limit = ' LIMIT ' . $limit;
|
||||
}
|
||||
|
||||
return $select . ' ' . $from . ' ' . $where . ' ' . $index_hint_list . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
|
||||
}
|
||||
|
|
@ -808,17 +977,23 @@ class DB
|
|||
{
|
||||
$sql = 'DELETE ';
|
||||
|
||||
$sql .= $with_priority?$query->getPriority():'';
|
||||
$sql .= $with_priority ? $query->getPriority() : '';
|
||||
$tables = $query->getTables();
|
||||
|
||||
$sql .= $tables[0]->getAlias();
|
||||
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$sql .= ' FROM '.$from;
|
||||
if($from == '')
|
||||
{
|
||||
return new Object(-1, "Invalid query");
|
||||
}
|
||||
$sql .= ' FROM ' . $from;
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $sql .= ' WHERE ' . $where;
|
||||
if($where != '')
|
||||
{
|
||||
$sql .= ' WHERE ' . $where;
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
|
@ -833,17 +1008,26 @@ class DB
|
|||
function getUpdateSql($query, $with_values = TRUE, $with_priority = FALSE)
|
||||
{
|
||||
$columnsList = $query->getUpdateString($with_values);
|
||||
if($columnsList == '') return new Object(-1, "Invalid query");
|
||||
if($columnsList == '')
|
||||
{
|
||||
return new Object(-1, "Invalid query");
|
||||
}
|
||||
|
||||
$tables = $query->getFromString($with_values);
|
||||
if($tables == '') return new Object(-1, "Invalid query");
|
||||
if($tables == '')
|
||||
{
|
||||
return new Object(-1, "Invalid query");
|
||||
}
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
if($where != '')
|
||||
{
|
||||
$where = ' WHERE ' . $where;
|
||||
}
|
||||
|
||||
$priority = $with_priority?$query->getPriority():'';
|
||||
$priority = $with_priority ? $query->getPriority() : '';
|
||||
|
||||
return "UPDATE $priority $tables SET $columnsList ".$where;
|
||||
return "UPDATE $priority $tables SET $columnsList " . $where;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -857,7 +1041,7 @@ class DB
|
|||
{
|
||||
$tableName = $query->getFirstTableName();
|
||||
$values = $query->getInsertString($with_values);
|
||||
$priority = $with_priority?$query->getPriority():'';
|
||||
$priority = $with_priority ? $query->getPriority() : '';
|
||||
|
||||
return "INSERT $priority INTO $tableName \n $values";
|
||||
}
|
||||
|
|
@ -884,16 +1068,22 @@ class DB
|
|||
if($type == 'master')
|
||||
{
|
||||
if(!$this->master_db['is_connected'])
|
||||
{
|
||||
$this->_connect($type);
|
||||
}
|
||||
$this->connection = 'Master ' . $this->master_db['db_hostname'];
|
||||
return $this->master_db["resource"];
|
||||
}
|
||||
|
||||
if($indx === NULL)
|
||||
{
|
||||
$indx = $this->_getSlaveConnectionStringIndex($type);
|
||||
}
|
||||
|
||||
if(!$this->slave_db[$indx]['is_connected'])
|
||||
{
|
||||
$this->_connect($type, $indx);
|
||||
}
|
||||
|
||||
$this->connection = 'Slave ' . $this->slave_db[$indx]['db_hostname'];
|
||||
return $this->slave_db[$indx]["resource"];
|
||||
|
|
@ -905,10 +1095,14 @@ class DB
|
|||
*/
|
||||
function _dbInfoExists()
|
||||
{
|
||||
if (!$this->master_db)
|
||||
if(!$this->master_db)
|
||||
{
|
||||
return FALSE;
|
||||
if (count($this->slave_db) === 0)
|
||||
}
|
||||
if(count($this->slave_db) === 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
@ -931,13 +1125,19 @@ class DB
|
|||
*/
|
||||
function close($type = 'master', $indx = 0)
|
||||
{
|
||||
if (!$this->isConnected($type, $indx))
|
||||
if(!$this->isConnected($type, $indx))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ($type == 'master')
|
||||
if($type == 'master')
|
||||
{
|
||||
$connection = &$this->master_db;
|
||||
}
|
||||
else
|
||||
{
|
||||
$connection = &$this->slave_db[$indx];
|
||||
}
|
||||
|
||||
$this->_close($connection["resource"]);
|
||||
|
||||
|
|
@ -960,11 +1160,15 @@ class DB
|
|||
*/
|
||||
function begin()
|
||||
{
|
||||
if (!$this->isConnected() || $this->transaction_started)
|
||||
if(!$this->isConnected() || $this->transaction_started)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if($this->_begin())
|
||||
{
|
||||
$this->transaction_started = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -983,10 +1187,14 @@ class DB
|
|||
*/
|
||||
function rollback()
|
||||
{
|
||||
if (!$this->isConnected() || !$this->transaction_started)
|
||||
if(!$this->isConnected() || !$this->transaction_started)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if($this->_rollback())
|
||||
{
|
||||
$this->transaction_started = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1006,10 +1214,14 @@ class DB
|
|||
*/
|
||||
function commit($force = FALSE)
|
||||
{
|
||||
if (!$force && (!$this->isConnected() || !$this->transaction_started))
|
||||
if(!$force && (!$this->isConnected() || !$this->transaction_started))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if($this->_commit())
|
||||
{
|
||||
$this->transaction_started = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1034,7 +1246,9 @@ class DB
|
|||
function _query($query, $connection = NULL)
|
||||
{
|
||||
if($connection == NULL)
|
||||
{
|
||||
$connection = $this->_getConnection('master');
|
||||
}
|
||||
// Notify to start a query execution
|
||||
$this->actStart($query);
|
||||
|
||||
|
|
@ -1061,10 +1275,14 @@ class DB
|
|||
&& $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;
|
||||
}
|
||||
|
|
@ -1100,17 +1318,25 @@ class DB
|
|||
*/
|
||||
function _connect($type = 'master', $indx = 0)
|
||||
{
|
||||
if ($this->isConnected($type, $indx))
|
||||
if($this->isConnected($type, $indx))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore if no DB information exists
|
||||
if (!$this->_dbInfoExists())
|
||||
if(!$this->_dbInfoExists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ($type == 'master')
|
||||
if($type == 'master')
|
||||
{
|
||||
$connection = &$this->master_db;
|
||||
}
|
||||
else
|
||||
{
|
||||
$connection = &$this->slave_db[$indx];
|
||||
}
|
||||
|
||||
$result = $this->__connect($connection);
|
||||
if($result === NULL || $result === FALSE)
|
||||
|
|
@ -1146,7 +1372,10 @@ class DB
|
|||
*/
|
||||
function actDBClassFinish()
|
||||
{
|
||||
if(!$this->query) return;
|
||||
if(!$this->query)
|
||||
{
|
||||
return;
|
||||
}
|
||||
$this->act_dbclass_finish = getMicroTime();
|
||||
$elapsed_dbclass_time = $this->act_dbclass_finish - $this->act_dbclass_start;
|
||||
$this->elapsed_dbclass_time = $elapsed_dbclass_time;
|
||||
|
|
@ -1176,6 +1405,5 @@ class DB
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
/* End of file DB.class.php */
|
||||
/* Location: ./classes/db/DB.class.php */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue