Updated DB classes for supporting prepared statements - SQL string can now be returned with '?' instead of argument values.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8458 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-06-07 19:01:58 +00:00
parent 313bfca8e8
commit b6a1088995
14 changed files with 107 additions and 52 deletions

View file

@ -457,16 +457,16 @@
$this->_query($query); $this->_query($query);
} }
function getSelectSql($query){ function getSelectSql($query, $with_values = true){
$select = $query->getSelectString(); $select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query"); if($select == '') return new Object(-1, "Invalid query");
$select = 'SELECT ' .$select; $select = 'SELECT ' .$select;
$from = $query->getFromString(); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '') return new Object(-1, "Invalid query");
$from = ' FROM '.$from; $from = ' FROM '.$from;
$where = $query->getWhereString(); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '') $where = ' WHERE ' . $where;
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
@ -481,7 +481,7 @@
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }
function getDeleteSql($query){ function getDeleteSql($query, $with_values = true){
$sql = 'DELETE '; $sql = 'DELETE ';
// TODO Add support for deleting based on alias, for both simple FROM and multi table join FROM clause // TODO Add support for deleting based on alias, for both simple FROM and multi table join FROM clause
@ -489,32 +489,32 @@
$sql .= $tables[0]->getAlias(); $sql .= $tables[0]->getAlias();
$from = $query->getFromString(); $from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query"); if($from == '') return new Object(-1, "Invalid query");
$sql .= ' FROM '.$from; $sql .= ' FROM '.$from;
$where = $query->getWhereString(); $where = $query->getWhereString($with_values);
if($where != '') $sql .= ' WHERE ' . $where; if($where != '') $sql .= ' WHERE ' . $where;
return $sql; return $sql;
} }
function getUpdateSql($query){ function getUpdateSql($query, $with_values = true){
$columnsList = $query->getSelectString(); $columnsList = $query->getSelectString();
if($columnsList == '') return new Object(-1, "Invalid query"); if($columnsList == '') return new Object(-1, "Invalid query");
$tableName = $query->getFirstTableName(); $tableName = $query->getFirstTableName();
if($tableName == '') return new Object(-1, "Invalid query"); if($tableName == '') return new Object(-1, "Invalid query");
$where = $query->getWhereString(); $where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where; if($where != '') $where = ' WHERE ' . $where;
return "UPDATE $tableName SET $columnsList ".$where; return "UPDATE $tableName SET $columnsList ".$where;
} }
function getInsertSql($query){ function getInsertSql($query, $with_values = true){
$tableName = $query->getFirstTableName(); $tableName = $query->getFirstTableName();
$values = $query->getInsertString(); $values = $query->getInsertString($with_values);
return "INSERT INTO $tableName \n $values"; return "INSERT INTO $tableName \n $values";
} }

View file

@ -434,6 +434,42 @@
return $this->_query($query); 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 * @brief Handle selectAct
* *
@ -451,6 +487,9 @@
return $buff; return $buff;
} }
function getParser(){
return new DBParser("[", "]");
}
} }

View file

