mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-08 03:01:43 +09:00
Added support for INSERT INTO ... SELECT .. statements.
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@10009 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
9da60d48a0
commit
5594488b23
7 changed files with 135 additions and 7 deletions
|
|
@ -92,6 +92,10 @@
|
|||
|
||||
$this->tables = $tables;
|
||||
}
|
||||
|
||||
function setSubquery($subquery){
|
||||
$this->subquery = $subquery;
|
||||
}
|
||||
|
||||
function setConditions($conditions){
|
||||
$this->conditions = array();
|
||||
|
|
@ -185,6 +189,20 @@
|
|||
|
||||
function getInsertString($with_values = true){
|
||||
$columnsList = '';
|
||||
if($this->subquery){ // means we have insert-select
|
||||
|
||||
foreach($this->columns as $column){
|
||||
$columnsList .= $column->getColumnName() . ', ';
|
||||
}
|
||||
$columnsList = substr($columnsList, 0, -2);
|
||||
|
||||
$selectStatement = $this->subquery->toString($with_values);
|
||||
$selectStatement = substr($selectStatement, 1, -1);
|
||||
|
||||
return "($columnsList) \n $selectStatement";
|
||||
}
|
||||
|
||||
|
||||
$valuesList = '';
|
||||
foreach($this->columns as $column){
|
||||
if($column->show()){
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
require(_XE_PATH_.'classes/xml/xmlquery/tags/column/ColumnTag.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/tags/column/SelectColumnTag.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/tags/column/InsertColumnTag.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/tags/column/InsertColumnTagWithoutArgument.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/tags/column/UpdateColumnTag.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/tags/column/SelectColumnsTag.class.php');
|
||||
require(_XE_PATH_.'classes/xml/xmlquery/tags/column/InsertColumnsTag.class.php');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
/**
|
||||
* @class InsertColumnTagWithoutArgument
|
||||
* @author Arnia Software
|
||||
* @brief Models the <column> tag inside an XML Query file whose action is 'insert-select'
|
||||
*
|
||||
**/
|
||||
|
||||
class InsertColumnTagWithoutArgument extends ColumnTag {
|
||||
|
||||
function InsertColumnTagWithoutArgument($column) {
|
||||
parent::ColumnTag($column->attrs->name);
|
||||
$dbParser = DB::getParser();
|
||||
$this->name = $dbParser->parseColumnName($this->name);
|
||||
}
|
||||
|
||||
function getExpressionString(){
|
||||
var_dump($this->name);
|
||||
return sprintf('new Expression(\'%s\')', $this->name);
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
foreach($xml_columns as $column){
|
||||
if($column->name === 'query') $this->columns[] = new QueryTag($column, true);
|
||||
else if(!isset($column->attrs->var) && !isset($column->attrs->default)) $this->columns[] = new InsertColumnTagWithoutArgument($column);
|
||||
else $this->columns[] = new InsertColumnTag($column);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ class QueryTag {
|
|||
//xml tags
|
||||
var $columns;
|
||||
var $tables;
|
||||
var $subquery;
|
||||
var $conditions;
|
||||
var $groups;
|
||||
var $navigation;
|
||||
|
|
@ -37,9 +38,12 @@ class QueryTag {
|
|||
$this->getColumns();
|
||||
$tables = $this->getTables();
|
||||
$this->setTableColumnTypes($tables);
|
||||
$this->getSubquery(); // Used for insert-select
|
||||
$this->getConditions();
|
||||
$this->getGroups();
|
||||
$this->getNavigation();
|
||||
|
||||
|
||||
$this->getPrebuff();
|
||||
$this->getBuff();
|
||||
}
|
||||
|
|
@ -78,7 +82,7 @@ class QueryTag {
|
|||
function getColumns(){
|
||||
if($this->action == 'select'){
|
||||
return $this->columns = new SelectColumnsTag($this->query->columns);
|
||||
}else if($this->action == 'insert'){
|
||||
}else if($this->action == 'insert' || $this->action == 'insert-select'){
|
||||
return $this->columns = new InsertColumnsTag($this->query->columns->column);
|
||||
}else if($this->action == 'update') {
|
||||
return $this->columns = new UpdateColumnsTag($this->query->columns->column);
|
||||
|
|
@ -88,6 +92,7 @@ class QueryTag {
|
|||
}
|
||||
|
||||
function getPrebuff(){
|
||||
if($this->isSubQuery) return;
|
||||
// TODO Check if this work with arguments in join clause
|
||||
$arguments = $this->getArguments();
|
||||
|
||||
|
|
@ -139,6 +144,8 @@ class QueryTag {
|
|||
$buff .= '$query->setColumns(' . $this->columns->toString() . ');'.PHP_EOL;
|
||||
|
||||
$buff .= '$query->setTables(' . $this->tables->toString() .');'.PHP_EOL;
|
||||
if($this->action == 'insert-select')
|
||||
$buff .= '$query->setSubquery(' . $this->subquery->toString() .');'.PHP_EOL;
|
||||
$buff .= '$query->setConditions('.$this->conditions->toString() .');'.PHP_EOL;
|
||||
$buff .= '$query->setGroups(' . $this->groups->toString() . ');'.PHP_EOL;
|
||||
$buff .= '$query->setOrder(' . $this->navigation->getOrderByString() .');'.PHP_EOL;
|
||||
|
|
@ -149,12 +156,18 @@ class QueryTag {
|
|||
}
|
||||
|
||||
function getTables(){
|
||||
if($this->query->index_hint->attrs->for == 'ALL' || Context::getDBType() == strtolower($this->query->index_hint->attrs->for))
|
||||
return $this->tables = new TablesTag($this->query->tables, $this->query->index_hint);
|
||||
else
|
||||
return $this->tables = new TablesTag($this->query->tables);
|
||||
if($this->query->index_hint->attrs->for == 'ALL' || Context::getDBType() == strtolower($this->query->index_hint->attrs->for))
|
||||
return $this->tables = new TablesTag($this->query->tables, $this->query->index_hint);
|
||||
else
|
||||
return $this->tables = new TablesTag($this->query->tables);
|
||||
}
|
||||
|
||||
function getSubquery(){
|
||||
if($this->query->query){
|
||||
$this->subquery = new QueryTag($this->query->query, true);
|
||||
}
|
||||
}
|
||||
|
||||
function getConditions(){
|
||||
return $this->conditions = new ConditionsTag($this->query->conditions);
|
||||
}
|
||||
|
|
@ -183,12 +196,13 @@ class QueryTag {
|
|||
return $this->buff;
|
||||
}
|
||||
|
||||
|
||||
function getArguments(){
|
||||
$arguments = array();
|
||||
if($this->columns)
|
||||
$arguments = array_merge($arguments, $this->columns->getArguments());
|
||||
$arguments = array_merge($arguments, $this->tables->getArguments());
|
||||
if($this->action =='insert-select')
|
||||
$arguments = array_merge($arguments, $this->subquery->getArguments());
|
||||
$arguments = array_merge($arguments, $this->tables->getArguments());
|
||||
$arguments = array_merge($arguments, $this->conditions->getArguments());
|
||||
$arguments = array_merge($arguments, $this->navigation->getArguments());
|
||||
return $arguments;
|
||||
|
|
|
|||
44
tests/classes/db/db/xml_query/mysql/MysqlInsertTest.php
Normal file
44
tests/classes/db/db/xml_query/mysql/MysqlInsertTest.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @class MysqlInsertTest
|
||||
* @brief Constains all test method for insert statements, using Mysql SQL syntax
|
||||
* @developer Corina Udrescu (xe_dev@arnia.ro)
|
||||
*/
|
||||
class MysqlInsertTest extends MysqlTest
|
||||
{
|
||||
/**
|
||||
* @brief _test - local helper method
|
||||
* @developer Corina Udrescu (xe_dev@arnia.ro)
|
||||
* @access private
|
||||
* @param $xml_file string - Path to XML file containing the query to be tested
|
||||
* @param $argsString string - String containing PHP code that initializez the arguments that the query receives
|
||||
* @param $expected string - Expected SQL query as string
|
||||
* @param $columnList array - Array containing the column names that will be retrieved, in case only a part of the ones in the query file are needed
|
||||
* @return void
|
||||
*/
|
||||
function _test($xml_file, $argsString, $expected, $columnList = NULL)
|
||||
{
|
||||
$this->_testQuery($xml_file, $argsString, $expected, 'getInsertSql', $columnList);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief testInsertSelectStatement - checks that when query action is 'insert-selct' an 'INSERT INTO .. SELECT ...' statement is properly generated
|
||||
* @developer Corina Udrescu (xe_dev@arnia.ro)
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testInsertSelectStatement()
|
||||
{
|
||||
$xml_file = _TEST_PATH_ . "db/xml_query/mysql/data/insert_select.xml";
|
||||
$argsString = '$args->condition_value = 7;';
|
||||
$expected = 'insert into `xe_table1` (`column1`, `column2`, `column3`)
|
||||
select `column4`, `column5`, `column6`
|
||||
from `xe_table2` as `table2`
|
||||
where `column4` >= 7';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file MysqlInsertTest.php */
|
||||
/* Location: ./tests/classes/db/db/xml_query/mysql/MysqlInsertTest.php */
|
||||
23
tests/classes/db/db/xml_query/mysql/data/insert_select.xml
Normal file
23
tests/classes/db/db/xml_query/mysql/data/insert_select.xml
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<query id="QUERY_ID" action="insert-select">
|
||||
<tables>
|
||||
<table name="table1" />
|
||||
</tables>
|
||||
<columns>
|
||||
<column name="column1" />
|
||||
<column name="column2" />
|
||||
<column name="column3" />
|
||||
</columns>
|
||||
<query>
|
||||
<tables>
|
||||
<table name="table2" />
|
||||
</tables>
|
||||
<columns>
|
||||
<column name="column4" />
|
||||
<column name="column5" />
|
||||
<column name="column6" />
|
||||
</columns>
|
||||
<conditions>
|
||||
<condition operation="more" column="column4" default="100" var="condition_value" />
|
||||
</conditions>
|
||||
</query>
|
||||
</query>
|
||||
Loading…
Add table
Add a link
Reference in a new issue