Support HAVING in XML queries

GROUP BY에 사용되는 <groups> 태그 안에 <having> 태그를 넣을 수 있습니다.
문법은 <conditions> 부분에 적용되는 것과 같습니다.

<query id="queryId" action="select">
    ...
    <groups>
        <group column="document_srl" />
        <having>
            <condition operation="more" column="document_srl" var="myvar" default="0" />
        </having>
    </groups>
    ...
</query>

결과: SELECT ... GROUP BY document_srl HAVING document_srl >= 0
This commit is contained in:
Kijin Sung 2018-07-03 15:40:52 +09:00
parent 2b5cded376
commit 20fa55a3fc
3 changed files with 89 additions and 6 deletions

View file

@ -711,6 +711,12 @@ class DB
$groupBy = ' GROUP BY ' . $groupBy;
}
$having = $query->getHavingString($with_values);
if($having != '')
{
$having = ' HAVING ' . $having;
}
$orderBy = $query->getOrderByString();
if($orderBy != '')
{
@ -723,7 +729,7 @@ class DB
$limit = ' LIMIT ' . $limit;
}
return $select . ' ' . $from . ' ' . $where . ' ' . $index_hint_list . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
return "$select $from $where $index_hint_list $groupBy $having $orderBy $limit";
}
/**

View file

@ -51,6 +51,12 @@ class Query extends BaseObject
*/
var $groups;
/**
* having list
* @var string|array
*/
var $having;
/**
* order list
* @var array
@ -232,6 +238,27 @@ class Query extends BaseObject
$this->groups = $groups;
}
function setHaving($conditions)
{
$this->having = array();
if(!isset($conditions) || count($conditions) === 0)
{
return;
}
if(!is_array($conditions))
{
$conditions = array($conditions);
}
foreach($conditions as $conditionGroup)
{
if($conditionGroup->show())
{
$this->having[] = $conditionGroup;
}
}
}
function setOrder($order)
{
if(!isset($order) || count($order) === 0)
@ -552,6 +579,30 @@ class Query extends BaseObject
return $groupBy;
}
/**
* Return having sql
* @param boolean $with_values
* @return string
*/
function getHavingString($with_values = TRUE)
{
$having = '';
$condition_count = 0;
foreach($this->having as $conditionGroup)
{
if($condition_count === 0)
{
$conditionGroup->setPipe("");
}
$condition_string = $conditionGroup->toString($with_values);
$having .= $condition_string;
$condition_count++;
}
return trim($having);
}
/**
* Return orderby sql
* @return string
@ -656,6 +707,19 @@ class Query extends BaseObject
}
}
// Having arguments
if(countobj($this->having) > 0)
{
foreach($this->having as $conditionGroup)
{
$args = $conditionGroup->getArguments();
if(countobj($args) > 0)
{
$this->arguments = array_merge($this->arguments, $args);
}
}
}
// Navigation arguments
if(countobj($this->orderby) > 0)
{
@ -669,6 +733,7 @@ class Query extends BaseObject
}
}
}
return $this->arguments;
}

View file

@ -71,6 +71,12 @@ class QueryTag
*/
var $groups;
/**
* Having in xml tags
* @var object
*/
var $having;
/**
* Navigation in xml tags
* @var object
@ -303,7 +309,8 @@ class QueryTag
$buff .= '$query->setSubquery(' . $this->subquery->toString() . ');' . PHP_EOL;
}
$buff .= '$query->setConditions(' . $this->conditions->toString() . ');' . PHP_EOL;
$buff .= '$query->setGroups(' . $this->groups->toString() . ');' . PHP_EOL;
$buff .= '$query->setGroups(' . $this->groups->toString() . ');' . PHP_EOL;
$buff .= '$query->setHaving(' . $this->having->toString() . ');' . PHP_EOL;
$buff .= '$query->setOrder(' . $this->navigation->getOrderByString() . ');' . PHP_EOL;
$buff .= '$query->setLimit(' . $this->navigation->getLimitString() . ');' . PHP_EOL;
@ -340,12 +347,16 @@ class QueryTag
{
if($this->query->groups)
{
return $this->groups = new GroupsTag($this->query->groups->group);
$this->groups = new GroupsTag($this->query->groups->group);
$this->having = new ConditionsTag($this->query->groups->having);
}
else
{
return $this->groups = new GroupsTag(NULL);
}
$this->groups = new GroupsTag(NULL);
$this->having = new ConditionsTag(array());
}
return $this->groups;
}
function getNavigation()
@ -385,7 +396,8 @@ class QueryTag
$arguments = array_merge($arguments, $this->subquery->getArguments());
}
$arguments = array_merge($arguments, $this->tables->getArguments());
$arguments = array_merge($arguments, $this->conditions->getArguments());
$arguments = array_merge($arguments, $this->conditions->getArguments());
$arguments = array_merge($arguments, $this->having->getArguments());
$arguments = array_merge($arguments, $this->navigation->getArguments());
return $arguments;
}