dbobjectsearch.class.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Define filters for a given class of objects (formerly named "filter")
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. define('TREE_OPERATOR_EQUALS', 0);
  25. define('TREE_OPERATOR_BELOW', 1);
  26. define('TREE_OPERATOR_BELOW_STRICT', 2);
  27. define('TREE_OPERATOR_NOT_BELOW', 3);
  28. define('TREE_OPERATOR_NOT_BELOW_STRICT', 4);
  29. class DBObjectSearch
  30. {
  31. private $m_aClasses; // queried classes (alias => class name), the first item is the class corresponding to this filter (the rest is coming from subfilters)
  32. private $m_aSelectedClasses; // selected for the output (alias => class name)
  33. private $m_oSearchCondition;
  34. private $m_aParams;
  35. private $m_aFullText;
  36. private $m_aPointingTo;
  37. private $m_aReferencedBy;
  38. private $m_aRelatedTo;
  39. private $m_bDataFiltered;
  40. // By default, some information may be hidden to the current user
  41. // But it may happen that we need to disable that feature
  42. private $m_bAllowAllData = false;
  43. public function __construct($sClass, $sClassAlias = null)
  44. {
  45. if (is_null($sClassAlias)) $sClassAlias = $sClass;
  46. assert('is_string($sClass)');
  47. assert('MetaModel::IsValidClass($sClass)'); // #@# could do better than an assert, or at least give the caller's reference
  48. // => idee d'un assert avec call stack (autre utilisation = echec sur query SQL)
  49. $this->m_aSelectedClasses = array($sClassAlias => $sClass);
  50. $this->m_aClasses = array($sClassAlias => $sClass);
  51. $this->m_oSearchCondition = new TrueExpression;
  52. $this->m_aParams = array();
  53. $this->m_aFullText = array();
  54. $this->m_aPointingTo = array();
  55. $this->m_aReferencedBy = array();
  56. $this->m_aRelatedTo = array();
  57. $this->m_bDataFiltered = false;
  58. }
  59. public function AllowAllData() {$this->m_bAllowAllData = true;}
  60. public function IsAllDataAllowed() {return $this->m_bAllowAllData;}
  61. public function IsDataFiltered() {return $this->m_bDataFiltered; }
  62. public function SetDataFiltered() {$this->m_bDataFiltered = true;}
  63. public function GetClassName($sAlias) {return $this->m_aClasses[$sAlias];}
  64. public function GetJoinedClasses() {return $this->m_aClasses;}
  65. public function GetClass()
  66. {
  67. return reset($this->m_aSelectedClasses);
  68. }
  69. public function GetClassAlias()
  70. {
  71. reset($this->m_aSelectedClasses);
  72. return key($this->m_aSelectedClasses);
  73. }
  74. public function GetFirstJoinedClass()
  75. {
  76. return reset($this->m_aClasses);
  77. }
  78. public function GetFirstJoinedClassAlias()
  79. {
  80. reset($this->m_aClasses);
  81. return key($this->m_aClasses);
  82. }
  83. public function SetSelectedClasses($aNewSet)
  84. {
  85. $this->m_aSelectedClasses = array();
  86. foreach ($aNewSet as $sAlias => $sClass)
  87. {
  88. if (!array_key_exists($sAlias, $this->m_aClasses))
  89. {
  90. throw new CoreException('Unexpected class alias', array('alias'=>$sAlias, 'expected'=>$this->m_aClasses));
  91. }
  92. $this->m_aSelectedClasses[$sAlias] = $sClass;
  93. }
  94. }
  95. public function GetSelectedClasses()
  96. {
  97. return $this->m_aSelectedClasses;
  98. }
  99. public function IsAny()
  100. {
  101. // #@# todo - if (!$this->m_oSearchCondition->IsTrue()) return false;
  102. if (count($this->m_aFullText) > 0) return false;
  103. if (count($this->m_aPointingTo) > 0) return false;
  104. if (count($this->m_aReferencedBy) > 0) return false;
  105. if (count($this->m_aRelatedTo) > 0) return false;
  106. if (count($this->m_aParentConditions) > 0) return false;
  107. return true;
  108. }
  109. public function Describe()
  110. {
  111. // To replace __Describe
  112. }
  113. public function DescribeConditionPointTo($sExtKeyAttCode, $aPointingTo)
  114. {
  115. if (empty($aPointingTo)) return "";
  116. foreach($aPointingTo as $iOperatorCode => $oFilter)
  117. {
  118. if ($oFilter->IsAny()) break;
  119. $oAtt = MetaModel::GetAttributeDef($this->GetClass(), $sExtKeyAttCode);
  120. $sOperator = '';
  121. switch($iOperatorCode)
  122. {
  123. case TREE_OPERATOR_EQUALS:
  124. $sOperator = 'having';
  125. break;
  126. case TREE_OPERATOR_BELOW:
  127. $sOperator = 'below';
  128. break;
  129. case TREE_OPERATOR_BELOW_STRICT:
  130. $sOperator = 'strictly below';
  131. break;
  132. case TREE_OPERATOR_NOT_BELOW:
  133. $sOperator = 'not below';
  134. break;
  135. case TREE_OPERATOR_NOT_BELOW_STRICT:
  136. $sOperator = 'strictly not below';
  137. break;
  138. }
  139. $aDescription[] = $oAtt->GetLabel()."$sOperator ({$oFilter->DescribeConditions()})";
  140. }
  141. return implode(' and ', $aDescription);
  142. }
  143. public function DescribeConditionRefBy($sForeignClass, $sForeignExtKeyAttCode)
  144. {
  145. if (!isset($this->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode])) return "";
  146. $oFilter = $this->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode];
  147. if ($oFilter->IsAny()) return "";
  148. $oAtt = MetaModel::GetAttributeDef($sForeignClass, $sForeignExtKeyAttCode);
  149. return "being ".$oAtt->GetLabel()." for ".$sForeignClass."s in ({$oFilter->DescribeConditions()})";
  150. }
  151. public function DescribeConditionRelTo($aRelInfo)
  152. {
  153. $oFilter = $aRelInfo['flt'];
  154. $sRelCode = $aRelInfo['relcode'];
  155. $iMaxDepth = $aRelInfo['maxdepth'];
  156. return "related ($sRelCode... peut mieux faire !, $iMaxDepth dig depth) to a {$oFilter->GetClass()} ({$oFilter->DescribeConditions()})";
  157. }
  158. public function DescribeConditions()
  159. {
  160. $aConditions = array();
  161. $aCondFT = array();
  162. foreach($this->m_aFullText as $sFullText)
  163. {
  164. $aCondFT[] = " contain word(s) '$sFullText'";
  165. }
  166. if (count($aCondFT) > 0)
  167. {
  168. $aConditions[] = "which ".implode(" and ", $aCondFT);
  169. }
  170. // #@# todo - review textual description of the JOIN and search condition (is that still feasible?)
  171. $aConditions[] = $this->RenderCondition();
  172. $aCondPoint = array();
  173. foreach($this->m_aPointingTo as $sExtKeyAttCode => $aPointingTo)
  174. {
  175. $aCondPoint[] = $this->DescribeConditionPointTo($sExtKeyAttCode, $aPointingTo);
  176. }
  177. if (count($aCondPoint) > 0)
  178. {
  179. $aConditions[] = implode(" and ", $aCondPoint);
  180. }
  181. $aCondReferred= array();
  182. foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
  183. {
  184. foreach($aReferences as $sForeignExtKeyAttCode=>$oForeignFilter)
  185. {
  186. if ($oForeignFilter->IsAny()) continue;
  187. $aCondReferred[] = $this->DescribeConditionRefBy($sForeignClass, $sForeignExtKeyAttCode);
  188. }
  189. }
  190. foreach ($this->m_aRelatedTo as $aRelInfo)
  191. {
  192. $aCondReferred[] = $this->DescribeConditionRelTo($aRelInfo);
  193. }
  194. if (count($aCondReferred) > 0)
  195. {
  196. $aConditions[] = implode(" and ", $aCondReferred);
  197. }
  198. foreach ($this->m_aParentConditions as $aRelInfo)
  199. {
  200. $aCondReferred[] = $this->DescribeConditionParent($aRelInfo);
  201. }
  202. return implode(" and ", $aConditions);
  203. }
  204. public function __DescribeHTML()
  205. {
  206. try
  207. {
  208. $sConditionDesc = $this->DescribeConditions();
  209. }
  210. catch (MissingQueryArgument $e)
  211. {
  212. $sConditionDesc = '?missing query argument?';
  213. }
  214. if (!empty($sConditionDesc))
  215. {
  216. return "Objects of class '".$this->GetClass()."', $sConditionDesc";
  217. }
  218. return "Any object of class '".$this->GetClass()."'";
  219. }
  220. protected function TransferConditionExpression($oFilter, $aTranslation)
  221. {
  222. //echo "<p>TransferConditionExpression:<br/>";
  223. //echo "Adding Conditions:<br/><pre>oFilter:\n".print_r($oFilter, true)."\naTranslation:\n".print_r($aTranslation, true)."</pre>\n";
  224. //echo "</p>";
  225. $oTranslated = $oFilter->GetCriteria()->Translate($aTranslation, false, false /* leave unresolved fields */);
  226. //echo "Adding Conditions (translated):<br/><pre>".print_r($oTranslated, true)."</pre>\n";
  227. $this->AddConditionExpression($oTranslated);
  228. // #@# what about collisions in parameter names ???
  229. $this->m_aParams = array_merge($this->m_aParams, $oFilter->m_aParams);
  230. }
  231. public function ResetCondition()
  232. {
  233. $this->m_oSearchCondition = new TrueExpression();
  234. $this->m_aParentConditions = array();
  235. // ? is that usefull/enough, do I need to rebuild the list after the subqueries ?
  236. }
  237. public function MergeConditionExpression($oExpression)
  238. {
  239. $this->m_oSearchCondition = $this->m_oSearchCondition->LogOr($oExpression);
  240. }
  241. public function AddConditionExpression($oExpression)
  242. {
  243. $this->m_oSearchCondition = $this->m_oSearchCondition->LogAnd($oExpression);
  244. }
  245. public function AddNameCondition($sName)
  246. {
  247. $oValueExpr = new ScalarExpression($sName);
  248. $oNameExpr = new FieldExpression('friendlyname', $this->GetClassAlias());
  249. $oNewCondition = new BinaryExpression($oNameExpr, '=', $oValueExpr);
  250. $this->AddConditionExpression($oNewCondition);
  251. }
  252. public function AddCondition($sFilterCode, $value, $sOpCode = null)
  253. {
  254. MyHelpers::CheckKeyInArray('filter code', $sFilterCode, MetaModel::GetClassFilterDefs($this->GetClass()));
  255. $oFilterDef = MetaModel::GetClassFilterDef($this->GetClass(), $sFilterCode);
  256. $oField = new FieldExpression($sFilterCode, $this->GetClassAlias());
  257. if (empty($sOpCode))
  258. {
  259. if ($sFilterCode == 'id')
  260. {
  261. $sOpCode = '=';
  262. }
  263. else
  264. {
  265. $oAttDef = MetaModel::GetAttributeDef($this->GetClass(), $sFilterCode);
  266. $oNewCondition = $oAttDef->GetSmartConditionExpression($value, $oField, $this->m_aParams);
  267. $this->AddConditionExpression($oNewCondition);
  268. return;
  269. }
  270. }
  271. MyHelpers::CheckKeyInArray('operator', $sOpCode, $oFilterDef->GetOperators());
  272. // Preserve backward compatibility - quick n'dirty way to change that API semantic
  273. //
  274. switch($sOpCode)
  275. {
  276. case 'SameDay':
  277. case 'SameMonth':
  278. case 'SameYear':
  279. case 'Today':
  280. case '>|':
  281. case '<|':
  282. case '=|':
  283. throw new CoreException('Deprecated operator, please consider using OQL (SQL) expressions like "(TO_DAYS(NOW()) - TO_DAYS(x)) AS AgeDays"', array('operator' => $sOpCode));
  284. break;
  285. case "IN":
  286. if (!is_array($value)) $value = array($value);
  287. $sListExpr = '('.implode(', ', CMDBSource::Quote($value)).')';
  288. $sOQLCondition = $oField->Render()." IN $sListExpr";
  289. break;
  290. case "NOTIN":
  291. if (!is_array($value)) $value = array($value);
  292. $sListExpr = '('.implode(', ', CMDBSource::Quote($value)).')';
  293. $sOQLCondition = $oField->Render()." NOT IN $sListExpr";
  294. break;
  295. case 'Contains':
  296. $this->m_aParams[$sFilterCode] = "%$value%";
  297. $sOperator = 'LIKE';
  298. break;
  299. case 'Begins with':
  300. $this->m_aParams[$sFilterCode] = "$value%";
  301. $sOperator = 'LIKE';
  302. break;
  303. case 'Finishes with':
  304. $this->m_aParams[$sFilterCode] = "%$value";
  305. $sOperator = 'LIKE';
  306. break;
  307. default:
  308. $this->m_aParams[$sFilterCode] = $value;
  309. $sOperator = $sOpCode;
  310. }
  311. switch($sOpCode)
  312. {
  313. case "IN":
  314. case "NOTIN":
  315. $oNewCondition = Expression::FromOQL($sOQLCondition);
  316. break;
  317. case 'Contains':
  318. case 'Begins with':
  319. case 'Finishes with':
  320. default:
  321. $oRightExpr = new VariableExpression($sFilterCode);
  322. $oNewCondition = new BinaryExpression($oField, $sOperator, $oRightExpr);
  323. }
  324. $this->AddConditionExpression($oNewCondition);
  325. }
  326. /**
  327. * Specify a condition on external keys or link sets
  328. * @param sAttSpec Can be either an attribute code or extkey->[sAttSpec] or linkset->[sAttSpec] and so on, recursively
  329. * Example: infra_list->ci_id->location_id->country
  330. * @param value The value to match
  331. * @return void
  332. */
  333. public function AddConditionAdvanced($sAttSpec, $value)
  334. {
  335. $sClass = $this->GetClass();
  336. $iPos = strpos($sAttSpec, '->');
  337. if ($iPos !== false)
  338. {
  339. $sAttCode = substr($sAttSpec, 0, $iPos);
  340. $sSubSpec = substr($sAttSpec, $iPos + 2);
  341. if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
  342. {
  343. throw new Exception("Invalid attribute code '$sClass/$sAttCode' in condition specification '$sAttSpec'");
  344. }
  345. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  346. if ($oAttDef->IsLinkSet())
  347. {
  348. $sTargetClass = $oAttDef->GetLinkedClass();
  349. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  350. $oNewFilter = new DBObjectSearch($sTargetClass);
  351. $oNewFilter->AddConditionAdvanced($sSubSpec, $value);
  352. $this->AddCondition_ReferencedBy($oNewFilter, $sExtKeyToMe);
  353. }
  354. elseif ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  355. {
  356. $sTargetClass = $oAttDef->GetTargetClass(EXTKEY_ABSOLUTE);
  357. $oNewFilter = new DBObjectSearch($sTargetClass);
  358. $oNewFilter->AddConditionAdvanced($sSubSpec, $value);
  359. $this->AddCondition_PointingTo($oNewFilter, $sAttCode);
  360. }
  361. else
  362. {
  363. throw new Exception("Attribute specification '$sAttSpec', '$sAttCode' should be either a link set or an external key");
  364. }
  365. }
  366. else
  367. {
  368. // $sAttSpec is an attribute code
  369. //
  370. $this->AddCondition($sAttSpec, $value);
  371. }
  372. }
  373. public function AddCondition_FullText($sFullText)
  374. {
  375. $this->m_aFullText[] = $sFullText;
  376. }
  377. public function AddCondition_Parent($sAttCode, $iOperatorCode, $oExpression)
  378. {
  379. $oAttDef = MetaModel::GetAttributeDef($this->GetClass(), $sAttCode);
  380. if (!$oAttDef instanceof AttributeHierarchicalKey)
  381. {
  382. throw new Exception("AddCondition_Parent can only be used on hierarchical keys. '$sAttCode' is not a hierarchical key.");
  383. }
  384. $this->m_aParentConditions[] = array(
  385. 'attCode' => $sAttCode,
  386. 'operator' => $iOperatorCode,
  387. 'expression' => $oExpression,
  388. );
  389. }
  390. protected function AddToNameSpace(&$aClassAliases, &$aAliasTranslation, $bTranslateMainAlias = true)
  391. {
  392. if ($bTranslateMainAlias)
  393. {
  394. $sOrigAlias = $this->GetClassAlias();
  395. if (array_key_exists($sOrigAlias, $aClassAliases))
  396. {
  397. $sNewAlias = MetaModel::GenerateUniqueAlias($aClassAliases, $sOrigAlias, $this->GetClass());
  398. //echo "<p>Generating a new alias for $sOrigAlias (already used). It is now: $sNewAlias</p>\n";
  399. $this->m_aSelectedClasses[$sNewAlias] = $this->GetClass();
  400. unset($this->m_aSelectedClasses[$sOrigAlias]);
  401. $this->m_aClasses[$sNewAlias] = $this->GetClass();
  402. unset($this->m_aClasses[$sOrigAlias]);
  403. // Translate the condition expression with the new alias
  404. $aAliasTranslation[$sOrigAlias]['*'] = $sNewAlias;
  405. }
  406. //echo "<p>Adding the alias ".$this->GetClass()." as ".$this->GetClassAlias()."</p>\n";
  407. // add the alias into the filter aliases list
  408. $aClassAliases[$this->GetClassAlias()] = $this->GetClass();
  409. }
  410. foreach($this->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
  411. {
  412. foreach($aPointingTo as $iOperatorCode => $aFilter)
  413. {
  414. foreach($aFilter as $sAlias => $oFilter)
  415. {
  416. $oFilter->AddToNameSpace($aClassAliases, $aAliasTranslation);
  417. }
  418. }
  419. }
  420. foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
  421. {
  422. foreach($aReferences as $sForeignExtKeyAttCode=>$oForeignFilter)
  423. {
  424. $oForeignFilter->AddToNameSpace($aClassAliases, $aAliasTranslation);
  425. }
  426. }
  427. }
  428. public function AddCondition_PointingTo(DBObjectSearch $oFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS)
  429. {
  430. $aAliasTranslation = array();
  431. $res = $this->AddCondition_PointingTo_InNameSpace($oFilter, $sExtKeyAttCode, $this->m_aClasses, $aAliasTranslation, $iOperatorCode);
  432. $this->TransferConditionExpression($oFilter, $aAliasTranslation);
  433. return $res;
  434. }
  435. protected function AddCondition_PointingTo_InNameSpace(DBObjectSearch $oFilter, $sExtKeyAttCode, &$aClassAliases, &$aAliasTranslation, $iOperatorCode)
  436. {
  437. //echo "<p style=\"color:green\">Calling: AddCondition_PointingTo_InNameSpace([<pre>".print_r($aClassAliases, true)."</pre></br>], [<pre>".print_r($aAliasTranslation, true)."</pre>]);</p>";
  438. if (!MetaModel::IsValidKeyAttCode($this->GetClass(), $sExtKeyAttCode))
  439. {
  440. throw new CoreWarning("The attribute code '$sExtKeyAttCode' is not an external key of the class '{$this->GetClass()}' - the condition will be ignored");
  441. }
  442. $oAttExtKey = MetaModel::GetAttributeDef($this->GetClass(), $sExtKeyAttCode);
  443. if(!MetaModel::IsSameFamilyBranch($oFilter->GetClass(), $oAttExtKey->GetTargetClass()))
  444. {
  445. 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()}");
  446. }
  447. if(($iOperatorCode != TREE_OPERATOR_EQUALS) && !($oAttExtKey instanceof AttributeHierarchicalKey))
  448. {
  449. throw new CoreException("The specified tree operator $isOperatorCode is not applicable to the key '{$this->GetClass()}::$sExtKeyAttCode', which is not a HierarchicalKey");
  450. }
  451. $bSamePointingTo = false;
  452. if (array_key_exists($sExtKeyAttCode, $this->m_aPointingTo))
  453. {
  454. if (array_key_exists($iOperatorCode, $this->m_aPointingTo[$sExtKeyAttCode]))
  455. {
  456. if (array_key_exists($oFilter->GetClassAlias(), $this->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode]))
  457. {
  458. //echo "<p style=\"color:red\">[".__LINE__."]this->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode][".$oFilter->GetFirstJoinedClassAlias()."]:<pre>\n".print_r($this->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode], true)."</pre>;</p>";
  459. $bSamePointingTo = true;
  460. }
  461. }
  462. }
  463. //echo "<p style=\"color:red\">[".__LINE__."]Calling: AddToNameSpace([".implode(',', $aClassAliases)."], [".implode(',', $aAliasTranslation)."]);</p>";
  464. if ($bSamePointingTo)
  465. {
  466. //echo "<p style=\"color:red\">[".__LINE__."]AddPointingTo: Merging filters for [$sExtKeyAttCode][$iOperatorCode][".$oFilter->GetClassAlias()."]</p>";
  467. // Same ext key, alias and same operator, merge the filters together
  468. // $sAlias = $oFilter->GetClassAlias();
  469. //echo "<p style=\"color:red\">[".__LINE__."]before: AddToNameSpace(aClassAliases[<pre>\n".print_r($aClassAliases, true)."</pre>], aAliasTranslation[<pre>\n".print_r($aAliasTranslation, true)."</pre>]);</p>";
  470. $oFilter->AddToNamespace($aClassAliases, $aAliasTranslation, true /* Don't translate the main alias */);
  471. //echo "<p style=\"color:blue\">[".__LINE__."]after: AddToNameSpace(aClassAliases[<pre>\n".print_r($aClassAliases, true)."</pre>], aAliasTranslation[<pre>\n".print_r($aAliasTranslation, true)."</pre>]);</p>";
  472. // $this->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode][$sAlias]->MergeWith($oFilter, $aClassAliases, $aAliasTranslation);
  473. $this->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode][$oFilter->GetClassAlias()] = $oFilter;
  474. }
  475. else
  476. {
  477. //echo "<p style=\"color:red\">[".__LINE__."]AddPointingTo: Adding a new PointingTo filter for [$sExtKeyAttCode][$iOperatorCode][".$oFilter->GetClassAlias()."]</p>";
  478. $oFilter->AddToNamespace($aClassAliases, $aAliasTranslation);
  479. $this->m_aPointingTo[$sExtKeyAttCode][$iOperatorCode][$oFilter->GetClassAlias()] = $oFilter;
  480. }
  481. }
  482. public function AddCondition_ReferencedBy(DBObjectSearch $oFilter, $sForeignExtKeyAttCode)
  483. {
  484. $aAliasTranslation = array();
  485. $res = $this->AddCondition_ReferencedBy_InNameSpace($oFilter, $sForeignExtKeyAttCode, $this->m_aClasses, $aAliasTranslation);
  486. $this->TransferConditionExpression($oFilter, $aAliasTranslation);
  487. return $res;
  488. }
  489. protected function AddCondition_ReferencedBy_InNameSpace(DBObjectSearch $oFilter, $sForeignExtKeyAttCode, &$aClassAliases, &$aAliasTranslation)
  490. {
  491. $sForeignClass = $oFilter->GetClass();
  492. $sForeignClassAlias = $oFilter->GetClassAlias();
  493. if (!MetaModel::IsValidKeyAttCode($sForeignClass, $sForeignExtKeyAttCode))
  494. {
  495. throw new CoreException("The attribute code '$sForeignExtKeyAttCode' is not an external key of the class '{$sForeignClass}' - the condition will be ignored");
  496. }
  497. $oAttExtKey = MetaModel::GetAttributeDef($sForeignClass, $sForeignExtKeyAttCode);
  498. if(!MetaModel::IsSameFamilyBranch($this->GetClass(), $oAttExtKey->GetTargetClass()))
  499. {
  500. 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()}");
  501. }
  502. if (array_key_exists($sForeignClass, $this->m_aReferencedBy) && array_key_exists($sForeignExtKeyAttCode, $this->m_aReferencedBy[$sForeignClass]))
  503. {
  504. $this->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode]->MergeWith_InNamespace($oFilter, $aClassAliases, $aAliasTranslation);
  505. }
  506. else
  507. {
  508. $oFilter->AddToNamespace($aClassAliases, $aAliasTranslation);
  509. // #@# The condition expression found in that filter should not be used - could be another kind of structure like a join spec tree !!!!
  510. //$oNewFilter = clone $oFilter;
  511. //$oNewFilter->ResetCondition();
  512. $this->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode]= $oFilter;
  513. }
  514. }
  515. public function AddCondition_LinkedTo(DBObjectSearch $oLinkFilter, $sExtKeyAttCodeToMe, $sExtKeyAttCodeTarget, DBObjectSearch $oFilterTarget)
  516. {
  517. $oLinkFilterFinal = clone $oLinkFilter;
  518. // todo : new function prototype
  519. $oLinkFilterFinal->AddCondition_PointingTo($sExtKeyAttCodeToMe);
  520. $this->AddCondition_ReferencedBy($oLinkFilterFinal, $sExtKeyAttCodeToMe);
  521. }
  522. public function AddCondition_RelatedTo(DBObjectSearch $oFilter, $sRelCode, $iMaxDepth)
  523. {
  524. MyHelpers::CheckValueInArray('relation code', $sRelCode, MetaModel::EnumRelations());
  525. $this->m_aRelatedTo[] = array('flt'=>$oFilter, 'relcode'=>$sRelCode, 'maxdepth'=>$iMaxDepth);
  526. }
  527. public function MergeWith($oFilter)
  528. {
  529. $aAliasTranslation = array();
  530. $res = $this->MergeWith_InNamespace($oFilter, $this->m_aClasses, $aAliasTranslation);
  531. $this->TransferConditionExpression($oFilter, $aAliasTranslation);
  532. return $res;
  533. }
  534. protected function MergeWith_InNamespace($oFilter, &$aClassAliases, &$aAliasTranslation)
  535. {
  536. if ($this->GetClass() != $oFilter->GetClass())
  537. {
  538. throw new CoreException("Attempting to merge a filter of class '{$this->GetClass()}' with a filter of class '{$oFilter->GetClass()}'");
  539. }
  540. // Translate search condition into our aliasing scheme
  541. $aAliasTranslation[$oFilter->GetClassAlias()]['*'] = $this->GetClassAlias();
  542. $this->m_aFullText = array_merge($this->m_aFullText, $oFilter->m_aFullText);
  543. $this->m_aRelatedTo = array_merge($this->m_aRelatedTo, $oFilter->m_aRelatedTo);
  544. foreach($oFilter->m_aPointingTo as $sExtKeyAttCode=>$aPointingTo)
  545. {
  546. foreach($aPointingTo as $iOperatorCode => $aFilter)
  547. {
  548. foreach($aFilter as $sAlias => $oExtFilter)
  549. {
  550. $this->AddCondition_PointingTo_InNamespace($oExtFilter, $sExtKeyAttCode, $aClassAliases, $aAliasTranslation, $iOperatorCode);
  551. }
  552. }
  553. }
  554. foreach($oFilter->m_aReferencedBy as $sForeignClass => $aReferences)
  555. {
  556. foreach($aReferences as $sForeignExtKeyAttCode => $oForeignFilter)
  557. {
  558. $this->AddCondition_ReferencedBy_InNamespace($oForeignFilter, $sForeignExtKeyAttCode, $aClassAliases, $aAliasTranslation);
  559. }
  560. }
  561. }
  562. public function GetCriteria() {return $this->m_oSearchCondition;}
  563. public function GetCriteria_FullText() {return $this->m_aFullText;}
  564. public function GetCriteria_PointingTo($sKeyAttCode = "")
  565. {
  566. if (empty($sKeyAttCode))
  567. {
  568. return $this->m_aPointingTo;
  569. }
  570. if (!array_key_exists($sKeyAttCode, $this->m_aPointingTo)) return array();
  571. return $this->m_aPointingTo[$sKeyAttCode];
  572. }
  573. public function GetCriteria_ReferencedBy($sRemoteClass = "", $sForeignExtKeyAttCode = "")
  574. {
  575. if (empty($sRemoteClass))
  576. {
  577. return $this->m_aReferencedBy;
  578. }
  579. if (!array_key_exists($sRemoteClass, $this->m_aReferencedBy)) return null;
  580. if (empty($sForeignExtKeyAttCode))
  581. {
  582. return $this->m_aReferencedBy[$sRemoteClass];
  583. }
  584. if (!array_key_exists($sForeignExtKeyAttCode, $this->m_aReferencedBy[$sRemoteClass])) return null;
  585. return $this->m_aReferencedBy[$sRemoteClass][$sForeignExtKeyAttCode];
  586. }
  587. public function GetCriteria_RelatedTo()
  588. {
  589. return $this->m_aRelatedTo;
  590. }
  591. public function GetInternalParams()
  592. {
  593. return $this->m_aParams;
  594. }
  595. public function RenderCondition()
  596. {
  597. return $this->m_oSearchCondition->Render($this->m_aParams, false);
  598. }
  599. public function serialize($bDevelopParams = false, $aContextParams = null)
  600. {
  601. $sOql = $this->ToOql($bDevelopParams, $aContextParams);
  602. return base64_encode(serialize(array($sOql, $this->m_aParams)));
  603. }
  604. static public function unserialize($sValue)
  605. {
  606. $aData = unserialize(base64_decode($sValue));
  607. $sOql = $aData[0];
  608. $aParams = $aData[1];
  609. // We've tried to use gzcompress/gzuncompress, but for some specific queries
  610. // it was not working at all (See Trac #193)
  611. // gzuncompress was issuing a warning "data error" and the return object was null
  612. return self::FromOQL($sOql, $aParams);
  613. }
  614. // SImple BUt Structured Query Languag - SubuSQL
  615. //
  616. static private function Value2Expression($value)
  617. {
  618. $sRet = $value;
  619. if (is_array($value))
  620. {
  621. $sRet = VS_START.implode(', ', $value).VS_END;
  622. }
  623. else if (!is_numeric($value))
  624. {
  625. $sRet = "'".addslashes($value)."'";
  626. }
  627. return $sRet;
  628. }
  629. static private function Expression2Value($sExpr)
  630. {
  631. $retValue = $sExpr;
  632. if ((substr($sExpr, 0, 1) == "'") && (substr($sExpr, -1, 1) == "'"))
  633. {
  634. $sNoQuotes = substr($sExpr, 1, -1);
  635. return stripslashes($sNoQuotes);
  636. }
  637. if ((substr($sExpr, 0, 1) == VS_START) && (substr($sExpr, -1, 1) == VS_END))
  638. {
  639. $sNoBracket = substr($sExpr, 1, -1);
  640. $aRetValue = array();
  641. foreach (explode(",", $sNoBracket) as $sItem)
  642. {
  643. $aRetValue[] = self::Expression2Value(trim($sItem));
  644. }
  645. return $aRetValue;
  646. }
  647. return $retValue;
  648. }
  649. // Alternative to object mapping: the data are transfered directly into an array
  650. // This is 10 times faster than creating a set of objects, and makes sense when optimization is required
  651. public function ToDataArray($aColumns = array(), $aOrderBy = array(), $aArgs = array())
  652. {
  653. $sSQL = MetaModel::MakeSelectQuery($this, $aOrderBy, $aArgs);
  654. $resQuery = CMDBSource::Query($sSQL);
  655. if (!$resQuery) return;
  656. if (count($aColumns) == 0)
  657. {
  658. $aColumns = array_keys(MetaModel::ListAttributeDefs($this->GetClass()));
  659. // Add the standard id (as first column)
  660. array_unshift($aColumns, 'id');
  661. }
  662. $aQueryCols = CMDBSource::GetColumns($resQuery);
  663. $sClassAlias = $this->GetClassAlias();
  664. $aColMap = array();
  665. foreach ($aColumns as $sAttCode)
  666. {
  667. $sColName = $sClassAlias.$sAttCode;
  668. if (in_array($sColName, $aQueryCols))
  669. {
  670. $aColMap[$sAttCode] = $sColName;
  671. }
  672. }
  673. $aRes = array();
  674. while ($aRow = CMDBSource::FetchArray($resQuery))
  675. {
  676. $aMappedRow = array();
  677. foreach ($aColMap as $sAttCode => $sColName)
  678. {
  679. $aMappedRow[$sAttCode] = $aRow[$sColName];
  680. }
  681. $aRes[] = $aMappedRow;
  682. }
  683. CMDBSource::FreeResult($resQuery);
  684. return $aRes;
  685. }
  686. public function ToOQL($bDevelopParams = false, $aContextParams = null)
  687. {
  688. // Currently unused, but could be useful later
  689. $bRetrofitParams = false;
  690. if ($bDevelopParams)
  691. {
  692. if (is_null($aContextParams))
  693. {
  694. $aParams = array_merge($this->m_aParams);
  695. }
  696. else
  697. {
  698. $aParams = array_merge($aContextParams, $this->m_aParams);
  699. }
  700. }
  701. else
  702. {
  703. // Leave it as is, the rendering will be made with parameters in clear
  704. $aParams = null;
  705. }
  706. $sSelectedClasses = implode(', ', array_keys($this->m_aSelectedClasses));
  707. $sRes = 'SELECT '.$sSelectedClasses.' FROM';
  708. $sRes .= ' '.$this->GetClass().' AS '.$this->GetClassAlias();
  709. $sRes .= $this->ToOQL_Joins();
  710. $sRes .= " WHERE ".$this->m_oSearchCondition->Render($aParams, $bRetrofitParams);
  711. // Temporary: add more info about other conditions, necessary to avoid strange behaviors with the cache
  712. foreach($this->m_aFullText as $sFullText)
  713. {
  714. $sRes .= " AND MATCHES '$sFullText'";
  715. }
  716. return $sRes;
  717. }
  718. protected function ToOQL_Joins()
  719. {
  720. $sRes = '';
  721. foreach($this->m_aPointingTo as $sExtKey => $aPointingTo)
  722. {
  723. foreach($aPointingTo as $iOperatorCode => $aFilter)
  724. {
  725. foreach($aFilter as $sAlias => $oFilter)
  726. {
  727. switch($iOperatorCode)
  728. {
  729. case TREE_OPERATOR_EQUALS:
  730. $sOperator = ' = ';
  731. break;
  732. case TREE_OPERATOR_BELOW:
  733. $sOperator = ' BELOW ';
  734. break;
  735. case TREE_OPERATOR_BELOW_STRICT:
  736. $sOperator = ' BELOW STRICT ';
  737. break;
  738. case TREE_OPERATOR_NOT_BELOW:
  739. $sOperator = ' NOT BELOW ';
  740. break;
  741. case TREE_OPERATOR_NOT_BELOW_STRICT:
  742. $sOperator = ' NOT BELOW STRICT ';
  743. break;
  744. }
  745. $sRes .= ' JOIN '.$oFilter->GetClass().' AS '.$oFilter->GetClassAlias().' ON '.$this->GetClassAlias().'.'.$sExtKey.$sOperator.$oFilter->GetClassAlias().'.id';
  746. $sRes .= $oFilter->ToOQL_Joins();
  747. }
  748. }
  749. }
  750. foreach($this->m_aReferencedBy as $sForeignClass=>$aReferences)
  751. {
  752. foreach($aReferences as $sForeignExtKeyAttCode=>$oForeignFilter)
  753. {
  754. $sRes .= ' JOIN '.$oForeignFilter->GetClass().' AS '.$oForeignFilter->GetClassAlias().' ON '.$oForeignFilter->GetClassAlias().'.'.$sForeignExtKeyAttCode.' = '.$this->GetClassAlias().'.id';
  755. $sRes .= $oForeignFilter->ToOQL_Joins();
  756. }
  757. }
  758. return $sRes;
  759. }
  760. protected function OQLExpressionToCondition($sQuery, $oExpression, $aClassAliases)
  761. {
  762. if ($oExpression instanceof BinaryOqlExpression)
  763. {
  764. $sOperator = $oExpression->GetOperator();
  765. $oLeft = $this->OQLExpressionToCondition($sQuery, $oExpression->GetLeftExpr(), $aClassAliases);
  766. $oRight = $this->OQLExpressionToCondition($sQuery, $oExpression->GetRightExpr(), $aClassAliases);
  767. return new BinaryExpression($oLeft, $sOperator, $oRight);
  768. }
  769. elseif ($oExpression instanceof FieldOqlExpression)
  770. {
  771. $sClassAlias = $oExpression->GetParent();
  772. $sFltCode = $oExpression->GetName();
  773. if (empty($sClassAlias))
  774. {
  775. // Try to find an alias
  776. // Build an array of field => array of aliases
  777. $aFieldClasses = array();
  778. foreach($aClassAliases as $sAlias => $sReal)
  779. {
  780. foreach(MetaModel::GetFiltersList($sReal) as $sAnFltCode)
  781. {
  782. $aFieldClasses[$sAnFltCode][] = $sAlias;
  783. }
  784. }
  785. if (!array_key_exists($sFltCode, $aFieldClasses))
  786. {
  787. throw new OqlNormalizeException('Unknown filter code', $sQuery, $oExpression->GetNameDetails(), array_keys($aFieldClasses));
  788. }
  789. if (count($aFieldClasses[$sFltCode]) > 1)
  790. {
  791. throw new OqlNormalizeException('Ambiguous filter code', $sQuery, $oExpression->GetNameDetails());
  792. }
  793. $sClassAlias = $aFieldClasses[$sFltCode][0];
  794. }
  795. else
  796. {
  797. if (!array_key_exists($sClassAlias, $aClassAliases))
  798. {
  799. throw new OqlNormalizeException('Unknown class [alias]', $sQuery, $oExpression->GetParentDetails(), array_keys($aClassAliases));
  800. }
  801. $sClass = $aClassAliases[$sClassAlias];
  802. if (!MetaModel::IsValidFilterCode($sClass, $sFltCode))
  803. {
  804. throw new OqlNormalizeException('Unknown filter code', $sQuery, $oExpression->GetNameDetails(), MetaModel::GetFiltersList($sClass));
  805. }
  806. }
  807. return new FieldExpression($sFltCode, $sClassAlias);
  808. }
  809. elseif ($oExpression instanceof VariableOqlExpression)
  810. {
  811. return new VariableExpression($oExpression->GetName());
  812. }
  813. elseif ($oExpression instanceof TrueOqlExpression)
  814. {
  815. return new TrueExpression;
  816. }
  817. elseif ($oExpression instanceof ScalarOqlExpression)
  818. {
  819. return new ScalarExpression($oExpression->GetValue());
  820. }
  821. elseif ($oExpression instanceof ListOqlExpression)
  822. {
  823. $aItems = array();
  824. foreach ($oExpression->GetItems() as $oItemExpression)
  825. {
  826. $aItems[] = $this->OQLExpressionToCondition($sQuery, $oItemExpression, $aClassAliases);
  827. }
  828. return new ListExpression($aItems);
  829. }
  830. elseif ($oExpression instanceof FunctionOqlExpression)
  831. {
  832. $aArgs = array();
  833. foreach ($oExpression->GetArgs() as $oArgExpression)
  834. {
  835. $aArgs[] = $this->OQLExpressionToCondition($sQuery, $oArgExpression, $aClassAliases);
  836. }
  837. return new FunctionExpression($oExpression->GetVerb(), $aArgs);
  838. }
  839. elseif ($oExpression instanceof IntervalOqlExpression)
  840. {
  841. return new IntervalExpression($oExpression->GetValue(), $oExpression->GetUnit());
  842. }
  843. else
  844. {
  845. throw new CoreException('Unknown expression type', array('class'=>get_class($oExpression), 'query'=>$sQuery));
  846. }
  847. }
  848. // Create a search definition that leads to 0 result, still a valid search object
  849. static public function FromEmptySet($sClass)
  850. {
  851. $oResultFilter = new DBObjectSearch($sClass);
  852. $oResultFilter->m_oSearchCondition = new FalseExpression;
  853. return $oResultFilter;
  854. }
  855. static protected $m_aOQLQueries = array();
  856. // Do not filter out depending on user rights
  857. // In particular when we are currently in the process of evaluating the user rights...
  858. static public function FromOQL_AllData($sQuery, $aParams = null)
  859. {
  860. $oRes = self::FromOQL($sQuery, $aParams);
  861. $oRes->AllowAllData();
  862. return $oRes;
  863. }
  864. static public function FromOQL($sQuery, $aParams = null)
  865. {
  866. if (empty($sQuery)) return null;
  867. // Query caching
  868. $bOQLCacheEnabled = true;
  869. if ($bOQLCacheEnabled && array_key_exists($sQuery, self::$m_aOQLQueries))
  870. {
  871. // hit!
  872. return clone self::$m_aOQLQueries[$sQuery];
  873. }
  874. $oOql = new OqlInterpreter($sQuery);
  875. $oOqlQuery = $oOql->ParseObjectQuery();
  876. $sClass = $oOqlQuery->GetClass();
  877. $sClassAlias = $oOqlQuery->GetClassAlias();
  878. if (!MetaModel::IsValidClass($sClass))
  879. {
  880. throw new OqlNormalizeException('Unknown class', $sQuery, $oOqlQuery->GetClassDetails(), MetaModel::GetClasses());
  881. }
  882. $oResultFilter = new DBObjectSearch($sClass, $sClassAlias);
  883. $aAliases = array($sClassAlias => $sClass);
  884. // Maintain an array of filters, because the flat list is in fact referring to a tree
  885. // And this will be an easy way to dispatch the conditions
  886. // $oResultFilter will be referenced by the other filters, or the other way around...
  887. $aJoinItems = array($sClassAlias => $oResultFilter);
  888. $aJoinSpecs = $oOqlQuery->GetJoins();
  889. if (is_array($aJoinSpecs))
  890. {
  891. foreach ($aJoinSpecs as $oJoinSpec)
  892. {
  893. $sJoinClass = $oJoinSpec->GetClass();
  894. $sJoinClassAlias = $oJoinSpec->GetClassAlias();
  895. if (!MetaModel::IsValidClass($sJoinClass))
  896. {
  897. throw new OqlNormalizeException('Unknown class', $sQuery, $oJoinSpec->GetClassDetails(), MetaModel::GetClasses());
  898. }
  899. if (array_key_exists($sJoinClassAlias, $aAliases))
  900. {
  901. if ($sJoinClassAlias != $sJoinClass)
  902. {
  903. throw new OqlNormalizeException('Duplicate class alias', $sQuery, $oJoinSpec->GetClassAliasDetails());
  904. }
  905. else
  906. {
  907. throw new OqlNormalizeException('Duplicate class name', $sQuery, $oJoinSpec->GetClassDetails());
  908. }
  909. }
  910. // Assumption: ext key on the left only !!!
  911. // normalization should take care of this
  912. $oLeftField = $oJoinSpec->GetLeftField();
  913. $sFromClass = $oLeftField->GetParent();
  914. $sExtKeyAttCode = $oLeftField->GetName();
  915. $oRightField = $oJoinSpec->GetRightField();
  916. $sToClass = $oRightField->GetParent();
  917. $sPKeyDescriptor = $oRightField->GetName();
  918. if ($sPKeyDescriptor != 'id')
  919. {
  920. throw new OqlNormalizeException('Wrong format for Join clause (right hand), expecting an id', $sQuery, $oRightField->GetNameDetails(), array('id'));
  921. }
  922. $aAliases[$sJoinClassAlias] = $sJoinClass;
  923. $aJoinItems[$sJoinClassAlias] = new DBObjectSearch($sJoinClass, $sJoinClassAlias);
  924. if (!array_key_exists($sFromClass, $aJoinItems))
  925. {
  926. throw new OqlNormalizeException('Unknown class in join condition (left expression)', $sQuery, $oLeftField->GetParentDetails(), array_keys($aJoinItems));
  927. }
  928. if (!array_key_exists($sToClass, $aJoinItems))
  929. {
  930. throw new OqlNormalizeException('Unknown class in join condition (right expression)', $sQuery, $oRightField->GetParentDetails(), array_keys($aJoinItems));
  931. }
  932. $aExtKeys = array_keys(MetaModel::GetExternalKeys($aAliases[$sFromClass]));
  933. if (!in_array($sExtKeyAttCode, $aExtKeys))
  934. {
  935. throw new OqlNormalizeException('Unknown external key in join condition (left expression)', $sQuery, $oLeftField->GetNameDetails(), $aExtKeys);
  936. }
  937. if ($sFromClass == $sJoinClassAlias)
  938. {
  939. $aJoinItems[$sToClass]->AddCondition_ReferencedBy($aJoinItems[$sFromClass], $sExtKeyAttCode);
  940. }
  941. else
  942. {
  943. $sOperator = $oJoinSpec->GetOperator();
  944. switch($sOperator)
  945. {
  946. case '=':
  947. $iOperatorCode = TREE_OPERATOR_EQUALS;
  948. break;
  949. case 'BELOW':
  950. $iOperatorCode = TREE_OPERATOR_BELOW;
  951. break;
  952. case 'BELOW_STRICT':
  953. $iOperatorCode = TREE_OPERATOR_BELOW_STRICT;
  954. break;
  955. case 'NOT_BELOW':
  956. $iOperatorCode = TREE_OPERATOR_NOT_BELOW;
  957. break;
  958. case 'NOT_BELOW_STRICT':
  959. $iOperatorCode = TREE_OPERATOR_NOT_BELOW_STRICT;
  960. break;
  961. }
  962. $aJoinItems[$sFromClass]->AddCondition_PointingTo($aJoinItems[$sToClass], $sExtKeyAttCode, $iOperatorCode);
  963. }
  964. }
  965. }
  966. // Check and prepare the select information
  967. $aSelected = array();
  968. foreach ($oOqlQuery->GetSelectedClasses() as $oClassDetails)
  969. {
  970. $sClassToSelect = $oClassDetails->GetValue();
  971. if (!array_key_exists($sClassToSelect, $aAliases))
  972. {
  973. throw new OqlNormalizeException('Unknown class [alias]', $sQuery, $oClassDetails, array_keys($aAliases));
  974. }
  975. $aSelected[$sClassToSelect] = $aAliases[$sClassToSelect];
  976. }
  977. $oResultFilter->m_aClasses = $aAliases;
  978. $oResultFilter->SetSelectedClasses($aSelected);
  979. $oConditionTree = $oOqlQuery->GetCondition();
  980. if ($oConditionTree instanceof Expression)
  981. {
  982. $oResultFilter->m_oSearchCondition = $oResultFilter->OQLExpressionToCondition($sQuery, $oConditionTree, $aAliases);
  983. }
  984. if (!is_null($aParams))
  985. {
  986. $oResultFilter->m_aParams = $aParams;
  987. }
  988. if ($bOQLCacheEnabled)
  989. {
  990. self::$m_aOQLQueries[$sQuery] = clone $oResultFilter;
  991. }
  992. return $oResultFilter;
  993. }
  994. public function toxpath()
  995. {
  996. // #@# a voir...
  997. }
  998. static public function fromxpath()
  999. {
  1000. // #@# a voir...
  1001. }
  1002. }
  1003. ?>