@ -11,6 +11,11 @@
var $orderby; var $orderby;
var $limit; var $limit;
var $arguments = array();
function addArgument($argument){
$this->arguments[] = $argument;
}
function setQueryId($queryID){ function setQueryId($queryID){
$this->queryID = $queryID; $this->queryID = $queryID;
@ -106,28 +111,28 @@
return $this->action; return $this->action;
} }
function getSelectString(){ function getSelectString($with_values = true){
$select = ''; $select = '';
foreach($this->columns as $column){ foreach($this->columns as $column){
if($column->show()) if($column->show())
$select .= $column->getExpression() . ', '; $select .= $column->getExpression($with_values) . ', ';
} }
if(trim($select) == '') return ''; if(trim($select) == '') return '';
$select = substr($select, 0, -2); $select = substr($select, 0, -2);
return $select; return $select;
} }
function getUpdateString(){ function getUpdateString($with_values = true){
return $this->getSelectString(); return $this->getSelectString($with_values);
} }
function getInsertString(){ function getInsertString($with_values = true){
$columnsList = ''; $columnsList = '';
$valuesList = ''; $valuesList = '';
foreach($this->columns as $column){ foreach($this->columns as $column){
if($column->show()){ if($column->show()){
$columnsList .= $column->getColumnName() . ', '; $columnsList .= $column->getColumnName() . ', ';
$valuesList .= $column->getValue() . ', '; $valuesList .= $column->getValue($with_values) . ', ';
} }
} }
$columnsList = substr($columnsList, 0, -2); $columnsList = substr($columnsList, 0, -2);
@ -140,23 +145,23 @@
return $this->tables; return $this->tables;
} }
function getFromString(){ function getFromString($with_values = true){
$from = ''; $from = '';
$simple_table_count = 0; $simple_table_count = 0;
foreach($this->tables as $table){ foreach($this->tables as $table){
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString() . ' '; if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' ';
else $from .= ', '.$table->toString() . ' '; else $from .= ', '.$table->toString($with_values) . ' ';
$simple_table_count++; $simple_table_count++;
} }
if(trim($from) == '') return ''; if(trim($from) == '') return '';
return $from; return $from;
} }
function getWhereString(){ function getWhereString($with_values = true){
$where = ''; $where = '';
if(count($this->conditions) > 0){ if(count($this->conditions) > 0){
foreach($this->conditions as $conditionGroup){ foreach($this->conditions as $conditionGroup){
$where .= $conditionGroup->toString(); $where .= $conditionGroup->toString($with_values);
} }
if(trim($where) == '') return ''; if(trim($where) == '') return '';

View file

@ -23,8 +23,10 @@
return is_a($this->argument, 'Argument'); return is_a($this->argument, 'Argument');
} }
function toString(){ function toString($withValue = true){
return $this->toStringWithValue(); if($withValue)
return $this->toStringWithValue();
return $this->toStringWithoutValue();
} }
function toStringWithoutValue(){ function toStringWithoutValue(){

View file

@ -4,12 +4,12 @@
var $conditions; var $conditions;
var $pipe; var $pipe;
function ConditionGroup($conditions, $pipe = ""){ function ConditionGroup($conditions, $pipe = "") {
$this->conditions = $conditions; $this->conditions = $conditions;
$this->pipe = $pipe; $this->pipe = $pipe;
} }
function toString(){ function toString($with_value = true){
if($this->pipe !== "") if($this->pipe !== "")
$group = $this->pipe .' ('; $group = $this->pipe .' (';
else $group = ''; else $group = '';
@ -19,7 +19,7 @@
foreach($this->conditions as $condition){ foreach($this->conditions as $condition){
if($condition->show()){ if($condition->show()){
if($cond_indx === 0) $condition->setPipe(""); if($cond_indx === 0) $condition->setPipe("");
$group .= $condition->toString() . ' '; $group .= $condition->toString($with_value) . ' ';
$cond_indx++; $cond_indx++;
} }
} }

View file

@ -15,8 +15,10 @@
$this->argument = $argument; $this->argument = $argument;
} }
function getValue(){ function getValue($with_values = true){
return $this->argument->getValue(); if($with_values)
return $this->argument->getValue();
return '?';
} }
function show(){ function show(){

View file

@ -14,8 +14,10 @@
$this->argument = $argument; $this->argument = $argument;
} }
function getExpression(){ function getExpression($with_value = true){
return $this->getExpressionWithValue(); if($with_value)
return $this->getExpressionWithValue();
return $this->getExpressionWithoutValue();
} }
function getExpressionWithValue(){ function getExpressionWithValue(){

View file

@ -20,12 +20,12 @@
$this->conditions = $conditions; $this->conditions = $conditions;
} }
function toString(){ function toString($with_value = true){
$part = $this->join_type . ' ' . $this->name ; $part = $this->join_type . ' ' . $this->name ;
$part .= $this->alias ? ' as ' . $this->alias : ''; $part .= $this->alias ? ' as ' . $this->alias : '';
$part .= ' on '; $part .= ' on ';
foreach($this->conditions as $conditionGroup) foreach($this->conditions as $conditionGroup)
$part .= $conditionGroup->toString(); $part .= $conditionGroup->toString($with_value);
return $part; return $part;
} }

View file

@ -14,8 +14,14 @@
class XmlQueryParser extends XmlParser { class XmlQueryParser extends XmlParser {
var $dbParser; var $dbParser;
var $db_type;
function XmlQueryParser($db_type = NULL){
$this->db_type = $db_type;
}
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);
@ -29,15 +35,17 @@
} }
// singleton // singleton
/* function &getDBParser(){ function &getDBParser(){
static $dbParser; static $dbParser;
if(!$dbParser){ if(!$dbParser){
//$oDB = &DB::getInstance(); if(isset($this->db_type))
//$dbParser = $oDB->getParser(); $oDB = &DB::getInstance($this->db_type);
return new DBParser('"'); else
$oDB = &DB::getInstance();
$dbParser = $oDB->getParser();
} }
return $dbParser; return $dbParser;
}*/ }
function getXmlFileContent($xml_file){ function getXmlFileContent($xml_file){
$buff = FileHandler::readFile($xml_file); $buff = FileHandler::readFile($xml_file);
@ -46,11 +54,5 @@
unset($buff); unset($buff);
return $xml_obj; return $xml_obj;
} }
function &getDBParser(){
if(!$this->dbParser)
$this->dbParser = new DBParser('"');
return $this->dbParser;
}
} }
?> ?>

