start implementing SubQuery

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8538 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
lickawtl 2011-06-27 13:45:52 +00:00
parent 763fc6d56b
commit af9ced8c5c
7 changed files with 160 additions and 91 deletions

View file

@ -10,28 +10,15 @@ require_once(_XE_PATH_.'classes/xml/xmlquery/tags/condition/JoinConditionsTag.cl
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/group/GroupsTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/NavigationTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/IndexTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/query/QueryTag.class.php');
class QueryParser {
var $query;
var $action;
var $query_id;
var $column_type;
function QueryParser($query){
$this->query = $query;
$this->action = $this->query->attrs->action;
$this->query_id = $this->query->attrs->id;
var $queryTag;
function QueryParser($query, $isSubQuery = false){
$this->queryTag = new QueryTag($query, $isSubQuery);
}
function getQueryId(){
return $this->query->attrs->query_id ? $this->query->attrs->query_id : $this->query->attrs->id;
}
function getAction(){
return $this->query->attrs->action;
}
function getTableInfo($query_id, $table_name){
$column_type = array();
@ -77,75 +64,10 @@ class QueryParser {
return $column_type;
}
function setTableColumnTypes($tables){
$query_id = $this->getQueryId();
if(!isset($this->column_type[$query_id])){
$table_tags = $tables->getTables();
$column_type = array();
foreach($table_tags as $table_tag){
$tag_column_type = $this->getTableInfo($query_id, $table_tag->getTableName());
$column_type = array_merge($column_type, $tag_column_type);
}
$this->column_type[$query_id] = $column_type;
}
}
function toString(){
if($this->action == 'select'){
$columns = new SelectColumnsTag($this->query->columns->column);
}else if($this->action == 'insert'){
$columns = new InsertColumnsTag($this->query->columns->column);
}else if($this->action == 'update') {
$columns = new UpdateColumnsTag($this->query->columns->column);
}else if($this->action == 'delete') {
$columns = null;
}
$tables = new TablesTag($this->query->tables->table);
$conditions = new ConditionsTag($this->query->conditions);
$groups = new GroupsTag($this->query->groups->group);
$navigation = new NavigationTag($this->query->navigation);
$this->setTableColumnTypes($tables);
// TODO Check if this work with arguments in join clause
$arguments = array();
if($columns)
$arguments = array_merge($arguments, $columns->getArguments());
$arguments = array_merge($arguments, $conditions->getArguments());
$arguments = array_merge($arguments, $navigation->getArguments());
$prebuff = '';
foreach($arguments as $argument){
if(isset($argument) && $argument->getArgumentName()){
$prebuff .= $argument->toString();
$prebuff .= sprintf("$%s_argument->setColumnType('%s');\n"
, $argument->getArgumentName()
, $this->column_type[$this->getQueryId()][$argument->getColumnName()] );
}
}
$prebuff .= "\n";
$buff = '';
if($columns)
$buff .= '$query->setColumns(' . $columns->toString() . ');'.PHP_EOL;
$buff .= '$query->setTables(' . $tables->toString() .');'.PHP_EOL;
$buff .= '$query->setConditions('.$conditions->toString() .');'.PHP_EOL;
$buff .= '$query->setGroups(' . $groups->toString() . ');'.PHP_EOL;
$buff .= '$query->setOrder(' . $navigation->getOrderByString() .');'.PHP_EOL;
$buff .= '$query->setLimit(' . $navigation->getLimitString() .');'.PHP_EOL;
return "<?php if(!defined('__ZBXE__')) exit();\n"
. '$query = new Query();'.PHP_EOL
. sprintf('$query->setQueryId("%s");%s', $this->query_id, "\n")
. sprintf('$query->setAction("%s");%s', $this->action, "\n")
. $prebuff
. $buff
. 'return $query; ?>';
return "<?php if(!defined('__ZBXE__')) exit();\n"
.$this->queryTag->toString()
. 'return $query; ?>';
}
}

View file

@ -21,7 +21,8 @@
if(!is_array($xml_columns)) $xml_columns = array($xml_columns);
foreach($xml_columns as $column){
$this->columns[] = new InsertColumnTag($column);
if($column->name === 'query') $this->columns[] = new QueryTag($column, true);
else $this->columns[] = new InsertColumnTag($column);
}
}

View file

@ -17,7 +17,8 @@
if(!is_array($xml_columns)) $xml_columns = array($xml_columns);
foreach($xml_columns as $column){
$this->columns[] = new SelectColumnTag($column);
if($column->name === 'query') $this->columns[] = new QueryTag($column, true);
else $this->columns[] = new SelectColumnTag($column);
}
}

View file

@ -19,7 +19,8 @@
if(!is_array($xml_columns)) $xml_columns = array($xml_columns);
foreach($xml_columns as $column){
$this->columns[] = new UpdateColumnTag($column);
if($column->name === 'query') $this->columns[] = new QueryTag($column, true);
else $this->columns[] = new UpdateColumnTag($column);
}
}

