Query cache file - added condition to skip null arguments that do not need validation and are not mandatory.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9066 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-09-06 15:50:30 +00:00
parent 34246aa267
commit 33df36e715
12 changed files with 148 additions and 116 deletions

View file

@ -11,7 +11,6 @@
var $_show; var $_show;
var $_value_to_string; var $_value_to_string;
function Condition($column_name, $argument, $operation, $pipe){ function Condition($column_name, $argument, $operation, $pipe){
$this->column_name = $column_name; $this->column_name = $column_name;
$this->argument = $argument; $this->argument = $argument;

View file

@ -3,31 +3,38 @@
class ConditionWithArgument extends Condition { class ConditionWithArgument extends Condition {
function ConditionWithArgument($column_name, $argument, $operation, $pipe = ""){ function ConditionWithArgument($column_name, $argument, $operation, $pipe = ""){
if($argument === null) { $this->_show = false; return; }
parent::Condition($column_name, $argument, $operation, $pipe); parent::Condition($column_name, $argument, $operation, $pipe);
$this->_value = $argument->getValue(); $this->_value = $argument->getValue();
} }
function getArgument(){ function getArgument(){
return $this->argument; if(!$this->show()) return;
return $this->argument;
} }
function toStringWithoutValue(){ function toStringWithoutValue(){
$value = $this->argument->getUnescapedValue(); $value = $this->argument->getUnescapedValue();
if(is_array($value)){ if(is_array($value)){
$q = ''; $q = '';
foreach ($value as $v) $q .= '?,'; foreach ($value as $v) $q .= '?,';
if($q !== '') $q = substr($q, 0, -1); if($q !== '') $q = substr($q, 0, -1);
$q = '(' . $q . ')'; $q = '(' . $q . ')';
} }
else $q = '?'; else $q = '?';
return $this->pipe . ' ' . $this->getConditionPart($q); return $this->pipe . ' ' . $this->getConditionPart($q);
} }
function show(){ function show(){
if(!$this->argument->isValid()) return false; if(!isset($this->_show)){
if($this->_value === '\'\'') return false; if(!$this->argument->isValid()) $this->_show = false;
return parent::show(); if($this->_value === '\'\'') $this->_show = false;
if(!isset($this->_show)){
return parent::show();
}
}
return $this->_show;
} }
} }

View file

@ -31,7 +31,7 @@
require(_XE_PATH_.'classes/xml/xmlquery/tags/condition/ConditionGroupTag.class.php'); require(_XE_PATH_.'classes/xml/xmlquery/tags/condition/ConditionGroupTag.class.php');
require(_XE_PATH_.'classes/xml/xmlquery/tags/group/GroupsTag.class.php'); require(_XE_PATH_.'classes/xml/xmlquery/tags/group/GroupsTag.class.php');
require(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/NavigationTag.class.php'); require(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/NavigationTag.class.php');
require(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/IndexTag.class.php'); require(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/IndexTag.class.php');
require(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/LimitTag.class.php'); require(_XE_PATH_.'classes/xml/xmlquery/tags/navigation/LimitTag.class.php');
@ -44,50 +44,41 @@
class XmlQueryParser extends XmlParser { class XmlQueryParser extends XmlParser {
static $dbParser = null;
var $db_type;
function XmlQueryParser($db_type = NULL){ function XmlQueryParser(){
$this->db_type = $db_type;
} }
function &getInstance($db_type = NULL){ function &getInstance(){
static $theInstance = null; static $theInstance = null;
if(!isset($theInstance)){ if(!isset($theInstance)){
$theInstance = new XmlQueryParser($db_type); $theInstance = new XmlQueryParser();
} }
return $theInstance; return $theInstance;
} }
function parse($query_id, $xml_file, $cache_file) { function parse($query_id, $xml_file, $cache_file) {
// Read xml file // Read xml file
$xml_obj = $this->getXmlFileContent($xml_file); $xml_obj = $this->getXmlFileContent($xml_file);
// insert, update, delete, select action // insert, update, delete, select action
$action = strtolower($xml_obj->query->attrs->action); $action = strtolower($xml_obj->query->attrs->action);
if(!$action) return; if(!$action) return;
$parser = new QueryParser($xml_obj->query); $parser = new QueryParser($xml_obj->query);
FileHandler::writeFile($cache_file, $parser->toString()); FileHandler::writeFile($cache_file, $parser->toString());
} }
// singleton // singleton
function &getDBParser(){ function &getDBParser($force = false){
if(!$self->dbParser){ static $dbParser = null;
is_a($this,'XmlQueryParser')?$self=&$this:$self=&XmlQueryParser::getInstance(); if(!$dbParser || $force) {
if(isset($self->db_type)) $oDB = &DB::getInstance();
$oDB = &DB::getInstance($self->db_type); $dbParser = $oDB->getParser();
else }
$oDB = &DB::getInstance();
$self->dbParser = $oDB->getParser();
}
return $self->dbParser;
}
function setDBParser($value){ return $dbParser;
$self->dbParser = $value;
} }
function getXmlFileContent($xml_file){ function getXmlFileContent($xml_file){

View file

@ -51,35 +51,58 @@
} }
function toString(){ function toString(){
if($this->isConditionArgument()) if($this->argument_validator->hasOnlyDefaultValue()){
$arg = sprintf("\n$%s_argument = new ConditionArgument('%s', %s, '%s');\n" return sprintf("\n$%s_argument = %s;\n"
, $this->argument_name , $this->argument_name
, $this->argument_name , $this->argument_validator->getDefaultValueString()
, '$args->'.$this->variable_name
, $this->operation
); );
}
else if($this->isConditionArgument()){
$arg = sprintf("\n$%s_argument = new Argument('%s', %s);\n" // Instantiation
, $this->argument_name $arg = sprintf("\n$%s_argument = new ConditionArgument('%s', %s, '%s');\n"
, $this->argument_name , $this->argument_name
, '$args->'.$this->variable_name); , $this->argument_name
, '$args->'.$this->variable_name
, $this->operation
);
// Call methods to validate argument and ensure default value
$arg .= $this->argument_validator->toString();
// Prepare condition string
$arg .= sprintf("$%s_argument->createConditionValue();\n"
, $this->argument_name
);
$arg .= $this->argument_validator->toString(); // Check that argument passed validation, else return
$arg .= sprintf("if(!$%s_argument->isValid()) return $%s_argument->getErrorMessage();\n"
, $this->argument_name
, $this->argument_name
);
}
else {
$arg = sprintf("\n$%s_argument = new Argument('%s', %s);\n"
, $this->argument_name
, $this->argument_name
, '$args->'.$this->variable_name);
if($this->isConditionArgument()){ $arg .= $this->argument_validator->toString();
$arg .= sprintf("$%s_argument->createConditionValue();\n"
, $this->argument_name
);
}
$arg .= sprintf("if(!$%s_argument->isValid()) return $%s_argument->getErrorMessage();\n" $arg .= sprintf("if(!$%s_argument->isValid()) return $%s_argument->getErrorMessage();\n"
, $this->argument_name , $this->argument_name
, $this->argument_name , $this->argument_name
); );
return $arg; }
}
// If the argument is null, skip it
if($this->argument_validator->isIgnorable()){
$arg = sprintf("if(isset(%s)) {", '$args->'.$this->variable_name)
. $arg
. sprintf("} else \n$%s_argument = null;", $this->argument_name);
}
return $arg;
}
} }

View file

@ -22,10 +22,15 @@
$this->max_length = $tag->attrs->max_length; $this->max_length = $tag->attrs->max_length;
} }
function isIgnorable(){
if(isset($this->default_value) || isset($this->notnull)) return false;
return true;
}
function toString(){ function toString(){
$validator = ''; $validator = '';
if(isset($this->default_value)){ if(isset($this->default_value)){
$this->default_value = new DefaultValue($this->argument_name, $this->default_value); $this->default_value = new DefaultValue($this->argument_name, $this->default_value);
if($this->default_value->isSequence()) if($this->default_value->isSequence())
$validator .= '$db = &DB::getInstance(); $sequence = $db->getNextSequence(); '; $validator .= '$db = &DB::getInstance(); $sequence = $db->getNextSequence(); ';
if($this->default_value->isOperation()) if($this->default_value->isOperation())
@ -33,10 +38,10 @@
, $this->argument_name , $this->argument_name
, $this->default_value->getOperation() , $this->default_value->getOperation()
); );
$validator .= sprintf("$%s_argument->ensureDefaultValue(%s);\n" $validator .= sprintf("$%s_argument->ensureDefaultValue(%s);\n"
, $this->argument_name , $this->argument_name
, $this->default_value->toString() , $this->default_value->toString()
); );
} }
if($this->notnull){ if($this->notnull){
$validator .= sprintf("$%s_argument->checkNotNull();\n" $validator .= sprintf("$%s_argument->checkNotNull();\n"

View file

@ -5,7 +5,7 @@ class QueryTag {
var $query_id; var $query_id;
var $column_type; var $column_type;
var $query; var $query;
//xml tags //xml tags
var $columns; var $columns;
var $tables; var $tables;
@ -19,7 +19,7 @@ class QueryTag {
var $join_type; var $join_type;
var $alias; var $alias;
function QueryTag($query, $isSubQuery = false){ function QueryTag($query, $isSubQuery = false){
$this->action = $query->attrs->action; $this->action = $query->attrs->action;
$this->query_id = $query->attrs->id; $this->query_id = $query->attrs->id;
@ -28,11 +28,11 @@ class QueryTag {
if($this->isSubQuery) $this->action = 'select'; if($this->isSubQuery) $this->action = 'select';
$this->alias = $query->attrs->alias; $this->alias = $query->attrs->alias;
$this->join_type = $query->attrs->join_type; $this->join_type = $query->attrs->join_type;
$this->getColumns(); $this->getColumns();
$tables = $this->getTables(); $tables = $this->getTables();
$this->setTableColumnTypes($tables); $this->setTableColumnTypes($tables);
$this->getConditions(); $this->getConditions();
$this->getGroups(); $this->getGroups();
$this->getNavigation(); $this->getNavigation();
$this->getPrebuff(); $this->getPrebuff();
@ -42,15 +42,15 @@ class QueryTag {
function show(){ function show(){
return true; return true;
} }
function getQueryId(){ function getQueryId(){
return $this->query->attrs->query_id ? $this->query->attrs->query_id : $this->query->attrs->id; return $this->query->attrs->query_id ? $this->query->attrs->query_id : $this->query->attrs->id;
} }
function getAction(){ function getAction(){
return $this->query->attrs->action; return $this->query->attrs->action;
} }
function setTableColumnTypes($tables){ function setTableColumnTypes($tables){
$query_id = $this->getQueryId(); $query_id = $this->getQueryId();
if(!isset($this->column_type[$query_id])){ if(!isset($this->column_type[$query_id])){
@ -65,39 +65,43 @@ class QueryTag {
$this->column_type[$query_id] = $column_type; $this->column_type[$query_id] = $column_type;
} }
} }
function getColumns(){ function getColumns(){
if($this->action == 'select'){ if($this->action == 'select'){
return $this->columns = new SelectColumnsTag($this->query->columns); return $this->columns = new SelectColumnsTag($this->query->columns);
}else if($this->action == 'insert'){ }else if($this->action == 'insert'){
return $this->columns = new InsertColumnsTag($this->query->columns->column); return $this->columns = new InsertColumnsTag($this->query->columns->column);
}else if($this->action == 'update') { }else if($this->action == 'update') {
return $this->columns = new UpdateColumnsTag($this->query->columns->column); return $this->columns = new UpdateColumnsTag($this->query->columns->column);
}else if($this->action == 'delete') { }else if($this->action == 'delete') {
return $this->columns = null; return $this->columns = null;
} }
} }
function getPrebuff(){ function getPrebuff(){
// TODO Check if this work with arguments in join clause // TODO Check if this work with arguments in join clause
$arguments = $this->getArguments(); $arguments = $this->getArguments();
$prebuff = ''; $prebuff = '';
foreach($arguments as $argument){ foreach($arguments as $argument){
if(isset($argument) && $argument->getArgumentName()){ if(isset($argument)){
$prebuff .= $argument->toString(); $arg_name = $argument->getArgumentName();
$column_type = $this->column_type[$this->getQueryId()][$argument->getColumnName()]; if($arg_name){
if(isset($column_type)) $prebuff .= $argument->toString();
$prebuff .= sprintf("$%s_argument->setColumnType('%s');\n" $column_type = $this->column_type[$this->getQueryId()][$argument->getColumnName()];
, $argument->getArgumentName() if(isset($column_type))
, $column_type ); $prebuff .= sprintf("if($%s_argument !== null) $%s_argument->setColumnType('%s');\n"
} , $arg_name
, $arg_name
, $column_type );
}
}
} }
$prebuff .= "\n"; $prebuff .= "\n";
return $this->preBuff = $prebuff; return $this->preBuff = $prebuff;
} }
function getBuff(){ function getBuff(){
$buff = ''; $buff = '';
if($this->isSubQuery){ if($this->isSubQuery){
@ -106,67 +110,67 @@ class QueryTag {
$buff .= ($this->columns ? $this->columns->toString() : 'null' ). ', '.PHP_EOL; $buff .= ($this->columns ? $this->columns->toString() : 'null' ). ', '.PHP_EOL;
$buff .= $this->tables->toString() .','.PHP_EOL; $buff .= $this->tables->toString() .','.PHP_EOL;
$buff .= $this->conditions->toString() .',' .PHP_EOL; $buff .= $this->conditions->toString() .',' .PHP_EOL;
$buff .= $this->groups->toString() . ',' .PHP_EOL; $buff .= $this->groups->toString() . ',' .PHP_EOL;
$buff .= $this->navigation->getOrderByString() .','.PHP_EOL; $buff .= $this->navigation->getOrderByString() .','.PHP_EOL;
$limit = $this->navigation->getLimitString() ; $limit = $this->navigation->getLimitString() ;
$buff .= $limit ? $limit : 'null' . PHP_EOL; $buff .= $limit ? $limit : 'null' . PHP_EOL;
$buff .= $this->join_type ? "'" . $this->join_type . "'" : ''; $buff .= $this->join_type ? "'" . $this->join_type . "'" : '';
$buff .= ')'; $buff .= ')';
$this->buff = $buff; $this->buff = $buff;
return $this->buff; return $this->buff;
} }
$buff .= '$query = new Query();'.PHP_EOL; $buff .= '$query = new Query();'.PHP_EOL;
$buff .= sprintf('$query->setQueryId("%s");%s', $this->query_id, "\n"); $buff .= sprintf('$query->setQueryId("%s");%s', $this->query_id, "\n");
$buff .= sprintf('$query->setAction("%s");%s', $this->action, "\n"); $buff .= sprintf('$query->setAction("%s");%s', $this->action, "\n");
$buff .= $this->preBuff; $buff .= $this->preBuff;
if($this->columns) if($this->columns)
$buff .= '$query->setColumns(' . $this->columns->toString() . ');'.PHP_EOL; $buff .= '$query->setColumns(' . $this->columns->toString() . ');'.PHP_EOL;
$buff .= '$query->setTables(' . $this->tables->toString() .');'.PHP_EOL; $buff .= '$query->setTables(' . $this->tables->toString() .');'.PHP_EOL;
$buff .= '$query->setConditions('.$this->conditions->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->setOrder(' . $this->navigation->getOrderByString() .');'.PHP_EOL; $buff .= '$query->setOrder(' . $this->navigation->getOrderByString() .');'.PHP_EOL;
$buff .= '$query->setLimit(' . $this->navigation->getLimitString() .');'.PHP_EOL; $buff .= '$query->setLimit(' . $this->navigation->getLimitString() .');'.PHP_EOL;
$this->buff = $buff; $this->buff = $buff;
return $this->buff; return $this->buff;
} }
function getTables(){ function getTables(){
return $this->tables = new TablesTag($this->query->tables); return $this->tables = new TablesTag($this->query->tables);
} }
function getConditions(){ function getConditions(){
return $this->conditions = new ConditionsTag($this->query->conditions); return $this->conditions = new ConditionsTag($this->query->conditions);
} }
function getGroups(){ function getGroups(){
return $this->groups = new GroupsTag($this->query->groups->group); return $this->groups = new GroupsTag($this->query->groups->group);
} }
function getNavigation(){ function getNavigation(){
return $this->navigation = new NavigationTag($this->query->navigation); return $this->navigation = new NavigationTag($this->query->navigation);
} }
function toString(){ function toString(){
return $this->buff; return $this->buff;
} }
function getTableString(){ function getTableString(){
return $this->buff; return $this->buff;
} }
function getConditionString(){ function getConditionString(){
return $this->buff; return $this->buff;
} }
function getExpressionString(){ function getExpressionString(){
return $this->buff; return $this->buff;
} }
function getArguments(){ function getArguments(){
$arguments = array(); $arguments = array();
if($this->columns) if($this->columns)
@ -174,8 +178,8 @@ class QueryTag {
$arguments = array_merge($arguments, $this->tables->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->navigation->getArguments()); $arguments = array_merge($arguments, $this->navigation->getArguments());
return $arguments; return $arguments;
} }
} }
?> ?>

View file

@ -34,6 +34,8 @@
// remove cache dir // remove cache dir
FileHandler::removeDir( _XE_PATH_ . 'files/cache'); FileHandler::removeDir( _XE_PATH_ . 'files/cache');
XmlQueryParser::getDBParser(true);
} }
/** /**
@ -41,7 +43,6 @@
*/ */
protected function tearDown() { protected function tearDown() {
unset($GLOBALS['__DB__']); unset($GLOBALS['__DB__']);
XmlQueryParser::setDBParser(null);
} }
} }
?> ?>

View file

@ -16,6 +16,7 @@
$db_info->slave_db = array(array('db_type' => 'cubrid','db_table_prefix' => 'xe_')); $db_info->slave_db = array(array('db_type' => 'cubrid','db_table_prefix' => 'xe_'));
$oContext->setDbInfo($db_info); $oContext->setDbInfo($db_info);
XmlQueryParser::getDBParser(true);
} }
/** /**
@ -23,7 +24,6 @@
*/ */
protected function tearDown() { protected function tearDown() {
unset($GLOBALS['__DB__']); unset($GLOBALS['__DB__']);
XmlQueryParser::setDBParser(null);
} }
} }
?> ?>

View file

@ -34,6 +34,8 @@
// remove cache dir // remove cache dir
FileHandler::removeDir( _XE_PATH_ . 'files/cache'); FileHandler::removeDir( _XE_PATH_ . 'files/cache');
XmlQueryParser::getDBParser(true);
} }
/** /**
@ -41,7 +43,6 @@
*/ */
protected function tearDown() { protected function tearDown() {
unset($GLOBALS['__DB__']); unset($GLOBALS['__DB__']);
XmlQueryParser::setDBParser(null);
} }
} }
?> ?>

