diff --git a/classes/xml/xmlquery/QueryParser.class.php b/classes/xml/xmlquery/QueryParser.class.php index d9bc88399..5272c7857 100644 --- a/classes/xml/xmlquery/QueryParser.class.php +++ b/classes/xml/xmlquery/QueryParser.class.php @@ -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 "setQueryId("%s");%s', $this->query_id, "\n") - . sprintf('$query->setAction("%s");%s', $this->action, "\n") - . $prebuff - . $buff - . 'return $query; ?>'; - - + return "queryTag->toString() + . 'return $query; ?>'; } } diff --git a/classes/xml/xmlquery/tags/column/InsertColumnsTag.class.php b/classes/xml/xmlquery/tags/column/InsertColumnsTag.class.php index 5f0893b1f..a067a6d10 100644 --- a/classes/xml/xmlquery/tags/column/InsertColumnsTag.class.php +++ b/classes/xml/xmlquery/tags/column/InsertColumnsTag.class.php @@ -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); } } diff --git a/classes/xml/xmlquery/tags/column/SelectColumnsTag.class.php b/classes/xml/xmlquery/tags/column/SelectColumnsTag.class.php index e5e741042..f669329e4 100644 --- a/classes/xml/xmlquery/tags/column/SelectColumnsTag.class.php +++ b/classes/xml/xmlquery/tags/column/SelectColumnsTag.class.php @@ -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); } } diff --git a/classes/xml/xmlquery/tags/column/UpdateColumnsTag.class.php b/classes/xml/xmlquery/tags/column/UpdateColumnsTag.class.php index a0a856af1..581f9ad8a 100644 --- a/classes/xml/xmlquery/tags/column/UpdateColumnsTag.class.php +++ b/classes/xml/xmlquery/tags/column/UpdateColumnsTag.class.php @@ -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); } } diff --git a/classes/xml/xmlquery/tags/condition/ConditionGroupTag.class.php b/classes/xml/xmlquery/tags/condition/ConditionGroupTag.class.php index 728be23b1..57b250e73 100644 --- a/classes/xml/xmlquery/tags/condition/ConditionGroupTag.class.php +++ b/classes/xml/xmlquery/tags/condition/ConditionGroupTag.class.php @@ -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); } } diff --git a/classes/xml/xmlquery/tags/query/QueryTag.class.php b/classes/xml/xmlquery/tags/query/QueryTag.class.php new file mode 100644 index 000000000..94906c6db --- /dev/null +++ b/classes/xml/xmlquery/tags/query/QueryTag.class.php @@ -0,0 +1,142 @@ +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; + } +} +?> \ No newline at end of file diff --git a/classes/xml/xmlquery/tags/table/TablesTag.class.php b/classes/xml/xmlquery/tags/table/TablesTag.class.php index d4560594b..aaa58ae63 100644 --- a/classes/xml/xmlquery/tags/table/TablesTag.class.php +++ b/classes/xml/xmlquery/tags/table/TablesTag.class.php @@ -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); } }