N.609 Some multi-column UNION queries failing with various symptoms such as "Class 'IT Department' not found" or "An object id must be an integer value"

SVN:trunk[4634]
This commit is contained in:
Romain Quetiez
2017-03-29 19:58:02 +00:00
parent c073611597
commit 3644553886
3 changed files with 42 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2015-2016 Combodo SARL
// Copyright (C) 2015-2017 Combodo SARL
//
// This file is part of iTop.
//
@@ -20,7 +20,7 @@
/**
* A union of DBObjectSearches
*
* @copyright Copyright (C) 2015-2016 Combodo SARL
* @copyright Copyright (C) 2015-2017 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -516,6 +516,12 @@ class DBUnionSearch extends DBSearch
}
}
$oSubQuery = $oSearch->GetSQLQueryStructure($aQueryAttToLoad, false, $aQueryGroupByExpr, $aSearchSelectedClasses);
if (count($aSearchAliases) > 1)
{
// Necessary to make sure that selected columns will match throughout all the queries
// (default order of selected fields depending on the order of JOINS)
$oSubQuery->SortSelectedFields();
}
$aSQLQueries[] = $oSubQuery;
}

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2015-2016 Combodo SARL
// Copyright (C) 2015-2017 Combodo SARL
//
// This file is part of iTop.
//
@@ -21,7 +21,7 @@
* SQLObjectQuery
* build a mySQL compatible SQL query
*
* @copyright Copyright (C) 2015-2016 Combodo SARL
* @copyright Copyright (C) 2015-2017 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -138,6 +138,11 @@ class SQLObjectQuery extends SQLQuery
$this->m_aFields = $aExpressions;
}
public function SortSelectedFields()
{
ksort($this->m_aFields);
}
public function AddSelect($sAlias, $oExpression)
{
$this->m_aFields[$sAlias] = $oExpression;

View File

@@ -5518,3 +5518,30 @@ class TestIntersectNotOptimized extends TestBizModel
echo "<p>Successfully tested the SQL query.</p>\n";
}
}
class TestBug609 extends TestBizModel
{
static public function GetName()
{
return 'UNION with JOINS ordered differently';
}
static public function GetDescription()
{
return '(N.609) Inconsistent SQL query (various symptoms, must mostly in the form of "Class \'IT Department\' not found"';
}
protected function DoExecute()
{
$sQueryA = 'SELECT t,o FROM Team AS t JOIN Organization AS o ON t.org_id = o.id';
$sQueryB = 'SELECT t,o FROM Organization AS o JOIN Team AS t ON t.org_id = o.id';
$oSearch = DBSearch::FromOQL("$sQueryB UNION $sQueryA");
$oSet = new DBObjectSet($oSearch);
while($oObject = $oSet->Fetch())
{
echo "Successfull load for <b>".$oObject->GetName()."</b><br>\n";
}
}
}