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);
}
function getSelectSql($query){
$select = $query->getSelectString();
function getSelectSql($query, $with_values = true){
$select = $query->getSelectString($with_values);
if($select == '') return new Object(-1, "Invalid query");
$select = 'SELECT ' .$select;
$from = $query->getFromString();
$from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query");
$from = ' FROM '.$from;
$where = $query->getWhereString();
$where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where;
$groupBy = $query->getGroupByString();
@ -481,7 +481,7 @@
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
}
function getDeleteSql($query){
function getDeleteSql($query, $with_values = true){
$sql = 'DELETE ';
// 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();
$from = $query->getFromString();
$from = $query->getFromString($with_values);
if($from == '') return new Object(-1, "Invalid query");
$sql .= ' FROM '.$from;
$where = $query->getWhereString();
$where = $query->getWhereString($with_values);
if($where != '') $sql .= ' WHERE ' . $where;
return $sql;
}
function getUpdateSql($query){
function getUpdateSql($query, $with_values = true){
$columnsList = $query->getSelectString();
if($columnsList == '') return new Object(-1, "Invalid query");
$tableName = $query->getFirstTableName();
if($tableName == '') return new Object(-1, "Invalid query");
$where = $query->getWhereString();
$where = $query->getWhereString($with_values);
if($where != '') $where = ' WHERE ' . $where;
return "UPDATE $tableName SET $columnsList ".$where;
}
function getInsertSql($query){
function getInsertSql($query, $with_values = true){
$tableName = $query->getFirstTableName();
$values = $query->getInsertString();
$values = $query->getInsertString($with_values);
return "INSERT INTO $tableName \n $values";
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -14,8 +14,14 @@
class XmlQueryParser extends XmlParser {
var $dbParser;
var $db_type;
function XmlQueryParser($db_type = NULL){
$this->db_type = $db_type;
}
function parse($query_id, $xml_file, $cache_file) {
// Read xml file
$xml_obj = $this->getXmlFileContent($xml_file);
@ -29,15 +35,17 @@
}
// singleton
/* function &getDBParser(){
function &getDBParser(){
static $dbParser;
if(!$dbParser){
//$oDB = &DB::getInstance();
//$dbParser = $oDB->getParser();
return new DBParser('"');
if(isset($this->db_type))
$oDB = &DB::getInstance($this->db_type);
else
$oDB = &DB::getInstance();
$dbParser = $oDB->getParser();
}
return $dbParser;
}*/
}
function getXmlFileContent($xml_file){
$buff = FileHandler::readFile($xml_file);
@ -46,11 +54,5 @@
unset($buff);
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){
if(isset($argument) && $argument->getArgumentName()){
$prebuff .= $argument->toString();
$prebuff .= sprintf("$%s_argument->escapeValue('%s');\n"
$prebuff .= sprintf("$%s_argument->setColumnType('%s');\n"
, $argument->getArgumentName()
, $this->column_type[$this->getQueryId()][$argument->getColumnName()] );
$prebuff .= sprintf('$query->addArgument($%s_argument);', $argument->getArgumentName());
}
}
$prebuff .= "\n";

View file

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