mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-01 00:02:21 +09:00
Merged 1.5.0-DB branch into 1.5.0 - added new DB classes.
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8697 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
commit
c9203dc356
105 changed files with 6279 additions and 3467 deletions
|
|
@ -357,13 +357,32 @@
|
|||
|
||||
if($source_args) $args = @clone($source_args);
|
||||
|
||||
$output = @include($cache_file);
|
||||
require_once(_XE_PATH_.'classes/xml/xmlquery/DBParser.class.php');
|
||||
require_once(_XE_PATH_.'classes/xml/xmlquery/argument/Argument.class.php');
|
||||
require_once(_XE_PATH_.'classes/xml/xmlquery/argument/SortArgument.class.php');
|
||||
require_once(_XE_PATH_.'classes/xml/xmlquery/argument/ConditionArgument.class.php');
|
||||
require_once(_XE_PATH_.'classes/xml/XmlQueryParser.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/expression/Expression.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/expression/SelectExpression.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/expression/InsertExpression.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/expression/UpdateExpression.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/table/Table.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/table/JoinTable.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/condition/ConditionGroup.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/condition/Condition.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/expression/StarExpression.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/order/OrderByColumn.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/limit/Limit.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/Query.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/Subquery.class.php');
|
||||
|
||||
|
||||
$output = include($cache_file);
|
||||
|
||||
if( (is_a($output, 'Object') || is_subclass_of($output, 'Object')) && !$output->toBool()) return $output;
|
||||
$output->_tables = ($output->_tables && is_array($output->_tables)) ? $output->_tables : array();
|
||||
|
||||
|
||||
// execute appropriate query
|
||||
switch($output->action) {
|
||||
switch($output->getAction()) {
|
||||
case 'insert' :
|
||||
$this->resetCountCache($output->tables);
|
||||
$output = $this->_executeInsertAct($output);
|
||||
|
|
@ -377,11 +396,12 @@
|
|||
$output = $this->_executeDeleteAct($output);
|
||||
break;
|
||||
case 'select' :
|
||||
// TODO Add property for Query object for Arg_columns
|
||||
$output->arg_columns = is_array($arg_columns)?$arg_columns:array();
|
||||
$output = $this->_executeSelectAct($output);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
|
@ -390,232 +410,6 @@
|
|||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief check $val with $filter_type
|
||||
* @param[in] $key key value
|
||||
* @param[in] $val value of $key
|
||||
* @param[in] $filter_type type of filter to check $val
|
||||
* @return object
|
||||
* @remarks this function is to be used from XmlQueryParser
|
||||
**/
|
||||
function checkFilter($key, $val, $filter_type) {
|
||||
global $lang;
|
||||
|
||||
switch($filter_type) {
|
||||
case 'email' :
|
||||
case 'email_address' :
|
||||
if(!preg_match('/^[_0-9a-z-]+(\.[_0-9a-z-]+)*@[0-9a-z-]+(\.[0-9a-z-]+)*$/is', $val)) return new Object(-1, sprintf($lang->filter->invalid_email, $lang->{$key} ? $lang->{$key} : $key));
|
||||
break;
|
||||
case 'homepage' :
|
||||
if(!preg_match('/^(http|https)+(:\/\/)+[0-9a-z_-]+\.[^ ]+$/is', $val)) return new Object(-1, sprintf($lang->filter->invalid_homepage, $lang->{$key} ? $lang->{$key} : $key));
|
||||
break;
|
||||
case 'userid' :
|
||||
case 'user_id' :
|
||||
if(!preg_match('/^[a-zA-Z]+([_0-9a-zA-Z]+)*$/is', $val)) return new Object(-1, sprintf($lang->filter->invalid_userid, $lang->{$key} ? $lang->{$key} : $key));
|
||||
break;
|
||||
case 'number' :
|
||||
case 'numbers' :
|
||||
if(is_array($val)) $val = join(',', $val);
|
||||
if(!preg_match('/^(-?)[0-9]+(,\-?[0-9]+)*$/is', $val)) return new Object(-1, sprintf($lang->filter->invalid_number, $lang->{$key} ? $lang->{$key} : $key));
|
||||
break;
|
||||
case 'alpha' :
|
||||
if(!preg_match('/^[a-z]+$/is', $val)) return new Object(-1, sprintf($lang->filter->invalid_alpha, $lang->{$key} ? $lang->{$key} : $key));
|
||||
break;
|
||||
case 'alpha_number' :
|
||||
if(!preg_match('/^[0-9a-z]+$/is', $val)) return new Object(-1, sprintf($lang->filter->invalid_alpha_number, $lang->{$key} ? $lang->{$key} : $key));
|
||||
break;
|
||||
}
|
||||
|
||||
return new Object();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns type of column
|
||||
* @param[in] $column_type_list list of column type
|
||||
* @param[in] $name name of column type
|
||||
* @return column type of $name
|
||||
* @remarks columns are usually like a.b, so it needs another function
|
||||
**/
|
||||
function getColumnType($column_type_list, $name) {
|
||||
if(strpos($name, '.') === false) return $column_type_list[$name];
|
||||
list($prefix, $name) = explode('.', $name);
|
||||
return $column_type_list[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns the value of condition
|
||||
* @param[in] $name name of condition
|
||||
* @param[in] $value value of condition
|
||||
* @param[in] $operation operation this is used in condition
|
||||
* @param[in] $type type of condition
|
||||
* @param[in] $column_type type of column
|
||||
* @return well modified $value
|
||||
* @remarks if $operation is like or like_prefix, $value itself will be modified
|
||||
* @remarks if $type is not 'number', call addQuotes() and wrap with ' '
|
||||
**/
|
||||
function getConditionValue($name, $value, $operation, $type, $column_type) {
|
||||
if(!in_array($operation,array('in','notin','between')) && $type == 'number') {
|
||||
if(is_array($value)){
|
||||
$value = join(',',$value);
|
||||
}
|
||||
if(strpos($value, ',') === false && strpos($value, '(') === false) return (int)$value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
if(!is_array($value) && strpos($name, '.') !== false && strpos($value, '.') !== false) {
|
||||
list($table_name, $column_name) = explode('.', $value);
|
||||
if($column_type[$column_name]) return $value;
|
||||
}
|
||||
|
||||
switch($operation) {
|
||||
case 'like_prefix' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = $value.'%';
|
||||
break;
|
||||
case 'like_tail' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = '%'.$value;
|
||||
break;
|
||||
case 'like' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = '%'.$value.'%';
|
||||
break;
|
||||
case 'notin' :
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type=='number') return join(',',$value);
|
||||
else return "'". join("','",$value)."'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
break;
|
||||
case 'in' :
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type=='number') return join(',',$value);
|
||||
else return "'". join("','",$value)."'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
break;
|
||||
case 'between' :
|
||||
if(!is_array($value)) $value = array($value);
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type!='number')
|
||||
{
|
||||
foreach($value as $k=>$v)
|
||||
{
|
||||
$value[$k] = "'".$v."'";
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
break;
|
||||
default:
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
}
|
||||
|
||||
return "'".$this->addQuotes($value)."'";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns part of condition
|
||||
* @param[in] $name name of condition
|
||||
* @param[in] $value value of condition
|
||||
* @param[in] $operation operation that is used in condition
|
||||
* @return detail condition
|
||||
**/
|
||||
function getConditionPart($name, $value, $operation) {
|
||||
switch($operation) {
|
||||
case 'equal' :
|
||||
case 'more' :
|
||||
case 'excess' :
|
||||
case 'less' :
|
||||
case 'below' :
|
||||
case 'like_tail' :
|
||||
case 'like_prefix' :
|
||||
case 'like' :
|
||||
case 'in' :
|
||||
case 'notin' :
|
||||
case 'notequal' :
|
||||
// if variable is not set or is not string or number, return
|
||||
if(!isset($value)) return;
|
||||
if($value === '') return;
|
||||
if(!in_array(gettype($value), array('string', 'integer'))) return;
|
||||
break;
|
||||
case 'between' :
|
||||
if(!is_array($value)) return;
|
||||
if(count($value)!=2) return;
|
||||
|
||||
}
|
||||
|
||||
switch($operation) {
|
||||
case 'equal' :
|
||||
return $name.' = '.$value;
|
||||
break;
|
||||
case 'more' :
|
||||
return $name.' >= '.$value;
|
||||
break;
|
||||
case 'excess' :
|
||||
return $name.' > '.$value;
|
||||
break;
|
||||
case 'less' :
|
||||
return $name.' <= '.$value;
|
||||
break;
|
||||
case 'below' :
|
||||
return $name.' < '.$value;
|
||||
break;
|
||||
case 'like_tail' :
|
||||
case 'like_prefix' :
|
||||
case 'like' :
|
||||
return $name.' like '.$value;
|
||||
break;
|
||||
case 'in' :
|
||||
return $name.' in ('.$value.')';
|
||||
break;
|
||||
case 'notin' :
|
||||
return $name.' not in ('.$value.')';
|
||||
break;
|
||||
case 'notequal' :
|
||||
return $name.' <> '.$value;
|
||||
break;
|
||||
case 'notnull' :
|
||||
return $name.' is not null';
|
||||
break;
|
||||
case 'null' :
|
||||
return $name.' is null';
|
||||
break;
|
||||
case 'between' :
|
||||
return $name.' between ' . $value[0] . ' and ' . $value[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns condition key
|
||||
* @param[in] $output result of query
|
||||
* @return array of conditions of $output
|
||||
**/
|
||||
function getConditionList($output) {
|
||||
$conditions = array();
|
||||
if(count($output->conditions)) {
|
||||
foreach($output->conditions as $key => $val) {
|
||||
if($val['condition']) {
|
||||
foreach($val['condition'] as $k => $v) {
|
||||
$conditions[] = $v['column'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns counter cache data
|
||||
|
|
@ -720,30 +514,260 @@
|
|||
$this->_query($query);
|
||||
}
|
||||
|
||||
function addQuotesArray($arr)
|
||||
{
|
||||
if(is_array($arr))
|
||||
{
|
||||
foreach($arr as $k => $v)
|
||||
{
|
||||
$arr[$k] = $this->addQuotes($v);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$arr = $this->addQuotes($arr);
|
||||
}
|
||||
function getSelectSql($query, $with_values = true){
|
||||
$select = $query->getSelectString($with_values);
|
||||
if($select == '') return new Object(-1, "Invalid query");
|
||||
$select = 'SELECT ' .$select;
|
||||
|
||||
return $arr;
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$from = ' FROM '.$from;
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
|
||||
$groupBy = $query->getGroupByString();
|
||||
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
|
||||
|
||||
$orderBy = $query->getOrderByString();
|
||||
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
|
||||
|
||||
$limit = $query->getLimitString();
|
||||
if($limit != '') $limit = ' LIMIT ' . $limit;
|
||||
|
||||
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Just like numbers, and operations needed to remove the rest
|
||||
function getDeleteSql($query, $with_values = true){
|
||||
$sql = 'DELETE ';
|
||||
|
||||
// TODO Add support for deleting based on alias, for both simple FROM and multi table join FROM clause
|
||||
$tables = $query->getTables();
|
||||
|
||||
$sql .= $tables[0]->getAlias();
|
||||
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$sql .= ' FROM '.$from;
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $sql .= ' WHERE ' . $where;
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
function getUpdateSql($query, $with_values = true){
|
||||
$columnsList = $query->getSelectString($with_values);
|
||||
if($columnsList == '') return new Object(-1, "Invalid query");
|
||||
|
||||
$tableName = $query->getFirstTableName();
|
||||
if($tableName == '') return new Object(-1, "Invalid query");
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
|
||||
return "UPDATE $tableName SET $columnsList ".$where;
|
||||
}
|
||||
|
||||
function getInsertSql($query, $with_values = true){
|
||||
$tableName = $query->getFirstTableName();
|
||||
$values = $query->getInsertString($with_values);
|
||||
|
||||
return "INSERT INTO $tableName \n $values";
|
||||
}
|
||||
|
||||
// HACK This is needed because on installation, the XmlQueryParer is used without any configured database
|
||||
// TODO Change this or make sure the query cache files created before db.config exists are deleted
|
||||
function getParser(){
|
||||
return new DBParser('"');
|
||||
}
|
||||
|
||||
|
||||
// TO BE REMOVED - Used for query compare
|
||||
/**
|
||||
* @brief returns type of column
|
||||
* @param[in] $column_type_list list of column type
|
||||
* @param[in] $name name of column type
|
||||
* @return column type of $name
|
||||
* @remarks columns are usually like a.b, so it needs another function
|
||||
**/
|
||||
function _filterNumber(&$value)
|
||||
{
|
||||
$value = preg_replace('/[^\d\w\+\-\*\/\.\(\)]/', '', $value);
|
||||
if(!$value) $value = 0;
|
||||
}
|
||||
function getColumnType($column_type_list, $name) {
|
||||
if(strpos($name, '.') === false) return $column_type_list[$name];
|
||||
list($prefix, $name) = explode('.', $name);
|
||||
return $column_type_list[$name];
|
||||
}
|
||||
/**
|
||||
* @brief returns the value of condition
|
||||
* @param[in] $name name of condition
|
||||
* @param[in] $value value of condition
|
||||
* @param[in] $operation operation this is used in condition
|
||||
* @param[in] $type type of condition
|
||||
* @param[in] $column_type type of column
|
||||
* @return well modified $value
|
||||
* @remarks if $operation is like or like_prefix, $value itself will be modified
|
||||
* @remarks if $type is not 'number', call addQuotes() and wrap with ' '
|
||||
**/
|
||||
function getConditionValue($name, $value, $operation, $type, $column_type) {
|
||||
if(!in_array($operation,array('in','notin','between')) && $type == 'number') {
|
||||
if(is_array($value)){
|
||||
$value = join(',',$value);
|
||||
}
|
||||
if(strpos($value, ',') === false && strpos($value, '(') === false) return (int)$value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
if(!is_array($value) && strpos($name, '.') !== false && strpos($value, '.') !== false) {
|
||||
list($table_name, $column_name) = explode('.', $value);
|
||||
if($column_type[$column_name]) return $value;
|
||||
}
|
||||
|
||||
switch($operation) {
|
||||
case 'like_prefix' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = $value.'%';
|
||||
break;
|
||||
case 'like_tail' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = '%'.$value;
|
||||
break;
|
||||
case 'like' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = '%'.$value.'%';
|
||||
break;
|
||||
case 'notin' :
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type=='number') return join(',',$value);
|
||||
else return "'". join("','",$value)."'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
break;
|
||||
case 'in' :
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type=='number') return join(',',$value);
|
||||
else return "'". join("','",$value)."'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
break;
|
||||
case 'between' :
|
||||
if(!is_array($value)) $value = array($value);
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type!='number')
|
||||
{
|
||||
foreach($value as $k=>$v)
|
||||
{
|
||||
$value[$k] = "'".$v."'";
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
break;
|
||||
default:
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
}
|
||||
|
||||
return "'".$this->addQuotes($value)."'";
|
||||
}
|
||||
/**
|
||||
* @brief returns part of condition
|
||||
* @param[in] $name name of condition
|
||||
* @param[in] $value value of condition
|
||||
* @param[in] $operation operation that is used in condition
|
||||
* @return detail condition
|
||||
**/
|
||||
function getConditionPart($name, $value, $operation) {
|
||||
switch($operation) {
|
||||
case 'equal' :
|
||||
case 'more' :
|
||||
case 'excess' :
|
||||
case 'less' :
|
||||
case 'below' :
|
||||
case 'like_tail' :
|
||||
case 'like_prefix' :
|
||||
case 'like' :
|
||||
case 'in' :
|
||||
case 'notin' :
|
||||
case 'notequal' :
|
||||
// if variable is not set or is not string or number, return
|
||||
if(!isset($value)) return;
|
||||
if($value === '') return;
|
||||
if(!in_array(gettype($value), array('string', 'integer'))) return;
|
||||
break;
|
||||
case 'between' :
|
||||
if(!is_array($value)) return;
|
||||
if(count($value)!=2) return;
|
||||
|
||||
}
|
||||
|
||||
switch($operation) {
|
||||
case 'equal' :
|
||||
return $name.' = '.$value;
|
||||
break;
|
||||
case 'more' :
|
||||
return $name.' >= '.$value;
|
||||
break;
|
||||
case 'excess' :
|
||||
return $name.' > '.$value;
|
||||
break;
|
||||
case 'less' :
|
||||
return $name.' <= '.$value;
|
||||
break;
|
||||
case 'below' :
|
||||
return $name.' < '.$value;
|
||||
break;
|
||||
case 'like_tail' :
|
||||
case 'like_prefix' :
|
||||
case 'like' :
|
||||
return $name.' like '.$value;
|
||||
break;
|
||||
case 'in' :
|
||||
return $name.' in ('.$value.')';
|
||||
break;
|
||||
case 'notin' :
|
||||
return $name.' not in ('.$value.')';
|
||||
break;
|
||||
case 'notequal' :
|
||||
return $name.' <> '.$value;
|
||||
break;
|
||||
case 'notnull' :
|
||||
return $name.' is not null';
|
||||
break;
|
||||
case 'null' :
|
||||
return $name.' is null';
|
||||
break;
|
||||
case 'between' :
|
||||
return $name.' between ' . $value[0] . ' and ' . $value[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns condition key
|
||||
* @param[in] $output result of query
|
||||
* @return array of conditions of $output
|
||||
**/
|
||||
function getConditionList($output) {
|
||||
$conditions = array();
|
||||
if(count($output->conditions)) {
|
||||
foreach($output->conditions as $key => $val) {
|
||||
if($val['condition']) {
|
||||
foreach($val['condition'] as $k => $v) {
|
||||
$conditions[] = $v['column'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $conditions;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@
|
|||
$this->_setDBInfo();
|
||||
$this->_connect();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief create an instance of this class
|
||||
*/
|
||||
|
|
@ -190,7 +190,7 @@
|
|||
{
|
||||
if (!$query || !$this->isConnected ()) return;
|
||||
|
||||
// Notify to start a query execution
|
||||
// Notify to start a query execution
|
||||
$this->actStart ($query);
|
||||
|
||||
// Execute the query
|
||||
|
|
@ -207,16 +207,18 @@
|
|||
$this->actFinish ();
|
||||
|
||||
// Return the result
|
||||
return $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Fetch the result
|
||||
**/
|
||||
function _fetch($result)
|
||||
function _fetch($result, $arrayIndexEndValue = NULL)
|
||||
{
|
||||
if (!$this->isConnected() || $this->isError() || !$result) return;
|
||||
|
||||
// TODO Improve this piece of code
|
||||
// This code trims values from char type columns
|
||||
$col_types = cubrid_column_types ($result);
|
||||
$col_names = cubrid_column_names ($result);
|
||||
$max = count ($col_types);
|
||||
|
|
@ -234,14 +236,16 @@
|
|||
}
|
||||
}
|
||||
|
||||
$output[] = $tmp;
|
||||
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp;
|
||||
else $output[] = $tmp;
|
||||
}
|
||||
|
||||
unset ($char_type_fields);
|
||||
|
||||
if ($result) cubrid_close_request($result);
|
||||
|
||||
if (count ($output) == 1) return $output[0];
|
||||
if($arrayIndexEndValue !== null && count($output) == 1) return $output[$arrayIndexEndValue + 1];
|
||||
else if (count($output) == 1) return $output[0];
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
|
@ -445,7 +449,6 @@
|
|||
if (!file_exists ($file_name)) return;
|
||||
// read xml file
|
||||
$buff = FileHandler::readFile ($file_name);
|
||||
|
||||
return $this->_createTable ($buff);
|
||||
}
|
||||
|
||||
|
|
@ -561,132 +564,19 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief return the condition
|
||||
**/
|
||||
function getCondition ($output)
|
||||
{
|
||||
if (!$output->conditions) return;
|
||||
$condition = $this->_getCondition ($output->conditions, $output->column_type, $output);
|
||||
if ($condition) $condition = ' where '.$condition;
|
||||
|
||||
return $condition;
|
||||
}
|
||||
|
||||
function _getCondition ($conditions, $column_type, &$output)
|
||||
{
|
||||
$condition = '';
|
||||
|
||||
foreach ($conditions as $val) {
|
||||
$sub_condition = '';
|
||||
|
||||
foreach ($val['condition'] as $v) {
|
||||
if (!isset ($v['value'])) continue;
|
||||
if ($v['value'] === '') continue;
|
||||
if(!in_array(gettype($v['value']), array('string', 'integer', 'double', 'array'))) continue;
|
||||
|
||||
$name = $v['column'];
|
||||
$operation = $v['operation'];
|
||||
$value = $v['value'];
|
||||
$type = $this->getColumnType ($column_type, $name);
|
||||
$pipe = $v['pipe'];
|
||||
$value = $this->getConditionValue ($name, $value, $operation, $type, $column_type);
|
||||
|
||||
if (!$value) {
|
||||
$value = $v['value'];
|
||||
if (strpos ($value, '(')) {
|
||||
$valuetmp = $value;
|
||||
}
|
||||
elseif (strpos ($value, ".") === false) {
|
||||
$valuetmp = $value;
|
||||
}
|
||||
else {
|
||||
$valuetmp = '"'.str_replace('.', '"."', $value).'"';
|
||||
}
|
||||
}
|
||||
else {
|
||||
$tmp = explode('.',$value);
|
||||
|
||||
if (count($tmp)==2) {
|
||||
$table = $tmp[0];
|
||||
$column = $tmp[1];
|
||||
|
||||
if ($column_type[$column] && (in_array ($table, $output->tables) ||
|
||||
array_key_exists($table, $output->tables))) {
|
||||
$valuetmp = sprintf('"%s"."%s"', $table, $column);
|
||||
}
|
||||
else {
|
||||
$valuetmp = $value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$valuetmp = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (strpos ($name, '(') > 0) {
|
||||
$nametmp = $name;
|
||||
}
|
||||
elseif (strpos ($name, ".") === false) {
|
||||
$nametmp = '"'.$name.'"';
|
||||
}
|
||||
else {
|
||||
$nametmp = '"'.str_replace('.', '"."', $name).'"';
|
||||
}
|
||||
$str = $this->getConditionPart ($nametmp, $valuetmp, $operation);
|
||||
if ($sub_condition) $sub_condition .= ' '.$pipe.' ';
|
||||
$sub_condition .= $str;
|
||||
}
|
||||
|
||||
if ($sub_condition) {
|
||||
if ($condition && $val['pipe']) {
|
||||
$condition .= ' '.$val['pipe'].' ';
|
||||
}
|
||||
$condition .= '('.$sub_condition.')';
|
||||
}
|
||||
}
|
||||
|
||||
return $condition;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief handles insertAct
|
||||
**/
|
||||
function _executeInsertAct ($output)
|
||||
function _executeInsertAct($queryObject)
|
||||
{
|
||||
// tables
|
||||
foreach ($output->tables as $val) {
|
||||
$table_list[] = '"'.$this->prefix.$val.'"';
|
||||
}
|
||||
|
||||
// columns
|
||||
foreach ($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
//if ($this->getColumnType ($output->column_type, $name) != 'number')
|
||||
if ($output->column_type[$name] != 'number') {
|
||||
if (!is_null($value)) {
|
||||
$value = "'" . $this->addQuotes($value) ."'";
|
||||
}
|
||||
else {
|
||||
if ($val['notnull']=='notnull') {
|
||||
$value = "''";
|
||||
}
|
||||
else {
|
||||
//$value = 'null';
|
||||
$value = "''";
|
||||
}
|
||||
}
|
||||
}
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = '"'.$name.'"';
|
||||
$value_list[] = $value;
|
||||
}
|
||||
|
||||
$query = sprintf ("insert into %s (%s) values (%s);", implode(',', $table_list), implode(',', $column_list), implode(',', $value_list));
|
||||
$query = $this->getInsertSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
|
||||
$result = $this->_query ($query);
|
||||
if ($result && !$this->transaction_started) {
|
||||
@cubrid_commit ($this->fd);
|
||||
|
|
@ -698,109 +588,29 @@
|
|||
/**
|
||||
* @brief handles updateAct
|
||||
**/
|
||||
function _executeUpdateAct ($output)
|
||||
function _executeUpdateAct($queryObject)
|
||||
{
|
||||
// tables
|
||||
foreach ($output->tables as $key => $val) {
|
||||
$table_list[] = '"'.$this->prefix.$val.'" as "'.$key.'"';
|
||||
}
|
||||
$query = $this->getUpdateSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
$check_click_count = true;
|
||||
$result = $this->_query($query);
|
||||
|
||||
// columns
|
||||
foreach ($output->columns as $key => $val) {
|
||||
if (!isset ($val['value'])) continue;
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
|
||||
if (substr ($value, -2) != '+1' || $output->column_type[$name] != 'number') {
|
||||
$check_click_count = false;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $key; $i++) {
|
||||
// not allows to define the same property repeatedly in a single query in CUBRID
|
||||
if ($output->columns[$i]['name'] == $name) break;
|
||||
}
|
||||
if ($i < $key) continue; // ignore the rest of properties if duplicated property found
|
||||
|
||||
if (strpos ($name, '.') !== false && strpos ($value, '.') !== false) {
|
||||
$column_list[] = $name.' = '.$value;
|
||||
}
|
||||
else {
|
||||
if ($output->column_type[$name] != 'number') {
|
||||
$check_column = false;
|
||||
$value = "'".$this->addQuotes ($value)."'";
|
||||
}
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = sprintf ("\"%s\" = %s", $name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
// conditional clause
|
||||
$condition = $this->getCondition ($output);
|
||||
|
||||
$check_click_count_condition = false;
|
||||
if ($check_click_count) {
|
||||
foreach ($output->conditions as $val) {
|
||||
if ($val['pipe'] == 'or') {
|
||||
$check_click_count_condition = false;
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($val['condition'] as $v) {
|
||||
if ($v['operation'] == 'equal') {
|
||||
$check_click_count_condition = true;
|
||||
}
|
||||
else {
|
||||
if ($v['operation'] == 'in' && !strpos ($v['value'], ',')) {
|
||||
$check_click_count_condition = true;
|
||||
}
|
||||
else {
|
||||
$check_click_count_condition = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($v['pipe'] == 'or') {
|
||||
$check_click_count_condition = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($check_click_count&& $check_click_count_condition && count ($output->tables) == 1 && count ($output->conditions) > 0 && count ($output->groups) == 0 && count ($output->order) == 0) {
|
||||
foreach ($output->columns as $v) {
|
||||
$incr_columns[] = 'incr("'.$v['name'].'")';
|
||||
}
|
||||
|
||||
$query = sprintf ('select %s from %s %s', join (',', $incr_columns), implode(',', $table_list), $condition);
|
||||
}
|
||||
else {
|
||||
$query = sprintf ("update %s set %s %s", implode (',', $table_list), implode (',', $column_list), $condition);
|
||||
}
|
||||
|
||||
$result = $this->_query ($query);
|
||||
if ($result && !$this->transaction_started) @cubrid_commit ($this->fd);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief handles deleteAct
|
||||
**/
|
||||
function _executeDeleteAct ($output)
|
||||
function _executeDeleteAct($queryObject)
|
||||
{
|
||||
// tables
|
||||
foreach ($output->tables as $val) {
|
||||
$table_list[] = '"'.$this->prefix.$val.'"';
|
||||
}
|
||||
$query = $this->getDeleteSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
// Conditional clauses
|
||||
$condition = $this->getCondition ($output);
|
||||
|
||||
$query = sprintf ("delete from %s %s", implode (',',$table_list), $condition);
|
||||
$result = $this->_query ($query);
|
||||
|
||||
if ($result && !$this->transaction_started) @cubrid_commit ($this->fd);
|
||||
|
||||
return $result;
|
||||
|
|
@ -810,454 +620,73 @@
|
|||
* @brief Handle selectAct
|
||||
*
|
||||
* to get a specific page list easily in select statement,\n
|
||||
* a method, navigation, is used
|
||||
* a method, navigation, is used
|
||||
**/
|
||||
function _executeSelectAct ($output)
|
||||
{
|
||||
// tables
|
||||
$table_list = array ();
|
||||
foreach ($output->tables as $key => $val) {
|
||||
$table_list[] = '"'.$this->prefix.$val.'" as "'.$key.'"';
|
||||
}
|
||||
$left_join = array ();
|
||||
// why???
|
||||
$left_tables = (array) $output->left_tables;
|
||||
// TODO Rewrite with Query object as input
|
||||
function _executeSelectAct($queryObject){
|
||||
$query = $this->getSelectSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
foreach ($left_tables as $key => $val) {
|
||||
$condition = $this->_getCondition ($output->left_conditions[$key], $output->column_type, $output);
|
||||
if ($condition) {
|
||||
$left_join[] = $val.' "'.$this->prefix.$output->_tables[$key]. '" "'.$key.'" on ('.$condition.')';
|
||||
}
|
||||
}
|
||||
|
||||
$click_count = array();
|
||||
if(!$output->columns){
|
||||
$output->columns = array(array('name'=>'*'));
|
||||
}
|
||||
|
||||
$column_list = array ();
|
||||
foreach ($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
|
||||
$click_count = '%s';
|
||||
if ($val['click_count'] && count ($output->conditions) > 0) {
|
||||
$click_count = 'incr(%s)';
|
||||
}
|
||||
|
||||
$alias = $val['alias'] ? sprintf ('"%s"', $val['alias']) : null;
|
||||
$_alias = $val['alias'];
|
||||
|
||||
if ($name == '*') {
|
||||
$column_list[] = $name;
|
||||
}
|
||||
elseif (strpos ($name, '.') === false && strpos ($name, '(') === false) {
|
||||
$name = sprintf ($click_count,$name);
|
||||
if ($alias) {
|
||||
$column_list[$alias] = sprintf('"%s" as %s', $name, $alias);
|
||||
}
|
||||
else {
|
||||
$column_list[] = sprintf ('"%s"', $name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (strpos ($name, '.') != false) {
|
||||
list ($prefix, $name) = explode('.', $name);
|
||||
if (($now_matchs = preg_match_all ("/\(/", $prefix, $xtmp)) > 0) {
|
||||
if ($now_matchs == 1) {
|
||||
$tmpval = explode ("(", $prefix);
|
||||
$tmpval[1] = sprintf ('"%s"', $tmpval[1]);
|
||||
$prefix = implode ("(", $tmpval);
|
||||
$tmpval = explode (")", $name);
|
||||
$tmpval[0] = sprintf ('"%s"', $tmpval[0]);
|
||||
$name = implode (")", $tmpval);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$prefix = sprintf ('"%s"', $prefix);
|
||||
$name = ($name == '*') ? $name : sprintf('"%s"',$name);
|
||||
}
|
||||
$xtmp = null;
|
||||
$now_matchs = null;
|
||||
if($alias) $column_list[$_alias] = sprintf ($click_count, sprintf ('%s.%s', $prefix, $name)) . ($alias ? sprintf (' as %s',$alias) : '');
|
||||
else $column_list[] = sprintf ($click_count, sprintf ('%s.%s', $prefix, $name));
|
||||
}
|
||||
elseif (($now_matchs = preg_match_all ("/\(/", $name, $xtmp)) > 0) {
|
||||
if ($now_matchs == 1 && preg_match ("/[a-zA-Z0-9]*\(\*\)/", $name) < 1) {
|
||||
$open_pos = strpos ($name, "(");
|
||||
$close_pos = strpos ($name, ")");
|
||||
|
||||
if (preg_match ("/,/", $name)) {
|
||||
$tmp_func_name = sprintf ('%s', substr ($name, 0, $open_pos));
|
||||
$tmp_params = sprintf ('%s', substr ($name, $open_pos + 1, $close_pos - $open_pos - 1));
|
||||
$tmpval = null;
|
||||
$tmpval = explode (',', $tmp_params);
|
||||
|
||||
foreach ($tmpval as $tmp_param) {
|
||||
$tmp_param_list[] = (!is_numeric ($tmp_param)) ? sprintf ('"%s"', $tmp_param) : $tmp_param;
|
||||
}
|
||||
|
||||
$tmpval = implode (',', $tmp_param_list);
|
||||
$name = sprintf ('%s(%s)', $tmp_func_name, $tmpval);
|
||||
}
|
||||
else {
|
||||
$name = sprintf ('%s("%s")', substr ($name, 0, $open_pos), substr ($name, $open_pos + 1, $close_pos - $open_pos - 1));
|
||||
}
|
||||
}
|
||||
|
||||
if($alias) $column_list[$_alias] = sprintf ($click_count, $name). ($alias ? sprintf (' as %s', $alias) : '');
|
||||
else $column_list[] = sprintf ($click_count, $name);
|
||||
}
|
||||
else {
|
||||
if($alias) $column_list[$_alias] = sprintf($click_count, $name). ($alias ? sprintf(' as %s',$alias) : '');
|
||||
else $column_list[] = sprintf($click_count, $name);
|
||||
}
|
||||
}
|
||||
$columns = implode (',', $column_list);
|
||||
}
|
||||
|
||||
$condition = $this->getCondition ($output);
|
||||
|
||||
$output->column_list = $column_list;
|
||||
if ($output->list_count && $output->page) {
|
||||
return ($this->_getNavigationData($table_list, $columns, $left_join, $condition, $output));
|
||||
}
|
||||
|
||||
if ($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
//if(in_array('list_order', $conditions) || in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if ($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
if (count ($output->groups)) {
|
||||
foreach ($output->groups as $key => $value) {
|
||||
if (strpos ($value, '.')) {
|
||||
$tmp = explode ('.', $value);
|
||||
$tmp[0] = sprintf ('"%s"', $tmp[0]);
|
||||
$tmp[1] = sprintf ('"%s"', $tmp[1]);
|
||||
$value = implode ('.', $tmp);
|
||||
}
|
||||
elseif (strpos ($value, '(')) {
|
||||
$value = $value;
|
||||
}
|
||||
else {
|
||||
$value = sprintf ('"%s"', $value);
|
||||
}
|
||||
$output->groups[$key] = $value;
|
||||
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
if($column_list[$value]) $output->arg_columns[] = $column_list[$value];
|
||||
}
|
||||
}
|
||||
$groupby_query = sprintf ('group by %s', implode(',', $output->groups));
|
||||
}
|
||||
|
||||
|
||||
// apply when using list_count
|
||||
if ($output->list_count['value']) {
|
||||
$start_count = 0;
|
||||
$list_count = $output->list_count['value'];
|
||||
|
||||
if ($output->order) {
|
||||
foreach ($output->order as $val) {
|
||||
if (strpos ($val[0], '.')) {
|
||||
$tmpval = explode ('.', $val[0]);
|
||||
$tmpval[0] = sprintf ('"%s"', $tmpval[0]);
|
||||
$tmpval[1] = sprintf ('"%s"', $tmpval[1]);
|
||||
$val[0] = implode ('.', $tmpval);
|
||||
}
|
||||
elseif (strpos ($val[0], '(')) $val[0] = $val[0];
|
||||
elseif ($val[0] == 'count') $val[0] = 'count (*)';
|
||||
else $val[0] = sprintf ('"%s"', $val[0]);
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
}
|
||||
if (count($index_list))
|
||||
$orderby_query = ' order by '.implode(',', $index_list);
|
||||
$orderby_query = sprintf ('%s for orderby_num() between %d and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
else {
|
||||
if (count ($output->groups)) {
|
||||
$orderby_query = sprintf ('%s having groupby_num() between %d'. ' and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
else {
|
||||
if ($condition) {
|
||||
$orderby_query = sprintf ('%s and inst_num() between %d'. ' and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
else {
|
||||
$orderby_query = sprintf ('%s where inst_num() between %d'. ' and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($output->order) {
|
||||
foreach ($output->order as $val) {
|
||||
if (strpos ($val[0], '.')) {
|
||||
$tmpval = explode ('.', $val[0]);
|
||||
$tmpval[0] = sprintf ('"%s"', $tmpval[0]);
|
||||
$tmpval[1] = sprintf ('"%s"', $tmpval[1]);
|
||||
$val[0] = implode ('.', $tmpval);
|
||||
}
|
||||
elseif (strpos ($val[0], '(')) $val[0] = $val[0];
|
||||
elseif ($val[0] == 'count') $val[0] = 'count (*)';
|
||||
else $val[0] = sprintf ('"%s"', $val[0]);
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
|
||||
if(count($output->arg_columns) && $column_list[$val]) $output->arg_columns[] = $column_list[$key];
|
||||
}
|
||||
|
||||
if (count ($index_list)) {
|
||||
$orderby_query = ' order by '.implode(',', $index_list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
unset($tmpCol);
|
||||
$tmpCol = explode('.', $col);
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[1];
|
||||
|
||||
if(strpos($col,'"')===false && strpos($col,' ')==false) $col = '"'.$col.'"';
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[0].'.'.$col;
|
||||
|
||||
$columns[] = $col;
|
||||
}
|
||||
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
$query = sprintf ("select %s from %s %s %s %s", $columns, implode (',',$table_list), implode (' ',$left_join), $condition, $groupby_query.$orderby_query);
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$result = $this->_query ($query);
|
||||
if ($this->isError ()) return;
|
||||
$data = $this->_fetch ($result);
|
||||
|
||||
$buff = new Object ();
|
||||
$buff->data = $data;
|
||||
|
||||
return $buff;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief displays the current stack trace. Fetch the result
|
||||
**/
|
||||
function backtrace ()
|
||||
{
|
||||
$output = "<div style='text-align: left;'>\n";
|
||||
$output .= "<b>Backtrace:</b><br />\n";
|
||||
$backtrace = debug_backtrace ();
|
||||
|
||||
foreach ($backtrace as $bt) {
|
||||
$args = '';
|
||||
foreach ($bt['args'] as $a) {
|
||||
if (!empty ($args)) {
|
||||
$args .= ', ';
|
||||
}
|
||||
switch (gettype ($a)) {
|
||||
case 'integer':
|
||||
case 'double':
|
||||
$args .= $a;
|
||||
break;
|
||||
case 'string':
|
||||
$a = htmlspecialchars (substr ($a, 0, 64)).
|
||||
((strlen ($a) > 64) ? '...' : '');
|
||||
$args .= "\"$a\"";
|
||||
break;
|
||||
case 'array':
|
||||
$args .= 'Array ('. count ($a).')';
|
||||
break;
|
||||
case 'object':
|
||||
$args .= 'Object ('.get_class ($a).')';
|
||||
break;
|
||||
case 'resource':
|
||||
$args .= 'Resource ('.strstr ($a, '#').')';
|
||||
break;
|
||||
case 'boolean':
|
||||
$args .= $a ? 'True' : 'False';
|
||||
break;
|
||||
case 'NULL':
|
||||
$args .= 'Null';
|
||||
break;
|
||||
default:
|
||||
$args .= 'Unknown';
|
||||
}
|
||||
}
|
||||
$output .= "<br />\n";
|
||||
$output .= "<b>file:</b> ".$bt['line']." - ". $bt['file']."<br />\n";
|
||||
$output .= "<b>call:</b> ".$bt['class']. $bt['type'].$bt['function'].$args."<br />\n";
|
||||
}
|
||||
$output .= "</div>\n";
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief paginates when navigation info exists in the query xml
|
||||
*
|
||||
* it is convenient although its structure is not good .. -_-;
|
||||
**/
|
||||
function _getNavigationData ($table_list, $columns, $left_join, $condition, $output) {
|
||||
require_once (_XE_PATH_.'classes/page/PageHandler.class.php');
|
||||
|
||||
$column_list = $output->column_list;
|
||||
|
||||
$count_condition = count($output->groups) ? sprintf('%s group by %s', $condition, implode(', ', $output->groups)) : $condition;
|
||||
$count_query = sprintf('select count(*) as "count" from %s %s %s', implode(', ', $table_list), implode(' ', $left_join), $count_condition);
|
||||
if (count($output->groups)) {
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
}
|
||||
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$result = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
$list_count = $output->list_count['value'];
|
||||
if (!$list_count) $list_count = 20;
|
||||
$page_count = $output->page_count['value'];
|
||||
if (!$page_count) $page_count = 10;
|
||||
$page = $output->page['value'];
|
||||
if (!$page) $page = 1;
|
||||
|
||||
// total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $list_count) + 1;
|
||||
}
|
||||
else {
|
||||
$total_page = 1;
|
||||
}
|
||||
|
||||
// check the page variables
|
||||
if ($page > $total_page) $page = $total_page;
|
||||
$start_count = ($page - 1) * $list_count;
|
||||
|
||||
if ($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
//if(in_array('list_order', $conditions) || in_array('update_order', $conditions)) {
|
||||
foreach ($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
if (count ($output->groups)) {
|
||||
foreach ($output->groups as $key => $value) {
|
||||
if (strpos ($value, '.')) {
|
||||
$tmp = explode ('.', $value);
|
||||
$tmp[0] = sprintf ('"%s"', $tmp[0]);
|
||||
$tmp[1] = sprintf ('"%s"', $tmp[1]);
|
||||
$value = implode ('.', $tmp);
|
||||
}
|
||||
elseif (strpos ($value, '(')) $value = $value;
|
||||
else $value = sprintf ('"%s"', $value);
|
||||
$output->groups[$key] = $value;
|
||||
}
|
||||
|
||||
$groupby_query = sprintf (' group by %s', implode (',', $output->groups));
|
||||
}
|
||||
|
||||
if ($output->order) {
|
||||
foreach ($output->order as $val) {
|
||||
if (strpos ($val[0], '.')) {
|
||||
$tmpval = explode ('.', $val[0]);
|
||||
$tmpval[0] = sprintf ('"%s"', $tmpval[0]);
|
||||
$tmpval[1] = sprintf ('"%s"', $tmpval[1]);
|
||||
$val[0] = implode ('.', $tmpval);
|
||||
}
|
||||
elseif (strpos ($val[0], '(')) $val[0] = $val[0];
|
||||
elseif ($val[0] == 'count') $val[0] = 'count (*)';
|
||||
else $val[0] = sprintf ('"%s"', $val[0]);
|
||||
$index_list[] = sprintf ('%s %s', $val[0], $val[1]);
|
||||
}
|
||||
|
||||
if (count ($index_list)) {
|
||||
$orderby_query = ' order by '.implode(',', $index_list);
|
||||
}
|
||||
|
||||
$orderby_query = sprintf ('%s for orderby_num() between %d and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
else {
|
||||
if (count($output->groups)) {
|
||||
$orderby_query = sprintf ('%s having groupby_num() between %d and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
else {
|
||||
if ($condition) {
|
||||
$orderby_query = sprintf ('%s and inst_num() between %d and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
else {
|
||||
$orderby_query = sprintf('%s where inst_num() between %d and %d', $orderby_query, $start_count + 1, $list_count + $start_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
unset($tmpCol);
|
||||
$tmpCol = explode('.', $col);
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[1];
|
||||
|
||||
if(strpos($col,'"')===false && strpos($col,' ')==false) $col = '"'.$col.'"';
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[0].'.'.$col;
|
||||
|
||||
$columns[] = $col;
|
||||
}
|
||||
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
$query = sprintf ("select %s from %s %s %s %s", $columns, implode (',',$table_list), implode (' ',$left_join), $condition, $groupby_query.$orderby_query);
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$result = $this->_query ($query);
|
||||
|
||||
if ($this->isError ()) {
|
||||
if ($this->isError ())
|
||||
return $this->queryError($queryObject);
|
||||
else
|
||||
return $this->queryPageLimit($queryObject, $result);
|
||||
}
|
||||
|
||||
function queryError($queryObject){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->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
|
||||
return $buff;
|
||||
}else
|
||||
return;
|
||||
}
|
||||
|
||||
function queryPageLimit($queryObject, $result){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
|
||||
// Total count
|
||||
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString()));
|
||||
if ($queryObject->getGroupByString() != '') {
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
}
|
||||
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$result_count = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result_count);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
// Total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
|
||||
} else $total_page = 1;
|
||||
|
||||
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
|
||||
$data = $this->_fetch($result, $virtual_no);
|
||||
|
||||
$buff = new Object ();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $queryObject->getLimit()->page->getValue();
|
||||
$buff->data = $data;
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page->getValue(), $queryObject->getLimit()->page_count);
|
||||
}else{
|
||||
$data = $this->_fetch($result);
|
||||
$buff = new Object ();
|
||||
$buff->total_count = 0;
|
||||
$buff->total_page = 0;
|
||||
$buff->page = 1;
|
||||
$buff->data = array ();
|
||||
|
||||
$buff->page_navigation = new PageHandler ($total_count, $total_page, $page, $page_count);
|
||||
|
||||
return $buff;
|
||||
$buff->data = $data;
|
||||
}
|
||||
|
||||
$virtual_no = $total_count - ($page - 1) * $list_count;
|
||||
while ($tmp = cubrid_fetch ($result, CUBRID_OBJECT)) {
|
||||
if ($tmp) {
|
||||
foreach ($tmp as $k => $v) {
|
||||
$tmp->{$k} = rtrim($v);
|
||||
}
|
||||
}
|
||||
$data[$virtual_no--] = $tmp;
|
||||
}
|
||||
|
||||
$buff = new Object ();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $page;
|
||||
$buff->data = $data;
|
||||
|
||||
$buff->page_navigation = new PageHandler ($total_count, $total_page, $page, $page_count);
|
||||
|
||||
return $buff;
|
||||
}
|
||||
|
||||
function getParser(){
|
||||
return new DBParser('"');
|
||||
}
|
||||
}
|
||||
|
||||
return new DBCubrid;
|
||||
|
|
|
|||
|
|
@ -291,7 +291,7 @@
|
|||
// Notify to start a query execution
|
||||
$this->actStart($query);
|
||||
// Execute the query statement
|
||||
$result = ibase_query($this->fd, $query);
|
||||
$result = @ibase_query($this->fd, $query);
|
||||
}
|
||||
else {
|
||||
// Notify to start a query execution
|
||||
|
|
@ -592,6 +592,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
if($auto_increment_list)
|
||||
foreach($auto_increment_list as $increment) {
|
||||
$schema = sprintf('CREATE GENERATOR GEN_%s_ID;', $table_name);
|
||||
$output = $this->_query($schema);
|
||||
|
|
@ -616,161 +617,32 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return conditional clause
|
||||
**/
|
||||
function getCondition($output) {
|
||||
if(!$output->conditions) return;
|
||||
$condition = $this->_getCondition($output->conditions,$output->column_type,$output->_tables);
|
||||
if($condition) $condition = ' where '.$condition;
|
||||
return $condition;
|
||||
}
|
||||
|
||||
function getLeftCondition($conditions,$column_type,$tables){
|
||||
return $this->_getCondition($conditions,$column_type,$tables);
|
||||
}
|
||||
|
||||
|
||||
function _getCondition($conditions,$column_type,$tables) {
|
||||
$condition = '';
|
||||
foreach($conditions as $val) {
|
||||
$sub_condition = '';
|
||||
foreach($val['condition'] as $v) {
|
||||
if(!isset($v['value'])) continue;
|
||||
if($v['value'] === '') continue;
|
||||
if(!in_array(gettype($v['value']), array('string', 'integer', 'double'))) continue;
|
||||
|
||||
$name = $v['column'];
|
||||
$operation = $v['operation'];
|
||||
$value = $v['value'];
|
||||
$type = $this->getColumnType($column_type,$name);
|
||||
$pipe = $v['pipe'];
|
||||
|
||||
$value = $this->getConditionValue('"'.$name.'"', $value, $operation, $type, $column_type);
|
||||
if(!$value) $value = $v['value'];
|
||||
|
||||
$name = $this->autoQuotes($name);
|
||||
$value = $this->autoValueQuotes($value, $tables);
|
||||
|
||||
$str = $this->getConditionPart($name, $value, $operation);
|
||||
if($sub_condition) $sub_condition .= ' '.$pipe.' ';
|
||||
$sub_condition .= $str;
|
||||
}
|
||||
if($sub_condition) {
|
||||
if($condition && $val['pipe']) $condition .= ' '.$val['pipe'].' ';
|
||||
$condition .= '('.$sub_condition.')';
|
||||
}
|
||||
}
|
||||
|
||||
return $condition;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle the insertAct
|
||||
**/
|
||||
function _executeInsertAct($output) {
|
||||
// tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '"'.$this->prefix.$val.'"';
|
||||
}
|
||||
// Columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
|
||||
$value = str_replace("'", "`", $value);
|
||||
|
||||
if($output->column_type[$name]=="text" || $output->column_type[$name]=="bigtext"){
|
||||
if(!isset($val['value'])) continue;
|
||||
$blh = ibase_blob_create($this->fd);
|
||||
ibase_blob_add($blh, $value);
|
||||
$value = ibase_blob_close($blh);
|
||||
}
|
||||
else if($output->column_type[$name]!='number') {
|
||||
// if(!$value) $value = 'null';
|
||||
}
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = '"'.$name.'"';
|
||||
$value_list[] = $value;
|
||||
$questions[] = "?";
|
||||
}
|
||||
|
||||
$query = sprintf("insert into %s (%s) values (%s);", implode(',',$table_list), implode(',',$column_list), implode(',', $questions));
|
||||
|
||||
$result = $this->_query($query, $value_list);
|
||||
if(!$this->transaction_started) @ibase_commit($this->fd);
|
||||
return $result;
|
||||
function _executeInsertAct($queryObject) {
|
||||
$query = $this->getInsertSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief handles updateAct
|
||||
**/
|
||||
function _executeUpdateAct($output) {
|
||||
// Tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '"'.$this->prefix.$val.'"';
|
||||
}
|
||||
// Columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
if(!isset($val['value'])) continue;
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
|
||||
$value = str_replace("'", "`", $value);
|
||||
|
||||
if(strpos($name,'.')!==false&&strpos($value,'.')!==false) $column_list[] = $name.' = '.$value;
|
||||
else {
|
||||
if($output->column_type[$name]=="text" || $output->column_type[$name]=="bigtext"){
|
||||
$blh = ibase_blob_create($this->fd);
|
||||
ibase_blob_add($blh, $value);
|
||||
$value = ibase_blob_close($blh);
|
||||
}
|
||||
else if($output->column_type[$name]=='number' ||
|
||||
$output->column_type[$name]=='bignumber' ||
|
||||
$output->column_type[$name]=='float') {
|
||||
// put double-quotes on column name if an expression is entered
|
||||
preg_match("/(?i)[a-z][a-z0-9_]+/", $value, $matches);
|
||||
|
||||
foreach($matches as $key => $val) {
|
||||
$value = str_replace($val, "\"".$val."\"", $value);
|
||||
}
|
||||
|
||||
if($matches != null) {
|
||||
$column_list[] = sprintf("\"%s\" = %s", $name, $value);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$values[] = $value;
|
||||
$column_list[] = sprintf('"%s" = ?', $name);
|
||||
}
|
||||
}
|
||||
// conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("update %s set %s %s;", implode(',',$table_list), implode(',',$column_list), $condition);
|
||||
$result = $this->_query($query, $values);
|
||||
if(!$this->transaction_started) @ibase_commit($this->fd);
|
||||
return $result;
|
||||
function _executeUpdateAct($queryObject) {
|
||||
$query = $this->getUpdateSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief handles deleteAct
|
||||
**/
|
||||
function _executeDeleteAct($output) {
|
||||
// Tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '"'.$this->prefix.$val.'"';
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("delete from %s %s;", implode(',',$table_list), $condition);
|
||||
|
||||
$result = $this->_query($query);
|
||||
if(!$this->transaction_started) @ibase_commit($this->fd);
|
||||
return $result;
|
||||
function _executeDeleteAct($queryObject) {
|
||||
$query = $this->getDeleteSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -779,265 +651,113 @@
|
|||
* In order to get a list of pages easily when selecting \n
|
||||
* it supports a method as navigation
|
||||
**/
|
||||
function _executeSelectAct($output) {
|
||||
// Tables
|
||||
$table_list = array();
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = sprintf("\"%s%s\" as \"%s\"", $this->prefix, $val, $key);
|
||||
}
|
||||
|
||||
$left_join = array();
|
||||
// why???
|
||||
$left_tables= (array)$output->left_tables;
|
||||
|
||||
foreach($left_tables as $key => $val) {
|
||||
$condition = $this->getLeftCondition($output->left_conditions[$key],$output->column_type,$output->_tables);
|
||||
if($condition){
|
||||
$left_join[] = $val . ' "'.$this->prefix.$output->_tables[$key].'" as "'.$key.'" on (' . $condition . ')';
|
||||
}
|
||||
}
|
||||
|
||||
$click_count = array();
|
||||
if(!$output->columns){
|
||||
$output->columns = array(array('name'=>'*'));
|
||||
}
|
||||
|
||||
$column_list = array();
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
if($val['click_count']) $click_count[] = $val['name'];
|
||||
|
||||
if($alias == "")
|
||||
$column_list[] = $this->autoQuotes($name);
|
||||
else
|
||||
$column_list[$alias] = sprintf("%s as \"%s\"", $this->autoQuotes($name), $alias);
|
||||
}
|
||||
$columns = implode(',',$column_list);
|
||||
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$output->column_list = $column_list;
|
||||
if($output->list_count && $output->page) return $this->_getNavigationData($table_list, $columns, $left_join, $condition, $output);
|
||||
// query added in the condition to use an index when ordering by list_order, update_order
|
||||
if($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and "%s" < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where "%s" < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
// apply when using list_count
|
||||
if($output->list_count['value']) $limit = sprintf('FIRST %d', $output->list_count['value']);
|
||||
else $limit = '';
|
||||
|
||||
|
||||
if($output->groups) {
|
||||
foreach($output->groups as $key => $val) {
|
||||
$group_list[] = $this->autoQuotes($val);
|
||||
if($column_list[$val]) $output->arg_columns[] = $column_list[$val];
|
||||
}
|
||||
if(count($group_list)) $groupby_query = sprintf(" group by %s", implode(",",$group_list));
|
||||
}
|
||||
|
||||
if($output->order) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$index_list[] = sprintf("%s %s", $this->autoQuotes($val[0]), $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $orderby_query = sprintf(" order by %s", implode(",",$index_list));
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
if(strpos($col,'"')===false && strpos($col,' ')==false) $columns[] = '"'.$col.'"';
|
||||
else $columns[] = $col;
|
||||
}
|
||||
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
$query = sprintf("select %s from %s %s %s %s", $columns, implode(',',$table_list),implode(' ',$left_join), $condition, $groupby_query.$orderby_query);
|
||||
$query .= ";";
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
if($this->isError()) {
|
||||
if(!$this->transaction_started) @ibase_rollback($this->fd);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = $this->_fetch($result, $output);
|
||||
if(!$this->transaction_started) @ibase_commit($this->fd);
|
||||
|
||||
if(count($click_count)>0 && count($output->conditions)>0){
|
||||
$_query = '';
|
||||
foreach($click_count as $k => $c) $_query .= sprintf(',%s=%s+1 ',$c,$c);
|
||||
$_query = sprintf('update %s set %s %s',implode(',',$table_list), substr($_query,1), $condition);
|
||||
$this->_query($_query);
|
||||
}
|
||||
|
||||
$buff = new Object();
|
||||
$buff->data = $data;
|
||||
return $buff;
|
||||
function _executeSelectAct($queryObject) {
|
||||
$query = $this->getSelectSql($queryObject);
|
||||
|
||||
if(is_a($query, 'Object')) return;
|
||||
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query ($query);
|
||||
|
||||
if ($this->isError ()) return $this->queryError($queryObject);
|
||||
else return $this->queryPageLimit($queryObject, $result);
|
||||
}
|
||||
|
||||
function queryError($queryObject) {
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->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
|
||||
}else
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief paginates when navigation info exists in the query xml
|
||||
*
|
||||
* it is convenient although its structure is not good .. -_-;
|
||||
**/
|
||||
function _getNavigationData($table_list, $columns, $left_join, $condition, $output) {
|
||||
require_once(_XE_PATH_.'classes/page/PageHandler.class.php');
|
||||
|
||||
$column_list = $output->column_list;
|
||||
|
||||
$query_groupby = '';
|
||||
if ($output->groups) {
|
||||
foreach ($output->groups as $key => $val){
|
||||
$group_list[] = $this->autoQuotes($val);
|
||||
if($column_list[$val]) $output->arg_columns[] = $column_list[$val];
|
||||
}
|
||||
if (count($group_list)) $query_groupby = sprintf(" GROUP BY %s", implode(", ", $group_list));
|
||||
function queryPageLimit($queryObject, $result) {
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
|
||||
// Total count
|
||||
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE ' . $queryObject->getWhereString()));
|
||||
if ($queryObject->getGroupByString() != '') {
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
}
|
||||
|
||||
/*
|
||||
// modified to get the number of rows by SELECT query with group by clause
|
||||
// activate the commented codes when you confirm it works correctly
|
||||
//
|
||||
$count_condition = strlen($query_groupby) ? sprintf('%s group by %s', $condition, $query_groupby) : $condition;
|
||||
$total_count = $this->getCountCache($output->tables, $count_condition);
|
||||
if($total_count === false) {
|
||||
$count_query = sprintf('select count(*) as "count" from %s %s %s', implode(', ', $table_list), implode(' ', $left_join), $count_condition);
|
||||
if (count($output->groups))
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
$result = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result);
|
||||
$total_count = (int)$count_output->count;
|
||||
$this->putCountCache($output->tables, $count_condition, $total_count);
|
||||
}
|
||||
*/
|
||||
// total number of rows
|
||||
$count_query = sprintf("select count(*) as \"count\" from %s %s %s", implode(',',$table_list),implode(' ',$left_join), $condition);
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id . ' count(*)'):'';
|
||||
$result = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result);
|
||||
if(!$this->transaction_started) @ibase_commit($this->fd);
|
||||
$count_query .= ( __DEBUG_QUERY__ & 1 && $output->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
|
||||
$result_count = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result_count);
|
||||
$total_count = (int) $count_output->count;
|
||||
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
$list_count = $output->list_count['value'];
|
||||
if(!$list_count) $list_count = 20;
|
||||
$page_count = $output->page_count['value'];
|
||||
if(!$page_count) $page_count = 10;
|
||||
$page = $output->page['value'];
|
||||
if(!$page) $page = 1;
|
||||
// total pages
|
||||
if($total_count) $total_page = (int)( ($total_count-1) / $list_count) + 1;
|
||||
else $total_page = 1;
|
||||
// check the page variables
|
||||
if($page > $total_page) $page = $total_page;
|
||||
$start_count = ($page-1)*$list_count;
|
||||
// query added in the condition to use an index when ordering by list_order, update_order
|
||||
if($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and "%s" < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where "%s" < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$limit = sprintf('FIRST %d SKIP %d ', $list_count, $start_count);
|
||||
|
||||
|
||||
if($output->order) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$index_list[] = sprintf("%s %s", $this->autoQuotes($val[0]), $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $orderby_query = sprintf(" ORDER BY %s", implode(",",$index_list));
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
if(strpos($col,'"')===false && strpos($col,' ')==false) $columns[] = '"'.$col.'"';
|
||||
else $columns[] = $col;
|
||||
}
|
||||
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
$query = sprintf('SELECT %s %s FROM %s %s %s, %s', $limit, $columns, implode(',',$table_list), implode(' ',$left_join), $condition, $groupby_query.$orderby_query);
|
||||
$query .= ";";
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
if($this->isError()) {
|
||||
if(!$this->transaction_started) @ibase_rollback($this->fd);
|
||||
|
||||
$buff = new Object();
|
||||
$buff->total_count = 0;
|
||||
$buff->total_page = 0;
|
||||
$buff->page = 1;
|
||||
$buff->data = array();
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
}
|
||||
|
||||
$virtual_no = $total_count - ($page-1)*$list_count;
|
||||
while($tmp = ibase_fetch_object($result)) {
|
||||
foreach($tmp as $key => $val){
|
||||
$type = $output->column_type[$key];
|
||||
// $key value is an alias when type value is null. get type by finding the actual column name
|
||||
if($type == null && $output->columns && count($output->columns)) {
|
||||
foreach($output->columns as $cols) {
|
||||
if($cols['alias'] == $key) {
|
||||
// checks if the format is table.column or a regular expression
|
||||
preg_match("/\w+[.](\w+)/", $cols['name'], $matches);
|
||||
if($matches) {
|
||||
$type = $output->column_type[$matches[1]];
|
||||
}
|
||||
else {
|
||||
$type = $output->column_type[$cols['name']];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(($type == "text" || $type == "bigtext") && $tmp->{$key}) {
|
||||
$blob_data = ibase_blob_info($tmp->{$key});
|
||||
$blob_hndl = ibase_blob_open($tmp->{$key});
|
||||
$tmp->{$key} = ibase_blob_get($blob_hndl, $blob_data[0]);
|
||||
ibase_blob_close($blob_hndl);
|
||||
}
|
||||
}
|
||||
// Total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
|
||||
} else
|
||||
$total_page = 1;
|
||||
|
||||
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
|
||||
while ($tmp = ibase_fetch_object($result))
|
||||
$data[$virtual_no--] = $tmp;
|
||||
}
|
||||
|
||||
if (!$this->transaction_started)
|
||||
@ibase_commit($this->fd);
|
||||
|
||||
if(!$this->transaction_started) @ibase_commit($this->fd);
|
||||
|
||||
$buff = new Object();
|
||||
$buff = new Object ();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $page;
|
||||
$buff->page = $queryObject->getLimit()->page->getValue();
|
||||
$buff->data = $data;
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page->getValue(), $queryObject->getLimit()->page_count);
|
||||
}else {
|
||||
$data = $this->_fetch($result);
|
||||
$buff = new Object ();
|
||||
$buff->data = $data;
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
}
|
||||
return $buff;
|
||||
}
|
||||
|
||||
function getParser() {
|
||||
return new DBParser('"');
|
||||
}
|
||||
|
||||
function getSelectSql($query, $with_values = true) {
|
||||
|
||||
if ($query->getLimit()) {
|
||||
$list_count = $query->getLimit()->list_count->getValue();
|
||||
if(!$query->getLimit()->page) $page = 1;
|
||||
else $page = $query->getLimit()->page->getValue();
|
||||
|
||||
$start_count = ($page - 1) * $list_count;
|
||||
$limit = sprintf('SELECT FIRST %d SKIP %d ', $list_count, $start_count);
|
||||
}
|
||||
|
||||
$select = $query->getSelectString($with_values);
|
||||
if ($select == '')
|
||||
return new Object(-1, "Invalid query");
|
||||
|
||||
if ($query->getLimit())
|
||||
$select = $limit . ' ' . $select;
|
||||
else
|
||||
$select = 'SELECT ' . $select;
|
||||
$from = $query->getFromString($with_values);
|
||||
if ($from == '')
|
||||
return new Object(-1, "Invalid query");
|
||||
$from = ' FROM ' . $from;
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if ($where != '')
|
||||
$where = ' WHERE ' . $where;
|
||||
|
||||
$groupBy = $query->getGroupByString();
|
||||
if ($groupBy != '')
|
||||
$groupBy = ' GROUP BY ' . $groupBy;
|
||||
|
||||
$orderBy = $query->getOrderByString();
|
||||
if ($orderBy != '')
|
||||
$orderBy = ' ORDER BY ' . $orderBy;
|
||||
|
||||
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return new DBFireBird;
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
var $prefix = 'xe'; // / <prefix of XE tables(One more XE can be installed on a single DB)
|
||||
var $param = array();
|
||||
var $comment_syntax = '/* %s */';
|
||||
|
||||
|
||||
/**
|
||||
* @brief column type used in mssql
|
||||
*
|
||||
|
|
@ -42,7 +42,7 @@
|
|||
$this->_setDBInfo();
|
||||
$this->_connect();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief create an instance of this class
|
||||
*/
|
||||
|
|
@ -70,7 +70,7 @@
|
|||
$this->password = $db_info->db_password;
|
||||
$this->database = $db_info->db_database;
|
||||
$this->prefix = $db_info->db_table_prefix;
|
||||
|
||||
|
||||
if(!substr($this->prefix,-1)!='_') $this->prefix .= '_';
|
||||
}
|
||||
|
||||
|
|
@ -85,12 +85,11 @@
|
|||
//sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
|
||||
//sqlsrv_configure( 'LogSubsystems', SQLSRV_LOG_SYSTEM_ALL );
|
||||
|
||||
$this->conn = sqlsrv_connect( $this->hostname,
|
||||
$this->conn = sqlsrv_connect( $this->hostname,
|
||||
array( 'Database' => $this->database,'UID'=>$this->userid,'PWD'=>$this->password ));
|
||||
|
||||
|
||||
// Check connections
|
||||
|
||||
|
||||
// Check connections
|
||||
if($this->conn){
|
||||
$this->is_connected = true;
|
||||
$this->password = md5($this->password);
|
||||
|
|
@ -104,7 +103,7 @@
|
|||
**/
|
||||
function close() {
|
||||
if($this->is_connected == false) return;
|
||||
|
||||
|
||||
$this->commit();
|
||||
sqlsrv_close($this->conn);
|
||||
$this->conn = null;
|
||||
|
|
@ -113,10 +112,11 @@
|
|||
/**
|
||||
* @brief handles quatation of the string variables from the query
|
||||
**/
|
||||
// TODO See what to do about this
|
||||
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 = str_replace("'","''",$string);
|
||||
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
|
|
@ -126,7 +126,7 @@
|
|||
function begin() {
|
||||
if($this->is_connected == false || $this->transaction_started) return;
|
||||
if(sqlsrv_begin_transaction( $this->conn ) === false) return;
|
||||
|
||||
|
||||
$this->transaction_started = true;
|
||||
}
|
||||
|
||||
|
|
@ -135,7 +135,7 @@
|
|||
**/
|
||||
function rollback() {
|
||||
if($this->is_connected == false || !$this->transaction_started) return;
|
||||
|
||||
|
||||
$this->transaction_started = false;
|
||||
sqlsrv_rollback( $this->conn );
|
||||
}
|
||||
|
|
@ -145,8 +145,8 @@
|
|||
**/
|
||||
function commit($force = false) {
|
||||
if(!$force && ($this->is_connected == false || !$this->transaction_started)) return;
|
||||
|
||||
$this->transaction_started = false;
|
||||
|
||||
$this->transaction_started = false;
|
||||
sqlsrv_commit( $this->conn );
|
||||
}
|
||||
|
||||
|
|
@ -159,24 +159,40 @@
|
|||
* object if a row returned \n
|
||||
* return\n
|
||||
**/
|
||||
|
||||
// TODO Support array arguments in sql server
|
||||
/*
|
||||
* $query_emp="select name from employee where id in (?,?,?)";
|
||||
$params_emp= Array(1,2,3);
|
||||
$res_emp = sqlsrv_query($conn, $query_emp, $params_emp);
|
||||
*
|
||||
*/
|
||||
|
||||
function _query($query) {
|
||||
if($this->is_connected == false || !$query) return;
|
||||
|
||||
$_param = array();
|
||||
|
||||
|
||||
if(count($this->param)){
|
||||
foreach($this->param as $k => $o){
|
||||
if($o['type'] == 'number'){
|
||||
$_param[] = &$o['value'];
|
||||
if($o->getType() == 'number'){
|
||||
$value = $o->getUnescapedValue();
|
||||
if(is_array($value)) $_param = array_merge($_param, $value);
|
||||
else $_param[] = $o->getUnescapedValue();
|
||||
}else{
|
||||
$_param[] = array(&$o['value'], SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'));
|
||||
$value = $o->getUnescapedValue();
|
||||
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'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Notify to start a query execution
|
||||
$this->actStart($query);
|
||||
|
||||
|
||||
// Run the query statement
|
||||
$result = false;
|
||||
if(count($_param)){
|
||||
|
|
@ -185,9 +201,9 @@
|
|||
$result = @sqlsrv_query($this->conn, $query);
|
||||
}
|
||||
// Error Check
|
||||
|
||||
|
||||
if(!$result) $this->setError(print_r(sqlsrv_errors(),true));
|
||||
|
||||
|
||||
// Notify to complete a query execution
|
||||
$this->actFinish();
|
||||
$this->param = array();
|
||||
|
|
@ -198,23 +214,27 @@
|
|||
/**
|
||||
* @brief Fetch results
|
||||
**/
|
||||
function _fetch($result) {
|
||||
function _fetch($result, $arrayIndexEndValue = NULL) {
|
||||
if(!$this->isConnected() || $this->isError() || !$result) return;
|
||||
|
||||
|
||||
$c = sqlsrv_num_fields($result);
|
||||
$m = null;
|
||||
$output = array();
|
||||
|
||||
|
||||
while(sqlsrv_fetch($result)){
|
||||
if(!$m) $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' ));
|
||||
$row->{$m[$i]['Name']} = sqlsrv_get_field( $result, $i, SQLSRV_PHPTYPE_STRING( 'utf-8' ));
|
||||
}
|
||||
$output[] = $row;
|
||||
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $row;
|
||||
else $output[] = $row;
|
||||
}
|
||||
|
||||
if(count($output)==1) return $output[0];
|
||||
if(count($output)==1) {
|
||||
if(isset($arrayIndexEndValue)) return $output;
|
||||
else return $output[0];
|
||||
}
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
|
@ -225,12 +245,12 @@
|
|||
function getNextSequence() {
|
||||
$query = sprintf("insert into %ssequence (seq) values (ident_incr('%ssequence'))", $this->prefix, $this->prefix);
|
||||
$this->_query($query);
|
||||
|
||||
|
||||
$query = sprintf("select ident_current('%ssequence')+1 as sequence", $this->prefix);
|
||||
$result = $this->_query($query);
|
||||
$tmp = $this->_fetch($result);
|
||||
|
||||
|
||||
|
||||
return $tmp->sequence;
|
||||
}
|
||||
|
||||
|
|
@ -239,9 +259,9 @@
|
|||
**/
|
||||
function isTableExists($target_name) {
|
||||
$query = sprintf("select name from sysobjects where name = '%s%s' and xtype='U'", $this->prefix, $this->addQuotes($target_name));
|
||||
$result = $this->_query($query);
|
||||
$result = $this->_query($query);
|
||||
$tmp = $this->_fetch($result);
|
||||
|
||||
|
||||
if(!$tmp) return false;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -386,11 +406,11 @@
|
|||
if($unique) $unique_list[$unique][] = $name;
|
||||
else if($index) $index_list[$index][] = $name;
|
||||
}
|
||||
|
||||
|
||||
$schema = sprintf('create table [%s] (xe_seq int identity(1,1),%s%s)', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"));
|
||||
$output = $this->_query($schema);
|
||||
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).']');
|
||||
|
|
@ -408,506 +428,143 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return conditional clause
|
||||
**/
|
||||
function getCondition($output) {
|
||||
if(!$output->conditions) return;
|
||||
$condition = $this->_getCondition($output->conditions,$output->column_type);
|
||||
if($condition) $condition = ' where '.$condition;
|
||||
return $condition;
|
||||
}
|
||||
|
||||
function getLeftCondition($conditions,$column_type){
|
||||
return $this->_getCondition($conditions,$column_type);
|
||||
}
|
||||
|
||||
|
||||
function _getCondition($conditions,$column_type) {
|
||||
$condition = '';
|
||||
|
||||
foreach($conditions as $val) {
|
||||
$sub_condition = '';
|
||||
foreach($val['condition'] as $v) {
|
||||
if(!isset($v['value'])) continue;
|
||||
if($v['value'] === '') continue;
|
||||
if(!in_array(gettype($v['value']), array('string', 'integer', 'double'))) continue;
|
||||
|
||||
$name = $v['column'];
|
||||
if(preg_match('/^substr\(/i',$name)) $name = preg_replace('/^substr\(/i','substring(',$name);
|
||||
$operation = $v['operation'];
|
||||
$value = $v['value'];
|
||||
|
||||
$type = $this->getColumnType($column_type,$name);
|
||||
$pipe = $v['pipe'];
|
||||
|
||||
$value = $this->getConditionValue($name, $value, $operation, $type, $column_type);
|
||||
if(!$value) $value = $v['value'];
|
||||
$str = $this->getConditionPart($name, $value, $operation);
|
||||
if($sub_condition) $sub_condition .= ' '.$pipe.' ';
|
||||
$sub_condition .= $str;
|
||||
}
|
||||
if($sub_condition) {
|
||||
if($condition && $val['pipe']) $condition .= ' '.$val['pipe'].' ';
|
||||
$condition .= '('.$sub_condition.')';
|
||||
}
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
|
||||
|
||||
function getConditionValue($name, $value, $operation, $type, $column_type) {
|
||||
|
||||
if($type == 'number') {
|
||||
if(strpos($value,',')===false && strpos($value,'(')===false){
|
||||
|
||||
if(is_integer($value)){
|
||||
$this->param[] = array('type'=>'number','value'=>(int)$value);
|
||||
return '?';
|
||||
}else{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(strpos($name,'.')!==false&&strpos($value,'.')!==false) {
|
||||
list($table_name, $column_name) = explode('.',$value);
|
||||
if($column_type[$column_name]){
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
switch($operation) {
|
||||
case 'like_prefix' :
|
||||
$value = preg_replace('/(^\'|\'$){1}/','',$value);
|
||||
$this->param[] = array('type'=>$column_type[$name],'value'=>$value);
|
||||
|
||||
$value = "? + '%'";
|
||||
break;
|
||||
case 'like_tail' :
|
||||
$value = preg_replace('/(^\'|\'$){1}/','',$value);
|
||||
$this->param[] = array('type'=>$column_type[$name],'value'=>$value);
|
||||
|
||||
$value = "'%' + ?";
|
||||
break;
|
||||
case 'like' :
|
||||
$value = preg_replace('/(^\'|\'$){1}/','',$value);
|
||||
$this->param[] = array('type'=>$column_type[$name],'value'=>$value);
|
||||
|
||||
$value = "'%' + ? + '%'";
|
||||
break;
|
||||
case 'notin' :
|
||||
preg_match_all('/,?\'([^\']*)\'/',$value,$match);
|
||||
$val = array();
|
||||
foreach($match[1] as $k => $v){
|
||||
$this->param[] = array('type'=>$column_type[$name],'value'=>trim($v));
|
||||
$val[] ='?';
|
||||
}
|
||||
$value = join(',',$val);
|
||||
break;
|
||||
case 'in' :
|
||||
preg_match_all('/,?\'([^\']*)\'/',$value,$match);
|
||||
$val = array();
|
||||
foreach($match[1] as $k => $v){
|
||||
$this->param[] = array('type'=>$column_type[$name],'value'=>trim($v));
|
||||
$val[] ='?';
|
||||
}
|
||||
$value = join(',',$val);
|
||||
break;
|
||||
default:
|
||||
$value = preg_replace('/(^\'|\'$){1}/','',$value);
|
||||
$this->param[] = array('type'=>$column_type[$name],'value'=>$value);
|
||||
$value = '?';
|
||||
break;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle the insertAct
|
||||
**/
|
||||
function _executeInsertAct($output) {
|
||||
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '['.$this->prefix.$val.']';
|
||||
}
|
||||
// List columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
|
||||
if($output->column_type[$name]!='number') {
|
||||
$value = $this->addQuotes($value);
|
||||
if(!$value) $value = '';
|
||||
} elseif(is_numeric($value)){
|
||||
if(!$value) $value = '';
|
||||
$value = (int)$value;
|
||||
} elseif(!$value){
|
||||
$value = '';
|
||||
}
|
||||
// sql injection 문제로 xml 선언이 number인 경우이면서 넘어온 값이 숫자형이 아니면 숫자형으로 강제 형변환
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = '['.$name.']';
|
||||
$value_list[] = '?';
|
||||
|
||||
$this->param[] = array('type'=>$output->column_type[$name], 'value'=>$value);
|
||||
}
|
||||
|
||||
$query = sprintf("insert into %s (%s) values (%s);", implode(',',$table_list), implode(',',$column_list), implode(',', $value_list));
|
||||
|
||||
// TODO Lookup _filterNumber against sql injection - see if it is still needed and how to integrate
|
||||
function _executeInsertAct($queryObject) {
|
||||
$query = $this->getInsertSql($queryObject, false);
|
||||
$this->param = $queryObject->getArguments();
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle updateAct
|
||||
**/
|
||||
function _executeUpdateAct($output) {
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '['.$this->prefix.$val.']';
|
||||
}
|
||||
|
||||
// List columns
|
||||
|
||||
foreach($output->columns as $key => $val) {
|
||||
if(!isset($val['value'])) continue;
|
||||
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
if(strpos($name,'.')!==false&&strpos($value,'.')!==false){
|
||||
$column_list[] = $name.' = '.$value;
|
||||
} else {
|
||||
if($output->column_type[$name]!='number'){
|
||||
$value = $this->addQuotes($value);
|
||||
if(!$value) $value = '';
|
||||
|
||||
$this->param[] = array('type'=>$output->column_type[$name], 'value'=>$value);
|
||||
$column_list[] = sprintf("[%s] = ?", $name);
|
||||
}elseif(!$value || is_numeric($value)){
|
||||
$value = (int)$value;
|
||||
|
||||
$this->param[] = array('type'=>$output->column_type[$name], 'value'=>$value);
|
||||
$column_list[] = sprintf("[%s] = ?", $name);
|
||||
}else{
|
||||
if(!$value) $value = '';
|
||||
$this->_filterNumber($value);
|
||||
$column_list[] = sprintf("[%s] = %s", $name, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("update %s set %s %s", implode(',',$table_list), implode(',',$column_list), $condition);
|
||||
|
||||
function _executeUpdateAct($queryObject) {
|
||||
$query = $this->getUpdateSql($queryObject, false);
|
||||
$this->param = $queryObject->getArguments();
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle deleteAct
|
||||
**/
|
||||
function _executeDeleteAct($output) {
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '['.$this->prefix.$val.']';
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("delete from %s %s", implode(',',$table_list), $condition);
|
||||
|
||||
function _executeDeleteAct($queryObject) {
|
||||
$query = $this->getDeleteSql($queryObject, false);
|
||||
$this->param = $queryObject->getArguments();
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
function getSelectSql($query){
|
||||
$with_value = false;
|
||||
|
||||
//$limitOffset = $query->getLimit()->getOffset();
|
||||
//if($limitOffset)
|
||||
// TODO Implement Limit with offset with subquery
|
||||
$limit = '';$limitCount = '';
|
||||
if($query->getLimit())
|
||||
$limitCount = $query->getLimit()->getLimit();
|
||||
if($limitCount != '') $limit = 'SELECT TOP ' . $limitCount;
|
||||
|
||||
$select = $query->getSelectString($with_values);
|
||||
if($select == '') return new Object(-1, "Invalid query");
|
||||
if($limit != '')
|
||||
$select = $limit.' '.$select;
|
||||
else
|
||||
$select = 'SELECT ' .$select;
|
||||
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$from = ' FROM '.$from;
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
|
||||
$groupBy = $query->getGroupByString();
|
||||
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
|
||||
|
||||
$orderBy = $query->getOrderByString();
|
||||
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
|
||||
|
||||
|
||||
|
||||
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle selectAct
|
||||
*
|
||||
* In order to get a list of pages easily when selecting \n
|
||||
* it supports a method as navigation
|
||||
**/
|
||||
function _executeSelectAct($output) {
|
||||
// List tables
|
||||
$table_list = array();
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '['.$this->prefix.$val.'] as '.$key;
|
||||
}
|
||||
function _executeSelectAct($queryObject) {
|
||||
$query = $this->getSelectSql($queryObject);
|
||||
|
||||
$left_join = array();
|
||||
// why???
|
||||
$left_tables= (array)$output->left_tables;
|
||||
if(strpos($query, "substr")) $query = str_replace ("substr", "substring", $query);
|
||||
|
||||
foreach($left_tables as $key => $val) {
|
||||
$condition = $this->_getCondition($output->left_conditions[$key],$output->column_type);
|
||||
if($condition){
|
||||
$left_join[] = $val . ' ['.$this->prefix.$output->_tables[$key].'] as '.$key . ' on (' . $condition . ')';
|
||||
}
|
||||
}
|
||||
// TODO Decide if we continue to pass parameters like this
|
||||
$this->param = $queryObject->getArguments();
|
||||
|
||||
$click_count = array();
|
||||
if(!$output->columns){
|
||||
$output->columns = array(array('name'=>'*'));
|
||||
}
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
|
||||
$column_list = array();
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
if(preg_match('/^substr\(/i',$name)) $name = preg_replace('/^substr\(/i','substring(',$name);
|
||||
$alias = $val['alias'];
|
||||
if($val['click_count']) $click_count[] = $val['name'];
|
||||
|
||||
if(substr($name,-1) == '*') {
|
||||
$column_list[] = $name;
|
||||
} elseif(strpos($name,'.')===false && strpos($name,'(')===false) {
|
||||
if($alias) $column_list[$alias] = sprintf('[%s] as [%s]', $name, $alias);
|
||||
else $column_list[] = sprintf('[%s]',$name);
|
||||
} else {
|
||||
if($alias) $column_list[$alias] = sprintf('%s as [%s]', $name, $alias);
|
||||
else $column_list[] = sprintf('%s',$name);
|
||||
}
|
||||
}
|
||||
$columns = implode(',',$column_list);
|
||||
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$output->column_list = $column_list;
|
||||
if($output->list_count && $output->page) return $this->_getNavigationData($table_list, $columns, $left_join, $condition, $output);
|
||||
// Add a condition to use an index when sorting in order by list_order, update_order
|
||||
if($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(count($output->groups)){
|
||||
foreach($output->groups as $k => $v ){
|
||||
if(preg_match('/^substr\(/i',$v)) $output->groups[$k] = preg_replace('/^substr\(/i','substring(',$v);
|
||||
if($column_list[$v]) $output->arg_columns[] = $column_list[$v];
|
||||
}
|
||||
$groupby_query = sprintf(' group by %s', implode(',',$output->groups));
|
||||
}
|
||||
|
||||
if($output->order && !preg_match('/count\(\*\)/i',$columns) ) {
|
||||
foreach($output->order as $key => $val) {
|
||||
if(preg_match('/^substr\(/i',$val[0])) $name = preg_replace('/^substr\(/i','substring(',$val[0]);
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $orderby_query = ' order by '.implode(',',$index_list);
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
unset($tmpCol);
|
||||
$tmpCol = explode('.', $col);
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[1];
|
||||
|
||||
if(strpos($col,'[')===false && strpos($col,' ')==false) $col = '['.$col.']';
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[0].'.'.$col;
|
||||
|
||||
$columns[] = $col;
|
||||
}
|
||||
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
$query = sprintf("%s from %s %s %s %s", $columns, implode(',',$table_list),implode(' ',$left_join), $condition, $groupby_query.$orderby_query);
|
||||
// Apply when using list_count
|
||||
if($output->list_count['value']) $query = sprintf('select top %d %s', $output->list_count['value'], $query);
|
||||
else $query = "select ".$query;
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
if($this->isError()) return;
|
||||
|
||||
if(count($click_count)>0 && count($output->conditions)>0){
|
||||
$_query = '';
|
||||
foreach($click_count as $k => $c) $_query .= sprintf(',%s=%s+1 ',$c,$c);
|
||||
$_query = sprintf('update %s set %s %s',implode(',',$table_list), substr($_query,1), $condition);
|
||||
$this->_query($_query);
|
||||
}
|
||||
|
||||
$data = $this->_fetch($result);
|
||||
|
||||
$buff = new Object();
|
||||
$buff->data = $data;
|
||||
return $buff;
|
||||
if ($this->isError ()) return $this->queryError($queryObject);
|
||||
else return $this->queryPageLimit($queryObject, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Paging is handled if navigation information exists in the query xml
|
||||
*
|
||||
* It is quite convenient although its structure is not good at all .. -_-;
|
||||
**/
|
||||
function _getNavigationData($table_list, $columns, $left_join, $condition, $output) {
|
||||
require_once(_XE_PATH_.'classes/page/PageHandler.class.php');
|
||||
function getParser(){
|
||||
return new DBParser("[", "]");
|
||||
}
|
||||
|
||||
$column_list = $output->column_list;
|
||||
function queryError($queryObject){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->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
|
||||
return $buff;
|
||||
}else
|
||||
return;
|
||||
}
|
||||
|
||||
// Get a total count
|
||||
if(count($output->groups)){
|
||||
foreach($output->groups as $k => $v ){
|
||||
if(preg_match('/^substr\(/i',$v)) $output->groups[$k] = preg_replace('/^substr\(/i','substring(',$v);
|
||||
if($column_list[$v]) $output->arg_columns[] = $column_list[$v];
|
||||
function queryPageLimit($queryObject, $result){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
|
||||
// Total count
|
||||
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString()));
|
||||
if ($queryObject->getGroupByString() != '') {
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
}
|
||||
$count_condition = sprintf('%s group by %s', $condition, implode(', ', $output->groups));
|
||||
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$result_count = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result_count);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
// Total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
|
||||
} else $total_page = 1;
|
||||
|
||||
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
|
||||
$data = $this->_fetch($result, $virtual_no);
|
||||
|
||||
$buff = new Object ();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $queryObject->getLimit()->page->getValue();
|
||||
$buff->data = $data;
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page, $queryObject->getLimit()->page_count);
|
||||
}else{
|
||||
$count_condition = $condition;
|
||||
$data = $this->_fetch($result);
|
||||
$buff = new Object ();
|
||||
$buff->data = $data;
|
||||
}
|
||||
|
||||
$count_query = sprintf("select count(*) as count from %s %s %s", implode(', ', $table_list), implode(' ', $left_join), $count_condition);
|
||||
if (count($output->groups)) $count_query = sprintf('select count(*) as count from (%s) xet', $count_query);
|
||||
|
||||
$param = $this->param;
|
||||
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id . ' count(*)'):'';
|
||||
$result = $this->_query($count_query);
|
||||
|
||||
$this->param = $param;
|
||||
$count_output = $this->_fetch($result);
|
||||
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
$list_count = $output->list_count['value'];
|
||||
if(!$list_count) $list_count = 20;
|
||||
$page_count = $output->page_count['value'];
|
||||
if(!$page_count) $page_count = 10;
|
||||
$page = $output->page['value'];
|
||||
if(!$page) $page = 1;
|
||||
// Get a total page
|
||||
if($total_count) $total_page = (int)( ($total_count-1) / $list_count) + 1;
|
||||
else $total_page = 1;
|
||||
// Check Page variables
|
||||
if($page > $total_page) $page = $total_page;
|
||||
$start_count = ($page-1)*$list_count;
|
||||
// Add a condition to use an index when sorting in order by list_order, update_order
|
||||
$conditions = $this->getConditionList($output);
|
||||
if($output->order) {
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add group by clause
|
||||
if(count($output->groups)){
|
||||
foreach($output->groups as $k => $v ){
|
||||
if(preg_match('/^substr\(/i',$v)) $output->groups[$k] = preg_replace('/^substr\(/i','substring(',$v);
|
||||
if($column_list[$v]) $output->arg_columns[] = $column_list[$v];
|
||||
}
|
||||
|
||||
$group = sprintf('group by %s', implode(',',$output->groups));
|
||||
}
|
||||
|
||||
// Add order by clause
|
||||
$order_targets = array();
|
||||
if($output->order) {
|
||||
foreach($output->order as $key => $val) {
|
||||
if(preg_match('/^substr\(/i',$val[0])) $name = preg_replace('/^substr\(/i','substring(',$val[0]);
|
||||
$order_targets[$val[0]] = $val[1];
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $order .= 'order by '.implode(',',$index_list);
|
||||
}
|
||||
if(!count($order_targets)) {
|
||||
if(in_array('list_order',$conditions)) $order_targets['list_order'] = 'asc';
|
||||
else $order_targets['xe_seq'] = 'desc';
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
unset($tmpCol);
|
||||
$tmpCol = explode('.', $col);
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[1];
|
||||
|
||||
if(strpos($col,'[')===false && strpos($col,' ')==false) $col = '['.$col.']';
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[0].'.'.$col;
|
||||
|
||||
$columns[] = $col;
|
||||
}
|
||||
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
if($start_count<1) {
|
||||
$query = sprintf('select top %d %s from %s %s %s %s %s', $list_count, $columns, implode(',',$table_list), implode(' ',$left_join), $condition, $group, $order);
|
||||
|
||||
} else {
|
||||
foreach($order_targets as $k => $v) {
|
||||
$first_columns[] = sprintf('%s(%s) as %s', $v=='asc'?'max':'min', $k, $k);
|
||||
$first_sub_columns[] = $k;
|
||||
}
|
||||
|
||||
// Fetch values to sort
|
||||
$param = $this->param;
|
||||
$first_query = sprintf("select %s from (select top %d %s from %s %s %s %s %s) xet", implode(',',$first_columns), $start_count, implode(',',$first_sub_columns), implode(',',$table_list), implode(' ',$left_join), $condition, $group, $order);
|
||||
$result = $this->_query($first_query);
|
||||
$this->param = $param;
|
||||
$tmp = $this->_fetch($result);
|
||||
|
||||
|
||||
|
||||
// Re-execute a query by using fetched values
|
||||
$sub_cond = array();
|
||||
foreach($order_targets as $k => $v) {
|
||||
$sub_cond[] = sprintf("%s %s '%s'", $k, $v=='asc'?'>':'<', $tmp->{$k});
|
||||
}
|
||||
$sub_condition = ' and( '.implode(' and ',$sub_cond).' )';
|
||||
|
||||
if($condition) $condition .= $sub_condition;
|
||||
else $condition = ' where '.$sub_condition;
|
||||
$query = sprintf('select top %d %s from %s %s %s %s %s', $list_count, $columns, implode(',',$table_list), implode(' ',$left_join), $condition, $group, $order);
|
||||
}
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
|
||||
if($this->isError()) {
|
||||
$buff = new Object();
|
||||
$buff->total_count = 0;
|
||||
$buff->total_page = 0;
|
||||
$buff->page = 1;
|
||||
$buff->data = array();
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
}
|
||||
|
||||
$virtual_no = $total_count - ($page-1)*$list_count;
|
||||
|
||||
$output = $this->_fetch($result);
|
||||
if(!is_array($output)) $output = array($output);
|
||||
|
||||
foreach($output as $k => $v) {
|
||||
$data[$virtual_no--] = $v;
|
||||
}
|
||||
|
||||
$buff = new Object();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $page;
|
||||
$buff->data = $data;
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
}
|
||||
return $buff;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@
|
|||
**/
|
||||
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, $this->fd);
|
||||
if(!is_numeric($string)) $string = @mysql_escape_string($string);
|
||||
return $string;
|
||||
}
|
||||
|
||||
|
|
@ -152,7 +152,7 @@
|
|||
// Notify to start a query execution
|
||||
$this->actStart($query);
|
||||
// Run the query statement
|
||||
$result = @mysql_query($query, $this->fd);
|
||||
$result = mysql_query($query, $this->fd);
|
||||
// Error Check
|
||||
if(mysql_error($this->fd)) $this->setError(mysql_errno($this->fd), mysql_error($this->fd));
|
||||
// Notify to complete a query execution
|
||||
|
|
@ -164,12 +164,16 @@
|
|||
/**
|
||||
* @brief Fetch results
|
||||
**/
|
||||
function _fetch($result) {
|
||||
function _fetch($result, $arrayIndexEndValue = NULL) {
|
||||
if(!$this->isConnected() || $this->isError() || !$result) return;
|
||||
while($tmp = $this->db_fetch_object($result)) {
|
||||
$output[] = $tmp;
|
||||
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp;
|
||||
else $output[] = $tmp;
|
||||
}
|
||||
if(count($output)==1){
|
||||
if(isset($arrayIndexEndValue)) return $output;
|
||||
else return $output[0];
|
||||
}
|
||||
if(count($output)==1) return $output[0];
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
|
@ -374,141 +378,46 @@
|
|||
if(!$output) return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return conditional clause
|
||||
**/
|
||||
function getCondition($output) {
|
||||
if(!$output->conditions) return;
|
||||
$condition = $this->_getCondition($output->conditions,$output->column_type);
|
||||
if($condition) $condition = ' where '.$condition;
|
||||
return $condition;
|
||||
}
|
||||
|
||||
function getLeftCondition($conditions,$column_type){
|
||||
return $this->_getCondition($conditions,$column_type);
|
||||
}
|
||||
|
||||
|
||||
function _getCondition($conditions,$column_type) {
|
||||
$condition = '';
|
||||
foreach($conditions as $val) {
|
||||
$sub_condition = '';
|
||||
foreach($val['condition'] as $v) {
|
||||
if(!isset($v['value'])) continue;
|
||||
if($v['value'] === '') continue;
|
||||
if(!in_array(gettype($v['value']), array('string', 'integer', 'double', 'array'))) continue;
|
||||
|
||||
$name = $v['column'];
|
||||
$operation = $v['operation'];
|
||||
$value = $v['value'];
|
||||
$type = $this->getColumnType($column_type,$name);
|
||||
$pipe = $v['pipe'];
|
||||
$value = $this->getConditionValue($name, $value, $operation, $type, $column_type);
|
||||
if(!$value) $value = $v['value'];
|
||||
$str = $this->getConditionPart($name, $value, $operation);
|
||||
if($sub_condition) $sub_condition .= ' '.$pipe.' ';
|
||||
$sub_condition .= $str;
|
||||
}
|
||||
if($sub_condition) {
|
||||
if($condition && $val['pipe']) $condition .= ' '.$val['pipe'].' ';
|
||||
$condition .= '('.$sub_condition.')';
|
||||
}
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle the insertAct
|
||||
**/
|
||||
function _executeInsertAct($output) {
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '`'.$this->prefix.$val.'`';
|
||||
}
|
||||
// List columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
|
||||
if($output->column_type[$name]!='number') {
|
||||
|
||||
if(!is_null($value)){
|
||||
$value = "'" . $this->addQuotes($value) ."'";
|
||||
}else{
|
||||
if($val['notnull']=='notnull') {
|
||||
$value = "''";
|
||||
} else {
|
||||
//$value = 'null';
|
||||
$value = "''";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//elseif(!$value || is_numeric($value)) $value = (int)$value;
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = '`'.$name.'`';
|
||||
$value_list[] = $value;
|
||||
}
|
||||
|
||||
function _executeInsertAct($queryObject) {
|
||||
// TODO See what priority does
|
||||
//priority setting
|
||||
$priority = '';
|
||||
if($output->priority) $priority = $output->priority['type'].'_priority';
|
||||
//$priority = '';
|
||||
//if($output->priority) $priority = $output->priority['type'].'_priority';
|
||||
|
||||
$query = sprintf("insert %s into %s (%s) values (%s);", $priority, implode(',',$table_list), implode(',',$column_list), implode(',', $value_list));
|
||||
$query = $this->getInsertSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle updateAct
|
||||
**/
|
||||
function _executeUpdateAct($output) {
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '`'.$this->prefix.$val.'` as '.$key;
|
||||
}
|
||||
// List columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
if(!isset($val['value'])) continue;
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
if(strpos($name,'.')!==false&&strpos($value,'.')!==false) $column_list[] = $name.' = '.$value;
|
||||
else {
|
||||
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'";
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = sprintf("`%s` = %s", $name, $value);
|
||||
}
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
function _executeUpdateAct($queryObject) {
|
||||
// TODO See what proiority does
|
||||
//priority setting
|
||||
$priority = '';
|
||||
if($output->priority) $priority = $output->priority['type'].'_priority';
|
||||
|
||||
$query = sprintf("update %s %s set %s %s", $priority, implode(',',$table_list), implode(',',$column_list), $condition);
|
||||
//$priority = '';
|
||||
//if($output->priority) $priority = $output->priority['type'].'_priority';
|
||||
|
||||
$query = $this->getUpdateSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle deleteAct
|
||||
**/
|
||||
function _executeDeleteAct($output) {
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '`'.$this->prefix.$val.'`';
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
//priority setting
|
||||
$priority = '';
|
||||
if($output->priority) $priority = $output->priority['type'].'_priority';
|
||||
|
||||
$query = sprintf("delete %s from %s %s", $priority, implode(',',$table_list), $condition);
|
||||
|
||||
function _executeDeleteAct($queryObject) {
|
||||
$query = $this->getDeleteSql($queryObject);
|
||||
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
//priority setting
|
||||
// TODO Check what priority does
|
||||
//$priority = '';
|
||||
//if($output->priority) $priority = $output->priority['type'].'_priority';
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
|
|
@ -518,261 +427,19 @@
|
|||
* In order to get a list of pages easily when selecting \n
|
||||
* it supports a method as navigation
|
||||
**/
|
||||
function _executeSelectAct($output) {
|
||||
// List tables
|
||||
$table_list = array();
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = '`'.$this->prefix.$val.'` as '.$key;
|
||||
}
|
||||
|
||||
$left_join = array();
|
||||
// why???
|
||||
$left_tables= (array)$output->left_tables;
|
||||
|
||||
foreach($left_tables as $key => $val) {
|
||||
$condition = $this->_getCondition($output->left_conditions[$key],$output->column_type);
|
||||
if($condition){
|
||||
$left_join[] = $val . ' `'.$this->prefix.$output->_tables[$key].'` as '.$key . ' on (' . $condition . ')';
|
||||
}
|
||||
}
|
||||
function _executeSelectAct($queryObject) {
|
||||
$query = $this->getSelectSql($queryObject);
|
||||
|
||||
$click_count = array();
|
||||
if(!$output->columns){
|
||||
$output->columns = array(array('name'=>'*'));
|
||||
}
|
||||
|
||||
$column_list = array();
|
||||
foreach($output->columns as $key => $val)
|
||||
{
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
if($val['click_count']) $click_count[] = $val['name'];
|
||||
|
||||
if(substr($name,-1) == '*')
|
||||
{
|
||||
$column_list[] = $name;
|
||||
}
|
||||
else if(strpos($name,'.')===false && strpos($name,'(')===false)
|
||||
{
|
||||
if($alias)
|
||||
{
|
||||
$col = sprintf('`%s` as `%s`', $name, $alias);
|
||||
$column_list[$alias] = $col;
|
||||
}
|
||||
else
|
||||
{
|
||||
$column_list[] = sprintf('`%s`',$name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if($alias)
|
||||
{
|
||||
$col = sprintf('%s as `%s`', $name, $alias);
|
||||
$column_list[$alias] = $col;
|
||||
}
|
||||
else
|
||||
{
|
||||
$column_list[] = sprintf('%s',$name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$columns = implode(',',$column_list);
|
||||
$output->column_list = $column_list;
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
if(count($output->index_hint))
|
||||
$index_hint = sprintf(' %s index (%s) ', $output->index_hint['type'], $output->index_hint['name']);
|
||||
|
||||
if($output->list_count && $output->page) return $this->_getNavigationData($table_list, $columns, $left_join, $index_hint, $condition, $output);
|
||||
|
||||
// Add a condition to use an index when sorting in order by list_order, update_order
|
||||
if($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(count($output->groups))
|
||||
{
|
||||
$groupby_query = sprintf(' group by %s', implode(',',$output->groups));
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
foreach($output->groups as $group)
|
||||
{
|
||||
if($column_list[$group]) $output->arg_columns[] = $column_list[$group];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($output->order) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $orderby_query .= ' order by '.implode(',',$index_list);
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
unset($tmpCol);
|
||||
$tmpCol = explode('.', $col);
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[1];
|
||||
|
||||
if(strpos($col,'`')===false && strpos($col,' ')==false) $col = '`'.$col.'`';
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[0].'.'.$col;
|
||||
|
||||
$columns[] = $col;
|
||||
}
|
||||
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
$query = sprintf("select %s from %s %s %s %s %s", $columns, implode(',',$table_list),implode(' ',$left_join), $index_hint, $condition, $groupby_query.$orderby_query);
|
||||
|
||||
// Apply when using list_count
|
||||
if($output->list_count['value']) $query = sprintf('%s limit %d', $query, $output->list_count['value']);
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
|
||||
$result = $this->_query($query);
|
||||
if($this->isError()) return;
|
||||
if(count($click_count) && count($output->conditions)){
|
||||
$_query = '';
|
||||
foreach($click_count as $k => $c) $_query .= sprintf(',%s=%s+1 ',$c,$c);
|
||||
$_query = sprintf('update %s set %s %s',implode(',',$table_list), substr($_query,1), $condition);
|
||||
$this->_query($_query);
|
||||
}
|
||||
|
||||
$data = $this->_fetch($result);
|
||||
|
||||
$buff = new Object();
|
||||
$buff->data = $data;
|
||||
|
||||
return $buff;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Paging is handled if navigation information exists in the query xml
|
||||
*
|
||||
* It is quite convenient although its structure is not good at all .. -_-;
|
||||
**/
|
||||
function _getNavigationData($table_list, $columns, $left_join, $index_hint, $condition, $output) {
|
||||
require_once(_XE_PATH_.'classes/page/PageHandler.class.php');
|
||||
|
||||
$column_list = $output->column_list;
|
||||
|
||||
// Get a total count
|
||||
$count_condition = count($output->groups) ? sprintf('%s group by %s', $condition, implode(', ', $output->groups)) : $condition;
|
||||
$count_query = sprintf("select count(*) as count from %s %s %s", implode(', ', $table_list), implode(' ', $left_join), $count_condition);
|
||||
if (count($output->groups)) $count_query = sprintf('select count(*) as count from (%s) xet', $count_query);
|
||||
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id . ' count(*)'):'';
|
||||
$result = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
$list_count = $output->list_count['value'];
|
||||
if(!$list_count) $list_count = 20;
|
||||
$page_count = $output->page_count['value'];
|
||||
if(!$page_count) $page_count = 10;
|
||||
$page = $output->page['value'];
|
||||
if(!$page) $page = 1;
|
||||
// Get a total page
|
||||
if($total_count) $total_page = (int)( ($total_count-1) / $list_count) + 1;
|
||||
else $total_page = 1;
|
||||
// Check Page variables
|
||||
if($page > $total_page) $page = $total_page;
|
||||
$start_count = ($page-1)*$list_count;
|
||||
// Add a condition to use an index when sorting in order by list_order, update_order
|
||||
if($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(count($output->groups)){
|
||||
$groupby_query = sprintf(' group by %s', implode(',',$output->groups));
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
foreach($output->groups as $group)
|
||||
{
|
||||
if($column_list[$group]) $output->arg_columns[] = $column_list[$group];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(count($output->order)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $orderby_query = ' order by '.implode(',',$index_list);
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = array();
|
||||
foreach($output->arg_columns as $col){
|
||||
unset($tmpCol);
|
||||
$tmpCol = explode('.', $col);
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[1];
|
||||
|
||||
if(strpos($col,'`')===false && strpos($col,' ')==false) $col = '`'.$col.'`';
|
||||
if(isset($tmpCol[1])) $col = $tmpCol[0].'.'.$col;
|
||||
|
||||
$columns[] = $col;
|
||||
}
|
||||
$columns = join(',',$columns);
|
||||
}
|
||||
|
||||
$query = sprintf("select %s from %s %s %s %s %s", $columns, implode(',',$table_list), implode(' ',$left_join), $index_hint, $condition, $groupby_query.$orderby_query);
|
||||
$query = sprintf('%s limit %d, %d', $query, $start_count, $list_count);
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
|
||||
$result = $this->_query($query);
|
||||
if($this->isError()) {
|
||||
$buff = new Object();
|
||||
$buff->total_count = 0;
|
||||
$buff->total_page = 0;
|
||||
$buff->page = 1;
|
||||
$buff->data = array();
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
}
|
||||
|
||||
$virtual_no = $total_count - ($page-1)*$list_count;
|
||||
$data = array();
|
||||
while($tmp = $this->db_fetch_object($result)) {
|
||||
$data[$virtual_no--] = $tmp;
|
||||
}
|
||||
$buff = new Object();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $page;
|
||||
$buff->data = $data;
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
|
||||
// TODO Add support for click count
|
||||
// TODO Add code for pagination
|
||||
|
||||
$result = $this->_query ($query);
|
||||
if ($this->isError ()) return $this->queryError($queryObject);
|
||||
else return $this->queryPageLimit($queryObject, $result);
|
||||
}
|
||||
|
||||
function db_insert_id()
|
||||
|
|
@ -784,6 +451,58 @@
|
|||
{
|
||||
return mysql_fetch_object($result);
|
||||
}
|
||||
|
||||
function getParser(){
|
||||
return new DBParser('`');
|
||||
}
|
||||
|
||||
function queryError($queryObject){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->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
|
||||
return $buff;
|
||||
}else
|
||||
return;
|
||||
}
|
||||
|
||||
function queryPageLimit($queryObject, $result){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
|
||||
// Total count
|
||||
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString()));
|
||||
if ($queryObject->getGroupByString() != '') {
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
}
|
||||
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$result_count = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result_count);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
// Total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
|
||||
} else $total_page = 1;
|
||||
|
||||
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
|
||||
$data = $this->_fetch($result, $virtual_no);
|
||||
|
||||
$buff = new Object ();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $queryObject->getLimit()->page->getValue();
|
||||
$buff->data = $data;
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page->getValue(), $queryObject->getLimit()->page_count);
|
||||
}else{
|
||||
$data = $this->_fetch($result);
|
||||
$buff = new Object ();
|
||||
$buff->data = $data;
|
||||
}
|
||||
return $buff;
|
||||
}
|
||||
}
|
||||
|
||||
return new DBMysql;
|
||||
|
|
|
|||
|
|
@ -226,15 +226,19 @@ class DBPostgresql extends DB
|
|||
/**
|
||||
* @brief Fetch results
|
||||
**/
|
||||
function _fetch($result)
|
||||
// TODO This is duplicate code - maybe we can find away to abastract the driver
|
||||
function _fetch($result, $arrayIndexEndValue = NULL)
|
||||
{
|
||||
if (!$this->isConnected() || $this->isError() || !$result)
|
||||
return;
|
||||
while ($tmp = pg_fetch_object($result)) {
|
||||
$output[] = $tmp;
|
||||
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $tmp;
|
||||
else $output[] = $tmp;
|
||||
}
|
||||
if (count($output) == 1)
|
||||
return $output[0];
|
||||
if(count($output)==1){
|
||||
if(isset($arrayIndexEndValue)) return $output;
|
||||
else return $output[0];
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
|
@ -493,428 +497,137 @@ class DBPostgresql extends DB
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return conditional clause
|
||||
**/
|
||||
function getCondition($output)
|
||||
{
|
||||
if (!$output->conditions)
|
||||
return;
|
||||
$condition = $this->_getCondition($output->conditions, $output->column_type);
|
||||
if ($condition)
|
||||
$condition = ' where ' . $condition;
|
||||
return $condition;
|
||||
}
|
||||
|
||||
function getLeftCondition($conditions, $column_type)
|
||||
{
|
||||
return $this->_getCondition($conditions, $column_type);
|
||||
}
|
||||
|
||||
|
||||
function _getCondition($conditions, $column_type)
|
||||
{
|
||||
$condition = '';
|
||||
foreach ($conditions as $val) {
|
||||
$sub_condition = '';
|
||||
foreach ($val['condition'] as $v) {
|
||||
if (!isset($v['value']))
|
||||
continue;
|
||||
if ($v['value'] === '')
|
||||
continue;
|
||||
if(!in_array(gettype($v['value']), array('string', 'integer', 'double', 'array'))) continue;
|
||||
continue;
|
||||
|
||||
$name = $v['column'];
|
||||
$operation = $v['operation'];
|
||||
$value = $v['value'];
|
||||
$type = $this->getColumnType($column_type, $name);
|
||||
$pipe = $v['pipe'];
|
||||
|
||||
$value = $this->getConditionValue($name, $value, $operation, $type, $column_type);
|
||||
if (!$value)
|
||||
$value = $v['value'];
|
||||
$str = $this->getConditionPart($name, $value, $operation);
|
||||
if ($sub_condition)
|
||||
$sub_condition .= ' ' . $pipe . ' ';
|
||||
$sub_condition .= $str;
|
||||
}
|
||||
if ($sub_condition) {
|
||||
if ($condition && $val['pipe'])
|
||||
$condition .= ' ' . $val['pipe'] . ' ';
|
||||
$condition .= '(' . $sub_condition . ')';
|
||||
}
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Handle the insertAct
|
||||
**/
|
||||
function _executeInsertAct($output)
|
||||
function _executeInsertAct($queryObject)
|
||||
{
|
||||
// List tables
|
||||
foreach ($output->tables as $key => $val) {
|
||||
$table_list[] = $this->prefix . $val;
|
||||
}
|
||||
// List columns
|
||||
foreach ($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
if ($output->column_type[$name] != 'number') {
|
||||
$value = "'" . $this->addQuotes($value) . "'";
|
||||
if (!$value)
|
||||
$value = 'null';
|
||||
}
|
||||
// sql injection 문제로 xml 선언이 number인 경우이면서 넘어온 값이 숫자형이 아니면 숫자형으로 강제 형변환
|
||||
// elseif (!$value || is_numeric($value)) $value = (int)$value;
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = $name;
|
||||
$value_list[] = $value;
|
||||
}
|
||||
|
||||
$query = sprintf("insert into %s (%s) values (%s);", implode(',', $table_list),
|
||||
implode(',', $column_list), implode(',', $value_list));
|
||||
$query = $this->getInsertSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle updateAct
|
||||
**/
|
||||
function _executeUpdateAct($output)
|
||||
function _executeUpdateAct($queryObject)
|
||||
{
|
||||
// List tables
|
||||
foreach ($output->tables as $key => $val) {
|
||||
//$table_list[] = $this->prefix.$val.' as '.$key;
|
||||
$table_list[] = $this->prefix . $val;
|
||||
}
|
||||
// List columns
|
||||
foreach ($output->columns as $key => $val) {
|
||||
if (!isset($val['value']))
|
||||
continue;
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
if (strpos($name, '.') !== false && strpos($value, '.') !== false)
|
||||
$column_list[] = $name . ' = ' . $value;
|
||||
else {
|
||||
if ($output->column_type[$name] != 'number')
|
||||
$value = "'" . $this->addQuotes($value) . "'";
|
||||
// sql injection 문제로 xml 선언이 number인 경우이면서 넘어온 값이 숫자형이 아니면 숫자형으로 강제 형변환
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = sprintf("%s = %s", $name, $value);
|
||||
}
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("update %s set %s %s", implode(',', $table_list), implode(',',
|
||||
$column_list), $condition);
|
||||
|
||||
$query = $this->getUpdateSql($queryObject);
|
||||
if(is_a($query, 'Object')) return;
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle deleteAct
|
||||
**/
|
||||
function _executeDeleteAct($output)
|
||||
function _executeDeleteAct($queryObject)
|
||||
{
|
||||
// List tables
|
||||
foreach ($output->tables as $key => $val) {
|
||||
$table_list[] = $this->prefix . $val;
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("delete from %s %s", implode(',', $table_list), $condition);
|
||||
|
||||
$query = $this->getDeleteSql($queryObject);
|
||||
|
||||
if(is_a($query, 'Object')) return;
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* override
|
||||
* @param $queryObject
|
||||
*/
|
||||
function getSelectSql($query){
|
||||
$select = $query->getSelectString();
|
||||
if($select == '') return new Object(-1, "Invalid query");
|
||||
$select = 'SELECT ' .$select;
|
||||
|
||||
$from = $query->getFromString();
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$from = ' FROM '.$from;
|
||||
|
||||
$where = $query->getWhereString();
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
|
||||
$groupBy = $query->getGroupByString();
|
||||
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
|
||||
|
||||
$orderBy = $query->getOrderByString();
|
||||
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
|
||||
|
||||
$limit = $query->getLimitString();
|
||||
if($limit != '') $limit = ' LIMIT ' . $query->getLimit()->getLimit() . ' OFFSET ' . $query->getLimit()->getOffset();
|
||||
|
||||
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handle selectAct
|
||||
*
|
||||
* In order to get a list of pages easily when selecting \n
|
||||
* it supports a method as navigation
|
||||
**/
|
||||
function _executeSelectAct($output)
|
||||
function _executeSelectAct($queryObject)
|
||||
{
|
||||
// List tables
|
||||
$table_list = array();
|
||||
foreach ($output->tables as $key => $val) {
|
||||
$table_list[] = $this->prefix . $val . ' as ' . $key;
|
||||
}
|
||||
|
||||
$left_join = array();
|
||||
// why???
|
||||
$left_tables = (array )$output->left_tables;
|
||||
|
||||
foreach ($left_tables as $key => $val) {
|
||||
$condition = $this->_getCondition($output->left_conditions[$key], $output->
|
||||
column_type);
|
||||
if ($condition) {
|
||||
$left_join[] = $val . ' ' . $this->prefix . $output->_tables[$key] . ' as ' . $key .
|
||||
' on (' . $condition . ')';
|
||||
}
|
||||
}
|
||||
|
||||
$click_count = array();
|
||||
if(!$output->columns){
|
||||
$output->columns = array(array('name'=>'*'));
|
||||
}
|
||||
|
||||
$column_list = array();
|
||||
foreach ($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
if($val['click_count']) $click_count[] = $val['name'];
|
||||
|
||||
if (substr($name, -1) == '*') {
|
||||
$column_list[] = $name;
|
||||
} elseif (strpos($name, '.') === false && strpos($name, '(') === false) {
|
||||
if ($alias)
|
||||
$column_list[$alias] = sprintf('%s as %s', $name, $alias);
|
||||
else
|
||||
$column_list[] = sprintf('%s', $name);
|
||||
} else {
|
||||
if ($alias)
|
||||
$column_list[$alias] = sprintf('%s as %s', $name, $alias);
|
||||
else
|
||||
$column_list[] = sprintf('%s', $name);
|
||||
$query = $this->getSelectSql($queryObject);
|
||||
|
||||
if(is_a($query, 'Object')) return;
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
|
||||
// TODO Add support for click count
|
||||
// TODO Add code for pagination
|
||||
|
||||
$result = $this->_query ($query);
|
||||
if ($this->isError ()) {
|
||||
if ($limit && $output->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
|
||||
return $buff;
|
||||
}else
|
||||
return;
|
||||
}
|
||||
}
|
||||
$columns = implode(',', $column_list);
|
||||
|
||||
$condition = $this->getCondition($output);
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
|
||||
// Total count
|
||||
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString()));
|
||||
if ($queryObject->getGroupByString() != '') {
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
}
|
||||
|
||||
$output->column_list = $column_list;
|
||||
if ($output->list_count && $output->page)
|
||||
return $this->_getNavigationData($table_list, $columns, $left_join, $condition,
|
||||
$output);
|
||||
// Add a condition to use an index when sorting in order by list_order, update_order
|
||||
if ($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if (!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach ($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if (!in_array($col, array('list_order', 'update_order')))
|
||||
continue;
|
||||
if ($condition)
|
||||
$condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else
|
||||
$condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):'';
|
||||
$result_count = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result_count);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
// Total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
|
||||
} else $total_page = 1;
|
||||
|
||||
|
||||
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
|
||||
$data = $this->_fetch($result, $virtual_no);
|
||||
|
||||
$buff = new Object ();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $queryObject->getLimit()->page->getValue();
|
||||
$buff->data = $data;
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page->getValue(), $queryObject->getLimit()->page_count);
|
||||
}else{
|
||||
$data = $this->_fetch($result);
|
||||
$buff = new Object ();
|
||||
$buff->data = $data;
|
||||
}
|
||||
|
||||
if (count($output->groups)) {
|
||||
/*
|
||||
var_dump("= column output start = ");
|
||||
var_dump(sizeof ($output->columns) . " = end length == ");
|
||||
var_dump($output->columns);
|
||||
var_dump("= column output end = " . "\n");
|
||||
var_dump($output->groups);
|
||||
var_dump("=== " . "\n");
|
||||
var_dump(debug_backtrace());
|
||||
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
} */
|
||||
$group_list = array();
|
||||
foreach ($output->groups as $gkey => $gval) {
|
||||
foreach ($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
if (trim($name) == trim($gval)) {
|
||||
$group_list[] = $alias;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if($column_list[$gval]) $output->arg_columns[] = $column_list[$gval];
|
||||
|
||||
}
|
||||
$groupby_query = sprintf(' group by %s', implode(',', $group_list));
|
||||
// var_dump($query);
|
||||
}
|
||||
|
||||
if ($output->order) {
|
||||
foreach ($output->order as $key => $val) {
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if (count($index_list)) $orderby_query = ' order by ' . implode(',', $index_list);
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = join(',',$output->arg_columns);
|
||||
}
|
||||
|
||||
$query = sprintf("select %s from %s %s %s %s", $columns, implode(',', $table_list), implode(' ', $left_join), $condition, $groupby_query.$orderby_query);
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
if ($this->isError())
|
||||
return;
|
||||
|
||||
if(count($click_count)>0 && count($output->conditions)>0){
|
||||
$_query = '';
|
||||
foreach($click_count as $k => $c) $_query .= sprintf(',%s=%s+1 ',$c,$c);
|
||||
$_query = sprintf('update %s set %s %s',implode(',',$table_list), substr($_query,1), $condition);
|
||||
$this->_query($_query);
|
||||
}
|
||||
|
||||
|
||||
$data = $this->_fetch($result);
|
||||
|
||||
$buff = new Object();
|
||||
$buff->data = $data;
|
||||
return $buff;
|
||||
return $buff;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Paging is handled if navigation information exists in the query xml
|
||||
*
|
||||
* It is quite convenient although its structure is not good at all .. -_-;
|
||||
**/
|
||||
function _getNavigationData($table_list, $columns, $left_join, $condition, $output)
|
||||
{
|
||||
require_once (_XE_PATH_ . 'classes/page/PageHandler.class.php');
|
||||
|
||||
$column_list = $output->column_list;
|
||||
/*
|
||||
// Modified to find total number of SELECT queries having group by clause
|
||||
// If it works correctly, uncomment the following codes
|
||||
//
|
||||
$count_condition = count($output->groups) ? sprintf('%s group by %s', $condition, implode(', ', $output->groups)) : $condition;
|
||||
$total_count = $this->getCountCache($output->tables, $count_condition);
|
||||
if ($total_count === false) {
|
||||
$count_query = sprintf('select count(*) as count from %s %s %s', implode(', ', $table_list), implode(' ', $left_join), $count_condition);
|
||||
if (count($output->groups))
|
||||
$count_query = sprintf('select count(*) as count from (%s) xet', $count_query);
|
||||
$result = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result);
|
||||
$total_count = (int)$count_output->count;
|
||||
$this->putCountCache($output->tables, $count_condition, $total_count);
|
||||
}
|
||||
*/
|
||||
|
||||
// Get a total count
|
||||
$count_query = sprintf("select count(*) as count from %s %s %s", implode(',', $table_list), implode(' ', $left_join), $condition);
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id . ' count(*)'):'';
|
||||
$result = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
$list_count = $output->list_count['value'];
|
||||
if (!$list_count) $list_count = 20;
|
||||
$page_count = $output->page_count['value'];
|
||||
if (!$page_count) $page_count = 10;
|
||||
$page = $output->page['value'];
|
||||
if (!$page)
|
||||
$page = 1;
|
||||
|
||||
// Get a total page
|
||||
if ($total_count) $total_page = (int)(($total_count - 1) / $list_count) + 1;
|
||||
else $total_page = 1;
|
||||
|
||||
// Check Page variables
|
||||
if ($page > $total_page) $page = $total_page;
|
||||
$start_count = ($page - 1) * $list_count;
|
||||
// Add a condition to use an index when sorting in order by list_order, update_order
|
||||
if ($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if (!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach ($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if (!in_array($col, array('list_order', 'update_order')))
|
||||
continue;
|
||||
if ($condition)
|
||||
$condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else
|
||||
$condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($output->groups)) {
|
||||
/*
|
||||
var_dump("= column output start = ");
|
||||
var_dump(sizeof ($output->columns) . " = end length == ");
|
||||
var_dump($output->columns);
|
||||
var_dump("= column output end = " . "\n");
|
||||
var_dump($output->groups);
|
||||
var_dump("=== " . "\n");
|
||||
var_dump(debug_backtrace());
|
||||
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
} */
|
||||
$group_list = array();
|
||||
foreach ($output->groups as $gkey => $gval) {
|
||||
foreach ($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
if (trim($name) == trim($gval)) {
|
||||
$group_list[] = $alias;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if($column_list[$gval]) $output->arg_columns[] = $column_list[$gval];
|
||||
|
||||
}
|
||||
$groupby_query = sprintf(' group by %s', implode(',', $group_list));
|
||||
// var_dump($query);
|
||||
}
|
||||
|
||||
if ($output->order) {
|
||||
foreach ($output->order as $key => $val) {
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if (count($index_list)) $orderby_query = ' order by ' . implode(',', $index_list);
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = join(',',$output->arg_columns);
|
||||
}
|
||||
|
||||
$query = sprintf("select %s from %s %s %s", $columns, implode(',', $table_list), implode(' ', $left_join), $condition);
|
||||
$query = sprintf('%s offset %d limit %d', $query, $start_count, $list_count);
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
|
||||
$result = $this->_query($query);
|
||||
if ($this->isError()) {
|
||||
$buff = new Object();
|
||||
$buff->total_count = 0;
|
||||
$buff->total_page = 0;
|
||||
$buff->page = 1;
|
||||
$buff->data = array();
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
}
|
||||
|
||||
$virtual_no = $total_count - ($page - 1) * $list_count;
|
||||
while ($tmp = pg_fetch_object($result)) {
|
||||
$data[$virtual_no--] = $tmp;
|
||||
}
|
||||
|
||||
$buff = new Object();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $page;
|
||||
$buff->data = $data;
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
|
||||
function getParser(){
|
||||
return new DBParser('"');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -425,7 +425,7 @@
|
|||
}
|
||||
// sql injection 문제로 xml 선언이 number인 경우이면서 넘어온 값이 숫자형이 아니면 숫자형으로 강제 형변환
|
||||
// elseif(!$value || is_numeric($value)) $value = (int)$value;
|
||||
else $this->_filterNumber($value);
|
||||
else $this->_filterNumber(&$value);
|
||||
|
||||
$column_list[] = $name;
|
||||
$value_list[] = $value;
|
||||
|
|
@ -454,7 +454,7 @@
|
|||
else {
|
||||
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'";
|
||||
// sql injection 문제로 xml 선언이 number인 경우이면서 넘어온 값이 숫자형이 아니면 숫자형으로 강제 형변환
|
||||
else $this->_filterNumber($value);
|
||||
else $this->_filterNumber(&$value);
|
||||
|
||||
$column_list[] = sprintf("%s = %s", $name, $value);
|
||||
}
|
||||
|
|
@ -473,7 +473,7 @@
|
|||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
foreach($table_list as $key => $val) {
|
||||
$condition = preg_replace('/'.$key.'\\./i', $val.'.', $condition);
|
||||
$condition = eregi_replace($key.'\\.', $val.'.', $condition);
|
||||
}
|
||||
// List columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
|
|
|
|||
|
|
@ -399,389 +399,118 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return conditional clause(where)
|
||||
**/
|
||||
function getCondition($output) {
|
||||
if(!$output->conditions) return;
|
||||
$condition = $this->_getCondition($output->conditions,$output->column_type);
|
||||
if($condition) $condition = ' where '.$condition;
|
||||
return $condition;
|
||||
}
|
||||
/**
|
||||
* @brief insertAct
|
||||
* */
|
||||
function _executeInsertAct($queryObject) {
|
||||
$query = $this->getInsertSql($queryObject);
|
||||
if (is_a($query, 'Object'))
|
||||
return;
|
||||
|
||||
function getLeftCondition($conditions,$column_type){
|
||||
return $this->_getCondition($conditions,$column_type);
|
||||
}
|
||||
$this->_prepare($query);
|
||||
|
||||
$val_count = count($val_list);
|
||||
for ($i = 0; $i < $val_count; $i++)
|
||||
$this->_bind($val_list[$i]);
|
||||
|
||||
function _getCondition($conditions,$column_type) {
|
||||
$condition = '';
|
||||
foreach($conditions as $val) {
|
||||
$sub_condition = '';
|
||||
foreach($val['condition'] as $v) {
|
||||
if(!isset($v['value'])) continue;
|
||||
if($v['value'] === '') continue;
|
||||
if(!in_array(gettype($v['value']), array('string', 'integer', 'double', 'array'))) continue;
|
||||
return $this->_execute();
|
||||
}
|
||||
|
||||
$name = $v['column'];
|
||||
$operation = $v['operation'];
|
||||
$value = $v['value'];
|
||||
$type = $this->getColumnType($column_type,$name);
|
||||
$pipe = $v['pipe'];
|
||||
/**
|
||||
* @brief updateAct
|
||||
* */
|
||||
function _executeUpdateAct($queryObject) {
|
||||
$query = $this->getUpdateSql($queryObject);
|
||||
if (is_a($query, 'Object'))
|
||||
return;
|
||||
|
||||
$value = $this->getConditionValue($name, $value, $operation, $type, $column_type);
|
||||
if(!$value) $value = $v['value'];
|
||||
$str = $this->getConditionPart($name, $value, $operation);
|
||||
if($sub_condition) $sub_condition .= ' '.$pipe.' ';
|
||||
$sub_condition .= $str;
|
||||
}
|
||||
if($sub_condition) {
|
||||
if($condition && $val['pipe']) $condition .= ' '.$val['pipe'].' ';
|
||||
$condition .= '('.$sub_condition.')';
|
||||
}
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
$this->_prepare($query);
|
||||
return $this->_execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief insertAct
|
||||
**/
|
||||
function _executeInsertAct($output) {
|
||||
// list tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = $this->prefix.$val;
|
||||
}
|
||||
// list columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
/**
|
||||
* @brief deleteAct
|
||||
* */
|
||||
function _executeDeleteAct($queryObject) {
|
||||
$query = $this->getDeleteSql($queryObject);
|
||||
if (is_a($query, 'Object'))
|
||||
return;
|
||||
|
||||
$key_list[] = $name;
|
||||
$this->_prepare($query);
|
||||
return $this->_execute();
|
||||
}
|
||||
|
||||
if($output->column_type[$name]!='number') $val_list[] = $this->addQuotes($value);
|
||||
else {
|
||||
$this->_filterNumber($value);
|
||||
$val_list[] = $value;
|
||||
}
|
||||
/**
|
||||
* @brief selectAct
|
||||
*
|
||||
* To fetch a list of the page conveniently when selecting, \n
|
||||
* navigation method supported
|
||||
* */
|
||||
function _executeSelectAct($queryObject) {
|
||||
$query = $this->getSelectSql($queryObject);
|
||||
if (is_a($query, 'Object'))
|
||||
return;
|
||||
|
||||
$prepare_list[] = '?';
|
||||
}
|
||||
$this->_prepare($query);
|
||||
$data = $this->_execute();
|
||||
if ($this->isError())
|
||||
return;
|
||||
|
||||
$query = sprintf("INSERT INTO %s (%s) VALUES (%s);", implode(',',$table_list), implode(',',$key_list), implode(',',$prepare_list));
|
||||
if ($this->isError())
|
||||
return $this->queryError($queryObject);
|
||||
else
|
||||
return $this->queryPageLimit($queryObject, $data);
|
||||
}
|
||||
|
||||
$this->_prepare($query);
|
||||
|
||||
$val_count = count($val_list);
|
||||
for($i=0;$i<$val_count;$i++) $this->_bind($val_list[$i]);
|
||||
|
||||
return $this->_execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief updateAct
|
||||
**/
|
||||
function _executeUpdateAct($output) {
|
||||
$table_count = count(array_values($output->tables));
|
||||
// If a target table is one
|
||||
if($table_count == 1) {
|
||||
// list tables
|
||||
list($target_table) = array_values($output->tables);
|
||||
$target_table = $this->prefix.$target_table;
|
||||
// list columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
if(!isset($val['value'])) continue;
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
if(strpos($name,'.')!==false&&strpos($value,'.')!==false) $column_list[] = $name.' = '.$value;
|
||||
else {
|
||||
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'";
|
||||
else $this->_filterNumber($value);
|
||||
|
||||
$column_list[] = sprintf("%s = %s", $name, $value);
|
||||
}
|
||||
}
|
||||
// List where cluase
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("update %s set %s %s", $target_table, implode(',',$column_list), $condition);
|
||||
// If tables to update are morea than two (In sqlite, it is possible to update a single table only. Let me know if you know a better way)
|
||||
} elseif($table_count == 2) {
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[$val] = $this->prefix.$key;
|
||||
}
|
||||
list($source_table, $target_table) = array_values($table_list);
|
||||
// List where cluase
|
||||
$condition = $this->getCondition($output);
|
||||
foreach($table_list as $key => $val) {
|
||||
$condition = preg_replace('/'.$key.'\\./i', $val.'.', $condition);
|
||||
}
|
||||
// List columns
|
||||
foreach($output->columns as $key => $val) {
|
||||
if(!isset($val['value'])) continue;
|
||||
$name = $val['name'];
|
||||
$value = $val['value'];
|
||||
list($s_prefix, $s_column) = explode('.',$name);
|
||||
list($t_prefix, $t_column) = explode('.',$value);
|
||||
|
||||
$s_table = $table_list[$s_prefix];
|
||||
$t_table = $table_list[$t_prefix];
|
||||
$column_list[] = sprintf(' %s = (select %s from %s %s) ', $s_column, $t_column, $t_table, $condition);
|
||||
}
|
||||
|
||||
$query = sprintf('update %s set %s where exists(select * from %s %s)', $source_table, implode(',', $column_list), $target_table, $condition);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->_prepare($query);
|
||||
return $this->_execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief deleteAct
|
||||
**/
|
||||
function _executeDeleteAct($output) {
|
||||
// List tables
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = $this->prefix.$val;
|
||||
}
|
||||
// List the conditional clause
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$query = sprintf("delete from %s %s", implode(',',$table_list), $condition);
|
||||
|
||||
$this->_prepare($query);
|
||||
return $this->_execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief selectAct
|
||||
*
|
||||
* To fetch a list of the page conveniently when selecting, \n
|
||||
* navigation method supported
|
||||
**/
|
||||
function _executeSelectAct($output) {
|
||||
// List tables
|
||||
$table_list = array();
|
||||
foreach($output->tables as $key => $val) {
|
||||
$table_list[] = $this->prefix.$val.' as '.$key;
|
||||
}
|
||||
|
||||
$left_join = array();
|
||||
// why???
|
||||
$left_tables= (array)$output->left_tables;
|
||||
|
||||
foreach($left_tables as $key => $val) {
|
||||
$condition = $this->_getCondition($output->left_conditions[$key],$output->column_type);
|
||||
if($condition){
|
||||
$left_join[] = $val . ' '.$this->prefix.$output->_tables[$key].' as '.$key . ' on (' . $condition . ')';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$click_count = array();
|
||||
if(!$output->columns){
|
||||
$output->columns = array(array('name'=>'*'));
|
||||
}
|
||||
|
||||
$column_list = array();
|
||||
foreach($output->columns as $key => $val) {
|
||||
$name = $val['name'];
|
||||
$alias = $val['alias'];
|
||||
if($val['click_count']) $click_count[] = $val['name'];
|
||||
|
||||
if(substr($name,-1) == '*') {
|
||||
$column_list[] = $name;
|
||||
} elseif(strpos($name,'.')===false && strpos($name,'(')===false) {
|
||||
if($alias) $column_list[$alias] = sprintf('%s as %s', $name, $alias);
|
||||
else $column_list[] = sprintf('%s',$name);
|
||||
} else {
|
||||
if($alias) $column_list[$alias] = sprintf('%s as %s', $name, $alias);
|
||||
else $column_list[] = sprintf('%s',$name);
|
||||
}
|
||||
}
|
||||
$columns = implode(',',$column_list);
|
||||
|
||||
$condition = $this->getCondition($output);
|
||||
|
||||
$output->column_list = $column_list;
|
||||
if($output->list_count && $output->page) return $this->_getNavigationData($table_list, $columns, $left_join, $condition, $output);
|
||||
// add the condition to the query to use an index for ordering by list_order, update_order
|
||||
if($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(count($output->groups)){
|
||||
$groupby_query = sprintf(' group by %s', implode(',',$output->groups));
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
foreach($output->groups as $group)
|
||||
{
|
||||
if($column_list[$group]) $output->arg_columns[] = $column_list[$group];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($output->order) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $orderby_query = ' order by '.implode(',',$index_list);
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = join(',',$output->arg_columns);
|
||||
}
|
||||
|
||||
$query = sprintf("select %s from %s %s %s %s", $columns, implode(',',$table_list),implode(' ',$left_join), $condition, $groupby_query.$orderby_query);
|
||||
// apply when using list_count
|
||||
if($output->list_count['value']) $query = sprintf('%s limit %d', $query, $output->list_count['value']);
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$this->_prepare($query);
|
||||
$data = $this->_execute();
|
||||
if($this->isError()) return;
|
||||
|
||||
if(count($click_count)>0 && count($output->conditions)>0){
|
||||
$_query = '';
|
||||
foreach($click_count as $k => $c) $_query .= sprintf(',%s=%s+1 ',$c,$c);
|
||||
$_query = sprintf('update %s set %s %s',implode(',',$table_list), substr($_query,1), $condition);
|
||||
$this->_query($_query);
|
||||
}
|
||||
|
||||
$buff = new Object();
|
||||
$buff->data = $data;
|
||||
return $buff;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Paging is handled if navigation information exists in the query xml
|
||||
*
|
||||
* It is quite convenient although its structure is not good at all .. -_-;
|
||||
**/
|
||||
function _getNavigationData($table_list, $columns, $left_join, $condition, $output) {
|
||||
require_once(_XE_PATH_.'classes/page/PageHandler.class.php');
|
||||
|
||||
$column_list = $output->column_list;
|
||||
/*
|
||||
// Modified to find total number of SELECT queries having group by clause
|
||||
// If it works correctly, uncomment the following codes
|
||||
//
|
||||
$count_condition = count($output->groups) ? sprintf('%s group by %s', $condition, implode(', ', $output->groups)) : $condition;
|
||||
$total_count = $this->getCountCache($output->tables, $count_condition);
|
||||
if($total_count === false) {
|
||||
$count_query = sprintf("select count(*) as count from %s %s %s", implode(', ', $table_list), implode(' ', $left_join), $count_condition);
|
||||
if (count($output->groups))
|
||||
$count_query = sprintf('select count(*) as count from (%s) xet', $count_query);
|
||||
$result = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result);
|
||||
$total_count = (int)$count_output->count;
|
||||
$this->putCountCache($output->tables, $count_condition, $total_count);
|
||||
}
|
||||
*/
|
||||
// Get a total count
|
||||
$count_query = sprintf("select count(*) as count from %s %s %s", implode(',',$table_list),implode(' ',$left_join), $condition);
|
||||
$count_query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id . ' count(*)'):'';
|
||||
$this->_prepare($count_query);
|
||||
$count_output = $this->_execute();
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
$list_count = $output->list_count['value'];
|
||||
if(!$list_count) $list_count = 20;
|
||||
$page_count = $output->page_count['value'];
|
||||
if(!$page_count) $page_count = 10;
|
||||
$page = $output->page['value'];
|
||||
if(!$page) $page = 1;
|
||||
// Get a total page
|
||||
if($total_count) $total_page = (int)( ($total_count-1) / $list_count) + 1;
|
||||
else $total_page = 1;
|
||||
// Check Page variables
|
||||
if($page > $total_page) $page = $total_page;
|
||||
$start_count = ($page-1)*$list_count;
|
||||
// Add a condition to use an index when sorting in order by list_order, update_order
|
||||
if($output->order) {
|
||||
$conditions = $this->getConditionList($output);
|
||||
if(!in_array('list_order', $conditions) && !in_array('update_order', $conditions)) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$col = $val[0];
|
||||
if(!in_array($col, array('list_order','update_order'))) continue;
|
||||
if($condition) $condition .= sprintf(' and %s < 2100000000 ', $col);
|
||||
else $condition = sprintf(' where %s < 2100000000 ', $col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(count($output->groups)){
|
||||
$groupby_query = sprintf(' group by %s', implode(',',$output->groups));
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
foreach($output->groups as $group)
|
||||
{
|
||||
if($column_list[$group]) $output->arg_columns[] = $column_list[$group];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($output->order) {
|
||||
foreach($output->order as $key => $val) {
|
||||
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
|
||||
if(count($output->arg_columns) && $column_list[$val[0]]) $output->arg_columns[] = $column_list[$val[0]];
|
||||
}
|
||||
if(count($index_list)) $orderby_query = ' order by '.implode(',',$index_list);
|
||||
}
|
||||
|
||||
if(count($output->arg_columns))
|
||||
{
|
||||
$columns = join(',',$output->arg_columns);
|
||||
}
|
||||
|
||||
// Return the result
|
||||
$buff = new Object();
|
||||
function queryError($queryObject) {
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->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, $total_page, $page, $page_count);
|
||||
$buff->page_navigation = new PageHandler(/* $total_count */0, /* $total_page */1, /* $page */1, /* $page_count */10); //default page handler values
|
||||
return $buff;
|
||||
}else
|
||||
return;
|
||||
}
|
||||
|
||||
// Query Execution
|
||||
$query = sprintf("select %s from %s %s %s %s", $columns, implode(',',$table_list),implode(' ',$left_join), $condition, $groupby_query.$orderby_query);
|
||||
$query = sprintf('%s limit %d, %d', $query, $start_count, $list_count);
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
|
||||
$this->_prepare($query);
|
||||
|
||||
if($this->isError()) {
|
||||
$this->setError($this->handler->errorCode(), print_r($this->handler->errorInfo(),true));
|
||||
$this->actFinish();
|
||||
return $buff;
|
||||
function queryPageLimit($queryObject, $data) {
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
|
||||
// Total count
|
||||
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE ' . $queryObject->getWhereString()));
|
||||
if ($queryObject->getGroupByString() != '') {
|
||||
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
|
||||
}
|
||||
|
||||
$this->stmt->execute();
|
||||
$count_query .= ( __DEBUG_QUERY__ & 1 && $output->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
|
||||
$this->_prepare($count_query);
|
||||
$count_output = $this->_execute();
|
||||
$total_count = (int) $count_output->count;
|
||||
|
||||
if($this->stmt->errorCode() != '00000') {
|
||||
$this->setError($this->stmt->errorCode(), print_r($this->stmt->errorInfo(),true));
|
||||
// Total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
|
||||
} else
|
||||
$total_page = 1;
|
||||
|
||||
$this->_prepare($this->getSelectSql($queryObject));
|
||||
$this->stmt->execute();
|
||||
if ($this->stmt->errorCode() != '00000') {
|
||||
$this->setError($this->stmt->errorCode(), print_r($this->stmt->errorInfo(), true));
|
||||
$this->actFinish();
|
||||
return $buff;
|
||||
}
|
||||
|
||||
$output = null;
|
||||
$virtual_no = $total_count - ($page-1)*$list_count;
|
||||
while($tmp = $this->stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
|
||||
//$data = $this->_fetch($result, $virtual_no);
|
||||
while ($tmp = $this->stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
unset($obj);
|
||||
foreach($tmp as $key => $val) {
|
||||
foreach ($tmp as $key => $val) {
|
||||
$pos = strpos($key, '.');
|
||||
if($pos) $key = substr($key, $pos+1);
|
||||
if ($pos)
|
||||
$key = substr($key, $pos + 1);
|
||||
$obj->{$key} = $val;
|
||||
}
|
||||
$data[$virtual_no--] = $obj;
|
||||
|
|
@ -790,16 +519,21 @@
|
|||
$this->stmt = null;
|
||||
$this->actFinish();
|
||||
|
||||
$buff = new Object();
|
||||
$buff = new Object ();
|
||||
$buff->total_count = $total_count;
|
||||
$buff->total_page = $total_page;
|
||||
$buff->page = $page;
|
||||
$buff->page = $queryObject->getLimit()->page->getValue();
|
||||
$buff->data = $data;
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page->getValue(), $queryObject->getLimit()->page_count);
|
||||
}else {
|
||||
//$data = $this->_fetch($result);
|
||||
$buff = new Object ();
|
||||
$buff->data = $data;
|
||||
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
|
||||
return $buff;
|
||||
}
|
||||
return $buff;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return new DBSqlite3_pdo;
|
||||
?>
|
||||
|
|
|
|||
296
classes/db/queryparts/Query.class.php
Normal file
296
classes/db/queryparts/Query.class.php
Normal file
|
|
@ -0,0 +1,296 @@
|
|||
<?php
|
||||
|
||||
class Query extends Object {
|
||||
var $queryID;
|
||||
var $action;
|
||||
|
||||
var $columns;
|
||||
var $tables;
|
||||
var $conditions;
|
||||
var $groups;
|
||||
var $orderby;
|
||||
var $limit;
|
||||
|
||||
var $arguments = null;
|
||||
|
||||
var $columnList = null;
|
||||
|
||||
function Query($queryID = null
|
||||
, $action = null
|
||||
, $columns = null
|
||||
, $tables = null
|
||||
, $conditions = null
|
||||
, $groups = null
|
||||
, $orderby = null
|
||||
, $limit = null){
|
||||
$this->queryID = $queryID;
|
||||
$this->action = $action;
|
||||
|
||||
if(!isset($tables)) return;
|
||||
$this->columns = $this->setColumns($columns);
|
||||
$this->tables = $this->setTables($tables);
|
||||
$this->conditions = $this->setConditions($conditions);
|
||||
$this->groups = $this->setGroups($groups);
|
||||
$this->orderby = $this->setOrder($orderby);
|
||||
$this->limit = $this->setLimit($limit);
|
||||
}
|
||||
|
||||
function show(){
|
||||
return true;
|
||||
}
|
||||
|
||||
function setQueryId($queryID){
|
||||
$this->queryID = $queryID;
|
||||
}
|
||||
|
||||
function setAction($action){
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
function setColumnList($columnList){
|
||||
$this->columnList = $columnList;
|
||||
}
|
||||
|
||||
function setColumns($columns){
|
||||
if(!isset($columns) || count($columns) === 0){
|
||||
$this->columns = array(new StarExpression());
|
||||
return;
|
||||
}
|
||||
|
||||
if(!is_array($columns)) $columns = array($columns);
|
||||
|
||||
$this->columns = $columns;
|
||||
}
|
||||
|
||||
function setTables($tables){
|
||||
if(!isset($tables) || count($tables) === 0){
|
||||
$this->setError(true);
|
||||
$this->setMessage("You must provide at least one table for the query.");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!is_array($tables)) $tables = array($tables);
|
||||
|
||||
$this->tables = $tables;
|
||||
}
|
||||
|
||||
function setConditions($conditions){
|
||||
if(!isset($conditions) || count($conditions) === 0) return;
|
||||
if(!is_array($conditions)) $conditions = array($conditions);
|
||||
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
function setGroups($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);
|
||||
|
||||
$this->orderby = $order;
|
||||
}
|
||||
|
||||
function setLimit($limit = NULL){
|
||||
if(!isset($limit)) return;
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
// START Fluent interface
|
||||
function select($columns= null){
|
||||
$this->action = 'select';
|
||||
$this->setColumns($columns);
|
||||
return $this;
|
||||
}
|
||||
|
||||
function from($tables){
|
||||
$this->setTables($tables);
|
||||
return $this;
|
||||
}
|
||||
|
||||
function where($conditions){
|
||||
$this->setConditions($conditions);
|
||||
return $this;
|
||||
}
|
||||
|
||||
function groupBy($groups){
|
||||
$this->setGroups($groups);
|
||||
return $this;
|
||||
}
|
||||
|
||||
function orderBy($order){
|
||||
$this->setOrder($order);
|
||||
return $this;
|
||||
}
|
||||
|
||||
function limit($limit){
|
||||
$this->setLimit($limit);
|
||||
return $this;
|
||||
}
|
||||
// END Fluent interface
|
||||
|
||||
function getAction(){
|
||||
return $this->action;
|
||||
}
|
||||
|
||||
function getSelectString($with_values = true){
|
||||
if(isset($this->columnList)){
|
||||
$selectColumns = array();
|
||||
$dbParser = XmlQueryParser::getDBParser();
|
||||
|
||||
foreach($this->columnList as $columnName){
|
||||
$columnName = $dbParser->escapeColumn($columnName);
|
||||
$selectColumns[] = new SelectExpression($columnName);
|
||||
}
|
||||
}
|
||||
else
|
||||
$selectColumns = $this->columns;
|
||||
|
||||
$select = '';
|
||||
foreach($selectColumns as $column){
|
||||
if($column->show())
|
||||
if(is_a($column, 'Subquery')){
|
||||
$select .= $column->toString($with_values) . ' as '. $column->getAlias() .', ';
|
||||
}
|
||||
else
|
||||
$select .= $column->getExpression($with_values) . ', ';
|
||||
}
|
||||
if(trim($select) == '') return '';
|
||||
$select = substr($select, 0, -2);
|
||||
return $select;
|
||||
}
|
||||
|
||||
function getUpdateString($with_values = true){
|
||||
return $this->getSelectString($with_values);
|
||||
}
|
||||
|
||||
function getInsertString($with_values = true){
|
||||
$columnsList = '';
|
||||
$valuesList = '';
|
||||
foreach($this->columns as $column){
|
||||
if($column->show()){
|
||||
$columnsList .= $column->getColumnName() . ', ';
|
||||
$valuesList .= $column->getValue($with_values) . ', ';
|
||||
}
|
||||
}
|
||||
$columnsList = substr($columnsList, 0, -2);
|
||||
$valuesList = substr($valuesList, 0, -2);
|
||||
|
||||
return "($columnsList) \n VALUES ($valuesList)";
|
||||
}
|
||||
|
||||
function getTables(){
|
||||
return $this->tables;
|
||||
}
|
||||
|
||||
// from table_a
|
||||
// from table_a inner join table_b on x=y
|
||||
// from (select * from table a) as x
|
||||
// from (select * from table t) as x inner join table y on y.x
|
||||
function getFromString($with_values = true){
|
||||
$from = '';
|
||||
$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(is_a($table, 'Subquery')) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
|
||||
|
||||
$simple_table_count++;
|
||||
}
|
||||
if(trim($from) == '') return '';
|
||||
return $from;
|
||||
}
|
||||
|
||||
function getWhereString($with_values = true){
|
||||
$where = '';
|
||||
if(count($this->conditions) > 0){
|
||||
$condition_count = 0;
|
||||
foreach($this->conditions as $conditionGroup){
|
||||
$condition_string = $conditionGroup->toString($with_values);
|
||||
if($condition_string !== '') $condition_count++;
|
||||
if($condition_count === 1){
|
||||
$conditionGroup->setPipe("");
|
||||
$condition_string = $conditionGroup->toString($with_values);
|
||||
}
|
||||
$where .= $condition_string;
|
||||
}
|
||||
if(trim($where) == '') return '';
|
||||
|
||||
}
|
||||
return $where;
|
||||
}
|
||||
|
||||
function getGroupByString(){
|
||||
$groupBy = '';
|
||||
if($this->groups) if($this->groups[0] !== "")
|
||||
$groupBy = implode(', ', $this->groups);
|
||||
return $groupBy;
|
||||
}
|
||||
|
||||
function getOrderByString(){
|
||||
if(count($this->orderby) === 0) return '';
|
||||
$orderBy = '';
|
||||
foreach($this->orderby as $order){
|
||||
$orderBy .= $order->toString() .', ';
|
||||
}
|
||||
$orderBy = substr($orderBy, 0, -2);
|
||||
return $orderBy;
|
||||
}
|
||||
|
||||
function getLimit(){
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
function getLimitString(){
|
||||
$limit = '';
|
||||
if(count($this->limit) > 0){
|
||||
$limit = '';
|
||||
$limit .= $this->limit->toString();
|
||||
}
|
||||
return $limit;
|
||||
}
|
||||
|
||||
function getFirstTableName(){
|
||||
return $this->tables[0]->getName();
|
||||
}
|
||||
|
||||
function getArguments(){
|
||||
if(!isset($this->arguments)){
|
||||
$this->arguments = array();
|
||||
|
||||
// Column arguments
|
||||
if(count($this->columns) > 0){ // The if is for delete statements, all others must have columns
|
||||
foreach($this->columns as $column){
|
||||
if($column->show()){
|
||||
$arg = $column->getArgument();
|
||||
if($arg) $this->arguments[] = $arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
return $this->arguments;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
||||
38
classes/db/queryparts/Subquery.class.php
Normal file
38
classes/db/queryparts/Subquery.class.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
class Subquery extends Query {
|
||||
var $alias;
|
||||
var $join_type;
|
||||
|
||||
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit, $join_type = null){
|
||||
$this->alias = $alias;
|
||||
|
||||
$this->queryID = null;
|
||||
$this->action = "select";
|
||||
|
||||
$this->columns = $columns;
|
||||
$this->tables = $tables;
|
||||
$this->conditions = $conditions;
|
||||
$this->groups = $groups;
|
||||
$this->orderby = $orderby;
|
||||
$this->limit = $limit;
|
||||
$this->join_type = $join_type;
|
||||
}
|
||||
|
||||
function getAlias(){
|
||||
return $this->alias;
|
||||
}
|
||||
|
||||
function isJoinTable(){
|
||||
if($this->join_type) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function toString($with_values = true){
|
||||
$oDB = &DB::getInstance();
|
||||
return '(' .$oDB->getSelectSql($this, $with_values) . ')';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
140
classes/db/queryparts/condition/Condition.class.php
Normal file
140
classes/db/queryparts/condition/Condition.class.php
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
class Condition {
|
||||
var $column_name;
|
||||
var $argument;
|
||||
var $operation;
|
||||
var $pipe;
|
||||
|
||||
var $_value;
|
||||
|
||||
function Condition($column_name, $argument, $operation, $pipe = ""){
|
||||
$this->column_name = $column_name;
|
||||
$this->argument = $argument;
|
||||
$this->operation = $operation;
|
||||
$this->pipe = $pipe;
|
||||
if($this->hasArgument())
|
||||
$this->_value = $argument->getValue();
|
||||
else if(is_a($this->argument, 'Subquery'))
|
||||
$this->_value = $argument->toString();
|
||||
else
|
||||
$this->_value = $argument;
|
||||
}
|
||||
|
||||
function hasArgument(){
|
||||
return is_a($this->argument, 'Argument');
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
if($this->hasArgument()) return $this->argument;
|
||||
return null;
|
||||
}
|
||||
|
||||
function toString($withValue = true){
|
||||
if(!$this->show()) return '';
|
||||
if($withValue)
|
||||
return $this->toStringWithValue();
|
||||
return $this->toStringWithoutValue();
|
||||
}
|
||||
|
||||
function toStringWithoutValue(){
|
||||
if($this->hasArgument()){
|
||||
$value = $this->argument->getUnescapedValue();
|
||||
|
||||
if(is_array($value)){
|
||||
$q = '';
|
||||
foreach ($value as $v) $q .= '?,';
|
||||
if($q !== '') $q = substr($q, 0, -1);
|
||||
$q = '(' . $q . ')';
|
||||
}
|
||||
else $q = '?';
|
||||
return $this->pipe . ' ' . $this->getConditionPart($q);
|
||||
}
|
||||
else return $this->toString();
|
||||
}
|
||||
|
||||
function toStringWithValue(){
|
||||
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
|
||||
}
|
||||
|
||||
function setPipe($pipe){
|
||||
$this->pipe = $pipe;
|
||||
}
|
||||
|
||||
function show(){
|
||||
if($this->hasArgument() && !$this->argument->isValid()) return false;
|
||||
if($this->hasArgument() && ($this->_value === '\'\'')) return false;
|
||||
if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') return false;
|
||||
switch($this->operation) {
|
||||
case 'equal' :
|
||||
case 'more' :
|
||||
case 'excess' :
|
||||
case 'less' :
|
||||
case 'below' :
|
||||
case 'like_tail' :
|
||||
case 'like_prefix' :
|
||||
case 'like' :
|
||||
case 'in' :
|
||||
case 'notin' :
|
||||
case 'notequal' :
|
||||
// if variable is not set or is not string or number, return
|
||||
if(!isset($this->_value)) return false;
|
||||
if($this->_value === '') return false;
|
||||
if(!in_array(gettype($this->_value), array('string', 'integer'))) return false;
|
||||
break;
|
||||
case 'between' :
|
||||
if(!is_array($this->_value)) return false;
|
||||
if(count($this->_value)!=2) return false;
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function getConditionPart($value) {
|
||||
$name = $this->column_name;
|
||||
$operation = $this->operation;
|
||||
|
||||
switch($operation) {
|
||||
case 'equal' :
|
||||
return $name.' = '.$value;
|
||||
break;
|
||||
case 'more' :
|
||||
return $name.' >= '.$value;
|
||||
break;
|
||||
case 'excess' :
|
||||
return $name.' > '.$value;
|
||||
break;
|
||||
case 'less' :
|
||||
return $name.' <= '.$value;
|
||||
break;
|
||||
case 'below' :
|
||||
return $name.' < '.$value;
|
||||
break;
|
||||
case 'like_tail' :
|
||||
case 'like_prefix' :
|
||||
case 'like' :
|
||||
return $name.' like '.$value;
|
||||
break;
|
||||
case 'in' :
|
||||
return $name.' in '.$value;
|
||||
break;
|
||||
case 'notin' :
|
||||
return $name.' not in '.$value;
|
||||
break;
|
||||
case 'notequal' :
|
||||
return $name.' <> '.$value;
|
||||
break;
|
||||
case 'notnull' :
|
||||
return $name.' is not null';
|
||||
break;
|
||||
case 'null' :
|
||||
return $name.' is null';
|
||||
break;
|
||||
case 'between' :
|
||||
return $name.' between ' . $value[0] . ' and ' . $value[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
50
classes/db/queryparts/condition/ConditionGroup.class.php
Normal file
50
classes/db/queryparts/condition/ConditionGroup.class.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
class ConditionGroup {
|
||||
var $conditions;
|
||||
var $pipe;
|
||||
|
||||
function ConditionGroup($conditions, $pipe = "") {
|
||||
$this->conditions = $conditions;
|
||||
$this->pipe = $pipe;
|
||||
}
|
||||
|
||||
function setPipe($pipe){
|
||||
$this->pipe = $pipe;
|
||||
}
|
||||
|
||||
function toString($with_value = true){
|
||||
if($this->pipe !== "")
|
||||
$group = $this->pipe .' (';
|
||||
else $group = '';
|
||||
|
||||
$cond_indx = 0;
|
||||
|
||||
foreach($this->conditions as $condition){
|
||||
if($condition->show()){
|
||||
if($cond_indx === 0) $condition->setPipe("");
|
||||
$group .= $condition->toString($with_value) . ' ';
|
||||
$cond_indx++;
|
||||
}
|
||||
}
|
||||
// If the group has no conditions in it, return ''
|
||||
if($cond_indx === 0) return '';
|
||||
|
||||
if($this->pipe !== "")
|
||||
$group .= ')';
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
function getArguments(){
|
||||
$args = array();
|
||||
foreach($this->conditions as $condition){
|
||||
if($condition->show()){
|
||||
$arg = $condition->getArgument();
|
||||
if($arg) $args[] = $arg;
|
||||
}
|
||||
}
|
||||
return $args;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @class ClickCountExpression
|
||||
* @author Arnia Software
|
||||
* @brief
|
||||
*
|
||||
*/
|
||||
|
||||
class ClickCountExpression extends SelectExpression {
|
||||
var $click_count;
|
||||
|
||||
function ClickCountExpression($column_name, $alias = NULL, $click_count = false){
|
||||
parent::SelectExpression($column_name, $alias);
|
||||
|
||||
if(!is_bool($click_count)){
|
||||
error_log("Click_count value for $column_name was not boolean", 0);
|
||||
$this->click_count = false;
|
||||
return;
|
||||
}
|
||||
$this->click_count = $click_count;
|
||||
}
|
||||
|
||||
function show() {
|
||||
return $this->click_count;
|
||||
}
|
||||
|
||||
function getExpression(){
|
||||
return "$this->column_name = $this->column_name + 1";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
35
classes/db/queryparts/expression/DeleteExpression.class.php
Normal file
35
classes/db/queryparts/expression/DeleteExpression.class.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
/**
|
||||
* @class DeleteExpression
|
||||
* @author Arnia Software
|
||||
* @brief
|
||||
*
|
||||
*/
|
||||
|
||||
// TODO Fix this class
|
||||
class DeleteExpression extends Expression {
|
||||
var $value;
|
||||
|
||||
function DeleteExpression($column_name, $value){
|
||||
parent::Expression($column_name);
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
function getExpression(){
|
||||
return "$this->column_name = $this->value";
|
||||
}
|
||||
|
||||
function getValue(){
|
||||
// TODO Escape value according to column type instead of variable type
|
||||
if(!is_numeric($this->value)) return "'".$this->value."'";
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
function show(){
|
||||
if(!$this->value) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
30
classes/db/queryparts/expression/Expression.class.php
Normal file
30
classes/db/queryparts/expression/Expression.class.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* @class Expression
|
||||
* @author Corina
|
||||
* @brief Represents an expression used in select/update/insert/delete statements
|
||||
*
|
||||
* Examples (expressions are inside double square brackets):
|
||||
* select [[columnA]], [[columnB as aliasB]] from tableA
|
||||
* update tableA set [[columnA = valueA]] where columnB = something
|
||||
*
|
||||
*/
|
||||
|
||||
class Expression {
|
||||
var $column_name;
|
||||
|
||||
function Expression($column_name){
|
||||
$this->column_name = $column_name;
|
||||
}
|
||||
|
||||
function getColumnName(){
|
||||
return $this->column_name;
|
||||
}
|
||||
|
||||
function show() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function getExpression() {
|
||||
}
|
||||
}
|
||||
35
classes/db/queryparts/expression/InsertExpression.class.php
Normal file
35
classes/db/queryparts/expression/InsertExpression.class.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @class InsertExpression
|
||||
* @author Arnia Software
|
||||
* @brief
|
||||
*
|
||||
*/
|
||||
|
||||
class InsertExpression extends Expression {
|
||||
var $argument;
|
||||
|
||||
function InsertExpression($column_name, $argument){
|
||||
parent::Expression($column_name);
|
||||
$this->argument = $argument;
|
||||
}
|
||||
|
||||
function getValue($with_values = true){
|
||||
if($with_values)
|
||||
return $this->argument->getValue();
|
||||
return '?';
|
||||
}
|
||||
|
||||
function show(){
|
||||
$value = $this->argument->getValue();
|
||||
if(!isset($value)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return $this->argument;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
36
classes/db/queryparts/expression/SelectExpression.class.php
Normal file
36
classes/db/queryparts/expression/SelectExpression.class.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @class SelectExpression
|
||||
* @author Arnia Software
|
||||
* @brief Represents an expresion that appears in the select clause
|
||||
*
|
||||
* @remarks
|
||||
* $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
|
||||
*/
|
||||
|
||||
class SelectExpression extends Expression {
|
||||
var $column_alias;
|
||||
|
||||
function SelectExpression($column_name, $alias = NULL){
|
||||
parent::Expression($column_name);
|
||||
$this->column_alias = $alias;
|
||||
}
|
||||
|
||||
function getExpression() {
|
||||
return sprintf("%s%s", $this->column_name, $this->column_alias ? " as ".$this->column_alias : "");
|
||||
}
|
||||
|
||||
function show() {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
20
classes/db/queryparts/expression/StarExpression.class.php
Normal file
20
classes/db/queryparts/expression/StarExpression.class.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @class StarExpression
|
||||
* @author Corina
|
||||
* @brief Represents the * in 'select * from ...' statements
|
||||
*
|
||||
*/
|
||||
|
||||
class StarExpression extends SelectExpression {
|
||||
|
||||
function StarExpression(){
|
||||
parent::SelectExpression("*");
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
56
classes/db/queryparts/expression/UpdateExpression.class.php
Normal file
56
classes/db/queryparts/expression/UpdateExpression.class.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
/**
|
||||
* @class UpdateExpression
|
||||
* @author Arnia Software
|
||||
* @brief
|
||||
*
|
||||
*/
|
||||
|
||||
class UpdateExpression extends Expression {
|
||||
var $argument;
|
||||
|
||||
function UpdateExpression($column_name, $argument){
|
||||
parent::Expression($column_name);
|
||||
$this->argument = $argument;
|
||||
}
|
||||
|
||||
function getExpression($with_value = true){
|
||||
if($with_value)
|
||||
return $this->getExpressionWithValue();
|
||||
return $this->getExpressionWithoutValue();
|
||||
}
|
||||
|
||||
function getExpressionWithValue(){
|
||||
$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";
|
||||
}
|
||||
|
||||
function getExpressionWithoutValue(){
|
||||
$operation = $this->argument->getColumnOperation();
|
||||
if(isset($operation))
|
||||
return "$this->column_name = $this->column_name $operation ?";
|
||||
return "$this->column_name = ?";
|
||||
}
|
||||
|
||||
function getValue(){
|
||||
// TODO Escape value according to column type instead of variable type
|
||||
$value = $this->argument->getValue();
|
||||
if(!is_numeric($value)) return "'".$value."'";
|
||||
return $value;
|
||||
}
|
||||
|
||||
function show(){
|
||||
if(!$this->argument->getValue()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return $this->argument;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
35
classes/db/queryparts/limit/Limit.class.php
Normal file
35
classes/db/queryparts/limit/Limit.class.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
class Limit {
|
||||
var $start;
|
||||
var $list_count;
|
||||
var $page_count;
|
||||
var $page;
|
||||
|
||||
function Limit($list_count, $page= NULL, $page_count= NULL){
|
||||
$this->list_count = $list_count;
|
||||
if ($page){
|
||||
$this->start = ($page-1)*$list_count->getValue();
|
||||
$this->page_count = $page_count;
|
||||
$this->page = $page;
|
||||
}
|
||||
}
|
||||
|
||||
function isPageHandler(){//in case you choose to use query limit in other cases than page select
|
||||
if ($this->page)return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
function getOffset(){
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
function getLimit(){
|
||||
return $this->list_count->getValue();
|
||||
}
|
||||
|
||||
function toString(){
|
||||
if ($this->page) return $this->start . ' , ' . $this->list_count->getValue();
|
||||
else return $this->list_count->getValue();
|
||||
}
|
||||
}
|
||||
?>
|
||||
27
classes/db/queryparts/order/OrderByColumn.class.php
Normal file
27
classes/db/queryparts/order/OrderByColumn.class.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
class OrderByColumn {
|
||||
var $column_name;
|
||||
var $sort_order;
|
||||
|
||||
function OrderByColumn($column_name, $sort_order){
|
||||
$this->column_name = $column_name;
|
||||
$this->sort_order = $sort_order;
|
||||
}
|
||||
|
||||
function toString(){
|
||||
$result = is_a($this->column_name, 'Argument') ? $this->column_name->getValue() : $this->column_name;
|
||||
$result .= ' ';
|
||||
$result .= is_a($this->sort_order, 'Argument') ? $this->sort_order->getValue() : $this->sort_order;
|
||||
return $result;
|
||||
}
|
||||
|
||||
function getArguments(){
|
||||
$args = array();
|
||||
if(is_a($this->column_name, 'Argument'))
|
||||
$args[]= $this->column_name;
|
||||
if(is_a($this->sort_order, 'Argument'))
|
||||
$args[] = $this->sort_order;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
37
classes/db/queryparts/table/JoinTable.class.php
Normal file
37
classes/db/queryparts/table/JoinTable.class.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @class JoinTable
|
||||
* @author Arnia Software
|
||||
* @brief
|
||||
*
|
||||
* @remarks
|
||||
* $conditions in an array of Condition objects
|
||||
*
|
||||
*/
|
||||
|
||||
class JoinTable extends Table {
|
||||
var $join_type;
|
||||
var $conditions;
|
||||
|
||||
function JoinTable($name, $alias, $join_type, $conditions){
|
||||
parent::Table($name, $alias);
|
||||
$this->join_type = $join_type;
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
function toString($with_value = true){
|
||||
$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;
|
||||
}
|
||||
|
||||
function isJoinTable(){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
30
classes/db/queryparts/table/Table.class.php
Normal file
30
classes/db/queryparts/table/Table.class.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
class Table {
|
||||
var $name;
|
||||
var $alias;
|
||||
|
||||
function Table($name, $alias = NULL){
|
||||
$this->name = $name;
|
||||
$this->alias = $alias;
|
||||
}
|
||||
|
||||
function toString(){
|
||||
//return $this->name;
|
||||
return sprintf("%s%s", $this->name, $this->alias ? ' as ' . $this->alias : '');
|
||||
}
|
||||
|
||||
function getName(){
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
function getAlias(){
|
||||
return $this->alias;
|
||||
}
|
||||
|
||||
function isJoinTable(){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue