N°3176 - OQL: Fix malformed UNION queries in portal scopes

Fix regression in Unit tests
This commit is contained in:
Eric
2020-07-22 17:30:38 +02:00
parent 1f66d53ab4
commit 8122270476
4 changed files with 64 additions and 65 deletions

View File

@@ -340,6 +340,7 @@ class DBObjectSearch extends DBSearch
$oTranslated = $oFilter->GetCriteria()->Translate($aTranslation, false, false /* leave unresolved fields */);
$this->AddConditionExpression($oTranslated);
$this->m_aParams = array_merge($this->m_aParams, $oFilter->m_aParams);
$oFilter->ResetCondition();
}
public function RenameParam($sOldName, $sNewName)
@@ -858,17 +859,15 @@ class DBObjectSearch extends DBSearch
}
/**
* @param DBObjectSearch $oFilter
* @param DBObjectSearch $oFilter (can be modified)
* @param $sExtKeyAttCode
* @param int $iOperatorCode
* @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
* @throws CoreException
* @throws CoreWarning
*/
public function AddCondition_PointingTo(DBObjectSearch $oOrigFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
public function AddCondition_PointingTo(DBObjectSearch $oFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
{
/** @var \DBObjectSearch $oFilter */
$oFilter = $oOrigFilter->DeepClone();
if (!MetaModel::IsValidKeyAttCode($this->GetClass(), $sExtKeyAttCode))
{
throw new CoreWarning("The attribute code '$sExtKeyAttCode' is not an external key of the class '{$this->GetClass()}'");
@@ -952,18 +951,15 @@ class DBObjectSearch extends DBSearch
}
/**
* @param DBObjectSearch $oFilter
* @param DBObjectSearch $oFilter (can be modified)
* @param $sForeignExtKeyAttCode
* @param int $iOperatorCode
* @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
* @return void
* @throws \CoreException
*/
public function AddCondition_ReferencedBy(DBObjectSearch $oOrigFilter, $sForeignExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
public function AddCondition_ReferencedBy(DBObjectSearch $oFilter, $sForeignExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
{
/** @var \DBObjectSearch $oFilter */
$oFilter = $oOrigFilter->DeepClone();
$sForeignClass = $oFilter->GetClass();
if (!MetaModel::IsValidKeyAttCode($sForeignClass, $sForeignExtKeyAttCode))
{

View File

@@ -381,14 +381,13 @@ class DBUnionSearch extends DBSearch
* @param $sExtKeyAttCode
* @param int $iOperatorCode
* @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
* @throws CoreException
* @throws CoreWarning
*/
public function AddCondition_PointingTo(DBObjectSearch $oFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
{
foreach ($this->aSearches as $oSearch)
{
$oSearch->AddCondition_PointingTo($oFilter, $sExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
$oConditionFilter = $oFilter->DeepClone();
$oSearch->AddCondition_PointingTo($oConditionFilter, $sExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
}
}
@@ -402,7 +401,8 @@ class DBUnionSearch extends DBSearch
{
foreach ($this->aSearches as $oSearch)
{
$oSearch->AddCondition_ReferencedBy($oFilter, $sForeignExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
$oConditionFilter = $oFilter->DeepClone();
$oSearch->AddCondition_ReferencedBy($oConditionFilter, $sForeignExtKeyAttCode, $iOperatorCode, $aRealiasingMap);
}
}

View File

@@ -455,52 +455,4 @@ class DBSearchIntersectTest extends ItopTestCase
return $aQueries;
}
/**
* Bug #2970
* @throws \CoreException
* @throws \CoreWarning
* @throws \OQLException
*/
public function testFilterOnJoin()
{
$sReq1 = "SELECT `L-1` FROM Organization AS `L-1` WHERE (`L-1`.`id` = :current_contact->org_id)";
$sReq2 = "SELECT `L-1-1` FROM CustomerContract AS `L-1-1` JOIN Organization AS `O` ON `L-1-1`.org_id = `O`.id WHERE (((`L-1-1`.`status` = 'active') OR (`L-1-1`.`status` = 'standby')) AND (`O`.`id` = :current_contact->org_id))";
$oFilter1 = DBSearch::FromOQL($sReq1);
$oFilter2 = DBSearch::FromOQL($sReq2);
$aRealiasingMap = array();
$oFilter1 = $oFilter1->Join($oFilter2,
DBSearch::JOIN_REFERENCED_BY,
'org_id',
TREE_OPERATOR_EQUALS, $aRealiasingMap);
$sRes1 = $oFilter1->ToOQL();
$this->debug($sRes1);
foreach($oFilter1->GetCriteria_ReferencedBy() as $sForeignClass => $aReferences)
{
foreach ($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
{
foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
{
foreach ($aFilters as $index => $oForeignFilter)
{
$this->debug($oForeignFilter->ToOQL());
}
}
}
}
$this->assertFalse(strpos($sRes1, '`O`.'));
$sReq3 = "SELECT `CustomerContract` FROM CustomerContract AS `CustomerContract` WHERE (`CustomerContract`.`org_id` IN ('2'))";
$oFilter3 = DBSearch::FromOQL($sReq3);
$oFilter1 = $oFilter1->Filter('L-1-1', $oFilter3);
$sRes1 = $oFilter1->ToOQL();
$this->debug($sRes1);
$this->assertFalse(strpos($sRes1, '`O`.'));
}
}

View File

@@ -15,7 +15,9 @@ use DBSearch;
* @preserveGlobalState disabled
* @backupGlobals disabled
*/
class DBSearchAddCondition_ReferencedByTest extends ItopDataTestCase {
class DBSearchJoinTest extends ItopDataTestCase {
const USE_TRANSACTION = false;
protected function setUp()
{
@@ -24,7 +26,7 @@ class DBSearchAddCondition_ReferencedByTest extends ItopDataTestCase {
}
/**
* @dataProvider AddCondition_ReferencedByProvider
* @dataProvider JoinProvider
*
* @param $sLeftSelect
* @param $sRightSelect
@@ -37,7 +39,7 @@ class DBSearchAddCondition_ReferencedByTest extends ItopDataTestCase {
* @throws \MySQLException
* @throws \OQLException
*/
public function testAddCondition_ReferencedBy($sLeftSelect, $sRightSelect, $sParentAtt, $sResult)
public function testJoin($sLeftSelect, $sRightSelect, $sParentAtt, $sResult)
{
$oLeftSearch = DBSearch::FromOQL($sLeftSelect);
$oRightSearch = DBSearch::FromOQL($sRightSelect);
@@ -50,7 +52,7 @@ class DBSearchAddCondition_ReferencedByTest extends ItopDataTestCase {
$this->assertEquals($sResult, $oResultSearch->ToOQL());
}
public function AddCondition_ReferencedByProvider()
public function JoinProvider()
{
// Breakpoint in BrowseBrickController::DisplayAction()
// $aLevelsProperties[$aLevelsPropertiesKeys[$i]]['search'] = $aLevelsProperties[$aLevelsPropertiesKeys[$i]]['search']->Join($aLevelsProperties[$aLevelsPropertiesKeys[$i + 1]]['search'],
@@ -77,4 +79,53 @@ class DBSearchAddCondition_ReferencedByTest extends ItopDataTestCase {
// ],
];
}
/**
* Bug #2970
* @throws \CoreException
* @throws \CoreWarning
* @throws \OQLException
*/
public function testFilterOnJoin()
{
$sReq1 = "SELECT `L-1` FROM Organization AS `L-1` WHERE (`L-1`.`id` = 2)";
$sReq2 = "SELECT `L-1-1` FROM CustomerContract AS `L-1-1` JOIN Organization AS `O` ON `L-1-1`.org_id = `O`.id WHERE (((`L-1-1`.`status` = 'active') OR (`L-1-1`.`status` = 'standby')) AND (`O`.`id` = 2))";
$oFilter1 = DBSearch::FromOQL($sReq1);
$oFilter2 = DBSearch::FromOQL($sReq2);
$aRealiasingMap = array();
$oFilter1 = $oFilter1->Join($oFilter2,
DBSearch::JOIN_REFERENCED_BY,
'org_id',
TREE_OPERATOR_EQUALS, $aRealiasingMap);
$sRes1 = $oFilter1->ToOQL();
$this->debug($sRes1);
foreach($oFilter1->GetCriteria_ReferencedBy() as $sForeignClass => $aReferences)
{
foreach ($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
{
foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
{
foreach ($aFilters as $index => $oForeignFilter)
{
$this->debug($oForeignFilter->ToOQL());
}
}
}
}
$this->assertFalse(strpos($sRes1, '`O`.'));
$sReq3 = "SELECT `CustomerContract` FROM CustomerContract AS `CustomerContract` WHERE (`CustomerContract`.`org_id` IN ('2'))";
$oFilter3 = DBSearch::FromOQL($sReq3);
$oFilter1 = $oFilter1->Filter('L-1-1', $oFilter3);
$sRes1 = $oFilter1->ToOQL();
$this->debug($sRes1);
$this->assertFalse(strpos($sRes1, '`O`.'));
}
}