View file

@ -119,9 +119,10 @@ class QueryParser {
foreach($arguments as $argument){ foreach($arguments as $argument){
if(isset($argument) && $argument->getArgumentName()){ if(isset($argument) && $argument->getArgumentName()){
$prebuff .= $argument->toString(); $prebuff .= $argument->toString();
$prebuff .= sprintf("$%s_argument->escapeValue('%s');\n" $prebuff .= sprintf("$%s_argument->setColumnType('%s');\n"
, $argument->getArgumentName() , $argument->getArgumentName()
, $this->column_type[$this->getQueryId()][$argument->getColumnName()] ); , $this->column_type[$this->getQueryId()][$argument->getColumnName()] );
$prebuff .= sprintf('$query->addArgument($%s_argument);', $argument->getArgumentName());
} }
} }
$prebuff .= "\n"; $prebuff .= "\n";

View file

@ -36,7 +36,7 @@
$this->value = $default_value; $this->value = $default_value;
} }
function escapeValue($column_type){ function setColumnType($column_type){
if(!isset($this->value)) return; if(!isset($this->value)) return;
if($column_type === '') return; if($column_type === '') return;

View file

@ -12,11 +12,11 @@
return _XE_PATH_ . $type ."/".$name."/queries/" . $query_name . ".xml"; return _XE_PATH_ . $type ."/".$name."/queries/" . $query_name . ".xml";
} }
function getNewParserOutput($xml_file, $escape_char = '"'){ function getNewParserOutput($xml_file, $escape_char = '"', $db_type = NULL){
$newXmlQueryParser = new XmlQueryParser(); $newXmlQueryParser = new XmlQueryParser($db_type);
$xml_obj = $newXmlQueryParser->getXmlFileContent($xml_file); $xml_obj = $newXmlQueryParser->getXmlFileContent($xml_file);
$dbParser = new DBParser($escape_char); $dbParser = $newXmlQueryParser->getDBParser();
$parser = new QueryParser($xml_obj->query, $dbParser); $parser = new QueryParser($xml_obj->query, $dbParser);
return $parser->toString(); return $parser->toString();
} }
@ -59,9 +59,9 @@
.'</pre>'; .'</pre>';
} }
function getNewParserOutputString($xml_file, $escape_char, $argsString){ function getNewParserOutputString($xml_file, $escape_char, $argsString, $db_type = NULL){
$outputString = ''; $outputString = '';
$outputString = $this->getNewParserOutput($xml_file, $escape_char); $outputString = $this->getNewParserOutput($xml_file, $escape_char, $db_type);
$outputString = $this->cleanOutputAndAddArgs($outputString, $argsString); $outputString = $this->cleanOutputAndAddArgs($outputString, $argsString);
return $outputString; return $outputString;
} }

View file

@ -6,6 +6,7 @@
function _test($xml_file, $argsString, $expected){ function _test($xml_file, $argsString, $expected){
$tester = new QueryTester(); $tester = new QueryTester();
$outputString = $tester->getNewParserOutputString($xml_file, '"', $argsString); $outputString = $tester->getNewParserOutputString($xml_file, '"', $argsString);
//echo $outputString;
$output = eval($outputString); $output = eval($outputString);
if(!is_a($output, 'Query')){ if(!is_a($output, 'Query')){

View file

@ -18,6 +18,7 @@
require_once(_XE_PATH_.'classes/db/DB.class.php'); require_once(_XE_PATH_.'classes/db/DB.class.php');
require_once(_XE_PATH_.'classes/db/DBCubrid.class.php'); require_once(_XE_PATH_.'classes/db/DBCubrid.class.php');
require_once(_XE_PATH_.'classes/db/DBMssql.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/DBParser.class.php'); 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/Argument.class.php');