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:
flyskyko 2013-02-04 09:36:46 +00:00
parent 7fe03148f0
commit 41fdaf00c3
29 changed files with 1846 additions and 798 deletions

View file

@ -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 */

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,5 @@
<?php
/**
* - DBMSSQL
* - Modified to use MSSQL driver by sol (sol@ngleader.com)
@ -9,12 +10,13 @@
*/
class DBMssql extends DB
{
/**
* prefix of XE tables(One more XE can be installed on a single DB)
* @var string
*/
var $prefix = 'xe';
var $param = array();
var $prefix = 'xe';
var $param = array();
var $comment_syntax = '/* %s */';
/**
@ -25,15 +27,15 @@ class DBMssql extends DB
* @var array
*/
var $column_type = array(
'bignumber' => 'bigint',
'number' => 'int',
'varchar' => 'varchar',
'char' => 'char',
'text' => 'text',
'bigtext' => 'text',
'date' => 'varchar(14)',
'float' => 'float',
);
'bignumber' => 'bigint',
'number' => 'int',
'varchar' => 'varchar',
'char' => 'char',
'text' => 'text',
'bigtext' => 'text',
'date' => 'varchar(14)',
'float' => 'float',
);
/**
* Constructor
@ -61,7 +63,10 @@ class DBMssql extends DB
*/
function isSupported()
{
if (!extension_loaded("sqlsrv")) return false;
if(!extension_loaded("sqlsrv"))
{
return false;
}
return true;
}
@ -81,7 +86,7 @@ class DBMssql extends DB
if(!$result)
{
$errors = print_r(sqlsrv_errors(), true);
$this->setError (-1, 'database connect fail' . PHP_EOL . $errors);
$this->setError(-1, 'database connect fail' . PHP_EOL . $errors);
return;
}
return $result;
@ -107,7 +112,10 @@ class DBMssql extends DB
*/
function addQuotes($string)
{
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
//if(!is_numeric($string)) $string = str_replace("'","''",$string);
return $string;
@ -121,7 +129,10 @@ class DBMssql extends DB
function _begin()
{
$connection = $this->_getConnection('master');
if(sqlsrv_begin_transaction($connection) === false) return;
if(sqlsrv_begin_transaction($connection) === false)
{
return;
}
return true;
}
@ -164,12 +175,21 @@ class DBMssql extends DB
{
foreach($this->param as $k => $o)
{
if($o->isColumnName()) continue;
if($o->isColumnName())
{
continue;
}
if($o->getType() == 'number')
{
$value = $o->getUnescapedValue();
if(is_array($value)) $_param = array_merge($_param, $value);
else $_param[] = $o->getUnescapedValue();
if(is_array($value))
{
$_param = array_merge($_param, $value);
}
else
{
$_param[] = $o->getUnescapedValue();
}
}
else
{
@ -177,9 +197,14 @@ class DBMssql extends DB
if(is_array($value))
{
foreach($value as $v)
{
$_param[] = array($v, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'));
}
}
else
{
$_param[] = array($value, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'));
}
else $_param[] = array($value, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'));
}
}
}
@ -196,18 +221,20 @@ class DBMssql extends DB
$stmt = sqlsrv_prepare($connection, $query);
}
if(!$stmt)
if(!$stmt)
{
$result = false;
}
else
else
{
$result = sqlsrv_execute($stmt);
}
// Error Check
if(!$result)
$this->setError(print_r(sqlsrv_errors(),true));
if(!$result)
{
$this->setError(print_r(sqlsrv_errors(), true));
}
$this->param = array();
@ -217,14 +244,16 @@ class DBMssql extends DB
/**
* Parameters to sqlsrv_prepare need to be references, and not literals
* Parameters are sent as an array, where each parameter can be:
* - a PHP variable (by reference)
* - a PHP array (containng param value, type and direction) -> also needs to be sent by reference
* - a PHP variable (by reference)
* - a PHP array (containng param value, type and direction) -> also needs to be sent by reference
* @param array $_param
* @return array
*/
function _getParametersByReference($_param)
function _getParametersByReference($_param)
{
$copy = array(); $args = array(); $i = 0;
$copy = array();
$args = array();
$i = 0;
foreach($_param as $key => $value)
{
if(is_array($value))
@ -235,7 +264,7 @@ class DBMssql extends DB
$value_arg[] = $value_copy[1];
$value_arg[] = $value_copy[2];
}
else
else
{
$value_arg = $value;
}
@ -254,30 +283,47 @@ class DBMssql extends DB
function _fetch($result, $arrayIndexEndValue = NULL)
{
$output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output;
if(!$this->isConnected() || $this->isError() || !$result)
{
return $output;
}
$c = sqlsrv_num_fields($result);
$m = null;
while(sqlsrv_fetch($result))
{
if(!$m) $m = sqlsrv_field_metadata($result);
unset($row);
for($i=0;$i<$c;$i++)
if(!$m)
{
$row->{$m[$i]['Name']} = sqlsrv_get_field( $result, $i, SQLSRV_PHPTYPE_STRING( 'utf-8' ));
$m = sqlsrv_field_metadata($result);
}
unset($row);
for($i = 0; $i < $c; $i++)
{
$row->{$m[$i]['Name']} = sqlsrv_get_field($result, $i, SQLSRV_PHPTYPE_STRING('utf-8'));
}
if($arrayIndexEndValue)
{
$output[$arrayIndexEndValue--] = $row;
}
else
{
$output[] = $row;
}
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $row;
else $output[] = $row;
}
if(count($output)==1)
if(count($output) == 1)
{
if(isset($arrayIndexEndValue)) return $output;
else return $output[0];
if(isset($arrayIndexEndValue))
{
return $output;
}
else
{
return $output[0];
}
}
return $output;
}
/**
@ -309,7 +355,10 @@ class DBMssql extends DB
$result = $this->_query($query);
$tmp = $this->_fetch($result);
if(!$tmp) return false;
if(!$tmp)
{
return false;
}
return true;
}
@ -323,17 +372,36 @@ class DBMssql extends DB
* @param boolean $notnull not null status, default value is false
* @return void
*/
function addColumn($table_name, $column_name, $type='number', $size='', $default = '', $notnull=false)
function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
{
if($this->isColumnExists($table_name, $column_name)) return;
if($this->isColumnExists($table_name, $column_name))
{
return;
}
$type = $this->column_type[$type];
if(strtoupper($type)=='INTEGER') $size = '';
if(strtoupper($type) == 'INTEGER')
{
$size = '';
}
$query = sprintf("alter table %s%s add %s ", $this->prefix, $table_name, $column_name);
if($size) $query .= sprintf(" %s(%s) ", $type, $size);
else $query .= sprintf(" %s ", $type);
if($default) $query .= sprintf(" default '%s' ", $default);
if($notnull) $query .= " not null ";
if($size)
{
$query .= sprintf(" %s(%s) ", $type, $size);
}
else
{
$query .= sprintf(" %s ", $type);
}
if($default)
{
$query .= sprintf(" default '%s' ", $default);
}
if($notnull)
{
$query .= " not null ";
}
return $this->_query($query);
}
@ -346,7 +414,10 @@ class DBMssql extends DB
*/
function dropColumn($table_name, $column_name)
{
if(!$this->isColumnExists($table_name, $column_name)) return;
if(!$this->isColumnExists($table_name, $column_name))
{
return;
}
$query = sprintf("alter table %s%s drop %s ", $this->prefix, $table_name, $column_name);
$this->_query($query);
}
@ -361,9 +432,15 @@ class DBMssql extends DB
{
$query = sprintf("select syscolumns.name as name from syscolumns, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = syscolumns.id and syscolumns.name = '%s'", $this->prefix, $table_name, $column_name);
$result = $this->_query($query);
if($this->isError()) return;
if($this->isError())
{
return;
}
$tmp = $this->_fetch($result);
if(!$tmp->name) return false;
if(!$tmp->name)
{
return false;
}
return true;
}
@ -379,10 +456,16 @@ class DBMssql extends DB
*/
function addIndex($table_name, $index_name, $target_columns, $is_unique = false)
{
if($this->isIndexExists($table_name, $index_name)) return;
if(!is_array($target_columns)) $target_columns = array($target_columns);
if($this->isIndexExists($table_name, $index_name))
{
return;
}
if(!is_array($target_columns))
{
$target_columns = array($target_columns);
}
$query = sprintf("create %s index %s on %s%s (%s)", $is_unique?'unique':'', $index_name, $this->prefix, $table_name, implode(',',$target_columns));
$query = sprintf("create %s index %s on %s%s (%s)", $is_unique ? 'unique' : '', $index_name, $this->prefix, $table_name, implode(',', $target_columns));
$this->_query($query);
}
@ -395,7 +478,10 @@ class DBMssql extends DB
*/
function dropIndex($table_name, $index_name, $is_unique = false)
{
if(!$this->isIndexExists($table_name, $index_name)) return;
if(!$this->isIndexExists($table_name, $index_name))
{
return;
}
$query = sprintf("drop index %s%s.%s", $this->prefix, $table_name, $index_name);
$this->_query($query);
}
@ -411,10 +497,16 @@ class DBMssql extends DB
$query = sprintf("select sysindexes.name as name from sysindexes, sysobjects where sysobjects.name = '%s%s' and sysobjects.id = sysindexes.id and sysindexes.name = '%s'", $this->prefix, $table_name, $index_name);
$result = $this->_query($query);
if($this->isError()) return;
if($this->isError())
{
return;
}
$tmp = $this->_fetch($result);
if(!$tmp->name) return false;
if(!$tmp->name)
{
return false;
}
return true;
}
@ -435,7 +527,10 @@ class DBMssql extends DB
*/
function createTableByXmlFile($file_name)
{
if(!file_exists($file_name)) return;
if(!file_exists($file_name))
{
return;
}
// read xml file
$buff = FileHandler::readFile($file_name);
return $this->_createTable($buff);
@ -457,26 +552,35 @@ class DBMssql extends DB
$xml_obj = $oXml->parse($xml_doc);
// Create a table schema
$table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name)) return;
if($this->isTableExists($table_name))
{
return;
}
if($table_name == 'sequence')
{
$table_name = $this->prefix.$table_name;
$table_name = $this->prefix . $table_name;
$query = sprintf('create table %s ( sequence int identity(1,1), seq int )', $table_name);
return $this->_query($query);
}
else
{
$table_name = $this->prefix.$table_name;
$table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column;
else $columns = $xml_obj->table->column;
if(!is_array($xml_obj->table->column))
{
$columns[] = $xml_obj->table->column;
}
else
{
$columns = $xml_obj->table->column;
}
$primary_list = array();
$unique_list = array();
$index_list = array();
$typeList = array('number'=>1, 'text'=>1);
$typeList = array('number' => 1, 'text' => 1);
foreach($columns as $column)
{
$name = $column->attrs->name;
@ -489,34 +593,39 @@ class DBMssql extends DB
$default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment;
$column_schema[] = sprintf('[%s] %s%s %s %s %s',
$name,
$this->column_type[$type],
!isset($typeList[$type])&&$size?'('.$size.')':'',
isset($default)?"default '".$default."'":'',
$notnull?'not null':'null',
$auto_increment?'identity(1,1)':''
);
$column_schema[] = sprintf('[%s] %s%s %s %s %s', $name, $this->column_type[$type], !isset($typeList[$type]) && $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : 'null', $auto_increment ? 'identity(1,1)' : '');
if($primary_key) $primary_list[] = $name;
else if($unique) $unique_list[$unique][] = $name;
else if($index) $index_list[$index][] = $name;
if($primary_key)
{
$primary_list[] = $name;
}
else if($unique)
{
$unique_list[$unique][] = $name;
}
else if($index)
{
$index_list[$index][] = $name;
}
}
if(count($primary_list))
if(count($primary_list))
{
$column_schema[] = sprintf("primary key (%s)", '"'.implode($primary_list,'","').'"');
}
$column_schema[] = sprintf("primary key (%s)", '"' . implode($primary_list, '","') . '"');
}
$schema = sprintf('create table [%s] (%s%s)', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"));
$schema = sprintf('create table [%s] (%s%s)', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"));
$output = $this->_query($schema);
if(!$output) return false;
if(!$output)
{
return false;
}
if(count($unique_list))
{
foreach($unique_list as $key => $val)
{
$query = sprintf("create unique index %s on %s (%s);", $key, $table_name, '['.implode('],[',$val).']');
$query = sprintf("create unique index %s on %s (%s);", $key, $table_name, '[' . implode('],[', $val) . ']');
$this->_query($query);
}
}
@ -525,7 +634,7 @@ class DBMssql extends DB
{
foreach($index_list as $key => $val)
{
$query = sprintf("create index %s on %s (%s);", $key, $table_name, '['.implode('],[',$val).']');
$query = sprintf("create index %s on %s (%s);", $key, $table_name, '[' . implode('],[', $val) . ']');
$this->_query($query);
}
}
@ -533,8 +642,6 @@ class DBMssql extends DB
}
}
/**
* Handles insertAct
* @todo Lookup _filterNumber against sql injection - see if it is still needed and how to integrate
@ -570,24 +677,35 @@ class DBMssql extends 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");
}
$from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query");
if($from == '')
{
return new Object(-1, "Invalid query");
}
$tables = $query->getTables();
$alias_list = '';
foreach($tables as $table)
{
$alias_list .= $table->getAlias();
}
implode(',', explode(' ', $alias_list));
$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 $alias_list SET $columnsList FROM ".$from.$where;
}
return "UPDATE $priority $alias_list SET $columnsList FROM " . $from . $where;
}
/**
* Handles deleteAct
@ -614,31 +732,56 @@ class DBMssql extends DB
//$limitOffset = $query->getLimit()->getOffset();
//if($limitOffset)
// TODO Implement Limit with offset with subquery
$limit = '';$limitCount = '';
$limit = '';
$limitCount = '';
$limitQueryPart = $query->getLimit();
if($limitQueryPart)
{
$limitCount = $limitQueryPart->getLimit();
if($limitCount != '') $limit = 'SELECT TOP ' . $limitCount;
}
if($limitCount != '')
{
$limit = 'SELECT TOP ' . $limitCount;
}
$select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query");
if($select == '')
{
return new Object(-1, "Invalid query");
}
if($limit != '')
$select = $limit.' '.$select;
{
$select = $limit . ' ' . $select;
}
else
$select = 'SELECT ' .$select;
{
$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;
}
$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;
}
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;
}
@ -655,16 +798,25 @@ class DBMssql extends DB
{
$query = $this->getSelectSql($queryObject);
if(strpos($query, "substr")) $query = str_replace ("substr", "substring", $query);
if(strpos($query, "substr"))
{
$query = str_replace("substr", "substring", $query);
}
// TODO Decide if we continue to pass parameters like this
$this->param = $queryObject->getArguments();
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
$query .= (__DEBUG_QUERY__ & 1 && $output->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query, $connection);
if ($this->isError ()) return $this->queryError($queryObject);
else return $this->queryPageLimit($queryObject, $result, $connection);
if($this->isError())
{
return $this->queryError($queryObject);
}
else
{
return $this->queryPageLimit($queryObject, $result, $connection);
}
}
/**
@ -685,18 +837,20 @@ class DBMssql extends DB
function queryError($queryObject)
{
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler())
if($limit && $limit->isPageHandler())
{
$buff = new Object ();
$buff->total_count = 0;
$buff->total_page = 0;
$buff->page = 1;
$buff->data = array ();
$buff->page_navigation = new PageHandler (/*$total_count*/0, /*$total_page*/1, /*$page*/1, /*$page_count*/10);//default page handler values
$buff->data = array();
$buff->page_navigation = new PageHandler(/* $total_count */0, /* $total_page */1, /* $page */1, /* $page_count */10); //default page handler values
return $buff;
}
else
{
return;
}
}
/**
@ -709,7 +863,7 @@ class DBMssql extends DB
function queryPageLimit($queryObject, $result, $connection)
{
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler())
if($limit && $limit->isPageHandler())
{
// Total count
$temp_where = $queryObject->getWhereString(true, false);
@ -724,9 +878,9 @@ class DBMssql extends DB
$count_query = sprintf('select %s %s %s %s'
, $temp_select
, 'FROM ' . $queryObject->getFromString(true)
, ($temp_where === '' ? '' : ' WHERE '. $temp_where)
, ($temp_where === '' ? '' : ' WHERE ' . $temp_where)
, ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '')
);
);
// If query uses grouping or distinct, count from original select
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
@ -739,24 +893,32 @@ class DBMssql extends DB
$total_count = (int) $count_output->count;
$list_count = $limit->list_count->getValue();
if (!$list_count)
if(!$list_count)
{
$list_count = 20;
}
$page_count = $limit->page_count->getValue();
if (!$page_count)
if(!$page_count)
{
$page_count = 10;
}
$page = $limit->page->getValue();
if (!$page)
if(!$page)
{
$page = 1;
}
// Total pages
if ($total_count)
if($total_count)
{
$total_page = (int) (($total_count - 1) / $list_count) + 1;
}
else
{
$total_page = 1;
}
// check the page variables
if ($page > $total_page)
if($page > $total_page)
{
// If requested page is bigger than total number of pages, return empty list

View file

@ -1,4 +1,5 @@
<?php
/**
* Class to use MySQL DBMS
* mysql handling class
@ -11,11 +12,12 @@
*/
class DBMysql extends DB
{
/**
* prefix of a tablename (One or more XEs can be installed in a single DB)
* @var string
*/
var $prefix = 'xe_'; // / <
var $prefix = 'xe_'; // / <
var $comment_syntax = '/* %s */';
/**
@ -26,15 +28,15 @@ class DBMysql extends DB
* @var array
*/
var $column_type = array(
'bignumber' => 'bigint',
'number' => 'bigint',
'varchar' => 'varchar',
'char' => 'char',
'text' => 'text',
'bigtext' => 'longtext',
'date' => 'varchar(14)',
'float' => 'float',
);
'bignumber' => 'bigint',
'number' => 'bigint',
'varchar' => 'varchar',
'char' => 'char',
'text' => 'text',
'bigtext' => 'longtext',
'date' => 'varchar(14)',
'float' => 'float',
);
/**
* Constructor
@ -62,7 +64,10 @@ class DBMysql extends DB
*/
function isSupported()
{
if(!function_exists('mysql_connect')) return false;
if(!function_exists('mysql_connect'))
{
return false;
}
return true;
}
@ -75,8 +80,10 @@ 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["db_hostname"], ':') === false && $connection["db_port"])
{
$connection["db_hostname"] .= ':' . $connection["db_port"];
}
// Attempt to connect
$result = @mysql_connect($connection["db_hostname"], $connection["db_userid"], $connection["db_password"]);
@ -87,9 +94,9 @@ class DBMysql extends DB
return;
}
// Error appears if the version is lower than 4.1
if(mysql_get_server_info($result)<"4.1")
if(mysql_get_server_info($result) < "4.1")
{
$this->setError(-1, "XE cannot be installed under the version of mysql 4.1. Current mysql version is ".mysql_get_server_info());
$this->setError(-1, "XE cannot be installed under the version of mysql 4.1. Current mysql version is " . mysql_get_server_info());
return;
}
// select db
@ -133,8 +140,14 @@ class DBMysql extends DB
*/
function addQuotes($string)
{
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(!is_numeric($string)) $string = @mysql_real_escape_string($string);
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
if(!is_numeric($string))
{
$string = @mysql_real_escape_string($string);
}
return $string;
}
@ -180,7 +193,10 @@ class DBMysql extends DB
// Run the query statement
$result = mysql_query($query, $connection);
// Error Check
if(mysql_error($connection)) $this->setError(mysql_errno($connection), mysql_error($connection));
if(mysql_error($connection))
{
$this->setError(mysql_errno($connection), mysql_error($connection));
}
// Return result
return $result;
}
@ -194,16 +210,31 @@ class DBMysql extends DB
function _fetch($result, $arrayIndexEndValue = NULL)
{
$output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output;
if(!$this->isConnected() || $this->isError() || !$result)
{
return $output;
}
while($tmp = $this->db_fetch_object($result))
{
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp;
else $output[] = $tmp;
if($arrayIndexEndValue)
{
$output[$arrayIndexEndValue--] = $tmp;
}
else
{
$output[] = $tmp;
}
}
if(count($output)==1)
if(count($output) == 1)
{
if(isset($arrayIndexEndValue)) return $output;
else return $output[0];
if(isset($arrayIndexEndValue))
{
return $output;
}
else
{
return $output[0];
}
}
$this->db_free_result($result);
return $output;
@ -239,7 +270,10 @@ class DBMysql extends DB
$query = sprintf("select password('%s') as password, old_password('%s') as old_password", $this->addQuotes($password), $this->addQuotes($password));
$result = $this->_query($query);
$tmp = $this->_fetch($result);
if($tmp->password == $saved_password || $tmp->old_password == $saved_password) return true;
if($tmp->password == $saved_password || $tmp->old_password == $saved_password)
{
return true;
}
return false;
}
@ -253,7 +287,10 @@ class DBMysql extends DB
$query = sprintf("show tables like '%s%s'", $this->prefix, $this->addQuotes($target_name));
$result = $this->_query($query);
$tmp = $this->_fetch($result);
if(!$tmp) return false;
if(!$tmp)
{
return false;
}
return true;
}
@ -267,16 +304,31 @@ class DBMysql extends DB
* @param boolean $notnull not null status, default value is false
* @return void
*/
function addColumn($table_name, $column_name, $type='number', $size='', $default = '', $notnull=false)
function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
{
$type = $this->column_type[$type];
if(strtoupper($type)=='INTEGER') $size = '';
if(strtoupper($type) == 'INTEGER')
{
$size = '';
}
$query = sprintf("alter table `%s%s` add `%s` ", $this->prefix, $table_name, $column_name);
if($size) $query .= sprintf(" %s(%s) ", $type, $size);
else $query .= sprintf(" %s ", $type);
if($default) $query .= sprintf(" default '%s' ", $default);
if($notnull) $query .= " not null ";
if($size)
{
$query .= sprintf(" %s(%s) ", $type, $size);
}
else
{
$query .= sprintf(" %s ", $type);
}
if($default)
{
$query .= sprintf(" default '%s' ", $default);
}
if($notnull)
{
$query .= " not null ";
}
return $this->_query($query);
}
@ -303,7 +355,10 @@ class DBMysql extends DB
{
$query = sprintf("show fields from `%s%s`", $this->prefix, $table_name);
$result = $this->_query($query);
if($this->isError()) return;
if($this->isError())
{
return;
}
$output = $this->_fetch($result);
if($output)
{
@ -311,7 +366,10 @@ class DBMysql extends DB
foreach($output as $key => $val)
{
$name = strtolower($val->Field);
if($column_name == $name) return true;
if($column_name == $name)
{
return true;
}
}
}
return false;
@ -329,9 +387,12 @@ class DBMysql extends DB
*/
function addIndex($table_name, $index_name, $target_columns, $is_unique = false)
{
if(!is_array($target_columns)) $target_columns = array($target_columns);
if(!is_array($target_columns))
{
$target_columns = array($target_columns);
}
$query = sprintf("alter table `%s%s` add %s index `%s` (%s);", $this->prefix, $table_name, $is_unique?'unique':'', $index_name, implode(',',$target_columns));
$query = sprintf("alter table `%s%s` add %s index `%s` (%s);", $this->prefix, $table_name, $is_unique ? 'unique' : '', $index_name, implode(',', $target_columns));
$this->_query($query);
}
@ -348,7 +409,6 @@ class DBMysql extends DB
$this->_query($query);
}
/**
* Check index status of the table
* @param string $table_name table name
@ -360,14 +420,26 @@ class DBMysql extends DB
//$query = sprintf("show indexes from %s%s where key_name = '%s' ", $this->prefix, $table_name, $index_name);
$query = sprintf("show indexes from `%s%s`", $this->prefix, $table_name);
$result = $this->_query($query);
if($this->isError()) return;
$output = $this->_fetch($result);
if(!$output) return;
if(!is_array($output)) $output = array($output);
for($i=0;$i<count($output);$i++)
if($this->isError())
{
if($output[$i]->Key_name == $index_name) return true;
return;
}
$output = $this->_fetch($result);
if(!$output)
{
return;
}
if(!is_array($output))
{
$output = array($output);
}
for($i = 0; $i < count($output); $i++)
{
if($output[$i]->Key_name == $index_name)
{
return true;
}
}
return false;
}
@ -389,7 +461,10 @@ class DBMysql extends DB
*/
function createTableByXmlFile($file_name)
{
if(!file_exists($file_name)) return;
if(!file_exists($file_name))
{
return;
}
// read xml file
$buff = FileHandler::readFile($file_name);
return $this->_createTable($buff);
@ -411,11 +486,20 @@ class DBMysql extends DB
$xml_obj = $oXml->parse($xml_doc);
// Create a table schema
$table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name)) return;
$table_name = $this->prefix.$table_name;
if($this->isTableExists($table_name))
{
return;
}
$table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column;
else $columns = $xml_obj->table->column;
if(!is_array($xml_obj->table->column))
{
$columns[] = $xml_obj->table->column;
}
else
{
$columns = $xml_obj->table->column;
}
$primary_list = array();
$unique_list = array();
@ -433,30 +517,32 @@ class DBMysql extends DB
$default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment;
$column_schema[] = sprintf('`%s` %s%s %s %s %s',
$name,
$this->column_type[$type],
$size?'('.$size.')':'',
isset($default)?"default '".$default."'":'',
$notnull?'not null':'',
$auto_increment?'auto_increment':''
);
$column_schema[] = sprintf('`%s` %s%s %s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : '', $auto_increment ? 'auto_increment' : '');
if($primary_key) $primary_list[] = $name;
else if($unique) $unique_list[$unique][] = $name;
else if($index) $index_list[$index][] = $name;
if($primary_key)
{
$primary_list[] = $name;
}
else if($unique)
{
$unique_list[$unique][] = $name;
}
else if($index)
{
$index_list[$index][] = $name;
}
}
if(count($primary_list))
{
$column_schema[] = sprintf("primary key (%s)", '`'.implode($primary_list,'`,`').'`');
$column_schema[] = sprintf("primary key (%s)", '`' . implode($primary_list, '`,`') . '`');
}
if(count($unique_list))
{
foreach($unique_list as $key => $val)
{
$column_schema[] = sprintf("unique %s (%s)", $key, '`'.implode($val,'`,`').'`');
$column_schema[] = sprintf("unique %s (%s)", $key, '`' . implode($val, '`,`') . '`');
}
}
@ -464,14 +550,15 @@ class DBMysql extends DB
{
foreach($index_list as $key => $val)
{
$column_schema[] = sprintf("index %s (%s)", $key, '`'.implode($val,'`,`').'`');
$column_schema[] = sprintf("index %s (%s)", $key, '`' . implode($val, '`,`') . '`');
}
}
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"), "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci");
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci");
$output = $this->_query($schema);
if(!$output) return false;
if(!$output)
return false;
}
/**
@ -484,7 +571,10 @@ class DBMysql extends DB
{
$query = $this->getInsertSql($queryObject, $with_values, true);
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
if(is_a($query, 'Object')) return;
if(is_a($query, 'Object'))
{
return;
}
return $this->_query($query);
}
@ -498,7 +588,10 @@ class DBMysql extends DB
{
$query = $this->getUpdateSql($queryObject, $with_values, true);
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
if(is_a($query, 'Object')) return;
if(is_a($query, 'Object'))
{
return;
}
return $this->_query($query);
}
@ -512,7 +605,10 @@ class DBMysql extends DB
{
$query = $this->getDeleteSql($queryObject, $with_values, true);
$query .= (__DEBUG_QUERY__ & 1 && $this->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
if(is_a($query, 'Object')) return;
if(is_a($query, 'Object'))
{
return;
}
return $this->_query($query);
}
@ -529,18 +625,24 @@ class DBMysql extends DB
{
$limit = $queryObject->getLimit();
$result = NULL;
if ($limit && $limit->isPageHandler())
if($limit && $limit->isPageHandler())
{
return $this->queryPageLimit($queryObject, $result, $connection, $with_values);
}
else
{
$query = $this->getSelectSql($queryObject, $with_values);
if (is_a($query, 'Object'))
if(is_a($query, 'Object'))
{
return;
}
$query .= (__DEBUG_QUERY__ & 1 && $queryObject->queryID) ? sprintf(' ' . $this->comment_syntax, $queryObject->queryID) : '';
$result = $this->_query($query, $connection);
if ($this->isError())
if($this->isError())
{
return $this->queryError($queryObject);
}
$data = $this->_fetch($result);
$buff = new Object ();
@ -607,18 +709,20 @@ class DBMysql extends DB
function queryError($queryObject)
{
$limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler())
if($limit && $limit->isPageHandler())
{
$buff = new Object ();
$buff->total_count = 0;
$buff->total_page = 0;
$buff->page = 1;
$buff->data = array ();
$buff->page_navigation = new PageHandler (/*$total_count*/0, /*$total_page*/1, /*$page*/1, /*$page_count*/10);//default page handler values
$buff->data = array();
$buff->page_navigation = new PageHandler(/* $total_count */0, /* $total_page */1, /* $page */1, /* $page_count */10); //default page handler values
return $buff;
}
else
{
return;
}
}
/**
@ -634,7 +738,7 @@ class DBMysql extends DB
$limit = $queryObject->getLimit();
// Total count
$temp_where = $queryObject->getWhereString($with_values, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString($with_values), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString($with_values), ($temp_where === '' ? '' : ' WHERE ' . $temp_where));
// Check for distinct query and if found update count query structure
$temp_select = $queryObject->getSelectString($with_values);
@ -645,34 +749,47 @@ class DBMysql extends DB
$count_query = sprintf('select %s %s %s %s'
, $temp_select == '*' ? '1' : $temp_select
, 'FROM ' . $queryObject->getFromString($with_values)
, ($temp_where === '' ? '' : ' WHERE '. $temp_where)
, ($temp_where === '' ? '' : ' WHERE ' . $temp_where)
, ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '')
);
);
// If query uses grouping or distinct, count from original select
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
}
$count_query .= (__DEBUG_QUERY__&1 && $queryObject->queryID)?sprintf (' '.$this->comment_syntax, $queryObject->queryID):'';
$count_query .= (__DEBUG_QUERY__ & 1 && $queryObject->queryID) ? sprintf(' ' . $this->comment_syntax, $queryObject->queryID) : '';
$result_count = $this->_query($count_query, $connection);
$count_output = $this->_fetch($result_count);
$total_count = (int)(isset($count_output->count) ? $count_output->count : NULL);
$total_count = (int) (isset($count_output->count) ? $count_output->count : NULL);
$list_count = $limit->list_count->getValue();
if (!$list_count) $list_count = 20;
if(!$list_count)
{
$list_count = 20;
}
$page_count = $limit->page_count->getValue();
if (!$page_count) $page_count = 10;
if(!$page_count)
{
$page_count = 10;
}
$page = $limit->page->getValue();
if (!$page) $page = 1;
if(!$page)
{
$page = 1;
}
// total pages
if ($total_count)
if($total_count)
{
$total_page = (int) (($total_count - 1) / $list_count) + 1;
}
else
{
$total_page = 1;
}
// check the page variables
if ($page > $total_page)
if($page > $total_page)
{
// If requested page is bigger than total number of pages, return empty list
$buff = new Object ();
@ -687,10 +804,12 @@ class DBMysql extends DB
$query = $this->getSelectPageSql($queryObject, $with_values, $start_count, $list_count);
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
$result = $this->_query ($query, $connection);
if ($this->isError ())
$query .= (__DEBUG_QUERY__ & 1 && $queryObject->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$result = $this->_query($query, $connection);
if($this->isError())
{
return $this->queryError($queryObject);
}
$virtual_no = $total_count - ($page - 1) * $list_count;
$data = $this->_fetch($result, $virtual_no);
@ -715,27 +834,46 @@ class DBMysql extends DB
function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0)
{
$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;
}
$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 = sprintf (' LIMIT %d, %d', $start_count, $list_count);
if($limit != '')
{
$limit = sprintf(' LIMIT %d, %d', $start_count, $list_count);
}
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
}
}
/* End of file DBMysql.class.php */
/* Location: ./classes/db/DBMysql.class.php */

