Added unit tests for correlated subqueries - select, from, where.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8556 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-06-30 15:36:03 +00:00
parent 5d1eb1c21e
commit 1353ade0c2
41 changed files with 661 additions and 256 deletions

View file

@ -319,6 +319,7 @@
require_once(_XE_PATH_.'classes/db/queryparts/order/OrderByColumn.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/limit/Limit.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/Query.class.php');
require_once(_XE_PATH_.'classes/db/queryparts/Subquery.class.php');
$output = include($cache_file);

View file

@ -199,7 +199,7 @@
/**
* @brief Fetch results
**/
function _fetch($result) {
function _fetch($result, $arrayIndexEndValue = NULL) {
if(!$this->isConnected() || $this->isError() || !$result) return;
$c = sqlsrv_num_fields($result);
@ -212,10 +212,14 @@
for($i=0;$i<$c;$i++){
$row->{$m[$i]['Name']} = sqlsrv_get_field( $result, $i, SQLSRV_PHPTYPE_STRING( 'utf-8' ));
}
$output[] = $row;
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $row;
else $output[] = $row;
}
if(count($output)==1) return $output[0];
if(count($output)==1) {
if(isset($arrayIndexEndValue)) return $output;
else return $output[0];
}
return $output;
}

View file

@ -135,8 +135,7 @@
foreach($this->columns as $column){
if($column->show())
if(is_a($column, 'Subquery')){
$oDB = &DB::getInstance();
$select .= '(' .$oDB->getSelectSql($column, $with_values) . ') as \''. $column->getAlias().'\', ';
$select .= $column->toString($with_values) . ' as '. $column->getAlias() .', ';
}
else
$select .= $column->getExpression($with_values) . ', ';
@ -169,12 +168,18 @@
return $this->tables;
}
// from table_a
// from table_a inner join table_b on x=y
// from (select * from table a) as x
// from (select * from table t) as x inner join table y on y.x
function getFromString($with_values = true){
$from = '';
$simple_table_count = 0;
foreach($this->tables as $table){
if($table->isJoinTable() || !$simple_table_count) $from .= $table->toString($with_values) . ' ';
else $from .= ', '.$table->toString($with_values) . ' ';
if(!$table->isJoinTable()) $from .= $table->getAlias() ? ' as ' . $table->getAlias() . ' ' : ' ';
$simple_table_count++;
}
if(trim($from) == '') return '';

View file

@ -2,12 +2,13 @@
class Subquery extends Query {
var $alias;
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit){
var $join_type;
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit, $join_type = null){
$this->alias = $alias;
$this->queryID = null;
$this->action = null;
$this->action = "select";
$this->columns = $columns;
$this->tables = $tables;
@ -15,11 +16,23 @@
$this->groups = $groups;
$this->orderby = $orderby;
$this->limit = $limit;
$this->join_type = $join_type;
}
function getAlias(){
return $this->alias;
}
function isJoinTable(){
if($this->join_type) return true;
return false;
}
function toString($with_values = true){
$oDB = &DB::getInstance();
return '(' .$oDB->getSelectSql($this, $with_values) . ')';
}
}
?>

View file

@ -15,6 +15,8 @@
$this->pipe = $pipe;
if($this->hasArgument())
$this->_value = $argument->getValue();
else if(is_a($this->argument, 'Subquery'))
$this->_value = $argument->toString();
else
$this->_value = $argument;
}
@ -50,27 +52,27 @@
function show(){
switch($this->operation) {
case 'equal' :
case 'more' :
case 'excess' :
case 'less' :
case 'below' :
case 'like_tail' :
case 'like_prefix' :
case 'like' :
case 'in' :
case 'notin' :
case 'notequal' :
// if variable is not set or is not string or number, return
if(!isset($this->_value)) return false;
if($this->_value === '') return false;
if(!in_array(gettype($this->_value), array('string', 'integer'))) return false;
break;
case 'between' :
if(!is_array($this->_value)) return false;
if(count($this->_value)!=2) return false;
case 'equal' :
case 'more' :
case 'excess' :
case 'less' :
case 'below' :
case 'like_tail' :
case 'like_prefix' :
case 'like' :
case 'in' :
case 'notin' :
case 'notequal' :
// if variable is not set or is not string or number, return
if(!isset($this->_value)) return false;
if($this->_value === '') return false;
if(!in_array(gettype($this->_value), array('string', 'integer'))) return false;
break;
case 'between' :
if(!is_array($this->_value)) return false;
if(count($this->_value)!=2) return false;
}
}
return true;
}
@ -78,43 +80,43 @@
$name = $this->column_name;
$operation = $this->operation;
switch($operation) {
case 'equal' :
return $name.' = '.$value;
break;
case 'more' :
return $name.' >= '.$value;
break;
case 'excess' :
return $name.' > '.$value;
break;
case 'less' :
return $name.' <= '.$value;
break;
case 'below' :
return $name.' < '.$value;
break;
case 'like_tail' :
case 'like_prefix' :
case 'like' :
return $name.' like '.$value;
break;
case 'in' :
return $name.' in ('.$value.')';
break;
case 'notin' :
return $name.' not in ('.$value.')';
break;
case 'notequal' :
return $name.' <> '.$value;
break;
case 'notnull' :
return $name.' is not null';
break;
case 'null' :
return $name.' is null';
break;
case 'between' :
switch($operation) {
case 'equal' :
return $name.' = '.$value;
break;
case 'more' :
return $name.' >= '.$value;
break;
case 'excess' :
return $name.' > '.$value;
break;
case 'less' :
return $name.' <= '.$value;
break;
case 'below' :
return $name.' < '.$value;
break;
case 'like_tail' :
case 'like_prefix' :
case 'like' :
return $name.' like '.$value;
break;
case 'in' :
return $name.' in ('.$value.')';
break;
case 'notin' :
return $name.' not in ('.$value.')';
break;
case 'notequal' :
return $name.' <> '.$value;
break;
case 'notnull' :
return $name.' is not null';
break;
case 'null' :
return $name.' is null';
break;
case 'between' :
return $name.' between ' . $value[0] . ' and ' . $value[1];
break;
}

View file

@ -10,7 +10,8 @@
}
function toString(){
return sprintf("%s%s", $this->name, $this->alias ? ' as ' . $this->alias : '');
return $this->name;
//return sprintf("%s%s", $this->name, $this->alias ? ' as ' . $this->alias : '');
}
function getName(){