1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227 |
- <?php
- // Copyright (C) 2010-2017 Combodo SARL
- //
- // This file is part of iTop.
- //
- // iTop is free software; you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // iTop is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with iTop. If not, see <http://www.gnu.org/licenses/>
- /**
- * Define filters for a given class of objects (formerly named "filter")
- *
- * @copyright Copyright (C) 2010-2017 Combodo SARL
- * @license http://opensource.org/licenses/AGPL-3.0
- */
- // Dev hack for disabling the some query build optimizations (Folding/Merging)
- define('ENABLE_OPT', true);
- class DBObjectSearch extends DBSearch
- {
- 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_oSearchCondition;
- private $m_aParams;
- private $m_aFullText;
- private $m_aPointingTo;
- private $m_aReferencedBy;
- // By default, some information may be hidden to the current user
- // But it may happen that we need to disable that feature
- protected $m_bAllowAllData = false;
- protected $m_bDataFiltered = false;
- public function __construct($sClass, $sClassAlias = null)
- {
- parent::__construct();
- if (is_null($sClassAlias)) $sClassAlias = $sClass;
- if(!is_string($sClass)) throw new Exception('DBObjectSearch::__construct called with a non-string parameter: $sClass = '.print_r($sClass, true));
- if(!MetaModel::IsValidClass($sClass)) throw new Exception('DBObjectSearch::__construct called for an invalid class: "'.$sClass.'"');
- $this->m_aSelectedClasses = array($sClassAlias => $sClass);
- $this->m_aClasses = array($sClassAlias => $sClass);
- $this->m_oSearchCondition = new TrueExpression;
- $this->m_aParams = array();
- $this->m_aFullText = array();
- $this->m_aPointingTo = array();
- $this->m_aReferencedBy = array();
- }
- public function AllowAllData($bAllowAllData = true) {$this->m_bAllowAllData = $bAllowAllData;}
- public function IsAllDataAllowed() {return $this->m_bAllowAllData;}
- protected function IsDataFiltered() {return $this->m_bDataFiltered; }
- protected function SetDataFiltered() {$this->m_bDataFiltered = true;}
- // Create a search definition that leads to 0 result, still a valid search object
- static public function FromEmptySet($sClass)
- {
- $oResultFilter = new DBObjectSearch($sClass);
- $oResultFilter->m_oSearchCondition = new FalseExpression;
- return $oResultFilter;
- }
- public function GetJoinedClasses() {return $this->m_aClasses;}
- public function GetClassName($sAlias)
- {
- if (array_key_exists($sAlias, $this->m_aSelectedClasses))
- {
- return $this->m_aSelectedClasses[$sAlias];
- }
- else
- {
- throw new CoreException("Invalid class alias '$sAlias'");
- }
- }
- public function GetClass()
- {
- return reset($this->m_aSelectedClasses);
- }
- public function GetClassAlias()
- {
- reset($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);
- }
- /**
- * Change the class (only subclasses are supported as of now, because the conditions must fit the new class)
- * Defaults to the first selected class (most of the time it is also the first joined class
- */
- public function ChangeClass($sNewClass, $sAlias = null)
- {
- if (is_null($sAlias))
- {
- $sAlias = $this->GetClassAlias();
- }
- else
- {
- if (!array_key_exists($sAlias, $this->m_aSelectedClasses))
- {
- // discard silently - necessary when recursing on the related nodes (see code below)
- return;
- }
- }
- $sCurrClass = $this->GetClassName($sAlias);
- if ($sNewClass == $sCurrClass)
- {
- // Skip silently
- return;
- }
- if (!MetaModel::IsParentClass($sCurrClass, $sNewClass))
- {
- throw new Exception("Could not change the search class from '$sCurrClass' to '$sNewClass'. Only child classes are permitted.");
- }
- // Change for this node
- //
- $this->m_aSelectedClasses[$sAlias] = $sNewClass;
- $this->m_aClasses[$sAlias] = $sNewClass;
- // Change for all the related node (yes, this was necessary with some queries - strange effects otherwise)
- //
- foreach($this->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oExtFilter)
- {
- $oExtFilter->ChangeClass($sNewClass, $sAlias);
- }
- }
- }
- foreach($this->m_aReferencedBy as $sForeignClass => $aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $oForeignFilter->ChangeClass($sNewClass, $sAlias);
- }
- }
- }
- }
- }
- public function GetSelectedClasses()
- {
- return $this->m_aSelectedClasses;
- }
- /**
- * @param array $aSelectedClasses array of aliases
- * @throws CoreException
- */
- public function SetSelectedClasses($aSelectedClasses)
- {
- $this->m_aSelectedClasses = array();
- foreach ($aSelectedClasses as $sAlias)
- {
- if (!array_key_exists($sAlias, $this->m_aClasses))
- {
- throw new CoreException("SetSelectedClasses: Invalid class alias $sAlias");
- }
- $this->m_aSelectedClasses[$sAlias] = $this->m_aClasses[$sAlias];
- }
- }
- /**
- * Change any alias of the query tree
- *
- * @param $sOldName
- * @param $sNewName
- * @return bool True if the alias has been found and changed
- */
- public function RenameAlias($sOldName, $sNewName)
- {
- $bFound = false;
- if (array_key_exists($sOldName, $this->m_aClasses))
- {
- $bFound = true;
- }
- if (array_key_exists($sNewName, $this->m_aClasses))
- {
- throw new Exception("RenameAlias: alias '$sNewName' already used");
- }
- $aClasses = array();
- foreach ($this->m_aClasses as $sAlias => $sClass)
- {
- if ($sAlias === $sOldName)
- {
- $aClasses[$sNewName] = $sClass;
- }
- else
- {
- $aClasses[$sAlias] = $sClass;
- }
- }
- $this->m_aClasses = $aClasses;
- $aSelectedClasses = array();
- foreach ($this->m_aSelectedClasses as $sAlias => $sClass)
- {
- if ($sAlias === $sOldName)
- {
- $aSelectedClasses[$sNewName] = $sClass;
- }
- else
- {
- $aSelectedClasses[$sAlias] = $sClass;
- }
- }
- $this->m_aSelectedClasses = $aSelectedClasses;
- $this->m_oSearchCondition->RenameAlias($sOldName, $sNewName);
- foreach($this->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oExtFilter)
- {
- $bFound = $oExtFilter->RenameAlias($sOldName, $sNewName) || $bFound;
- }
- }
- }
- foreach($this->m_aReferencedBy as $sForeignClass => $aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $bFound = $oForeignFilter->RenameAlias($sOldName, $sNewName) || $bFound;
- }
- }
- }
- }
- return $bFound;
- }
- public function SetModifierProperty($sPluginClass, $sProperty, $value)
- {
- $this->m_aModifierProperties[$sPluginClass][$sProperty] = $value;
- }
- public function GetModifierProperties($sPluginClass)
- {
- if (array_key_exists($sPluginClass, $this->m_aModifierProperties))
- {
- return $this->m_aModifierProperties[$sPluginClass];
- }
- else
- {
- return array();
- }
- }
- public function IsAny()
- {
- if (!$this->m_oSearchCondition->IsTrue()) return false;
- if (count($this->m_aFullText) > 0) return false;
- if (count($this->m_aPointingTo) > 0) return false;
- if (count($this->m_aReferencedBy) > 0) return false;
- return true;
- }
- protected function TransferConditionExpression($oFilter, $aTranslation)
- {
- // Prevent collisions in the parameter names by renaming them if needed
- foreach($this->m_aParams as $sParam => $value)
- {
- if (array_key_exists($sParam, $oFilter->m_aParams) && ($value != $oFilter->m_aParams[$sParam]))
- {
- // Generate a new and unique name for the collinding parameter
- $index = 1;
- while(array_key_exists($sParam.$index, $oFilter->m_aParams))
- {
- $index++;
- }
- $secondValue = $oFilter->m_aParams[$sParam];
- $oFilter->RenameParam($sParam, $sParam.$index);
- unset($oFilter->m_aParams[$sParam]);
- $oFilter->m_aParams[$sParam.$index] = $secondValue;
- }
- }
- $oTranslated = $oFilter->GetCriteria()->Translate($aTranslation, false, false /* leave unresolved fields */);
- $this->AddConditionExpression($oTranslated);
- $this->m_aParams = array_merge($this->m_aParams, $oFilter->m_aParams);
- }
- protected function RenameParam($sOldName, $sNewName)
- {
- $this->m_oSearchCondition->RenameParam($sOldName, $sNewName);
- foreach($this->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oExtFilter)
- {
- $oExtFilter->RenameParam($sOldName, $sNewName);
- }
- }
- }
- foreach($this->m_aReferencedBy as $sForeignClass => $aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $oForeignFilter->RenameParam($sOldName, $sNewName);
- }
- }
- }
- }
- }
-
- public function ResetCondition()
- {
- $this->m_oSearchCondition = new TrueExpression();
- // ? is that usefull/enough, do I need to rebuild the list after the subqueries ?
- }
- public function MergeConditionExpression($oExpression)
- {
- $this->m_oSearchCondition = $this->m_oSearchCondition->LogOr($oExpression);
- }
- public function AddConditionExpression($oExpression)
- {
- $this->m_oSearchCondition = $this->m_oSearchCondition->LogAnd($oExpression);
- }
- public function AddNameCondition($sName)
- {
- $oValueExpr = new ScalarExpression($sName);
- $oNameExpr = new FieldExpression('friendlyname', $this->GetClassAlias());
- $oNewCondition = new BinaryExpression($oNameExpr, '=', $oValueExpr);
- $this->AddConditionExpression($oNewCondition);
- }
- public function AddCondition($sFilterCode, $value, $sOpCode = null, $bParseSeachString = false)
- {
- MyHelpers::CheckKeyInArray('filter code in class: '.$this->GetClass(), $sFilterCode, MetaModel::GetClassFilterDefs($this->GetClass()));
- $oFilterDef = MetaModel::GetClassFilterDef($this->GetClass(), $sFilterCode);
- $oField = new FieldExpression($sFilterCode, $this->GetClassAlias());
- if (empty($sOpCode))
- {
- if ($sFilterCode == 'id')
- {
- $sOpCode = '=';
- }
- else
- {
- $oAttDef = MetaModel::GetAttributeDef($this->GetClass(), $sFilterCode);
- $oNewCondition = $oAttDef->GetSmartConditionExpression($value, $oField, $this->m_aParams, $bParseSeachString);
- $this->AddConditionExpression($oNewCondition);
- return;
- }
- }
- MyHelpers::CheckKeyInArray('operator', $sOpCode, $oFilterDef->GetOperators());
- // Parse search strings if needed and if the filter code corresponds to a valid attcode
- if($bParseSeachString && MetaModel::IsValidAttCode($this->GetClass(), $sFilterCode))
- {
- $oAttDef = MetaModel::GetAttributeDef($sClass, $sFilterCode);
- $value = $oAttDef->ParseSearchString($value);
- }
- // Preserve backward compatibility - quick n'dirty way to change that API semantic
- //
- switch($sOpCode)
- {
- case 'SameDay':
- case 'SameMonth':
- case 'SameYear':
- case 'Today':
- case '>|':
- case '<|':
- case '=|':
- throw new CoreException('Deprecated operator, please consider using OQL (SQL) expressions like "(TO_DAYS(NOW()) - TO_DAYS(x)) AS AgeDays"', array('operator' => $sOpCode));
- break;
- case "IN":
- if (!is_array($value)) $value = array($value);
- if (count($value) === 0) throw new Exception('AddCondition '.$sOpCode.': Value cannot be an empty array.');
- $sListExpr = '('.implode(', ', CMDBSource::Quote($value)).')';
- $sOQLCondition = $oField->Render()." IN $sListExpr";
- break;
- case "NOTIN":
- if (!is_array($value)) $value = array($value);
- if (count($value) === 0) throw new Exception('AddCondition '.$sOpCode.': Value cannot be an empty array.');
- $sListExpr = '('.implode(', ', CMDBSource::Quote($value)).')';
- $sOQLCondition = $oField->Render()." NOT IN $sListExpr";
- break;
- case 'Contains':
- $this->m_aParams[$sFilterCode] = "%$value%";
- $sOperator = 'LIKE';
- break;
- case 'Begins with':
- $this->m_aParams[$sFilterCode] = "$value%";
- $sOperator = 'LIKE';
- break;
- case 'Finishes with':
- $this->m_aParams[$sFilterCode] = "%$value";
- $sOperator = 'LIKE';
- break;
- default:
- if ($value === null)
- {
- switch ($sOpCode)
- {
- case '=':
- $sOpCode = '*Expression*';
- $oExpression = new FunctionExpression('ISNULL', array($oField));
- break;
- case '!=':
- $sOpCode = '*Expression*';
- $oExpression = new FunctionExpression('ISNULL', array($oField));
- $oExpression = new BinaryExpression($oExpression, '=', new ScalarExpression(0));
- break;
- default:
- throw new Exception("AddCondition on null value: unsupported operator '$sOpCode''");
- }
- }
- else
- {
- $this->m_aParams[$sFilterCode] = $value;
- $sOperator = $sOpCode;
- }
- }
- switch($sOpCode)
- {
- case '*Expression*':
- $oNewCondition = $oExpression;
- break;
- case "IN":
- case "NOTIN":
- $oNewCondition = Expression::FromOQL($sOQLCondition);
- break;
- case 'Contains':
- case 'Begins with':
- case 'Finishes with':
- default:
- $oRightExpr = new VariableExpression($sFilterCode);
- $oNewCondition = new BinaryExpression($oField, $sOperator, $oRightExpr);
- }
- $this->AddConditionExpression($oNewCondition);
- }
- /**
- * Specify a condition on external keys or link sets
- * @param sAttSpec Can be either an attribute code or extkey->[sAttSpec] or linkset->[sAttSpec] and so on, recursively
- * Example: infra_list->ci_id->location_id->country
- * @param value The value to match (can be an array => IN(val1, val2...)
- * @return void
- */
- public function AddConditionAdvanced($sAttSpec, $value)
- {
- $sClass = $this->GetClass();
- $iPos = strpos($sAttSpec, '->');
- if ($iPos !== false)
- {
- $sAttCode = substr($sAttSpec, 0, $iPos);
- $sSubSpec = substr($sAttSpec, $iPos + 2);
- if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
- {
- throw new Exception("Invalid attribute code '$sClass/$sAttCode' in condition specification '$sAttSpec'");
- }
- $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
- if ($oAttDef->IsLinkSet())
- {
- $sTargetClass = $oAttDef->GetLinkedClass();
- $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
- $oNewFilter = new DBObjectSearch($sTargetClass);
- $oNewFilter->AddConditionAdvanced($sSubSpec, $value);
- $this->AddCondition_ReferencedBy($oNewFilter, $sExtKeyToMe);
- }
- elseif ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
- {
- $sTargetClass = $oAttDef->GetTargetClass(EXTKEY_ABSOLUTE);
- $oNewFilter = new DBObjectSearch($sTargetClass);
- $oNewFilter->AddConditionAdvanced($sSubSpec, $value);
- $this->AddCondition_PointingTo($oNewFilter, $sAttCode);
- }
- else
- {
- throw new Exception("Attribute specification '$sAttSpec', '$sAttCode' should be either a link set or an external key");
- }
- }
- else
- {
- // $sAttSpec is an attribute code
- //
- if (is_array($value))
- {
- $oField = new FieldExpression($sAttSpec, $this->GetClass());
- $oListExpr = ListExpression::FromScalars($value);
- $oInValues = new BinaryExpression($oField, 'IN', $oListExpr);
- $this->AddConditionExpression($oInValues);
- }
- else
- {
- $this->AddCondition($sAttSpec, $value);
- }
- }
- }
- public function AddCondition_FullText($sFullText)
- {
- $this->m_aFullText[] = $sFullText;
- }
- protected function AddToNameSpace(&$aClassAliases, &$aAliasTranslation, $bTranslateMainAlias = true)
- {
- if ($bTranslateMainAlias)
- {
- $sOrigAlias = $this->GetFirstJoinedClassAlias();
- if (array_key_exists($sOrigAlias, $aClassAliases))
- {
- $sNewAlias = MetaModel::GenerateUniqueAlias($aClassAliases, $sOrigAlias, $this->GetFirstJoinedClass());
- if (isset($this->m_aSelectedClasses[$sOrigAlias]))
- {
- $this->m_aSelectedClasses[$sNewAlias] = $this->GetFirstJoinedClass();
- unset($this->m_aSelectedClasses[$sOrigAlias]);
- }
- // TEMPORARY ALGORITHM (m_aClasses is not correctly updated, it is not possible to add a subtree onto a subnode)
- // Replace the element at the same position (unset + set is not enough because the hash array is ordered)
- $aPrevList = $this->m_aClasses;
- $this->m_aClasses = array();
- foreach ($aPrevList as $sSomeAlias => $sSomeClass)
- {
- if ($sSomeAlias == $sOrigAlias)
- {
- $this->m_aClasses[$sNewAlias] = $sSomeClass; // note: GetFirstJoinedClass now returns '' !!!
- }
- else
- {
- $this->m_aClasses[$sSomeAlias] = $sSomeClass;
- }
- }
-
- // Translate the condition expression with the new alias
- $aAliasTranslation[$sOrigAlias]['*'] = $sNewAlias;
- }
-
- // add the alias into the filter aliases list
- $aClassAliases[$this->GetFirstJoinedClassAlias()] = $this->GetFirstJoinedClass();
- }
-
- foreach($this->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oFilter)
- {
- $oFilter->AddToNameSpace($aClassAliases, $aAliasTranslation);
- }
- }
- }
- foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $oForeignFilter->AddToNameSpace($aClassAliases, $aAliasTranslation);
- }
- }
- }
- }
- }
- // Browse the tree nodes recursively
- //
- protected function GetNode($sAlias)
- {
- if ($this->GetFirstJoinedClassAlias() == $sAlias)
- {
- return $this;
- }
- else
- {
- foreach($this->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oFilter)
- {
- $ret = $oFilter->GetNode($sAlias);
- if (is_object($ret))
- {
- return $ret;
- }
- }
- }
- }
- foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $ret = $oForeignFilter->GetNode($sAlias);
- if (is_object($ret))
- {
- return $ret;
- }
- }
- }
- }
- }
- }
- // Not found
- return null;
- }
- /**
- * Helper to
- * - convert a translation table (format optimized for the translation in an expression tree) into simple hash
- * - compile over an eventually existing map
- *
- * @param $aRealiasingMap Map to update
- * @param $aAliasTranslation Translation table resulting from calls to MergeWith_InNamespace
- * @return array of <old-alias> => <new-alias>
- */
- protected function UpdateRealiasingMap(&$aRealiasingMap, $aAliasTranslation)
- {
- if ($aRealiasingMap !== null)
- {
- foreach ($aAliasTranslation as $sPrevAlias => $aRules)
- {
- if (isset($aRules['*']))
- {
- $sNewAlias = $aRules['*'];
- $sOriginalAlias = array_search($sPrevAlias, $aRealiasingMap);
- if ($sOriginalAlias !== false)
- {
- $aRealiasingMap[$sOriginalAlias] = $sNewAlias;
- }
- else
- {
- $aRealiasingMap[$sPrevAlias] = $sNewAlias;
- }
- }
- }
- }
- }
- /**
- * Completes the list of alias=>class by browsing the whole structure recursively
- * This a workaround to handle some cases in which the list of classes is not correctly updated.
- * This code should disappear as soon as DBObjectSearch get split between a container search class and a Node class
- *
- * @param $aClasses List to be completed
- */
- protected function RecomputeClassList(&$aClasses)
- {
- $aClasses[$this->GetFirstJoinedClassAlias()] = $this->GetFirstJoinedClass();
- // Recurse in the query tree
- foreach($this->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oFilter)
- {
- $oFilter->RecomputeClassList($aClasses);
- }
- }
- }
- foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $oForeignFilter->RecomputeClassList($aClasses);
- }
- }
- }
- }
- }
- /**
- * @param DBObjectSearch $oFilter
- * @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)
- {
- if (!MetaModel::IsValidKeyAttCode($this->GetClass(), $sExtKeyAttCode))
- {
- throw new CoreWarning("The attribute code '$sExtKeyAttCode' is not an external key of the class '{$this->GetClass()}'");
- }
- $oAttExtKey = MetaModel::GetAttributeDef($this->GetClass(), $sExtKeyAttCode);
- if(!MetaModel::IsSameFamilyBranch($oFilter->GetClass(), $oAttExtKey->GetTargetClass()))
- {
- throw new CoreException("The specified filter (pointing to {$oFilter->GetClass()}) is not compatible with the key '{$this->GetClass()}::$sExtKeyAttCode', which is pointing to {$oAttExtKey->GetTargetClass()}");
- }
- if(($iOperatorCode != TREE_OPERATOR_EQUALS) && !($oAttExtKey instanceof AttributeHierarchicalKey))
- {
- throw new CoreException("The specified tree operator $iOperatorCode is not applicable to the key '{$this->GetClass()}::$sExtKeyAttCode', which is not a HierarchicalKey");
- }
- // Note: though it seems to be a good practice to clone the given source filter
- // (as it was done and fixed an issue in Intersect())
- // this was not implemented here because it was causing a regression (login as admin, select an org, click on any badge)
- // root cause: FromOQL relies on the fact that the passed filter can be modified later
- // NO: $oFilter = $oFilter->DeepClone();
- // See also: Trac #639, and self::AddCondition_ReferencedBy()
- $aAliasTranslation = array();
- $res = $this->AddCondition_PointingTo_InNameSpace($oFilter, $sExtKeyAttCode, $this->m_aClasses, $aAliasTranslation, $iOperatorCode);
- $this->TransferConditionExpression($oFilter, $aAliasTranslation);
- $this->UpdateRealiasingMap($aRealiasingMap, $aAliasTranslation);
- if (ENABLE_OPT && ($oFilter->GetClass() == $oFilter->GetFirstJoinedClass()))
- {
- if (isset($oFilter->m_aReferencedBy[$this->GetClass()][$sExtKeyAttCode][$iOperatorCode]))
- {
- foreach ($oFilter->m_aReferencedBy[$this->GetClass()][$sExtKeyAttCode][$iOperatorCode] as $oRemoteFilter)
- {
- if ($this->GetClass() == $oRemoteFilter->GetClass())
- {
- // Optimization - fold sibling query
- $aAliasTranslation = array();
- $this->MergeWith_InNamespace($oRemoteFilter, $this->m_aClasses, $aAliasTranslation);
- unset($oFilter->m_aReferencedBy[$this->GetClass()][$sExtKeyAttCode][$iOperatorCode]);
- $this->m_oSearchCondition = $this->m_oSearchCondition->Translate($aAliasTranslation, false, false);
- $this->UpdateRealiasingMap($aRealiasingMap, $aAliasTranslation);
- break;
- }
- }
- }
- }
- $this->RecomputeClassList($this->m_aClasses);
- return $res;
- }
- protected function AddCondition_PointingTo_InNameSpace(DBObjectSearch $oFilter, $sExtKeyAttCode, &$aClassAliases, &$aAliasTranslation, $iOperatorCode)
- {
- // Find the node on which the new tree must be attached (most of the time it is "this")
- $oReceivingFilter = $this->GetNode($this->GetClassAlias());
- $bMerged = false;
- if (ENABLE_OPT && isset($oReceivingFilter->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode]))
- {
- foreach ($oReceivingFilter->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode] as $oExisting)
- {
- if ($oExisting->GetClass() == $oFilter->GetClass())
- {
- $oExisting->MergeWith_InNamespace($oFilter, $oExisting->m_aClasses, $aAliasTranslation);
- $bMerged = true;
- break;
- }
- }
- }
- if (!$bMerged)
- {
- $oFilter->AddToNamespace($aClassAliases, $aAliasTranslation);
- $oReceivingFilter->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode][] = $oFilter;
- }
- }
- /**
- * @param DBObjectSearch $oFilter
- * @param $sForeignExtKeyAttCode
- * @param int $iOperatorCode
- * @param null $aRealiasingMap array of <old-alias> => <new-alias>, for each alias that has changed
- */
- public function AddCondition_ReferencedBy(DBObjectSearch $oFilter, $sForeignExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS, &$aRealiasingMap = null)
- {
- $sForeignClass = $oFilter->GetClass();
- if (!MetaModel::IsValidKeyAttCode($sForeignClass, $sForeignExtKeyAttCode))
- {
- throw new CoreException("The attribute code '$sForeignExtKeyAttCode' is not an external key of the class '{$sForeignClass}'");
- }
- $oAttExtKey = MetaModel::GetAttributeDef($sForeignClass, $sForeignExtKeyAttCode);
- if(!MetaModel::IsSameFamilyBranch($this->GetClass(), $oAttExtKey->GetTargetClass()))
- {
- // à refaire en spécifique dans FromOQL
- throw new CoreException("The specified filter (objects referencing an object of class {$this->GetClass()}) is not compatible with the key '{$sForeignClass}::$sForeignExtKeyAttCode', which is pointing to {$oAttExtKey->GetTargetClass()}");
- }
- // Note: though it seems to be a good practice to clone the given source filter
- // (as it was done and fixed an issue in Intersect())
- // this was not implemented here because it was causing a regression (login as admin, select an org, click on any badge)
- // root cause: FromOQL relies on the fact that the passed filter can be modified later
- // NO: $oFilter = $oFilter->DeepClone();
- // See also: Trac #639, and self::AddCondition_PointingTo()
- $aAliasTranslation = array();
- $res = $this->AddCondition_ReferencedBy_InNameSpace($oFilter, $sForeignExtKeyAttCode, $this->m_aClasses, $aAliasTranslation, $iOperatorCode);
- $this->TransferConditionExpression($oFilter, $aAliasTranslation);
- $this->UpdateRealiasingMap($aRealiasingMap, $aAliasTranslation);
- if (ENABLE_OPT && ($oFilter->GetClass() == $oFilter->GetFirstJoinedClass()))
- {
- if (isset($oFilter->m_aPointingTo[$sForeignExtKeyAttCode][$iOperatorCode]))
- {
- foreach ($oFilter->m_aPointingTo[$sForeignExtKeyAttCode][$iOperatorCode] as $oRemoteFilter)
- {
- if ($this->GetClass() == $oRemoteFilter->GetClass())
- {
- // Optimization - fold sibling query
- $aAliasTranslation = array();
- $this->MergeWith_InNamespace($oRemoteFilter, $this->m_aClasses, $aAliasTranslation);
- unset($oFilter->m_aPointingTo[$sForeignExtKeyAttCode][$iOperatorCode]);
- $this->m_oSearchCondition = $this->m_oSearchCondition->Translate($aAliasTranslation, false, false);
- $this->UpdateRealiasingMap($aRealiasingMap, $aAliasTranslation);
- break;
- }
- }
- }
- }
- $this->RecomputeClassList($this->m_aClasses);
- return $res;
- }
- protected function AddCondition_ReferencedBy_InNameSpace(DBSearch $oFilter, $sForeignExtKeyAttCode, &$aClassAliases, &$aAliasTranslation, $iOperatorCode)
- {
- $sForeignClass = $oFilter->GetClass();
- // Find the node on which the new tree must be attached (most of the time it is "this")
- $oReceivingFilter = $this->GetNode($this->GetClassAlias());
- $bMerged = false;
- if (ENABLE_OPT && isset($oReceivingFilter->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode][$iOperatorCode]))
- {
- foreach ($oReceivingFilter->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode][$iOperatorCode] as $oExisting)
- {
- if ($oExisting->GetClass() == $oFilter->GetClass())
- {
- $oExisting->MergeWith_InNamespace($oFilter, $oExisting->m_aClasses, $aAliasTranslation);
- $bMerged = true;
- break;
- }
- }
- }
- if (!$bMerged)
- {
- $oFilter->AddToNamespace($aClassAliases, $aAliasTranslation);
- $oReceivingFilter->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode][$iOperatorCode][] = $oFilter;
- }
- }
- public function Intersect(DBSearch $oFilter)
- {
- if ($oFilter instanceof DBUnionSearch)
- {
- // Develop!
- $aFilters = $oFilter->GetSearches();
- }
- else
- {
- $aFilters = array($oFilter);
- }
- $aSearches = array();
- foreach ($aFilters as $oRightFilter)
- {
- // Limitation: the queried class must be the first declared class
- if ($this->GetFirstJoinedClassAlias() != $this->GetClassAlias())
- {
- throw new CoreException("Limitation: cannot merge two queries if the queried class ({$this->GetClass()} AS {$this->GetClassAlias()}) is not the first joined class ({$this->GetFirstJoinedClass()} AS {$this->GetFirstJoinedClassAlias()})");
- }
- if ($oRightFilter->GetFirstJoinedClassAlias() != $oRightFilter->GetClassAlias())
- {
- throw new CoreException("Limitation: cannot merge two queries if the queried class ({$oRightFilter->GetClass()} AS {$oRightFilter->GetClassAlias()}) is not the first joined class ({$oRightFilter->GetFirstJoinedClass()} AS {$oRightFilter->GetFirstJoinedClassAlias()})");
- }
- $oLeftFilter = $this->DeepClone();
- $oRightFilter = $oRightFilter->DeepClone();
- $bAllowAllData = ($oLeftFilter->IsAllDataAllowed() && $oRightFilter->IsAllDataAllowed());
- $oLeftFilter->AllowAllData($bAllowAllData);
- if ($oLeftFilter->GetClass() != $oRightFilter->GetClass())
- {
- if (MetaModel::IsParentClass($oLeftFilter->GetClass(), $oRightFilter->GetClass()))
- {
- // Specialize $oLeftFilter
- $oLeftFilter->ChangeClass($oRightFilter->GetClass());
- }
- elseif (MetaModel::IsParentClass($oRightFilter->GetClass(), $oLeftFilter->GetClass()))
- {
- // Specialize $oRightFilter
- $oRightFilter->ChangeClass($oLeftFilter->GetClass());
- }
- else
- {
- throw new CoreException("Attempting to merge a filter of class '{$oLeftFilter->GetClass()}' with a filter of class '{$oRightFilter->GetClass()}'");
- }
- }
- $aAliasTranslation = array();
- $oLeftFilter->MergeWith_InNamespace($oRightFilter, $oLeftFilter->m_aClasses, $aAliasTranslation);
- $oLeftFilter->TransferConditionExpression($oRightFilter, $aAliasTranslation);
- $aSearches[] = $oLeftFilter;
- }
- if (count($aSearches) == 1)
- {
- // return a DBObjectSearch
- return $aSearches[0];
- }
- else
- {
- return new DBUnionSearch($aSearches);
- }
- }
- protected function MergeWith_InNamespace($oFilter, &$aClassAliases, &$aAliasTranslation)
- {
- if ($this->GetClass() != $oFilter->GetClass())
- {
- throw new CoreException("Attempting to merge a filter of class '{$this->GetClass()}' with a filter of class '{$oFilter->GetClass()}'");
- }
- // Translate search condition into our aliasing scheme
- $aAliasTranslation[$oFilter->GetClassAlias()]['*'] = $this->GetClassAlias();
- $this->m_aFullText = array_merge($this->m_aFullText, $oFilter->m_aFullText);
- foreach($oFilter->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oExtFilter)
- {
- $this->AddCondition_PointingTo_InNamespace($oExtFilter, $sExtKeyAttCode, $aClassAliases, $aAliasTranslation, $iOperatorCode);
- }
- }
- }
- foreach($oFilter->m_aReferencedBy as $sForeignClass => $aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $this->AddCondition_ReferencedBy_InNamespace($oForeignFilter, $sForeignExtKeyAttCode, $aClassAliases, $aAliasTranslation, $iOperatorCode);
- }
- }
- }
- }
- }
- public function GetCriteria() {return $this->m_oSearchCondition;}
- public function GetCriteria_FullText() {return $this->m_aFullText;}
- protected function GetCriteria_PointingTo($sKeyAttCode = "")
- {
- if (empty($sKeyAttCode))
- {
- return $this->m_aPointingTo;
- }
- if (!array_key_exists($sKeyAttCode, $this->m_aPointingTo)) return array();
- return $this->m_aPointingTo[$sKeyAttCode];
- }
- protected function GetCriteria_ReferencedBy()
- {
- return $this->m_aReferencedBy;
- }
- public function SetInternalParams($aParams)
- {
- return $this->m_aParams = $aParams;
- }
- public function GetInternalParams()
- {
- return $this->m_aParams;
- }
- public function GetQueryParams($bExcludeMagicParams = true)
- {
- $aParams = array();
- $this->m_oSearchCondition->Render($aParams, true);
- if ($bExcludeMagicParams)
- {
- $aRet = array();
- // Make the list of acceptable arguments... could be factorized with run_query, into oSearch->GetQueryParams($bExclude magic params)
- $aNakedMagicArguments = array();
- foreach (MetaModel::PrepareQueryArguments(array()) as $sArgName => $value)
- {
- $iPos = strpos($sArgName, '->object()');
- if ($iPos === false)
- {
- $aNakedMagicArguments[$sArgName] = $value;
- }
- else
- {
- $aNakedMagicArguments[substr($sArgName, 0, $iPos)] = true;
- }
- }
- foreach ($aParams as $sParam => $foo)
- {
- $iPos = strpos($sParam, '->');
- if ($iPos === false)
- {
- $sRefName = $sParam;
- }
- else
- {
- $sRefName = substr($sParam, 0, $iPos);
- }
- if (!array_key_exists($sRefName, $aNakedMagicArguments))
- {
- $aRet[$sParam] = $foo;
- }
- }
- }
- return $aRet;
- }
- public function ListConstantFields()
- {
- return $this->m_oSearchCondition->ListConstantFields();
- }
-
- /**
- * Turn the parameters (:xxx) into scalar values in order to easily
- * serialize a search
- */
- public function ApplyParameters($aArgs)
- {
- return $this->m_oSearchCondition->ApplyParameters(array_merge($this->m_aParams, $aArgs));
- }
-
- public function ToOQL($bDevelopParams = false, $aContextParams = null, $bWithAllowAllFlag = false)
- {
- // Currently unused, but could be useful later
- $bRetrofitParams = false;
- if ($bDevelopParams)
- {
- if (is_null($aContextParams))
- {
- $aParams = array_merge($this->m_aParams);
- }
- else
- {
- $aParams = array_merge($aContextParams, $this->m_aParams);
- }
- $aParams = MetaModel::PrepareQueryArguments($aParams);
- }
- else
- {
- // Leave it as is, the rendering will be made with parameters in clear
- $aParams = null;
- }
- $aSelectedAliases = array();
- foreach ($this->m_aSelectedClasses as $sAlias => $sClass)
- {
- $aSelectedAliases[] = '`' . $sAlias . '`';
- }
- $sSelectedClasses = implode(', ', $aSelectedAliases);
- $sRes = 'SELECT '.$sSelectedClasses.' FROM';
- $sRes .= ' ' . $this->GetFirstJoinedClass() . ' AS `' . $this->GetFirstJoinedClassAlias() . '`';
- $sRes .= $this->ToOQL_Joins();
- $sRes .= " WHERE ".$this->m_oSearchCondition->Render($aParams, $bRetrofitParams);
- // Temporary: add more info about other conditions, necessary to avoid strange behaviors with the cache
- foreach($this->m_aFullText as $sFullText)
- {
- $sRes .= " AND MATCHES '$sFullText'";
- }
- if ($bWithAllowAllFlag && $this->m_bAllowAllData)
- {
- $sRes .= " ALLOW ALL DATA";
- }
- return $sRes;
- }
- protected function OperatorCodeToOQL($iOperatorCode)
- {
- switch($iOperatorCode)
- {
- case TREE_OPERATOR_EQUALS:
- $sOperator = ' = ';
- break;
- case TREE_OPERATOR_BELOW:
- $sOperator = ' BELOW ';
- break;
- case TREE_OPERATOR_BELOW_STRICT:
- $sOperator = ' BELOW STRICT ';
- break;
- case TREE_OPERATOR_NOT_BELOW:
- $sOperator = ' NOT BELOW ';
- break;
- case TREE_OPERATOR_NOT_BELOW_STRICT:
- $sOperator = ' NOT BELOW STRICT ';
- break;
- case TREE_OPERATOR_ABOVE:
- $sOperator = ' ABOVE ';
- break;
- case TREE_OPERATOR_ABOVE_STRICT:
- $sOperator = ' ABOVE STRICT ';
- break;
- case TREE_OPERATOR_NOT_ABOVE:
- $sOperator = ' NOT ABOVE ';
- break;
- case TREE_OPERATOR_NOT_ABOVE_STRICT:
- $sOperator = ' NOT ABOVE STRICT ';
- break;
- }
- return $sOperator;
- }
- protected function ToOQL_Joins()
- {
- $sRes = '';
- foreach($this->m_aPointingTo as $sExtKey => $aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- $sOperator = $this->OperatorCodeToOQL($iOperatorCode);
- foreach($aFilter as $oFilter)
- {
- $sRes .= ' JOIN ' . $oFilter->GetFirstJoinedClass() . ' AS `' . $oFilter->GetFirstJoinedClassAlias() . '` ON `' . $this->GetFirstJoinedClassAlias() . '`.' . $sExtKey . $sOperator . '`' . $oFilter->GetFirstJoinedClassAlias() . '`.id';
- $sRes .= $oFilter->ToOQL_Joins();
- }
- }
- }
- foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- $sOperator = $this->OperatorCodeToOQL($iOperatorCode);
- foreach ($aFilters as $oForeignFilter)
- {
- $sRes .= ' JOIN ' . $oForeignFilter->GetFirstJoinedClass() . ' AS `' . $oForeignFilter->GetFirstJoinedClassAlias() . '` ON `' . $oForeignFilter->GetFirstJoinedClassAlias() . '`.' . $sForeignExtKeyAttCode . $sOperator . '`' . $this->GetFirstJoinedClassAlias() . '`.id';
- $sRes .= $oForeignFilter->ToOQL_Joins();
- }
- }
- }
- }
- return $sRes;
- }
- protected function OQLExpressionToCondition($sQuery, $oExpression, $aClassAliases)
- {
- if ($oExpression instanceof BinaryOqlExpression)
- {
- $sOperator = $oExpression->GetOperator();
- $oLeft = $this->OQLExpressionToCondition($sQuery, $oExpression->GetLeftExpr(), $aClassAliases);
- $oRight = $this->OQLExpressionToCondition($sQuery, $oExpression->GetRightExpr(), $aClassAliases);
- return new BinaryExpression($oLeft, $sOperator, $oRight);
- }
- elseif ($oExpression instanceof FieldOqlExpression)
- {
- $sClassAlias = $oExpression->GetParent();
- $sFltCode = $oExpression->GetName();
- if (empty($sClassAlias))
- {
- // Need to find the right alias
- // Build an array of field => array of aliases
- $aFieldClasses = array();
- foreach($aClassAliases as $sAlias => $sReal)
- {
- foreach(MetaModel::GetFiltersList($sReal) as $sAnFltCode)
- {
- $aFieldClasses[$sAnFltCode][] = $sAlias;
- }
- }
- $sClassAlias = $aFieldClasses[$sFltCode][0];
- }
- return new FieldExpression($sFltCode, $sClassAlias);
- }
- elseif ($oExpression instanceof VariableOqlExpression)
- {
- return new VariableExpression($oExpression->GetName());
- }
- elseif ($oExpression instanceof TrueOqlExpression)
- {
- return new TrueExpression;
- }
- elseif ($oExpression instanceof ScalarOqlExpression)
- {
- return new ScalarExpression($oExpression->GetValue());
- }
- elseif ($oExpression instanceof ListOqlExpression)
- {
- $aItems = array();
- foreach ($oExpression->GetItems() as $oItemExpression)
- {
- $aItems[] = $this->OQLExpressionToCondition($sQuery, $oItemExpression, $aClassAliases);
- }
- return new ListExpression($aItems);
- }
- elseif ($oExpression instanceof FunctionOqlExpression)
- {
- $aArgs = array();
- foreach ($oExpression->GetArgs() as $oArgExpression)
- {
- $aArgs[] = $this->OQLExpressionToCondition($sQuery, $oArgExpression, $aClassAliases);
- }
- return new FunctionExpression($oExpression->GetVerb(), $aArgs);
- }
- elseif ($oExpression instanceof IntervalOqlExpression)
- {
- return new IntervalExpression($oExpression->GetValue(), $oExpression->GetUnit());
- }
- else
- {
- throw new CoreException('Unknown expression type', array('class'=>get_class($oExpression), 'query'=>$sQuery));
- }
- }
- public function InitFromOqlQuery(OqlQuery $oOqlQuery, $sQuery)
- {
- $oModelReflection = new ModelReflectionRuntime();
- $sClass = $oOqlQuery->GetClass($oModelReflection);
- $sClassAlias = $oOqlQuery->GetClassAlias();
- $aAliases = array($sClassAlias => $sClass);
- // Note: the condition must be built here, it may be altered later on when optimizing some joins
- $oConditionTree = $oOqlQuery->GetCondition();
- if ($oConditionTree instanceof Expression)
- {
- $aRawAliases = array($sClassAlias => $sClass);
- $aJoinSpecs = $oOqlQuery->GetJoins();
- if (is_array($aJoinSpecs))
- {
- foreach ($aJoinSpecs as $oJoinSpec)
- {
- $aRawAliases[$oJoinSpec->GetClassAlias()] = $oJoinSpec->GetClass();
- }
- }
- $this->m_oSearchCondition = $this->OQLExpressionToCondition($sQuery, $oConditionTree, $aRawAliases);
- }
- // Maintain an array of filters, because the flat list is in fact referring to a tree
- // And this will be an easy way to dispatch the conditions
- // $this will be referenced by the other filters, or the other way around...
- $aJoinItems = array($sClassAlias => $this);
- $aJoinSpecs = $oOqlQuery->GetJoins();
- if (is_array($aJoinSpecs))
- {
- $aAliasTranslation = array();
- foreach ($aJoinSpecs as $oJoinSpec)
- {
- $sJoinClass = $oJoinSpec->GetClass();
- $sJoinClassAlias = $oJoinSpec->GetClassAlias();
- if (isset($aAliasTranslation[$sJoinClassAlias]['*']))
- {
- $sJoinClassAlias = $aAliasTranslation[$sJoinClassAlias]['*'];
- }
- // Assumption: ext key on the left only !!!
- // normalization should take care of this
- $oLeftField = $oJoinSpec->GetLeftField();
- $sFromClass = $oLeftField->GetParent();
- if (isset($aAliasTranslation[$sFromClass]['*']))
- {
- $sFromClass = $aAliasTranslation[$sFromClass]['*'];
- }
- $sExtKeyAttCode = $oLeftField->GetName();
- $oRightField = $oJoinSpec->GetRightField();
- $sToClass = $oRightField->GetParent();
- if (isset($aAliasTranslation[$sToClass]['*']))
- {
- $sToClass = $aAliasTranslation[$sToClass]['*'];
- }
- $aAliases[$sJoinClassAlias] = $sJoinClass;
- $aJoinItems[$sJoinClassAlias] = new DBObjectSearch($sJoinClass, $sJoinClassAlias);
- $sOperator = $oJoinSpec->GetOperator();
- switch($sOperator)
- {
- case '=':
- default:
- $iOperatorCode = TREE_OPERATOR_EQUALS;
- break;
- case 'BELOW':
- $iOperatorCode = TREE_OPERATOR_BELOW;
- break;
- case 'BELOW_STRICT':
- $iOperatorCode = TREE_OPERATOR_BELOW_STRICT;
- break;
- case 'NOT_BELOW':
- $iOperatorCode = TREE_OPERATOR_NOT_BELOW;
- break;
- case 'NOT_BELOW_STRICT':
- $iOperatorCode = TREE_OPERATOR_NOT_BELOW_STRICT;
- break;
- case 'ABOVE':
- $iOperatorCode = TREE_OPERATOR_ABOVE;
- break;
- case 'ABOVE_STRICT':
- $iOperatorCode = TREE_OPERATOR_ABOVE_STRICT;
- break;
- case 'NOT_ABOVE':
- $iOperatorCode = TREE_OPERATOR_NOT_ABOVE;
- break;
- case 'NOT_ABOVE_STRICT':
- $iOperatorCode = TREE_OPERATOR_NOT_ABOVE_STRICT;
- break;
- }
- if ($sFromClass == $sJoinClassAlias)
- {
- $oReceiver = $aJoinItems[$sToClass];
- $oNewComer = $aJoinItems[$sFromClass];
- $oReceiver->AddCondition_ReferencedBy_InNameSpace($oNewComer, $sExtKeyAttCode, $oReceiver->m_aClasses, $aAliasTranslation, $iOperatorCode);
- }
- else
- {
- $oReceiver = $aJoinItems[$sFromClass];
- $oNewComer = $aJoinItems[$sToClass];
- $oReceiver->AddCondition_PointingTo_InNameSpace($oNewComer, $sExtKeyAttCode, $oReceiver->m_aClasses, $aAliasTranslation, $iOperatorCode);
- }
- }
- $this->m_oSearchCondition = $this->m_oSearchCondition->Translate($aAliasTranslation, false, false /* leave unresolved fields */);
- }
- // Check and prepare the select information
- $this->m_aSelectedClasses = array();
- foreach ($oOqlQuery->GetSelectedClasses() as $oClassDetails)
- {
- $sClassToSelect = $oClassDetails->GetValue();
- $this->m_aSelectedClasses[$sClassToSelect] = $aAliases[$sClassToSelect];
- }
- $this->m_aClasses = $aAliases;
- }
- ////////////////////////////////////////////////////////////////////////////
- //
- // Construction of the SQL queries
- //
- ////////////////////////////////////////////////////////////////////////////
- public function MakeDeleteQuery($aArgs = array())
- {
- $aModifierProperties = MetaModel::MakeModifierProperties($this);
- $oBuild = new QueryBuilderContext($this, $aModifierProperties);
- $oSQLQuery = $this->MakeSQLObjectQuery($oBuild, null, array());
- $oSQLQuery->SetCondition($oBuild->m_oQBExpressions->GetCondition());
- $oSQLQuery->SetSelect($oBuild->m_oQBExpressions->GetSelect());
- $aScalarArgs = MetaModel::PrepareQueryArguments($aArgs, $this->GetInternalParams());
- return $oSQLQuery->RenderDelete($aScalarArgs);
- }
- public function MakeUpdateQuery($aValues, $aArgs = array())
- {
- // $aValues is an array of $sAttCode => $value
- $aModifierProperties = MetaModel::MakeModifierProperties($this);
- $oBuild = new QueryBuilderContext($this, $aModifierProperties);
- $oSQLQuery = $this->MakeSQLObjectQuery($oBuild, null, $aValues);
- $oSQLQuery->SetCondition($oBuild->m_oQBExpressions->GetCondition());
- $oSQLQuery->SetSelect($oBuild->m_oQBExpressions->GetSelect());
- $aScalarArgs = MetaModel::PrepareQueryArguments($aArgs, $this->GetInternalParams());
- return $oSQLQuery->RenderUpdate($aScalarArgs);
- }
- public function GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null)
- {
- // Hide objects that are not visible to the current user
- //
- $oSearch = $this;
- if (!$this->IsAllDataAllowed() && !$this->IsDataFiltered())
- {
- $oVisibleObjects = UserRights::GetSelectFilter($this->GetClass(), $this->GetModifierProperties('UserRightsGetSelectFilter'));
- if ($oVisibleObjects === false)
- {
- // Make sure this is a valid search object, saying NO for all
- $oVisibleObjects = DBObjectSearch::FromEmptySet($this->GetClass());
- }
- if (is_object($oVisibleObjects))
- {
- $oVisibleObjects->AllowAllData();
- $oSearch = $this->Intersect($oVisibleObjects);
- $oSearch->SetDataFiltered();
- }
- else
- {
- // should be true at this point, meaning that no additional filtering
- // is required
- }
- }
- // Compute query modifiers properties (can be set in the search itself, by the context, etc.)
- //
- $aModifierProperties = MetaModel::MakeModifierProperties($oSearch);
- // Create a unique cache id
- //
- if (self::$m_bQueryCacheEnabled || self::$m_bTraceQueries)
- {
- // Need to identify the query
- $sOqlQuery = $oSearch->ToOql(false, null, true);
- if (count($aModifierProperties))
- {
- array_multisort($aModifierProperties);
- $sModifierProperties = json_encode($aModifierProperties);
- }
- else
- {
- $sModifierProperties = '';
- }
- $sRawId = $sOqlQuery.$sModifierProperties;
- if (!is_null($aAttToLoad))
- {
- $sRawId .= json_encode($aAttToLoad);
- }
- if (!is_null($aGroupByExpr))
- {
- foreach($aGroupByExpr as $sAlias => $oExpr)
- {
- $sRawId .= 'g:'.$sAlias.'!'.$oExpr->Render();
- }
- }
- $sRawId .= $bGetCount;
- if (is_array($aSelectedClasses))
- {
- $sRawId .= implode(',', $aSelectedClasses); // Unions may alter the list of selected columns
- }
- $sRawId .= $oSearch->GetArchiveMode() ? '--arch' : '';
- $sRawId .= $oSearch->GetShowObsoleteData() ? '--obso' : '';
- $sOqlId = md5($sRawId);
- }
- else
- {
- $sOqlQuery = "SELECTING... ".$oSearch->GetClass();
- $sOqlId = "query id ? n/a";
- }
- // Query caching
- //
- if (self::$m_bQueryCacheEnabled)
- {
- // Warning: using directly the query string as the key to the hash array can FAIL if the string
- // is long and the differences are only near the end... so it's safer (but not bullet proof?)
- // to use a hash (like md5) of the string as the key !
- //
- // Example of two queries that were found as similar by the hash array:
- // SELECT SLT JOIN lnkSLTToSLA AS L1 ON L1.slt_id=SLT.id JOIN SLA ON L1.sla_id = SLA.id JOIN lnkContractToSLA AS L2 ON L2.sla_id = SLA.id JOIN CustomerContract ON L2.contract_id = CustomerContract.id WHERE SLT.ticket_priority = 1 AND SLA.service_id = 3 AND SLT.metric = 'TTO' AND CustomerContract.customer_id = 2
- // and
- // SELECT SLT JOIN lnkSLTToSLA AS L1 ON L1.slt_id=SLT.id JOIN SLA ON L1.sla_id = SLA.id JOIN lnkContractToSLA AS L2 ON L2.sla_id = SLA.id JOIN CustomerContract ON L2.contract_id = CustomerContract.id WHERE SLT.ticket_priority = 1 AND SLA.service_id = 3 AND SLT.metric = 'TTR' AND CustomerContract.customer_id = 2
- // the only difference is R instead or O at position 285 (TTR instead of TTO)...
- //
- if (array_key_exists($sOqlId, self::$m_aQueryStructCache))
- {
- // hit!
- $oSQLQuery = unserialize(serialize(self::$m_aQueryStructCache[$sOqlId]));
- // Note: cloning is not enough because the subtree is made of objects
- }
- elseif (self::$m_bUseAPCCache)
- {
- // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter
- //
- $sOqlAPCCacheId = 'itop-'.MetaModel::GetEnvironmentId().'-query-cache-'.$sOqlId;
- $oKPI = new ExecutionKPI();
- $result = apc_fetch($sOqlAPCCacheId);
- $oKPI->ComputeStats('Query APC (fetch)', $sOqlQuery);
- if (is_object($result))
- {
- $oSQLQuery = $result;
- self::$m_aQueryStructCache[$sOqlId] = $oSQLQuery;
- }
- }
- }
- if (!isset($oSQLQuery))
- {
- $oKPI = new ExecutionKPI();
- $oSQLQuery = $oSearch->BuildSQLQueryStruct($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr, $aSelectedClasses);
- $oKPI->ComputeStats('BuildSQLQueryStruct', $sOqlQuery);
- if (self::$m_bQueryCacheEnabled)
- {
- if (self::$m_bUseAPCCache)
- {
- $oKPI = new ExecutionKPI();
- apc_store($sOqlAPCCacheId, $oSQLQuery, self::$m_iQueryCacheTTL);
- $oKPI->ComputeStats('Query APC (store)', $sOqlQuery);
- }
- self::$m_aQueryStructCache[$sOqlId] = $oSQLQuery->DeepClone();
- }
- }
- return $oSQLQuery;
- }
- protected function BuildSQLQueryStruct($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr = null, $aSelectedClasses = null)
- {
- $oBuild = new QueryBuilderContext($this, $aModifierProperties, $aGroupByExpr, $aSelectedClasses);
- $oSQLQuery = $this->MakeSQLObjectQuery($oBuild, $aAttToLoad, array());
- $oSQLQuery->SetCondition($oBuild->m_oQBExpressions->GetCondition());
- if ($aGroupByExpr)
- {
- $aCols = $oBuild->m_oQBExpressions->GetGroupBy();
- $oSQLQuery->SetGroupBy($aCols);
- $oSQLQuery->SetSelect($aCols);
- }
- else
- {
- $oSQLQuery->SetSelect($oBuild->m_oQBExpressions->GetSelect());
- }
- $aMandatoryTables = null;
- if (self::$m_bOptimizeQueries)
- {
- if ($bGetCount)
- {
- // Simplify the query if just getting the count
- $oSQLQuery->SetSelect(array());
- }
- $oBuild->m_oQBExpressions->GetMandatoryTables($aMandatoryTables);
- $oSQLQuery->OptimizeJoins($aMandatoryTables);
- }
- // Filter tables as late as possible: do not interfere with the optimization process
- foreach ($oBuild->GetFilteredTables() as $sTableAlias => $aConditions)
- {
- if ($aMandatoryTables && array_key_exists($sTableAlias, $aMandatoryTables))
- {
- foreach ($aConditions as $oCondition)
- {
- $oSQLQuery->AddCondition($oCondition);
- }
- }
- }
- return $oSQLQuery;
- }
- protected function MakeSQLObjectQuery(&$oBuild, $aAttToLoad = null, $aValues = array())
- {
- // 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)
- $sClass = $this->GetFirstJoinedClass();
- $sClassAlias = $this->GetFirstJoinedClassAlias();
- $bIsOnQueriedClass = array_key_exists($sClassAlias, $oBuild->GetRootFilter()->GetSelectedClasses());
- self::DbgTrace("Entering: ".$this->ToOQL().", ".($bIsOnQueriedClass ? "MAIN" : "SECONDARY"));
- $sRootClass = MetaModel::GetRootClass($sClass);
- $sKeyField = MetaModel::DBGetKey($sClass);
- if ($bIsOnQueriedClass)
- {
- // default to the whole list of attributes + the very std id/finalclass
- $oBuild->m_oQBExpressions->AddSelect($sClassAlias.'id', new FieldExpression('id', $sClassAlias));
- if (is_null($aAttToLoad) || !array_key_exists($sClassAlias, $aAttToLoad))
- {
- $sSelectedClass = $oBuild->GetSelectedClass($sClassAlias);
- $aAttList = MetaModel::ListAttributeDefs($sSelectedClass);
- }
- else
- {
- $aAttList = $aAttToLoad[$sClassAlias];
- }
- foreach ($aAttList as $sAttCode => $oAttDef)
- {
- if (!$oAttDef->IsScalar()) continue;
- // keep because it can be used for sorting - if (!$oAttDef->LoadInObject()) continue;
- if ($oAttDef->IsBasedOnOQLExpression())
- {
- $oBuild->m_oQBExpressions->AddSelect($sClassAlias.$sAttCode, new FieldExpression($sAttCode, $sClassAlias));
- }
- else
- {
- foreach ($oAttDef->GetSQLExpressions() as $sColId => $sSQLExpr)
- {
- $oBuild->m_oQBExpressions->AddSelect($sClassAlias.$sAttCode.$sColId, new FieldExpression($sAttCode.$sColId, $sClassAlias));
- }
- }
- }
- // Transform the full text condition into additional condition expression
- $aFullText = $this->GetCriteria_FullText();
- if (count($aFullText) > 0)
- {
- $aFullTextFields = array();
- foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
- {
- if (!$oAttDef->IsScalar()) continue;
- if ($oAttDef->IsExternalKey()) continue;
- $aFullTextFields[] = new FieldExpression($sAttCode, $sClassAlias);
- }
- $oTextFields = new CharConcatWSExpression(' ', $aFullTextFields);
-
- foreach($aFullText as $sFTNeedle)
- {
- $oNewCond = new BinaryExpression($oTextFields, 'LIKE', new ScalarExpression("%$sFTNeedle%"));
- $oBuild->m_oQBExpressions->AddCondition($oNewCond);
- }
- }
- }
- //echo "<p>oQBExpr ".__LINE__.": <pre>\n".print_r($oBuild->m_oQBExpressions, true)."</pre></p>\n";
- $aExpectedAtts = array(); // array of (attcode => fieldexpression)
- //echo "<p>".__LINE__.": GetUnresolvedFields($sClassAlias, ...)</p>\n";
- $oBuild->m_oQBExpressions->GetUnresolvedFields($sClassAlias, $aExpectedAtts);
- // Compute a clear view of required joins (from the current class)
- // Build the list of external keys:
- // -> ext keys required by an explicit join
- // -> ext keys mentionned in a 'pointing to' condition
- // -> ext keys required for an external field
- // -> ext keys required for a friendly name
- //
- $aExtKeys = array(); // array of sTableClass => array of (sAttCode (keys) => array of (sAttCode (fields)=> oAttDef))
- //
- // Optimization: could be partially computed once for all (cached) ?
- //
- if ($bIsOnQueriedClass)
- {
- // Get all Ext keys for the queried class (??)
- foreach(MetaModel::GetKeysList($sClass) as $sKeyAttCode)
- {
- $sKeyTableClass = MetaModel::GetAttributeOrigin($sClass, $sKeyAttCode);
- $aExtKeys[$sKeyTableClass][$sKeyAttCode] = array();
- }
- }
- // Get all Ext keys used by the filter
- foreach ($this->GetCriteria_PointingTo() as $sKeyAttCode => $aPointingTo)
- {
- if (array_key_exists(TREE_OPERATOR_EQUALS, $aPointingTo))
- {
- $sKeyTableClass = MetaModel::GetAttributeOrigin($sClass, $sKeyAttCode);
- $aExtKeys[$sKeyTableClass][$sKeyAttCode] = array();
- }
- }
- $aFNJoinAlias = array(); // array of (subclass => alias)
- foreach ($aExpectedAtts as $sExpectedAttCode => $oExpression)
- {
- if (!MetaModel::IsValidAttCode($sClass, $sExpectedAttCode)) continue;
- $oAttDef = MetaModel::GetAttributeDef($sClass, $sExpectedAttCode);
- if ($oAttDef->IsBasedOnOQLExpression())
- {
- // To optimize: detect a restriction on child classes in the condition expression
- // e.g. SELECT FunctionalCI WHERE finalclass IN ('Server', 'VirtualMachine')
- $oExpression = static::GetPolymorphicExpression($sClass, $sExpectedAttCode);
- $aRequiredFields = array();
- $oExpression->GetUnresolvedFields('', $aRequiredFields);
- $aTranslateFields = array();
- foreach($aRequiredFields as $sSubClass => $aFields)
- {
- foreach($aFields as $sAttCode => $oField)
- {
- $oAttDef = MetaModel::GetAttributeDef($sSubClass, $sAttCode);
- if ($oAttDef->IsExternalKey())
- {
- $sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sAttCode);
- $aExtKeys[$sClassOfAttribute][$sAttCode] = array();
- }
- elseif ($oAttDef->IsExternalField())
- {
- $sKeyAttCode = $oAttDef->GetKeyAttCode();
- $sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sKeyAttCode);
- $aExtKeys[$sClassOfAttribute][$sKeyAttCode][$sAttCode] = $oAttDef;
- }
- else
- {
- $sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sAttCode);
- }
- if (MetaModel::IsParentClass($sClassOfAttribute, $sClass))
- {
- // The attribute is part of the standard query
- //
- $sAliasForAttribute = $sClassAlias;
- }
- else
- {
- // The attribute will be available from an additional outer join
- // For each subclass (table) one single join is enough
- //
- if (!array_key_exists($sClassOfAttribute, $aFNJoinAlias))
- {
- $sAliasForAttribute = $oBuild->GenerateClassAlias($sClassAlias.'_fn_'.$sClassOfAttribute, $sClassOfAttribute);
- $aFNJoinAlias[$sClassOfAttribute] = $sAliasForAttribute;
- }
- else
- {
- $sAliasForAttribute = $aFNJoinAlias[$sClassOfAttribute];
- }
- }
- $aTranslateFields[$sSubClass][$sAttCode] = new FieldExpression($sAttCode, $sAliasForAttribute);
- }
- }
- $oExpression = $oExpression->Translate($aTranslateFields, false);
- $aTranslateNow = array();
- $aTranslateNow[$sClassAlias][$sExpectedAttCode] = $oExpression;
- $oBuild->m_oQBExpressions->Translate($aTranslateNow, false);
- }
- }
- // Add the ext fields used in the select (eventually adds an external key)
- foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
- {
- if ($oAttDef->IsExternalField())
- {
- if (array_key_exists($sAttCode, $aExpectedAtts))
- {
- // Add the external attribute
- $sKeyAttCode = $oAttDef->GetKeyAttCode();
- $sKeyTableClass = MetaModel::GetAttributeOrigin($sClass, $sKeyAttCode);
- $aExtKeys[$sKeyTableClass][$sKeyAttCode][$sAttCode] = $oAttDef;
- }
- }
- }
- // First query built upon on the leaf (ie current) class
- //
- self::DbgTrace("Main (=leaf) class, call MakeSQLObjectQuerySingleTable()");
- if (MetaModel::HasTable($sClass))
- {
- $oSelectBase = $this->MakeSQLObjectQuerySingleTable($oBuild, $aAttToLoad, $sClass, $aExtKeys, $aValues);
- }
- else
- {
- $oSelectBase = null;
- // As the join will not filter on the expected classes, we have to specify it explicitely
- $sExpectedClasses = implode("', '", MetaModel::EnumChildClasses($sClass, ENUM_CHILD_CLASSES_ALL));
- $oFinalClassRestriction = Expression::FromOQL("`$sClassAlias`.finalclass IN ('$sExpectedClasses')");
- $oBuild->m_oQBExpressions->AddCondition($oFinalClassRestriction);
- }
- // Then we join the queries of the eventual parent classes (compound model)
- foreach(MetaModel::EnumParentClasses($sClass) as $sParentClass)
- {
- if (!MetaModel::HasTable($sParentClass)) continue;
- self::DbgTrace("Parent class: $sParentClass... let's call MakeSQLObjectQuerySingleTable()");
- $oSelectParentTable = $this->MakeSQLObjectQuerySingleTable($oBuild, $aAttToLoad, $sParentClass, $aExtKeys, $aValues);
- if (is_null($oSelectBase))
- {
- $oSelectBase = $oSelectParentTable;
- }
- else
- {
- $oSelectBase->AddInnerJoin($oSelectParentTable, $sKeyField, MetaModel::DBGetKey($sParentClass));
- }
- }
- // Filter on objects referencing me
- //
- foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
- {
- foreach($aReferences as $sForeignExtKeyAttCode => $aFiltersByOperator)
- {
- foreach ($aFiltersByOperator as $iOperatorCode => $aFilters)
- {
- foreach ($aFilters as $oForeignFilter)
- {
- $oForeignKeyAttDef = MetaModel::GetAttributeDef($sForeignClass, $sForeignExtKeyAttCode);
- self::DbgTrace("Referenced by foreign key: $sForeignExtKeyAttCode... let's call MakeSQLObjectQuery()");
- //self::DbgTrace($oForeignFilter);
- //self::DbgTrace($oForeignFilter->ToOQL());
- //self::DbgTrace($oSelectForeign);
- //self::DbgTrace($oSelectForeign->RenderSelect(array()));
- $sForeignClassAlias = $oForeignFilter->GetFirstJoinedClassAlias();
- $oBuild->m_oQBExpressions->PushJoinField(new FieldExpression($sForeignExtKeyAttCode, $sForeignClassAlias));
- if ($oForeignKeyAttDef instanceof AttributeObjectKey)
- {
- $sClassAttCode = $oForeignKeyAttDef->Get('class_attcode');
- // Add the condition: `$sForeignClassAlias`.$sClassAttCode IN (subclasses of $sClass')
- $oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses($sClass, ENUM_CHILD_CLASSES_ALL));
- $oClassExpr = new FieldExpression($sClassAttCode, $sForeignClassAlias);
- $oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr);
- $oBuild->m_oQBExpressions->AddCondition($oClassRestriction);
- }
- $oSelectForeign = $oForeignFilter->MakeSQLObjectQuery($oBuild, $aAttToLoad);
- $oJoinExpr = $oBuild->m_oQBExpressions->PopJoinField();
- $sForeignKeyTable = $oJoinExpr->GetParent();
- $sForeignKeyColumn = $oJoinExpr->GetName();
- if ($iOperatorCode == TREE_OPERATOR_EQUALS)
- {
- $oSelectBase->AddInnerJoin($oSelectForeign, $sKeyField, $sForeignKeyColumn, $sForeignKeyTable);
- }
- else
- {
- // Hierarchical key
- $KeyLeft = $oForeignKeyAttDef->GetSQLLeft();
- $KeyRight = $oForeignKeyAttDef->GetSQLRight();
- $oSelectBase->AddInnerJoinTree($oSelectForeign, $KeyLeft, $KeyRight, $KeyLeft, $KeyRight, $sForeignKeyTable, $iOperatorCode, true);
- }
- }
- }
- }
- }
- // Additional JOINS for Friendly names
- //
- foreach ($aFNJoinAlias as $sSubClass => $sSubClassAlias)
- {
- $oSubClassFilter = new DBObjectSearch($sSubClass, $sSubClassAlias);
- $oSelectFN = $oSubClassFilter->MakeSQLObjectQuerySingleTable($oBuild, $aAttToLoad, $sSubClass, $aExtKeys, array());
- $oSelectBase->AddLeftJoin($oSelectFN, $sKeyField, MetaModel::DBGetKey($sSubClass));
- }
- // That's all... cross fingers and we'll get some working query
- //MyHelpers::var_dump_html($oSelectBase, true);
- //MyHelpers::var_dump_html($oSelectBase->RenderSelect(), true);
- if (self::$m_bDebugQuery) $oSelectBase->DisplayHtml();
- return $oSelectBase;
- }
- protected function MakeSQLObjectQuerySingleTable(&$oBuild, $aAttToLoad, $sTableClass, $aExtKeys, $aValues)
- {
- // $aExtKeys is an array of sTableClass => array of (sAttCode (keys) => array of sAttCode (fields))
- // Prepare the query for a single table (compound objects)
- // Ignores the items (attributes/filters) that are not on the target table
- // Perform an (inner or left) join for every external key (and specify the expected fields)
- //
- // Returns an SQLQuery
- //
- $sTargetClass = $this->GetFirstJoinedClass();
- $sTargetAlias = $this->GetFirstJoinedClassAlias();
- $sTable = MetaModel::DBGetTable($sTableClass);
- $sTableAlias = $oBuild->GenerateTableAlias($sTargetAlias.'_'.$sTable, $sTable);
- $aTranslation = array();
- $aExpectedAtts = array();
- $oBuild->m_oQBExpressions->GetUnresolvedFields($sTargetAlias, $aExpectedAtts);
-
- $bIsOnQueriedClass = array_key_exists($sTargetAlias, $oBuild->GetRootFilter()->GetSelectedClasses());
- self::DbgTrace("Entering: tableclass=$sTableClass, filter=".$this->ToOQL().", ".($bIsOnQueriedClass ? "MAIN" : "SECONDARY"));
- // 1 - SELECT and UPDATE
- //
- // Note: no need for any values nor fields for foreign Classes (ie not the queried Class)
- //
- $aUpdateValues = array();
- // 1/a - Get the key and friendly name
- //
- // We need one pkey to be the key, let's take the first one available
- $oSelectedIdField = null;
- $oIdField = new FieldExpressionResolved(MetaModel::DBGetKey($sTableClass), $sTableAlias);
- $aTranslation[$sTargetAlias]['id'] = $oIdField;
- if ($bIsOnQueriedClass)
- {
- // Add this field to the list of queried fields (required for the COUNT to work fine)
- $oSelectedIdField = $oIdField;
- }
- // 1/b - Get the other attributes
- //
- foreach(MetaModel::ListAttributeDefs($sTableClass) as $sAttCode=>$oAttDef)
- {
- // Skip this attribute if not defined in this table
- if (MetaModel::GetAttributeOrigin($sTargetClass, $sAttCode) != $sTableClass) continue;
- // Skip this attribute if not made of SQL columns
- if (count($oAttDef->GetSQLExpressions()) == 0) continue;
- // Update...
- //
- if ($bIsOnQueriedClass && array_key_exists($sAttCode, $aValues))
- {
- assert ($oAttDef->IsBasedOnDBColumns());
- foreach ($oAttDef->GetSQLValues($aValues[$sAttCode]) as $sColumn => $sValue)
- {
- $aUpdateValues[$sColumn] = $sValue;
- }
- }
- }
- // 2 - The SQL query, for this table only
- //
- $oSelectBase = new SQLObjectQuery($sTable, $sTableAlias, array(), $bIsOnQueriedClass, $aUpdateValues, $oSelectedIdField);
- // 3 - Resolve expected expressions (translation table: alias.attcode => table.column)
- //
- foreach(MetaModel::ListAttributeDefs($sTableClass) as $sAttCode=>$oAttDef)
- {
- // Skip this attribute if not defined in this table
- if (MetaModel::GetAttributeOrigin($sTargetClass, $sAttCode) != $sTableClass) continue;
- // Select...
- //
- if ($oAttDef->IsExternalField())
- {
- // skip, this will be handled in the joined tables (done hereabove)
- }
- else
- {
- // standard field, or external key
- // add it to the output
- foreach ($oAttDef->GetSQLExpressions() as $sColId => $sSQLExpr)
- {
- if (array_key_exists($sAttCode.$sColId, $aExpectedAtts))
- {
- $oFieldSQLExp = new FieldExpressionResolved($sSQLExpr, $sTableAlias);
- foreach (MetaModel::EnumPlugins('iQueryModifier') as $sPluginClass => $oQueryModifier)
- {
- $oFieldSQLExp = $oQueryModifier->GetFieldExpression($oBuild, $sTargetClass, $sAttCode, $sColId, $oFieldSQLExp, $oSelectBase);
- }
- $aTranslation[$sTargetAlias][$sAttCode.$sColId] = $oFieldSQLExp;
- }
- }
- }
- }
- // 4 - The external keys -> joins...
- //
- $aAllPointingTo = $this->GetCriteria_PointingTo();
- if (array_key_exists($sTableClass, $aExtKeys))
- {
- foreach ($aExtKeys[$sTableClass] as $sKeyAttCode => $aExtFields)
- {
- $oKeyAttDef = MetaModel::GetAttributeDef($sTableClass, $sKeyAttCode);
- $aPointingTo = $this->GetCriteria_PointingTo($sKeyAttCode);
- if (!array_key_exists(TREE_OPERATOR_EQUALS, $aPointingTo))
- {
- // The join was not explicitely defined in the filter,
- // we need to do it now
- $sKeyClass = $oKeyAttDef->GetTargetClass();
- $sKeyClassAlias = $oBuild->GenerateClassAlias($sKeyClass.'_'.$sKeyAttCode, $sKeyClass);
- $oExtFilter = new DBObjectSearch($sKeyClass, $sKeyClassAlias);
- $aAllPointingTo[$sKeyAttCode][TREE_OPERATOR_EQUALS][$sKeyClassAlias] = $oExtFilter;
- }
- }
- }
- foreach ($aAllPointingTo as $sKeyAttCode => $aPointingTo)
- {
- foreach($aPointingTo as $iOperatorCode => $aFilter)
- {
- foreach($aFilter as $oExtFilter)
- {
- if (!MetaModel::IsValidAttCode($sTableClass, $sKeyAttCode)) continue; // Not defined in the class, skip it
- // The aliases should not conflict because normalization occured while building the filter
- $oKeyAttDef = MetaModel::GetAttributeDef($sTableClass, $sKeyAttCode);
- $sKeyClass = $oExtFilter->GetFirstJoinedClass();
- $sKeyClassAlias = $oExtFilter->GetFirstJoinedClassAlias();
- // Note: there is no search condition in $oExtFilter, because normalization did merge the condition onto the top of the filter tree
- if ($iOperatorCode == TREE_OPERATOR_EQUALS)
- {
- if (array_key_exists($sTableClass, $aExtKeys) && array_key_exists($sKeyAttCode, $aExtKeys[$sTableClass]))
- {
- // Specify expected attributes for the target class query
- // ... and use the current alias !
- $aTranslateNow = array(); // Translation for external fields - must be performed before the join is done (recursion...)
- foreach($aExtKeys[$sTableClass][$sKeyAttCode] as $sAttCode => $oAtt)
- {
- $oExtAttDef = $oAtt->GetExtAttDef();
- if ($oExtAttDef->IsBasedOnOQLExpression())
- {
- $aTranslateNow[$sTargetAlias][$sAttCode] = new FieldExpression($oExtAttDef->GetCode(), $sKeyClassAlias);
- }
- else
- {
- $sExtAttCode = $oAtt->GetExtAttCode();
- // Translate mainclass.extfield => remoteclassalias.remotefieldcode
- $oRemoteAttDef = MetaModel::GetAttributeDef($sKeyClass, $sExtAttCode);
- foreach ($oRemoteAttDef->GetSQLExpressions() as $sColId => $sRemoteAttExpr)
- {
- $aTranslateNow[$sTargetAlias][$sAttCode.$sColId] = new FieldExpression($sExtAttCode, $sKeyClassAlias);
- }
- }
- }
- if ($oKeyAttDef instanceof AttributeObjectKey)
- {
- // Add the condition: `$sTargetAlias`.$sClassAttCode IN (subclasses of $sKeyClass')
- $sClassAttCode = $oKeyAttDef->Get('class_attcode');
- $oClassAttDef = MetaModel::GetAttributeDef($sTargetClass, $sClassAttCode);
- foreach ($oClassAttDef->GetSQLExpressions() as $sColId => $sSQLExpr)
- {
- $aTranslateNow[$sTargetAlias][$sClassAttCode.$sColId] = new FieldExpressionResolved($sSQLExpr, $sTableAlias);
- }
- $oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses($sKeyClass, ENUM_CHILD_CLASSES_ALL));
- $oClassExpr = new FieldExpression($sClassAttCode, $sTargetAlias);
- $oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr);
- $oBuild->m_oQBExpressions->AddCondition($oClassRestriction);
- }
- // Translate prior to recursing
- //
- $oBuild->m_oQBExpressions->Translate($aTranslateNow, false);
- self::DbgTrace("External key $sKeyAttCode (class: $sKeyClass), call MakeSQLObjectQuery()");
- $oBuild->m_oQBExpressions->PushJoinField(new FieldExpression('id', $sKeyClassAlias));
-
- $oSelectExtKey = $oExtFilter->MakeSQLObjectQuery($oBuild, $aAttToLoad);
-
- $oJoinExpr = $oBuild->m_oQBExpressions->PopJoinField();
- $sExternalKeyTable = $oJoinExpr->GetParent();
- $sExternalKeyField = $oJoinExpr->GetName();
-
- $aCols = $oKeyAttDef->GetSQLExpressions(); // Workaround a PHP bug: sometimes issuing a Notice if invoking current(somefunc())
- $sLocalKeyField = current($aCols); // get the first column for an external key
-
- self::DbgTrace("External key $sKeyAttCode, Join on $sLocalKeyField = $sExternalKeyField");
- if ($oKeyAttDef->IsNullAllowed())
- {
- $oSelectBase->AddLeftJoin($oSelectExtKey, $sLocalKeyField, $sExternalKeyField, $sExternalKeyTable);
- }
- else
- {
- $oSelectBase->AddInnerJoin($oSelectExtKey, $sLocalKeyField, $sExternalKeyField, $sExternalKeyTable);
- }
- }
- }
- elseif(MetaModel::GetAttributeOrigin($sKeyClass, $sKeyAttCode) == $sTableClass)
- {
- $oBuild->m_oQBExpressions->PushJoinField(new FieldExpression($sKeyAttCode, $sKeyClassAlias));
- $oSelectExtKey = $oExtFilter->MakeSQLObjectQuery($oBuild, $aAttToLoad);
- $oJoinExpr = $oBuild->m_oQBExpressions->PopJoinField();
- $sExternalKeyTable = $oJoinExpr->GetParent();
- $sExternalKeyField = $oJoinExpr->GetName();
- $sLeftIndex = $sExternalKeyField.'_left'; // TODO use GetSQLLeft()
- $sRightIndex = $sExternalKeyField.'_right'; // TODO use GetSQLRight()
-
- $LocalKeyLeft = $oKeyAttDef->GetSQLLeft();
- $LocalKeyRight = $oKeyAttDef->GetSQLRight();
-
- $oSelectBase->AddInnerJoinTree($oSelectExtKey, $LocalKeyLeft, $LocalKeyRight, $sLeftIndex, $sRightIndex, $sExternalKeyTable, $iOperatorCode);
- }
- }
- }
- }
- // Translate the selected columns
- //
- $oBuild->m_oQBExpressions->Translate($aTranslation, false);
- // Filter out archived records
- //
- if (MetaModel::IsArchivable($sTableClass))
- {
- if (!$oBuild->GetRootFilter()->GetArchiveMode())
- {
- $bIsOnJoinedClass = array_key_exists($sTargetAlias, $oBuild->GetRootFilter()->GetJoinedClasses());
- if ($bIsOnJoinedClass)
- {
- if (MetaModel::IsParentClass($sTableClass, $sTargetClass))
- {
- $oNotArchived = new BinaryExpression(new FieldExpressionResolved('archive_flag', $sTableAlias), '=', new ScalarExpression(0));
- $oBuild->AddFilteredTable($sTableAlias, $oNotArchived);
- }
- }
- }
- }
- return $oSelectBase;
- }
- /**
- * Get the expression for the class and its subclasses (if finalclass = 'subclass' ...)
- * Simplifies the final expression by grouping classes having the same expression
- */
- static protected function GetPolymorphicExpression($sClass, $sAttCode)
- {
- // 1st step - get all of the required expressions (instantiable classes)
- // and group them using their OQL representation
- //
- $aExpressions = array(); // signature => array('expression' => oExp, 'classes' => array of classes)
- foreach (MetaModel::EnumChildClasses($sClass, ENUM_CHILD_CLASSES_ALL) as $sSubClass)
- {
- if (($sSubClass != $sClass) && MetaModel::IsAbstract($sSubClass)) continue;
- $oAttDef = MetaModel::GetAttributeDef($sSubClass, $sAttCode);
- $oSubClassExp = $oAttDef->GetOQLExpression($sSubClass);
- // 3rd step - position the attributes in the hierarchy of classes
- //
- $oSubClassExp->Browse(function($oNode) use ($sSubClass) {
- if ($oNode instanceof FieldExpression)
- {
- $sAttCode = $oNode->GetName();
- $oAttDef = MetaModel::GetAttributeDef($sSubClass, $sAttCode);
- if ($oAttDef->IsExternalField())
- {
- $sKeyAttCode = $oAttDef->GetKeyAttCode();
- $sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sKeyAttCode);
- }
- else
- {
- $sClassOfAttribute = MetaModel::GetAttributeOrigin($sSubClass, $sAttCode);
- }
- $sParent = MetaModel::GetAttributeOrigin($sClassOfAttribute, $oNode->GetName());
- $oNode->SetParent($sParent);
- }
- });
- $sSignature = $oSubClassExp->Render();
- if (!array_key_exists($sSignature, $aExpressions))
- {
- $aExpressions[$sSignature] = array(
- 'expression' => $oSubClassExp,
- 'classes' => array(),
- );
- }
- $aExpressions[$sSignature]['classes'][] = $sSubClass;
- }
- // 2nd step - build the final name expression depending on the finalclass
- //
- if (count($aExpressions) == 1)
- {
- $aExpData = reset($aExpressions);
- $oExpression = $aExpData['expression'];
- }
- else
- {
- $oExpression = null;
- foreach ($aExpressions as $sSignature => $aExpData)
- {
- $oClassListExpr = ListExpression::FromScalars($aExpData['classes']);
- $oClassExpr = new FieldExpression('finalclass', $sClass);
- $oClassInList = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr);
- if (is_null($oExpression))
- {
- $oExpression = $aExpData['expression'];
- }
- else
- {
- $oExpression = new FunctionExpression('IF', array($oClassInList, $aExpData['expression'], $oExpression));
- }
- }
- }
- return $oExpression;
- }
- }
|