View file

@ -1,5 +1,7 @@
<?php
require_once('DBMysql.class.php');
/**
* Class to use MySQL innoDB DBMS
* mysql innodb handling class
@ -92,7 +94,10 @@ class DBMysql_innodb extends DBMysql
// Run the query statement
$result = @mysql_query($query, $connection);
// Error Check
if(mysql_error($connection)) $this->setError(mysql_errno($connection), mysql_error($connection));
if(mysql_error($connection))
{
$this->setError(mysql_errno($connection), mysql_error($connection));
}
// Return result
return $result;
}
@ -113,11 +118,20 @@ class DBMysql_innodb extends DBMysql
$xml_obj = $oXml->parse($xml_doc);
// Create a table schema
$table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name)) return;
$table_name = $this->prefix.$table_name;
if($this->isTableExists($table_name))
{
return;
}
$table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column)) $columns[] = $xml_obj->table->column;
else $columns = $xml_obj->table->column;
if(!is_array($xml_obj->table->column))
{
$columns[] = $xml_obj->table->column;
}
else
{
$columns = $xml_obj->table->column;
}
foreach($columns as $column)
{
@ -131,30 +145,32 @@ class DBMysql_innodb extends DBMysql
$default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment;
$column_schema[] = sprintf('`%s` %s%s %s %s %s',
$name,
$this->column_type[$type],
$size?'('.$size.')':'',
isset($default)?"default '".$default."'":'',
$notnull?'not null':'',
$auto_increment?'auto_increment':''
);
$column_schema[] = sprintf('`%s` %s%s %s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : '', $auto_increment ? 'auto_increment' : '');
if($primary_key) $primary_list[] = $name;
else if($unique) $unique_list[$unique][] = $name;
else if($index) $index_list[$index][] = $name;
if($primary_key)
{
$primary_list[] = $name;
}
else if($unique)
{
$unique_list[$unique][] = $name;
}
else if($index)
{
$index_list[$index][] = $name;
}
}
if(count($primary_list))
{
$column_schema[] = sprintf("primary key (%s)", '`'.implode($primary_list,'`,`').'`');
$column_schema[] = sprintf("primary key (%s)", '`' . implode($primary_list, '`,`') . '`');
}
if(count($unique_list))
{
foreach($unique_list as $key => $val)
{
$column_schema[] = sprintf("unique %s (%s)", $key, '`'.implode($val,'`,`').'`');
$column_schema[] = sprintf("unique %s (%s)", $key, '`' . implode($val, '`,`') . '`');
}
}
@ -162,15 +178,19 @@ class DBMysql_innodb extends DBMysql
{
foreach($index_list as $key => $val)
{
$column_schema[] = sprintf("index %s (%s)", $key, '`'.implode($val,'`,`').'`');
$column_schema[] = sprintf("index %s (%s)", $key, '`' . implode($val, '`,`') . '`');
}
}
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"), "ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci");
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci");
$output = $this->_query($schema);
if(!$output) return false;
if(!$output)
{
return false;
}
}
}
/* End of file DBMysql_innodb.class.php */
/* Location: ./classes/db/DBMysql_innodb.class.php */