View file

@ -11,7 +11,8 @@
if(count($conditions))require_once(_XE_PATH_.'classes/xml/xmlquery/tags/condition/ConditionTag.class.php');
foreach($conditions as $condition){
$this->conditions[] = new ConditionTag($condition);
if($condition->name === 'query') $this->conditions[] = new QueryTag($condition, true);
else $this->conditions[] = new ConditionTag($condition);
}
}

View file

@ -0,0 +1,142 @@
<?php
class QueryTag {
var $action;
var $query_id;
var $column_type;
var $query;
//xml tags
var $columns;
var $tables;
var $conditions;
var $groups;
var $navigation;
var $arguments;
var $preBuff;
var $buff;
var $isSubQuery;
function QueryTag($query, $isSubQuery = false){
$this->action = $query->attrs->action;
$this->query_id = $query->attrs->id;
$this->query = $query;
$this->isSubQuery = $isSubQuery;
$this->getColumns();
$tables = $this->getTables();
$this->setTableColumnTypes($tables);
$this->getConditions();
$this->getGroups();
$this->getNavigation();
$this->getPrebuff();
$this->getBuff();
}
function getQueryId(){
return $this->query->attrs->query_id ? $this->query->attrs->query_id : $this->query->attrs->id;
}
function getAction(){
return $this->query->attrs->action;
}
function setTableColumnTypes($tables){
$query_id = $this->getQueryId();
if(!isset($this->column_type[$query_id])){
$table_tags = $tables->getTables();
$column_type = array();
foreach($table_tags as $table_tag){
$tag_column_type = QueryParser::getTableInfo($query_id, $table_tag->getTableName());
$column_type = array_merge($column_type, $tag_column_type);
}
$this->column_type[$query_id] = $column_type;
}
}
function getColumns(){
if($this->action == 'select'){
return $this->columns = new SelectColumnsTag($this->query->columns->column);
}else if($this->action == 'insert'){
return $this->columns = new InsertColumnsTag($this->query->columns->column);
}else if($this->action == 'update') {
return $this->columns = new UpdateColumnsTag($this->query->columns->column);
}else if($this->action == 'delete') {
return $this->columns = null;
}
}
function getPrebuff(){
// TODO Check if this work with arguments in join clause
$arguments = array();
if($this->columns)
$arguments = array_merge($arguments, $this->columns->getArguments());
$arguments = array_merge($arguments, $this->conditions->getArguments());
$arguments = array_merge($arguments, $this->navigation->getArguments());
$prebuff = '';
foreach($arguments as $argument){
if(isset($argument) && $argument->getArgumentName()){
$prebuff .= $argument->toString();
$prebuff .= sprintf("$%s_argument->setColumnType('%s');\n"
, $argument->getArgumentName()
, $this->column_type[$this->getQueryId()][$argument->getColumnName()] );
}
}
$prebuff .= "\n";
return $this->preBuff = $prebuff;
}
function getBuff(){
$buff = '';
if($this->isSubQuery) $buff .= '$query = new Query();'.PHP_EOL;
else $buff .= '$query = new Query();'.PHP_EOL;
$buff .= sprintf('$query->setQueryId("%s");%s', $this->query_id, "\n");
$buff .= sprintf('$query->setAction("%s");%s', $this->action, "\n");
$buff .= $this->preBuff;
if($this->columns)
$buff .= '$query->setColumns(' . $this->columns->toString() . ');'.PHP_EOL;
$buff .= '$query->setTables(' . $this->tables->toString() .');'.PHP_EOL;
$buff .= '$query->setConditions('.$this->conditions->toString() .');'.PHP_EOL;
$buff .= '$query->setGroups(' . $this->groups->toString() . ');'.PHP_EOL;
$buff .= '$query->setOrder(' . $this->navigation->getOrderByString() .');'.PHP_EOL;
$buff .= '$query->setLimit(' . $this->navigation->getLimitString() .');'.PHP_EOL;
return $this->buff = $buff;
}
function getTables(){
return $this->tables = new TablesTag($this->query->tables->table);
}
function getConditions(){
return $this->conditions = new ConditionsTag($this->query->conditions);
}
function getGroups(){
return $this->groups = new GroupsTag($this->query->groups->group);
}
function getNavigation(){
return $this->navigation = new NavigationTag($this->query->navigation);
}
function toString(){
return $this->buff;
}
function getTableString(){
return $this->buff;
}
function getConditionString(){
return $this->buff;
}
function getExpressionString(){
return $this->buff;
}
}
?>

View file

@ -10,7 +10,8 @@
if(count($xml_tables)) require_once(_XE_PATH_.'classes/xml/xmlquery/tags/table/TableTag.class.php');
foreach($xml_tables as $table){
$this->tables[] = new TableTag($table);
if($table->name === 'query') $this->tables[] = new QueryTag($table, true);
else $this->tables[] = new TableTag($table);
}
}