View file

@ -14,11 +14,12 @@
$db_info->slave_db = array(array('db_type' => 'mssql','db_table_prefix' => 'xe_')); $db_info->slave_db = array(array('db_type' => 'mssql','db_table_prefix' => 'xe_'));
$oContext->setDbInfo($db_info); $oContext->setDbInfo($db_info);
XmlQueryParser::getDBParser(true);
} }
protected function tearDown() { protected function tearDown() {
unset($GLOBALS['__DB__']); unset($GLOBALS['__DB__']);
XmlQueryParser::setDBParser(null);
} }
} }
?> ?>

View file

@ -14,7 +14,7 @@
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);
} }
function testRquiredParameter(){ function testRequiredParameter(){
$xml_file = _XE_PATH_ . "modules/module/queries/getAdminId.xml"; $xml_file = _XE_PATH_ . "modules/module/queries/getAdminId.xml";
$argsString = ''; $argsString = '';
$expected = 'Date incorecte! Query-ul nu a putut fi executat.'; $expected = 'Date incorecte! Query-ul nu a putut fi executat.';

View file

@ -14,7 +14,7 @@
$this->_test($xml_file, $argsString, $expected, array(10)); $this->_test($xml_file, $argsString, $expected, array(10));
} }
function testRquiredParameter(){ function testRequiredParameter(){
$xml_file = _XE_PATH_ . "modules/module/queries/getAdminId.xml"; $xml_file = _XE_PATH_ . "modules/module/queries/getAdminId.xml";
$argsString = ''; $argsString = '';
$expected = 'Date incorecte! Query-ul nu a putut fi executat.'; $expected = 'Date incorecte! Query-ul nu a putut fi executat.';