View file

@ -1,4 +1,5 @@
<?php
require_once('DBMysql.class.php');
/**
@ -11,7 +12,8 @@ require_once('DBMysql.class.php');
* @package /classes/db
* @version 0.1
*/
class DBMysqli extends DBMysql {
class DBMysqli extends DBMysql
{
/**
* Constructor
@ -30,7 +32,10 @@ class DBMysqli extends DBMysql {
*/
function isSupported()
{
if(!function_exists('mysqli_connect')) return false;
if(!function_exists('mysqli_connect'))
{
return false;
}
return true;
}
@ -52,28 +57,28 @@ class DBMysqli extends DBMysql {
function __connect($connection)
{
// Attempt to connect
if ($connection["db_port"])
if($connection["db_port"])
{
$result = @mysqli_connect($connection["db_hostname"]
, $connection["db_userid"]
, $connection["db_password"]
, $connection["db_database"]
, $connection["db_port"]);
, $connection["db_userid"]
, $connection["db_password"]
, $connection["db_database"]
, $connection["db_port"]);
}
else
{
$result = @mysqli_connect($connection["db_hostname"]
, $connection["db_userid"]
, $connection["db_password"]
, $connection["db_database"]);
, $connection["db_userid"]
, $connection["db_password"]
, $connection["db_database"]);
}
$error = mysqli_connect_errno();
if($error)
{
$this->setError($error,mysqli_connect_error());
$this->setError($error, mysqli_connect_error());
return;
}
mysqli_set_charset($result,'utf8');
mysqli_set_charset($result, 'utf8');
return $result;
}
@ -95,11 +100,14 @@ class DBMysqli extends DBMysql {
*/
function addQuotes($string)
{
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc())
{
$string = stripslashes(str_replace("\\", "\\\\", $string));
}
if(!is_numeric($string))
{
$connection = $this->_getConnection('master');
$string = mysqli_escape_string($connection,$string);
$string = mysqli_escape_string($connection, $string);
}
return $string;
}
@ -114,7 +122,7 @@ class DBMysqli extends DBMysql {
function __query($query, $connection)
{
if($this->use_prepared_statements == 'Y')
{
{
// 1. Prepare query
$stmt = mysqli_prepare($connection, $query);
if($stmt)
@ -126,7 +134,7 @@ class DBMysqli extends DBMysql {
if(!empty($params))
{
$args[0] = $stmt;
$args[1] = $types;
$args[1] = $types;
$i = 2;
foreach($params as $key => $param)
@ -136,25 +144,28 @@ class DBMysqli extends DBMysql {
}
// 2. Bind parameters
$status = call_user_func_array('mysqli_stmt_bind_param',$args);
$status = call_user_func_array('mysqli_stmt_bind_param', $args);
if(!$status)
$this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
{
$this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
}
}
// 3. Execute query
$status = mysqli_stmt_execute($stmt);
if(!$status)
{
$this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true));
}
// Return stmt for other processing - like retrieving resultset (_fetch)
return $stmt;
// mysqli_stmt_close($stmt);
}
}
// Run the query statement
$result = mysqli_query($connection,$query);
$result = mysqli_query($connection, $query);
// Error Check
$error = mysqli_error($connection);
if($error)
@ -176,7 +187,10 @@ class DBMysqli extends DBMysql {
{
$types = '';
$params = array();
if(!$this->param) return;
if(!$this->param)
{
return;
}
foreach($this->param as $k => $o)
{
@ -184,21 +198,24 @@ class DBMysqli extends DBMysql {
$type = $o->getType();
// Skip column names -> this should be concatenated to query string
if($o->isColumnName()) continue;
if($o->isColumnName())
{
continue;
}
switch($type)
{
case 'number' :
$type = 'i';
case 'number' :
$type = 'i';
break;
case 'varchar' :
$type = 's';
break;
default:
case 'varchar' :
$type = 's';
}
break;
default:
$type = 's';
}
if(is_array($value))
if(is_array($value))
{
foreach($value as $v)
{
@ -211,10 +228,7 @@ class DBMysqli extends DBMysql {
$params[] = $value;
$types .= $type;
}
}
}
}
/**
@ -230,7 +244,10 @@ class DBMysqli extends DBMysql {
return parent::_fetch($result, $arrayIndexEndValue);
}
$output = array();
if(!$this->isConnected() || $this->isError() || !$result) return $output;
if(!$this->isConnected() || $this->isError() || !$result)
{
return $output;
}
// Prepared stements: bind result variable and fetch data
$stmt = $result;
@ -246,13 +263,18 @@ class DBMysqli extends DBMysql {
foreach($fields as $field)
{
if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails
$field->name = 'repeat_' . $field->name;
{
$field->name = 'repeat_' . $field->name;
}
// Array passed needs to contain references, not values
$row[$field->name] = "";
$resultArray[$field->name] = &$row[$field->name];
if($field->type == 252) $longtext_exists = true;
if($field->type == 252)
{
$longtext_exists = true;
}
}
$resultArray = array_merge(array($stmt), $resultArray);
@ -270,15 +292,21 @@ class DBMysqli extends DBMysql {
foreach($resultArray as $key => $value)
{
if($key === 0) continue; // Skip stmt object
if(strpos($key, 'repeat_')) $key = substr($key, 6);
if($key === 0)
{
continue; // Skip stmt object
}
if(strpos($key, 'repeat_'))
{
$key = substr($key, 6);
}
$resultObject->$key = $value;
}
$rows[] = $resultObject;
}
mysqli_stmt_close($stmt);
mysqli_stmt_close($stmt);
if($arrayIndexEndValue)
{
@ -287,19 +315,25 @@ class DBMysqli extends DBMysql {
$output[$arrayIndexEndValue--] = $row;
}
}
else
else
{
$output = $rows;
}
if(count($output)==1)
if(count($output) == 1)
{
if(isset($arrayIndexEndValue)) return $output;
else return $output[0];
if(isset($arrayIndexEndValue))
{
return $output;
}
else
{
return $output[0];
}
}
return $output;
}
}
/**
* Handles insertAct
@ -307,7 +341,8 @@ class DBMysqli extends DBMysql {
* @param boolean $with_values
* @return resource
*/
function _executeInsertAct($queryObject, $with_values = false){
function _executeInsertAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_executeInsertAct($queryObject);
@ -324,11 +359,12 @@ class DBMysqli extends DBMysql {
* @param boolean $with_values
* @return resource
*/
function _executeUpdateAct($queryObject, $with_values = false) {
function _executeUpdateAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_executeUpdateAct($queryObject);
}
}
$this->param = $queryObject->getArguments();
$result = parent::_executeUpdateAct($queryObject, $with_values);
unset($this->param);
@ -341,16 +377,17 @@ class DBMysqli extends DBMysql {
* @param boolean $with_values
* @return resource
*/
function _executeDeleteAct($queryObject, $with_values = false) {
function _executeDeleteAct($queryObject, $with_values = false)
{
if($this->use_prepared_statements != 'Y')
{
return parent::_executeDeleteAct($queryObject);
}
}
$this->param = $queryObject->getArguments();
$result = parent::_executeDeleteAct($queryObject, $with_values);
unset($this->param);
return $result;
}
}
/**
* Handle selectAct
@ -366,9 +403,9 @@ class DBMysqli extends DBMysql {
if($this->use_prepared_statements != 'Y')
{
return parent::_executeSelectAct($queryObject, $connection);
}
}
$this->param = $queryObject->getArguments();
$result = parent::_executeSelectAct($queryObject, $connection, $with_values);
$result = parent::_executeSelectAct($queryObject, $connection, $with_values);
unset($this->param);
return $result;
}
@ -382,7 +419,7 @@ class DBMysqli extends DBMysql {
function db_insert_id()
{
$connection = $this->_getConnection('master');
return mysqli_insert_id($connection);
return mysqli_insert_id($connection);
}
/**
@ -402,8 +439,9 @@ class DBMysqli extends DBMysql {
*/
function db_free_result(&$result)
{
return mysqli_free_result($result);
}
return mysqli_free_result($result);
}
}
/* End of file DBMysqli.class.php */
/* Location: ./classes/db/DBMysqli.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
@ -6,16 +7,19 @@
*/
class Query extends Object
{
/**
* Query id, defined in query xml file
* @var string
*/
var $queryID;
/**
* DML type, ex) INSERT, DELETE, UPDATE, SELECT
* @var string
*/
var $action;
/**
* priority level ex)LOW_PRIORITY, HIGHT_PRIORITY
* @var string
@ -27,26 +31,31 @@ class Query extends Object
* @var string|array
*/
var $columns;
/**
* table list
* @var string|array
*/
var $tables;
/**
* condition list
* @var string|array
*/
var $conditions;
/**
* group list
* @var string|array
*/
var $groups;
/**
* order list
* @var array
*/
var $orderby;
/**
* limit count
* @var int
@ -85,20 +94,24 @@ class Query extends Object
* @return void
*/
function Query($queryID = NULL
, $action = NULL
, $columns = NULL
, $tables = NULL
, $conditions = NULL
, $groups = NULL
, $orderby = NULL
, $limit = NULL
, $priority = NULL)
, $action = NULL
, $columns = NULL
, $tables = NULL
, $conditions = NULL
, $groups = NULL
, $orderby = NULL
, $limit = NULL
, $priority = NULL)
{
$this->queryID = $queryID;
$this->action = $action;
$this->priority = $priority;
if(!isset($tables)) return;
if(!isset($tables))
{
return;
}
$this->columns = $this->setColumns($columns);
$this->tables = $this->setTables($tables);
$this->conditions = $this->setConditions($conditions);
@ -153,7 +166,10 @@ class Query extends Object
return;
}
if(!is_array($columns)) $columns = array($columns);
if(!is_array($columns))
{
$columns = array($columns);
}
$this->columns = $columns;
}
@ -167,7 +183,10 @@ class Query extends Object
return;
}
if(!is_array($tables)) $tables = array($tables);
if(!is_array($tables))
{
$tables = array($tables);
}
$this->tables = $tables;
}
@ -180,34 +199,58 @@ class Query extends Object
function setConditions($conditions)
{
$this->conditions = array();
if(!isset($conditions) || count($conditions) === 0) return;
if(!is_array($conditions)) $conditions = array($conditions);
if(!isset($conditions) || count($conditions) === 0)
{
return;
}
if(!is_array($conditions))
{
$conditions = array($conditions);
}
foreach($conditions as $conditionGroup)
{
if($conditionGroup->show()) $this->conditions[] = $conditionGroup;
if($conditionGroup->show())
{
$this->conditions[] = $conditionGroup;
}
}
}
function setGroups($groups)
{
if(!isset($groups) || count($groups) === 0) return;
if(!is_array($groups)) $groups = array($groups);
if(!isset($groups) || count($groups) === 0)
{
return;
}
if(!is_array($groups))
{
$groups = array($groups);
}
$this->groups = $groups;
}
function setOrder($order)
{
if(!isset($order) || count($order) === 0) return;
if(!is_array($order)) $order = array($order);
if(!isset($order) || count($order) === 0)
{
return;
}
if(!is_array($order))
{
$order = array($order);
}
$this->orderby = $order;
}
function setLimit($limit = NULL)
{
if(!isset($limit)) return;
if(!isset($limit))
{
return;
}
$this->limit = $limit;
}
@ -217,7 +260,7 @@ class Query extends Object
* @param string|array $columns
* @return Query return Query instance
*/
function select($columns= NULL)
function select($columns = NULL)
{
$this->action = 'select';
$this->setColumns($columns);
@ -278,6 +321,7 @@ class Query extends Object
$this->setLimit($limit);
return $this;
}
// END Fluent interface
function getAction()
@ -287,7 +331,7 @@ class Query extends Object
function getPriority()
{
return $this->priority?'LOW_PRIORITY':'';
return $this->priority ? 'LOW_PRIORITY' : '';
}
/**
@ -304,9 +348,12 @@ class Query extends Object
function getClickCountColumns()
{
$click_count_columns = array();
foreach($this->columns as $column){
foreach($this->columns as $column)
{
if($column->show() && is_a($column, 'ClickCountExpression'))
{
$click_count_columns[] = $column;
}
}
return $click_count_columns;
}
@ -321,12 +368,16 @@ class Query extends Object
foreach($this->columns as $column)
{
if($column->show())
{
if($column->isSubquery())
{
$select[] = $column->toString($with_values) . ' as '. $column->getAlias();
$select[] = $column->toString($with_values) . ' as ' . $column->getAlias();
}
else
{
$select[] = $column->getExpression($with_values);
}
}
}
return trim(implode($select, ', '));
}
@ -341,7 +392,9 @@ class Query extends Object
foreach($this->columns as $column)
{
if($column->show())
{
$update[] = $column->getExpression($with_values);
}
}
return trim(implode($update, ', '));
}
@ -401,14 +454,26 @@ class Query extends Object
$simple_table_count = 0;
foreach($this->tables as $table)
{
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' ';
else $from .= ', '.$table->toString($with_values) . ' ';
if($table->isJoinTable() || !$simple_table_count)
{
$from .= $table->toString($with_values) . ' ';
}
else
{
$from .= ', ' . $table->toString($with_values) . ' ';
}
if(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
if(is_a($table, 'Subquery'))
{
$from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
}
$simple_table_count++;
}
if(trim($from) == '') return '';
if(trim($from) == '')
{
return '';
}
return $from;
}
@ -435,11 +500,13 @@ class Query extends Object
}
if($with_optimization &&
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order')))
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order')))
{
if($condition_count !== 0)
{
$where = '(' . $where . ') ';
}
foreach($this->orderby as $order)
{
@ -448,7 +515,9 @@ class Query extends Object
{
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if($condition_count === 0)
{
$opt_condition->setPipe("");
}
$where .= $opt_condition->toString($with_values) . ' ';
$condition_count++;
}
@ -465,8 +534,13 @@ class Query extends Object
function getGroupByString()
{
$groupBy = '';
if($this->groups) if($this->groups[0] !== "")
$groupBy = implode(', ', $this->groups);
if($this->groups)
{
if($this->groups[0] !== "")
{
$groupBy = implode(', ', $this->groups);
}
}
return $groupBy;
}
@ -478,11 +552,14 @@ class Query extends Object
{
if(!$this->_orderByString)
{
if(count($this->orderby) === 0) return '';
if(count($this->orderby) === 0)
{
return '';
}
$orderBy = '';
foreach($this->orderby as $order)
{
$orderBy .= $order->toString() .', ';
$orderBy .= $order->toString() . ', ';
}
$orderBy = substr($orderBy, 0, -2);
$this->_orderByString = $orderBy;
@ -533,7 +610,10 @@ class Query extends Object
if($table->isJoinTable() || is_a($table, 'Subquery'))
{
$args = $table->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
if($args)
{
$this->arguments = array_merge($this->arguments, $args);
}
}
}
}
@ -547,30 +627,43 @@ class Query extends Object
if($column->show())
{
$args = $column->getArguments();
if($args) $this->arguments = array_merge($this->arguments, $args);
if($args)
{
$this->arguments = array_merge($this->arguments, $args);
}
}
}
}
// Condition arguments
if(count($this->conditions) > 0)
{
foreach($this->conditions as $conditionGroup)
{
$args = $conditionGroup->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
if(count($args) > 0)
{
$this->arguments = array_merge($this->arguments, $args);
}
}
}
// Navigation arguments
if(count($this->orderby) > 0)
{
foreach($this->orderby as $order)
{
$args = $order->getArguments();
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
if(count($args) > 0)
{
$this->arguments = array_merge($this->arguments, $args);
}
}
}
}
return $this->arguments;
}
}
}
/* End of file Query.class.php */
/* Location: ./classes/db/queryparts/Query.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts
@ -6,11 +7,13 @@
*/
class Subquery extends Query
{
/**
* table alias
* @var string
*/
var $alias;
/**
* join type
* @var string
@ -52,7 +55,10 @@ class Subquery extends Query
function isJoinTable()
{
if($this->join_type) return true;
if($this->join_type)
{
return true;
}
return false;
}
@ -60,14 +66,14 @@ class Subquery extends Query
{
$oDB = &DB::getInstance();
return '(' .$oDB->getSelectSql($this, $with_values) . ')';
return '(' . $oDB->getSelectSql($this, $with_values) . ')';
}
function isSubquery()
{
return true;
}
}
/* End of file Subquery.class.php */
/* Location: ./classes/db/queryparts/Subquery.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
@ -6,12 +7,14 @@
*/
class Condition
{
/**
* column name
* @var string
*/
var $column_name;
var $argument;
/**
* operation can use 'equal', 'more', 'excess', 'less', 'below', 'like_tail', 'like_prefix', 'like', 'notlike_tail',
* 'notlike_prefix', 'notlike', 'in', 'notin', 'not_in', 'and', 'or', 'xor', 'not', 'notequal', 'between'
@ -19,14 +22,13 @@ class Condition
* @var string
*/
var $operation;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $_value;
var $_show;
var $_value_to_string;
@ -58,13 +60,13 @@ class Condition
*/
function toString($withValue = true)
{
if (!isset($this->_value_to_string))
if(!isset($this->_value_to_string))
{
if (!$this->show())
if(!$this->show())
{
$this->_value_to_string = '';
}
else if ($withValue)
else if($withValue)
{
$this->_value_to_string = $this->toStringWithValue();
}
@ -135,17 +137,34 @@ class Condition
case 'not':
case 'notequal' :
// if variable is not set or is not string or number, return
if(!isset($this->_value)) { $this->_show = false; break;}
if($this->_value === '') { $this->_show = false; break; }
$tmpArray = array('string'=>1, 'integer'=>1);
if(!isset($this->_value))
{
$this->_show = false;
break;
}
if($this->_value === '')
{
$this->_show = false;
break;
}
$tmpArray = array('string' => 1, 'integer' => 1);
if(!isset($tmpArray[gettype($this->_value)]))
{
$this->_show = false; break;
$this->_show = false;
break;
}
break;
case 'between' :
if(!is_array($this->_value)) { $this->_show = false; break;}
if(count($this->_value)!=2) {$this->_show = false; break;}
if(!is_array($this->_value))
{
$this->_show = false;
break;
}
if(count($this->_value) != 2)
{
$this->_show = false;
break;
}
case 'null':
case 'notnull':
break;
@ -171,67 +190,68 @@ class Condition
switch($operation)
{
case 'equal' :
return $name.' = '.$value;
return $name . ' = ' . $value;
break;
case 'more' :
return $name.' >= '.$value;
return $name . ' >= ' . $value;
break;
case 'excess' :
return $name.' > '.$value;
return $name . ' > ' . $value;
break;
case 'less' :
return $name.' <= '.$value;
return $name . ' <= ' . $value;
break;
case 'below' :
return $name.' < '.$value;
return $name . ' < ' . $value;
break;
case 'like_tail' :
case 'like_prefix' :
case 'like' :
if(defined('__CUBRID_VERSION__')
&& __CUBRID_VERSION__ >= '8.4.1')
return $name.' rlike '.$value;
if(defined('__CUBRID_VERSION__')
&& __CUBRID_VERSION__ >= '8.4.1')
return $name . ' rlike ' . $value;
else
return $name.' like '.$value;
return $name . ' like ' . $value;
break;
case 'notlike_tail' :
case 'notlike_prefix' :
case 'notlike' :
return $name.' not like '.$value;
return $name . ' not like ' . $value;
break;
case 'in' :
return $name.' in '.$value;
return $name . ' in ' . $value;
break;
case 'notin' :
case 'not_in' :
return $name.' not in '.$value;
return $name . ' not in ' . $value;
break;
case 'notequal' :
return $name.' <> '.$value;
return $name . ' <> ' . $value;
break;
case 'notnull' :
return $name.' is not null';
return $name . ' is not null';
break;
case 'null' :
return $name.' is null';
return $name . ' is null';
break;
case 'and' :
return $name.' & '.$value;
return $name . ' & ' . $value;
break;
case 'or' :
return $name.' | '.$value;
return $name . ' | ' . $value;
break;
case 'xor' :
return $name.' ^ '.$value;
return $name . ' ^ ' . $value;
break;
case 'not' :
return $name.' ~ '.$value;
return $name . ' ~ ' . $value;
break;
case 'between' :
return $name.' between ' . $value[0] . ' and ' . $value[1];
return $name . ' between ' . $value[0] . ' and ' . $value[1];
break;
}
}
}
/* End of file Condition.class.php */
/* Location: ./classes/db/queryparts/condition/Condition.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
@ -6,17 +7,18 @@
*/
class ConditionGroup
{
/**
* condition list
* @var array
*/
var $conditions;
/**
* pipe can use 'and', 'or'...
* @var string
*/
var $pipe;
var $_group;
var $_show;
@ -32,10 +34,18 @@ class ConditionGroup
foreach($conditions as $condition)
{
if($condition->show())
{
$this->conditions[] = $condition;
}
}
if(count($this->conditions) === 0)
{
$this->_show = false;
}
else
{
$this->_show = true;
}
if(count($this->conditions) === 0) $this->_show = false;
else $this->_show = true;
$this->pipe = $pipe;
}
@ -47,7 +57,10 @@ class ConditionGroup
function setPipe($pipe)
{
if($this->pipe !== $pipe) $this->_group = null;
if($this->pipe !== $pipe)
{
$this->_group = null;
}
$this->pipe = $pipe;
}
@ -65,7 +78,10 @@ class ConditionGroup
foreach($this->conditions as $condition)
{
if($cond_indx === 0) $condition->setPipe("");
if($cond_indx === 0)
{
$condition->setPipe("");
}
$group .= $condition->toString($with_value) . ' ';
$cond_indx++;
}
@ -90,10 +106,14 @@ class ConditionGroup
foreach($this->conditions as $condition)
{
$arg = $condition->getArgument();
if($arg) $args[] = $arg;
if($arg)
{
$args[] = $arg;
}
}
return $args;
}
}
/* End of file ConditionGroup.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionGroup.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
@ -6,6 +7,7 @@
*/
class ConditionSubquery extends Condition
{
/**
* constructor
* @param string $column_name
@ -19,6 +21,7 @@ class ConditionSubquery extends Condition
parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $this->argument->toString();
}
}
/* End of file ConditionSubquery.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionSubquery.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
@ -6,6 +7,7 @@
*/
class ConditionWithArgument extends Condition
{
/**
* constructor
* @param string $column_name
@ -16,14 +18,19 @@ class ConditionWithArgument extends Condition
*/
function ConditionWithArgument($column_name, $argument, $operation, $pipe = "")
{
if($argument === null) { $this->_show = false; return; }
if($argument === null)
{
$this->_show = false;
return;
}
parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $argument->getValue();
}
function getArgument()
{
if(!$this->show()) return;
if(!$this->show())
return;
return $this->argument;
}
@ -38,11 +45,17 @@ class ConditionWithArgument extends Condition
if(is_array($value))
{
$q = '';
foreach ($value as $v) $q .= '?,';
if($q !== '') $q = substr($q, 0, -1);
foreach($value as $v)
{
$q .= '?,';
}
if($q !== '')
{
$q = substr($q, 0, -1);
}
$q = '(' . $q . ')';
}
else
else
{
// Prepared statements: column names should not be sent as query arguments, but instead concatenated to query string
if($this->argument->isColumnName())
@ -64,8 +77,14 @@ class ConditionWithArgument extends Condition
{
if(!isset($this->_show))
{
if(!$this->argument->isValid()) $this->_show = false;
if($this->_value === '\'\'') $this->_show = false;
if(!$this->argument->isValid())
{
$this->_show = false;
}
if($this->_value === '\'\'')
{
$this->_show = false;
}
if(!isset($this->_show))
{
return parent::show();
@ -73,6 +92,7 @@ class ConditionWithArgument extends Condition
}
return $this->_show;
}
}
/* End of file ConditionWithArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithArgument.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/condition
@ -6,6 +7,7 @@
*/
class ConditionWithoutArgument extends Condition
{
/**
* constructor
* @param string $column_name
@ -17,16 +19,21 @@ class ConditionWithoutArgument extends Condition
function ConditionWithoutArgument($column_name, $argument, $operation, $pipe = "")
{
parent::Condition($column_name, $argument, $operation, $pipe);
$tmpArray = array('in'=>1, 'notin'=>1, 'not_in'=>1);
$tmpArray = array('in' => 1, 'notin' => 1, 'not_in' => 1);
if(isset($tmpArray[$operation]))
{
if(is_array($argument)) $argument = implode($argument, ',');
$this->_value = '('. $argument .')';
if(is_array($argument))
{
$argument = implode($argument, ',');
}
$this->_value = '(' . $argument . ')';
}
else
{
$this->_value = $argument;
}
}
}
}
/* End of file ConditionWithoutArgument.class.php */
/* Location: ./classes/db/queryparts/condition/ConditionWithoutArgument.class.php */

View file

@ -1,4 +1,5 @@
<?php
<?php
/**
* ClickCountExpression
* @author Arnia Software
@ -7,6 +8,7 @@
*/
class ClickCountExpression extends SelectExpression
{
/**
* click count
* @var bool
@ -53,7 +55,7 @@ class ClickCountExpression extends SelectExpression
return "$this->column_name";
}
}
}
}
/* End of file ClickCountExpression.class.php */
/* Location: ./classes/db/queryparts/expression/ClickCountExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php
<?php
/**
* DeleteExpression
*
@ -9,6 +10,7 @@
*/
class DeleteExpression extends Expression
{
/**
* column value
* @var mixed
@ -39,15 +41,22 @@ class DeleteExpression extends Expression
function getValue()
{
// TODO Escape value according to column type instead of variable type
if(!is_numeric($this->value)) return "'".$this->value."'";
if(!is_numeric($this->value))
{
return "'" . $this->value . "'";
}
return $this->value;
}
function show()
{
if(!$this->value) return false;
if(!$this->value)
{
return false;
}
return true;
}
}
/* End of file DeleteExpression.class.php */
/* Location: ./classes/db/queryparts/expression/DeleteExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* Expression
* Represents an expression used in select/update/insert/delete statements
@ -13,6 +14,7 @@
*/
class Expression
{
/**
* column name
* @var string
@ -45,8 +47,9 @@ class Expression
*/
function getExpression()
{
}
}
}
/* End of file Expression.class.php */
/* Location: ./classes/db/queryparts/expression/Expression.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* InsertExpression
*
@ -8,6 +9,7 @@
*/
class InsertExpression extends Expression
{
/**
* argument
* @var object
@ -29,15 +31,23 @@ class InsertExpression extends Expression
function getValue($with_values = true)
{
if($with_values)
{
return $this->argument->getValue();
}
return '?';
}
function show()
{
if(!$this->argument) return false;
if(!$this->argument)
{
return false;
}
$value = $this->argument->getValue();
if(!isset($value)) return false;
if(!isset($value))
{
return false;
}
return true;
}
@ -48,11 +58,16 @@ class InsertExpression extends Expression
function getArguments()
{
if ($this->argument)
if($this->argument)
{
return array($this->argument);
}
else
{
return array();
}
}
}
/* End of file InsertExpression.class.php */
/* Location: ./classes/db/queryparts/expression/InsertExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* SelectExpression
* Represents an expresion that appears in the select clause
@ -6,8 +7,8 @@
* $column_name can be:
* - a table column name
* - an sql function - like count(*)
* - an sql expression - substr(column_name, 1, 8) or score1 + score2
* $column_name is already escaped
* - an sql expression - substr(column_name, 1, 8) or score1 + score2
* $column_name is already escaped
*
* @author Arnia Software
* @package /classes/db/queryparts/expression
@ -15,6 +16,7 @@
*/
class SelectExpression extends Expression
{
/**
* column alias name
* @var string
@ -39,7 +41,7 @@ class SelectExpression extends Expression
*/
function getExpression()
{
return sprintf("%s%s", $this->column_name, $this->column_alias ? " as ".$this->column_alias : "");
return sprintf("%s%s", $this->column_name, $this->column_alias ? " as " . $this->column_alias : "");
}
function show()
@ -61,6 +63,7 @@ class SelectExpression extends Expression
{
return false;
}
}
/* End of file SelectExpression.class.php */
/* Location: ./classes/db/queryparts/expression/SelectExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php
<?php
/**
* StarExpression
* Represents the * in 'select * from ...' statements
@ -9,6 +10,7 @@
*/
class StarExpression extends SelectExpression
{
/**
* constructor, set the column to asterisk
* @return void
@ -28,6 +30,7 @@ class StarExpression extends SelectExpression
// StarExpression has no arguments
return array();
}
}
/* End of file StarExpression.class.php */
/* Location: ./classes/db/queryparts/expression/StarExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* UpdateExpression
*
@ -8,6 +9,7 @@
*/
class UpdateExpression extends Expression
{
/**
* argument
* @var object
@ -33,7 +35,9 @@ class UpdateExpression extends Expression
function getExpression($with_value = true)
{
if($with_value)
{
return $this->getExpressionWithValue();
}
return $this->getExpressionWithoutValue();
}
@ -46,7 +50,9 @@ class UpdateExpression extends Expression
$value = $this->argument->getValue();
$operation = $this->argument->getColumnOperation();
if(isset($operation))
{
return "$this->column_name = $this->column_name $operation $value";
}
return "$this->column_name = $value";
}
@ -59,7 +65,9 @@ class UpdateExpression extends Expression
{
$operation = $this->argument->getColumnOperation();
if(isset($operation))
{
return "$this->column_name = $this->column_name $operation ?";
}
return "$this->column_name = ?";
}
@ -67,15 +75,24 @@ class UpdateExpression extends Expression
{
// TODO Escape value according to column type instead of variable type
$value = $this->argument->getValue();
if(!is_numeric($value)) return "'".$value."'";
if(!is_numeric($value))
{
return "'" . $value . "'";
}
return $value;
}
function show()
{
if(!$this->argument) return false;
if(!$this->argument)
{
return false;
}
$value = $this->argument->getValue();
if(!isset($value)) return false;
if(!isset($value))
{
return false;
}
return true;
}
@ -86,12 +103,16 @@ class UpdateExpression extends Expression
function getArguments()
{
if ($this->argument)
if($this->argument)
{
return array($this->argument);
}
else
{
return array();
}
}
}
}
/* End of file UpdateExpression.class.php */
/* Location: ./classes/db/queryparts/expression/UpdateExpression.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* UpdateExpression
*
@ -8,6 +9,7 @@
*/
class UpdateExpressionWithoutArgument extends UpdateExpression
{
/**
* argument
* @var object
@ -35,15 +37,24 @@ class UpdateExpressionWithoutArgument extends UpdateExpression
{
// TODO Escape value according to column type instead of variable type
$value = $this->argument;
if(!is_numeric($value)) return "'".$value."'";
if(!is_numeric($value))
{
return "'" . $value . "'";
}
return $value;
}
function show()
{
if(!$this->argument) return false;
if(!$this->argument)
{
return false;
}
$value = $this->argument;
if(!isset($value)) return false;
if(!isset($value))
{
return false;
}
return true;
}
@ -56,6 +67,7 @@ class UpdateExpressionWithoutArgument extends UpdateExpression
{
return array();
}
}
/* End of file UpdateExpressionWithoutArgument.class.php */
/* Location: ./classes/db/queryparts/expression/UpdateExpressionWithoutArgument.class.php */

View file

@ -1,4 +1,5 @@
<?php
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/limit
@ -6,21 +7,25 @@
*/
class Limit
{
/**
* start number
* @var int
*/
var $start;
/**
* list count
* @var int
*/
var $list_count;
/**
* page count
* @var int
*/
var $page_count;
/**
* current page
* @var int
@ -34,10 +39,10 @@ class Limit
* @param int $page_count
* @return void
*/
function Limit($list_count, $page= NULL, $page_count= NULL)
function Limit($list_count, $page = NULL, $page_count = NULL)
{
$this->list_count = $list_count;
if ($page)
if($page)
{
$list_count_value = $list_count->getValue();
$page_value = $page->getValue();
@ -53,8 +58,14 @@ class Limit
*/
function isPageHandler()
{
if ($this->page)return true;
else return false;
if($this->page)
{
return true;
}
else
{
return false;
}
}
function getOffset()
@ -69,9 +80,16 @@ class Limit
function toString()
{
if ($this->page) return $this->start . ' , ' . $this->list_count->getValue();
else return $this->list_count->getValue();
if($this->page)
{
return $this->start . ' , ' . $this->list_count->getValue();
}
else
{
return $this->list_count->getValue();
}
}
}
/* End of file Limit.class.php */
/* Location: ./classes/db/limit/Limit.class.php */

View file

@ -1,4 +1,5 @@
<?php
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/order
@ -6,11 +7,13 @@
*/
class OrderByColumn
{
/**
* column name
* @var string
*/
var $column_name;
/**
* sort order
* @var string
@ -46,10 +49,15 @@ class OrderByColumn
{
$args = array();
if(is_a($this->column_name, 'Argument'))
$args[]= $this->column_name;
{
$args[] = $this->column_name;
}
if(is_a($this->sort_order, 'Argument'))
$args[] = $this->sort_order;
{
$args[] = $this->sort_order;
}
}
}
/* End of file OrderByColumn.class.php */
/* Location: ./classes/db/order/OrderByColumn.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
@ -6,16 +7,19 @@
*/
class CubridTableWithHint extends Table
{
/**
* table name
* @var string
*/
var $name;
/**
* table alias
* @var string
*/
var $alias;
/**
* index hint list
* @var array
@ -53,14 +57,15 @@ class CubridTableWithHint extends Table
if($index_hint_type !== 'IGNORE')
{
$result .= $this->alias . '.'
. '"' . $prefix . substr($index_hint->getIndexName(), 1)
. ($index_hint_type == 'FORCE' ? '(+)' : '')
. ', ';
. '"' . $prefix . substr($index_hint->getIndexName(), 1)
. ($index_hint_type == 'FORCE' ? '(+)' : '')
. ', ';
}
}
$result = substr($result, 0, -2);
return $result;
}
}
/* End of file CubridTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/CubridTableWithHint.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
@ -6,11 +7,13 @@
*/
class IndexHint
{
/**
* index name
* @var string
*/
var $index_name;
/**
* index hint type, ex) IGNORE, FORCE, USE...
* @var string
@ -38,6 +41,7 @@ class IndexHint
{
return $this->index_hint_type;
}
}
/* End of file IndexHint.class.php */
/* Location: ./classes/db/queryparts/table/IndexHint.class.php */

View file

@ -1,4 +1,5 @@
<?php
<?php
/**
* class JoinTable
* $conditions in an array of Condition objects
@ -9,11 +10,13 @@
*/
class JoinTable extends Table
{
/**
* join type
* @var string
*/
var $join_type;
/**
* condition list
* @var array
@ -37,11 +40,13 @@ class JoinTable extends Table
function toString($with_value = true)
{
$part = $this->join_type . ' ' . $this->name ;
$part = $this->join_type . ' ' . $this->name;
$part .= $this->alias ? ' as ' . $this->alias : '';
$part .= ' on ';
foreach($this->conditions as $conditionGroup)
{
$part .= $conditionGroup->toString($with_value);
}
return $part;
}
@ -54,9 +59,12 @@ class JoinTable extends Table
{
$args = array();
foreach($this->conditions as $conditionGroup)
$args = array_merge($args, $conditionGroup->getArguments());
{
$args = array_merge($args, $conditionGroup->getArguments());
}
return $args;
}
}
/* End of file JoinTable.class.php */
/* Location: ./classes/db/queryparts/table/JoinTable.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
@ -6,16 +7,19 @@
*/
class MssqlTableWithHint extends Table
{
/**
* table name
* @var string
*/
var $name;
/**
* table alias
* @var string
*/
var $alias;
/**
* index hint type, ex) IGNORE, FORCE, USE...
* @var array
@ -40,12 +44,14 @@ class MssqlTableWithHint extends Table
$result = parent::toString();
$index_hint_string = '';
$indexTypeList = array('USE'=>1, 'FORCE'=>1);
$indexTypeList = array('USE' => 1, 'FORCE' => 1);
foreach($this->index_hints_list as $index_hint)
{
$index_hint_type = $index_hint->getIndexHintType();
if(isset($indexTypeList[$index_hint_type]))
{
$index_hint_string .= 'INDEX(' . $index_hint->getIndexName() . '), ';
}
}
if($index_hint_string != '')
{
@ -53,6 +59,7 @@ class MssqlTableWithHint extends Table
}
return $result;
}
}
/* End of file MssqlTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/MssqlTableWithHint.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
@ -6,16 +7,19 @@
*/
class MysqlTableWithHint extends Table
{
/**
* table name
* @var string
*/
var $name;
/**
* table alias
* @var string
*/
var $alias;
/**
* index hint type, ex) IGNORE, FORCE, USE...
* @var array
@ -39,13 +43,24 @@ class MysqlTableWithHint extends Table
{
$result = parent::toString();
$use_index_hint = ''; $force_index_hint = ''; $ignore_index_hint = '';
$use_index_hint = '';
$force_index_hint = '';
$ignore_index_hint = '';
foreach($this->index_hints_list as $index_hint)
{
$index_hint_type = $index_hint->getIndexHintType();
if($index_hint_type == 'USE') $use_index_hint .= $index_hint->getIndexName() . ', ';
else if($index_hint_type == 'FORCE') $force_index_hint .= $index_hint->getIndexName() . ', ';
else if($index_hint_type == 'IGNORE') $ignore_index_hint .= $index_hint->getIndexName() . ', ';
if($index_hint_type == 'USE')
{
$use_index_hint .= $index_hint->getIndexName() . ', ';
}
else if($index_hint_type == 'FORCE')
{
$force_index_hint .= $index_hint->getIndexName() . ', ';
}
else if($index_hint_type == 'IGNORE')
{
$ignore_index_hint .= $index_hint->getIndexName() . ', ';
}
}
if($use_index_hint != '')
{
@ -61,6 +76,7 @@ class MysqlTableWithHint extends Table
}
return $result;
}
}
/* End of file MysqlTableWithHint.class.php */
/* Location: ./classes/db/queryparts/table/MysqlTableWithHint.class.php */

View file

@ -1,4 +1,5 @@
<?php
<?php
/**
* @author NHN (developers@xpressengine.com)
* @package /classes/db/queryparts/table
@ -6,11 +7,13 @@
*/
class Table
{
/**
* table name
* @var string
*/
var $name;
/**
* table alias
* @var string
@ -49,7 +52,7 @@ class Table
{
return false;
}
}
}
/* End of file Table.class.php */
/* Location: ./classes/db/queryparts/table/Table.class.php */