mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-08 19:21:40 +09:00
First version of uncorellated select subquery.
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8539 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
af9ced8c5c
commit
0a53ac3e9d
5 changed files with 315 additions and 18 deletions
|
|
@ -526,5 +526,192 @@
|
|||
return new DBParser('"');
|
||||
}
|
||||
|
||||
|
||||
// TO BE REMOVED - Used for query compare
|
||||
/**
|
||||
* @brief returns type of column
|
||||
* @param[in] $column_type_list list of column type
|
||||
* @param[in] $name name of column type
|
||||
* @return column type of $name
|
||||
* @remarks columns are usually like a.b, so it needs another function
|
||||
**/
|
||||
function getColumnType($column_type_list, $name) {
|
||||
if(strpos($name, '.') === false) return $column_type_list[$name];
|
||||
list($prefix, $name) = explode('.', $name);
|
||||
return $column_type_list[$name];
|
||||
}
|
||||
/**
|
||||
* @brief returns the value of condition
|
||||
* @param[in] $name name of condition
|
||||
* @param[in] $value value of condition
|
||||
* @param[in] $operation operation this is used in condition
|
||||
* @param[in] $type type of condition
|
||||
* @param[in] $column_type type of column
|
||||
* @return well modified $value
|
||||
* @remarks if $operation is like or like_prefix, $value itself will be modified
|
||||
* @remarks if $type is not 'number', call addQuotes() and wrap with ' '
|
||||
**/
|
||||
function getConditionValue($name, $value, $operation, $type, $column_type) {
|
||||
if(!in_array($operation,array('in','notin','between')) && $type == 'number') {
|
||||
if(is_array($value)){
|
||||
$value = join(',',$value);
|
||||
}
|
||||
if(strpos($value, ',') === false && strpos($value, '(') === false) return (int)$value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
if(!is_array($value) && strpos($name, '.') !== false && strpos($value, '.') !== false) {
|
||||
list($table_name, $column_name) = explode('.', $value);
|
||||
if($column_type[$column_name]) return $value;
|
||||
}
|
||||
|
||||
switch($operation) {
|
||||
case 'like_prefix' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = $value.'%';
|
||||
break;
|
||||
case 'like_tail' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = '%'.$value;
|
||||
break;
|
||||
case 'like' :
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
$value = '%'.$value.'%';
|
||||
break;
|
||||
case 'notin' :
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type=='number') return join(',',$value);
|
||||
else return "'". join("','",$value)."'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
break;
|
||||
case 'in' :
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type=='number') return join(',',$value);
|
||||
else return "'". join("','",$value)."'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
break;
|
||||
case 'between' :
|
||||
if(!is_array($value)) $value = array($value);
|
||||
$value = $this->addQuotesArray($value);
|
||||
if($type!='number')
|
||||
{
|
||||
foreach($value as $k=>$v)
|
||||
{
|
||||
$value[$k] = "'".$v."'";
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
break;
|
||||
default:
|
||||
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
|
||||
}
|
||||
|
||||
return "'".$this->addQuotes($value)."'";
|
||||
}
|
||||
/**
|
||||
* @brief returns part of condition
|
||||
* @param[in] $name name of condition
|
||||
* @param[in] $value value of condition
|
||||
* @param[in] $operation operation that is used in condition
|
||||
* @return detail condition
|
||||
**/
|
||||
function getConditionPart($name, $value, $operation) {
|
||||
switch($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($value)) return;
|
||||
if($value === '') return;
|
||||
if(!in_array(gettype($value), array('string', 'integer'))) return;
|
||||
break;
|
||||
case 'between' :
|
||||
if(!is_array($value)) return;
|
||||
if(count($value)!=2) return;
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns condition key
|
||||
* @param[in] $output result of query
|
||||
* @return array of conditions of $output
|
||||
**/
|
||||
function getConditionList($output) {
|
||||
$conditions = array();
|
||||
if(count($output->conditions)) {
|
||||
foreach($output->conditions as $key => $val) {
|
||||
if($val['condition']) {
|
||||
foreach($val['condition'] as $k => $v) {
|
||||
$conditions[] = $v['column'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $conditions;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -12,7 +12,30 @@
|
|||
var $limit;
|
||||
|
||||
var $arguments = null;
|
||||
|
||||
function Query($queryID = null
|
||||
, $action = null
|
||||
, $columns = null
|
||||
, $tables = null
|
||||
, $conditions = null
|
||||
, $groups = null
|
||||
, $orderby = null
|
||||
, $limit = null){
|
||||
$this->queryID = $queryID;
|
||||
$this->action = $action;
|
||||
|
||||
$this->columns = $columns;
|
||||
$this->tables = $tables;
|
||||
$this->conditions = $conditions;
|
||||
$this->groups = $groups;
|
||||
$this->orderby = $orderby;
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
function show(){
|
||||
return true;
|
||||
}
|
||||
|
||||
function setQueryId($queryID){
|
||||
$this->queryID = $queryID;
|
||||
}
|
||||
|
|
@ -110,8 +133,14 @@
|
|||
function getSelectString($with_values = true){
|
||||
$select = '';
|
||||
foreach($this->columns as $column){
|
||||
var_dump($column);
|
||||
if($column->show())
|
||||
$select .= $column->getExpression($with_values) . ', ';
|
||||
if(is_a($column, 'Subquery')){
|
||||
$oDB = &DB::getInstance();
|
||||
$select .= '(' .$oDB->getSelectSql($column, $with_values) . ') as \''. $column->getAlias().'\', ';
|
||||
}
|
||||
else
|
||||
$select .= $column->getExpression($with_values) . ', ';
|
||||
}
|
||||
if(trim($select) == '') return '';
|
||||
$select = substr($select, 0, -2);
|
||||
|
|
|
|||
25
classes/db/queryparts/Subquery.class.php
Normal file
25
classes/db/queryparts/Subquery.class.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
class Subquery extends Query {
|
||||
var $alias;
|
||||
|
||||
function Subquery($alias, $columns, $tables, $conditions, $groups, $orderby, $limit){
|
||||
$this->alias = $alias;
|
||||
|
||||
$this->queryID = null;
|
||||
$this->action = null;
|
||||
|
||||
$this->columns = $columns;
|
||||
$this->tables = $tables;
|
||||
$this->conditions = $conditions;
|
||||
$this->groups = $groups;
|
||||
$this->orderby = $orderby;
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
function getAlias(){
|
||||
return $this->alias;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -6,7 +6,10 @@
|
|||
class SelectColumnsTag {
|
||||
var $columns;
|
||||
|
||||
function SelectColumnsTag($xml_columns){
|
||||
function SelectColumnsTag($xml_columns_tag){
|
||||
$xml_columns = $xml_columns_tag->column;
|
||||
$xml_queries = $xml_columns_tag->query;
|
||||
|
||||
$this->columns = array();
|
||||
|
||||
if(!$xml_columns) {
|
||||
|
|
@ -15,17 +18,31 @@
|
|||
}
|
||||
|
||||
if(!is_array($xml_columns)) $xml_columns = array($xml_columns);
|
||||
|
||||
|
||||
foreach($xml_columns as $column){
|
||||
if($column->name === 'query') $this->columns[] = new QueryTag($column, true);
|
||||
if($column->node_name == 'query') $this->columns[] = new QueryTag($column, true);
|
||||
else $this->columns[] = new SelectColumnTag($column);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(!$xml_queries) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!is_array($xml_queries)) $xml_queries = array($xml_queries);
|
||||
|
||||
foreach($xml_queries as $column){
|
||||
$this->columns[] = new QueryTag($column, true);
|
||||
}
|
||||
}
|
||||
|
||||
function toString(){
|
||||
$output_columns = 'array(' . PHP_EOL;
|
||||
foreach($this->columns as $column){
|
||||
$output_columns .= $column->getExpressionString() . PHP_EOL . ',';
|
||||
if(is_a($column, 'QueryTag'))
|
||||
$output_columns .= $column->toString() . PHP_EOL . ',';
|
||||
else
|
||||
$output_columns .= $column->getExpressionString() . PHP_EOL . ',';
|
||||
}
|
||||
$output_columns = substr($output_columns, 0, -1);
|
||||
$output_columns .= ')';
|
||||
|
|
@ -33,7 +50,12 @@
|
|||
}
|
||||
|
||||
function getArguments(){
|
||||
return array();
|
||||
$arguments = array();
|
||||
foreach($this->columns as $column){
|
||||
if(is_a($column, 'QueryTag'))
|
||||
$arguments = array_merge($arguments, $column->getArguments());
|
||||
}
|
||||
return $arguments;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -17,12 +17,16 @@ class QueryTag {
|
|||
var $buff;
|
||||
var $isSubQuery;
|
||||
|
||||
var $alias;
|
||||
|
||||
function QueryTag($query, $isSubQuery = false){
|
||||
$this->action = $query->attrs->action;
|
||||
$this->query_id = $query->attrs->id;
|
||||
$this->query = $query;
|
||||
$this->isSubQuery = $isSubQuery;
|
||||
|
||||
if($this->isSubQuery) $this->action = 'select';
|
||||
$this->alias = $query->attrs->alias;
|
||||
|
||||
$this->getColumns();
|
||||
$tables = $this->getTables();
|
||||
$this->setTableColumnTypes($tables);
|
||||
|
|
@ -33,6 +37,10 @@ class QueryTag {
|
|||
$this->getBuff();
|
||||
}
|
||||
|
||||
function show(){
|
||||
return true;
|
||||
}
|
||||
|
||||
function getQueryId(){
|
||||
return $this->query->attrs->query_id ? $this->query->attrs->query_id : $this->query->attrs->id;
|
||||
}
|
||||
|
|
@ -56,7 +64,7 @@ class QueryTag {
|
|||
|
||||
function getColumns(){
|
||||
if($this->action == 'select'){
|
||||
return $this->columns = new SelectColumnsTag($this->query->columns->column);
|
||||
return $this->columns = new SelectColumnsTag($this->query->columns);
|
||||
}else if($this->action == 'insert'){
|
||||
return $this->columns = new InsertColumnsTag($this->query->columns->column);
|
||||
}else if($this->action == 'update') {
|
||||
|
|
@ -68,11 +76,7 @@ class QueryTag {
|
|||
|
||||
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());
|
||||
$arguments = $this->getArguments();
|
||||
|
||||
$prebuff = '';
|
||||
foreach($arguments as $argument){
|
||||
|
|
@ -90,8 +94,27 @@ class QueryTag {
|
|||
|
||||
function getBuff(){
|
||||
$buff = '';
|
||||
if($this->isSubQuery) $buff .= '$query = new Query();'.PHP_EOL;
|
||||
else $buff .= '$query = new Query();'.PHP_EOL;
|
||||
echo 'start ---';
|
||||
var_dump($this);
|
||||
echo 'end ---';
|
||||
//echo 'Luam un query care e '.$this->isSubQuery;
|
||||
if($this->isSubQuery){
|
||||
$buff = 'new Subquery(';
|
||||
$buff .= "'" . $this->alias . '\', ';
|
||||
$buff .= ($this->columns ? $this->columns->toString() : 'null' ). ', '.PHP_EOL;
|
||||
$buff .= $this->tables->toString() .','.PHP_EOL;
|
||||
$buff .= $this->conditions->toString() .',' .PHP_EOL;
|
||||
$buff .= $this->groups->toString() . ',' .PHP_EOL;
|
||||
$buff .= $this->navigation->getOrderByString() .','.PHP_EOL;
|
||||
$limit = $this->navigation->getLimitString() ;
|
||||
$buff .= $limit ? $limit : 'null' . PHP_EOL;
|
||||
$buff .= ')';
|
||||
|
||||
$this->buff = $buff;
|
||||
return $this->buff;
|
||||
}
|
||||
|
||||
$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;
|
||||
|
|
@ -104,8 +127,9 @@ class QueryTag {
|
|||
$buff .= '$query->setOrder(' . $this->navigation->getOrderByString() .');'.PHP_EOL;
|
||||
$buff .= '$query->setLimit(' . $this->navigation->getLimitString() .');'.PHP_EOL;
|
||||
|
||||
return $this->buff = $buff;
|
||||
}
|
||||
$this->buff = $buff;
|
||||
return $this->buff;
|
||||
}
|
||||
|
||||
function getTables(){
|
||||
return $this->tables = new TablesTag($this->query->tables->table);
|
||||
|
|
@ -138,5 +162,15 @@ class QueryTag {
|
|||
function getExpressionString(){
|
||||
return $this->buff;
|
||||
}
|
||||
|
||||
function getArguments(){
|
||||
$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());
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue