mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-19 15:22:17 +02:00
Finalized queries with multiple objects
SVN:trunk[329]
This commit is contained in:
@@ -43,7 +43,7 @@ define('SIBUSQLTHISREGEXP', "/this\\.(.*)/U");
|
|||||||
*/
|
*/
|
||||||
class DBObjectSearch
|
class DBObjectSearch
|
||||||
{
|
{
|
||||||
private $m_aClasses; // queried classes (alias => class name)
|
private $m_aClasses; // queried classes (alias => class name), the first item is the class corresponding to this filter (the rest is coming from subfilters)
|
||||||
private $m_aSelectedClasses; // selected for the output (alias => class name)
|
private $m_aSelectedClasses; // selected for the output (alias => class name)
|
||||||
private $m_oSearchCondition;
|
private $m_oSearchCondition;
|
||||||
private $m_aParams;
|
private $m_aParams;
|
||||||
@@ -84,6 +84,16 @@ class DBObjectSearch
|
|||||||
return key($this->m_aSelectedClasses);
|
return key($this->m_aSelectedClasses);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function GetFirstJoinedClass()
|
||||||
|
{
|
||||||
|
return reset($this->m_aClasses);
|
||||||
|
}
|
||||||
|
public function GetFirstJoinedClassAlias()
|
||||||
|
{
|
||||||
|
reset($this->m_aClasses);
|
||||||
|
return key($this->m_aClasses);
|
||||||
|
}
|
||||||
|
|
||||||
public function SetSelectedClasses($aNewSet)
|
public function SetSelectedClasses($aNewSet)
|
||||||
{
|
{
|
||||||
$this->m_aSelectedClasses = array();
|
$this->m_aSelectedClasses = array();
|
||||||
@@ -322,7 +332,7 @@ class DBObjectSearch
|
|||||||
{
|
{
|
||||||
$sNewAlias = MetaModel::GenerateUniqueAlias($aClassAliases, $sOrigAlias, $this->GetClass());
|
$sNewAlias = MetaModel::GenerateUniqueAlias($aClassAliases, $sOrigAlias, $this->GetClass());
|
||||||
$this->m_aSelectedClasses[$sNewAlias] = $this->GetClass();
|
$this->m_aSelectedClasses[$sNewAlias] = $this->GetClass();
|
||||||
unset($sOrigAlias, $this->m_aSelectedClasses[$sNewAlias]);
|
unset($this->m_aSelectedClasses[$sOrigAlias]);
|
||||||
|
|
||||||
// Translate the condition expression with the new alias
|
// Translate the condition expression with the new alias
|
||||||
$aAliasTranslation[$sOrigAlias]['*'] = $sNewAlias;
|
$aAliasTranslation[$sOrigAlias]['*'] = $sNewAlias;
|
||||||
@@ -513,6 +523,11 @@ class DBObjectSearch
|
|||||||
$sValue = $this->GetClass()."\n";
|
$sValue = $this->GetClass()."\n";
|
||||||
$sValue .= $this->GetClassAlias()."\n";
|
$sValue .= $this->GetClassAlias()."\n";
|
||||||
|
|
||||||
|
foreach($this->m_aSelectedClasses as $sClassAlias => $sClass)
|
||||||
|
{
|
||||||
|
// A stands for "Aliases"
|
||||||
|
$sValue .= "S:$sClassAlias:$sClass\n";
|
||||||
|
}
|
||||||
foreach($this->m_aClasses as $sClassAlias => $sClass)
|
foreach($this->m_aClasses as $sClassAlias => $sClass)
|
||||||
{
|
{
|
||||||
// A stands for "Aliases"
|
// A stands for "Aliases"
|
||||||
@@ -573,6 +588,9 @@ class DBObjectSearch
|
|||||||
$aCondition = explode(":", $aValues[$i++]);
|
$aCondition = explode(":", $aValues[$i++]);
|
||||||
switch ($aCondition[0])
|
switch ($aCondition[0])
|
||||||
{
|
{
|
||||||
|
case "S":
|
||||||
|
$oFilter->m_aSelectedClasses[$aCondition[1]] = $aCondition[2];
|
||||||
|
break;
|
||||||
case "A":
|
case "A":
|
||||||
$oFilter->m_aClasses[$aCondition[1]] = $aCondition[2];
|
$oFilter->m_aClasses[$aCondition[1]] = $aCondition[2];
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1283,22 +1283,25 @@ abstract class MetaModel
|
|||||||
$aOrderSpec = array();
|
$aOrderSpec = array();
|
||||||
foreach ($aOrderBy as $sFieldAlias => $bAscending)
|
foreach ($aOrderBy as $sFieldAlias => $bAscending)
|
||||||
{
|
{
|
||||||
MyHelpers::CheckValueInArray('field name in ORDER BY spec', $sFieldAlias, self::GetAttributesList($oFilter->GetClass()));
|
MyHelpers::CheckValueInArray('field name in ORDER BY spec', $sFieldAlias, self::GetAttributesList($oFilter->GetFirstJoinedClass()));
|
||||||
if (!is_bool($bAscending))
|
if (!is_bool($bAscending))
|
||||||
{
|
{
|
||||||
throw new CoreException("Wrong direction in ORDER BY spec, found '$bAscending' and expecting a boolean value");
|
throw new CoreException("Wrong direction in ORDER BY spec, found '$bAscending' and expecting a boolean value");
|
||||||
}
|
}
|
||||||
$aOrderSpec[$oFilter->GetClassAlias().$sFieldAlias] = $bAscending;
|
$aOrderSpec[$oFilter->GetFirstJoinedClassAlias().$sFieldAlias] = $bAscending;
|
||||||
}
|
}
|
||||||
// By default, force the name attribute to be the ordering key
|
// By default, force the name attribute to be the ordering key
|
||||||
//
|
//
|
||||||
if (empty($aOrderSpec))
|
if (empty($aOrderSpec))
|
||||||
{
|
{
|
||||||
$sNameAttCode = self::GetNameAttributeCode($oFilter->GetClass());
|
foreach ($oFilter->GetSelectedClasses() as $sSelectedAlias => $sSelectedClass)
|
||||||
if (!empty($sNameAttCode))
|
|
||||||
{
|
{
|
||||||
// By default, simply order on the "name" attribute, ascending
|
$sNameAttCode = self::GetNameAttributeCode($sSelectedClass);
|
||||||
$aOrderSpec[$oFilter->GetClassAlias().$sNameAttCode] = true;
|
if (!empty($sNameAttCode))
|
||||||
|
{
|
||||||
|
// By default, simply order on the "name" attribute, ascending
|
||||||
|
$aOrderSpec[$sSelectedAlias.$sNameAttCode] = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1377,8 +1380,8 @@ abstract class MetaModel
|
|||||||
// Note: query class might be different than the class of the filter
|
// Note: query class might be different than the class of the filter
|
||||||
// -> this occurs when we are linking our class to an external class (referenced by, or pointing to)
|
// -> this occurs when we are linking our class to an external class (referenced by, or pointing to)
|
||||||
// $aExpectedAtts is an array of sAttCode=>array of columns
|
// $aExpectedAtts is an array of sAttCode=>array of columns
|
||||||
$sClass = $oFilter->GetClass();
|
$sClass = $oFilter->GetFirstJoinedClass();
|
||||||
$sClassAlias = $oFilter->GetClassAlias();
|
$sClassAlias = $oFilter->GetFirstJoinedClassAlias();
|
||||||
|
|
||||||
$bIsOnQueriedClass = array_key_exists($sClassAlias, $aSelectedClasses);
|
$bIsOnQueriedClass = array_key_exists($sClassAlias, $aSelectedClasses);
|
||||||
if ($bIsOnQueriedClass)
|
if ($bIsOnQueriedClass)
|
||||||
@@ -1474,7 +1477,7 @@ abstract class MetaModel
|
|||||||
//self::DbgTrace($oSelectForeign->RenderSelect(array()));
|
//self::DbgTrace($oSelectForeign->RenderSelect(array()));
|
||||||
$oSelectForeign = self::MakeQuery($aSelectedClasses, $oConditionTree, $aClassAliases, $aTableAliases, $aTranslation, $oForeignFilter, $aExpAtts);
|
$oSelectForeign = self::MakeQuery($aSelectedClasses, $oConditionTree, $aClassAliases, $aTableAliases, $aTranslation, $oForeignFilter, $aExpAtts);
|
||||||
|
|
||||||
$sForeignClassAlias = $oForeignFilter->GetClassAlias();
|
$sForeignClassAlias = $oForeignFilter->GetFirstJoinedClassAlias();
|
||||||
$sForeignKeyTable = $aTranslation[$sForeignClassAlias][$sForeignKeyAttCode][0];
|
$sForeignKeyTable = $aTranslation[$sForeignClassAlias][$sForeignKeyAttCode][0];
|
||||||
$sForeignKeyColumn = $aTranslation[$sForeignClassAlias][$sForeignKeyAttCode][1];
|
$sForeignKeyColumn = $aTranslation[$sForeignClassAlias][$sForeignKeyAttCode][1];
|
||||||
$oSelectBase->AddInnerJoin($oSelectForeign, $sKeyField, $sForeignKeyColumn, $sForeignKeyTable);
|
$oSelectBase->AddInnerJoin($oSelectForeign, $sKeyField, $sForeignKeyColumn, $sForeignKeyTable);
|
||||||
@@ -1536,8 +1539,8 @@ abstract class MetaModel
|
|||||||
//
|
//
|
||||||
// Returns an SQLQuery
|
// Returns an SQLQuery
|
||||||
//
|
//
|
||||||
$sTargetClass = $oFilter->GetClass();
|
$sTargetClass = $oFilter->GetFirstJoinedClass();
|
||||||
$sTargetAlias = $oFilter->GetClassAlias();
|
$sTargetAlias = $oFilter->GetFirstJoinedClassAlias();
|
||||||
$sTable = self::DBGetTable($sTableClass);
|
$sTable = self::DBGetTable($sTableClass);
|
||||||
$sTableAlias = self::GenerateUniqueAlias($aTableAliases, $sTargetAlias.'_'.$sTable, $sTable);
|
$sTableAlias = self::GenerateUniqueAlias($aTableAliases, $sTargetAlias.'_'.$sTable, $sTable);
|
||||||
|
|
||||||
@@ -1662,8 +1665,8 @@ abstract class MetaModel
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// The aliases should not conflict because normalization occured while building the filter
|
// The aliases should not conflict because normalization occured while building the filter
|
||||||
$sKeyClass = $oExtFilter->GetClass();
|
$sKeyClass = $oExtFilter->GetFirstJoinedClass();
|
||||||
$sKeyClassAlias = $oExtFilter->GetClassAlias();
|
$sKeyClassAlias = $oExtFilter->GetFirstJoinedClassAlias();
|
||||||
|
|
||||||
// Note: there is no search condition in $oExtFilter, because normalization did merge the condition onto the top of the filter tree
|
// Note: there is no search condition in $oExtFilter, because normalization did merge the condition onto the top of the filter tree
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user