expression.class.inc.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. <?php
  2. // Copyright (C) 2010-2017 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * General definition of an expression tree (could be OQL, SQL or whatever)
  20. *
  21. * @copyright Copyright (C) 2010-2017 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class MissingQueryArgument extends CoreException
  25. {
  26. }
  27. abstract class Expression
  28. {
  29. /**
  30. * Perform a deep clone (as opposed to "clone" which does copy a reference to the underlying objects)
  31. **/
  32. public function DeepClone()
  33. {
  34. return unserialize(serialize($this));
  35. }
  36. // recursive translation of identifiers
  37. abstract public function GetUnresolvedFields($sAlias, &$aUnresolved);
  38. abstract public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true);
  39. // recursive rendering (aArgs used as input by default, or used as output if bRetrofitParams set to True
  40. abstract public function Render(&$aArgs = null, $bRetrofitParams = false);
  41. /**
  42. * Recursively browse the expression tree
  43. * @param Closure $callback
  44. * @return mixed
  45. */
  46. abstract public function Browse(Closure $callback);
  47. abstract public function ApplyParameters($aArgs);
  48. // recursively builds an array of class => fieldname
  49. abstract public function ListRequiredFields();
  50. // recursively list field parents ($aTable = array of sParent => dummy)
  51. abstract public function CollectUsedParents(&$aTable);
  52. abstract public function IsTrue();
  53. // recursively builds an array of [classAlias][fieldName] => value
  54. abstract public function ListConstantFields();
  55. public function RequiresField($sClass, $sFieldName)
  56. {
  57. // #@# todo - optimize : this is called quite often when building a single query !
  58. $aRequired = $this->ListRequiredFields();
  59. if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false;
  60. return true;
  61. }
  62. public function serialize()
  63. {
  64. return base64_encode($this->Render());
  65. }
  66. static public function unserialize($sValue)
  67. {
  68. return self::FromOQL(base64_decode($sValue));
  69. }
  70. /**
  71. * @param $sConditionExpr
  72. * @return Expression
  73. */
  74. static public function FromOQL($sConditionExpr)
  75. {
  76. $oOql = new OqlInterpreter($sConditionExpr);
  77. $oExpression = $oOql->ParseExpression();
  78. return $oExpression;
  79. }
  80. static public function FromSQL($sSQL)
  81. {
  82. $oSql = new SQLExpression($sSQL);
  83. return $oSql;
  84. }
  85. public function LogAnd($oExpr)
  86. {
  87. if ($this->IsTrue()) return clone $oExpr;
  88. if ($oExpr->IsTrue()) return clone $this;
  89. return new BinaryExpression($this, 'AND', $oExpr);
  90. }
  91. public function LogOr($oExpr)
  92. {
  93. return new BinaryExpression($this, 'OR', $oExpr);
  94. }
  95. abstract public function RenameParam($sOldName, $sNewName);
  96. abstract public function RenameAlias($sOldName, $sNewName);
  97. /**
  98. * Make the most relevant label, given the value of the expression
  99. *
  100. * @param DBSearch oFilter The context in which this expression has been used
  101. * @param string sValue The value returned by the query, for this expression
  102. * @param string sDefault The default value if no relevant label could be computed
  103. * @return The label
  104. */
  105. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  106. {
  107. return $sDefault;
  108. }
  109. }
  110. class SQLExpression extends Expression
  111. {
  112. protected $m_sSQL;
  113. public function __construct($sSQL)
  114. {
  115. $this->m_sSQL = $sSQL;
  116. }
  117. public function IsTrue()
  118. {
  119. return false;
  120. }
  121. // recursive rendering
  122. public function Render(&$aArgs = null, $bRetrofitParams = false)
  123. {
  124. return $this->m_sSQL;
  125. }
  126. public function Browse(Closure $callback)
  127. {
  128. $callback($this);
  129. }
  130. public function ApplyParameters($aArgs)
  131. {
  132. }
  133. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  134. {
  135. }
  136. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  137. {
  138. return clone $this;
  139. }
  140. public function ListRequiredFields()
  141. {
  142. return array();
  143. }
  144. public function CollectUsedParents(&$aTable)
  145. {
  146. }
  147. public function ListConstantFields()
  148. {
  149. return array();
  150. }
  151. public function RenameParam($sOldName, $sNewName)
  152. {
  153. // Do nothing, since there is nothing to rename
  154. }
  155. public function RenameAlias($sOldName, $sNewName)
  156. {
  157. // Do nothing, since there is nothing to rename
  158. }
  159. }
  160. class BinaryExpression extends Expression
  161. {
  162. protected $m_oLeftExpr; // filter code or an SQL expression (later?)
  163. protected $m_oRightExpr;
  164. protected $m_sOperator;
  165. public function __construct($oLeftExpr, $sOperator, $oRightExpr)
  166. {
  167. if (!is_object($oLeftExpr))
  168. {
  169. throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr)));
  170. }
  171. if (!is_object($oRightExpr))
  172. {
  173. throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr)));
  174. }
  175. if (!$oLeftExpr instanceof Expression)
  176. {
  177. throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr)));
  178. }
  179. if (!$oRightExpr instanceof Expression)
  180. {
  181. throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr)));
  182. }
  183. $this->m_oLeftExpr = $oLeftExpr;
  184. $this->m_oRightExpr = $oRightExpr;
  185. $this->m_sOperator = $sOperator;
  186. }
  187. public function IsTrue()
  188. {
  189. // return true if we are certain that it will be true
  190. if ($this->m_sOperator == 'AND')
  191. {
  192. if ($this->m_oLeftExpr->IsTrue() && $this->m_oRightExpr->IsTrue()) return true;
  193. }
  194. elseif ($this->m_sOperator == 'OR')
  195. {
  196. if ($this->m_oLeftExpr->IsTrue() || $this->m_oRightExpr->IsTrue()) return true;
  197. }
  198. return false;
  199. }
  200. public function GetLeftExpr()
  201. {
  202. return $this->m_oLeftExpr;
  203. }
  204. public function GetRightExpr()
  205. {
  206. return $this->m_oRightExpr;
  207. }
  208. public function GetOperator()
  209. {
  210. return $this->m_sOperator;
  211. }
  212. // recursive rendering
  213. public function Render(&$aArgs = null, $bRetrofitParams = false)
  214. {
  215. $sOperator = $this->GetOperator();
  216. $sLeft = $this->GetLeftExpr()->Render($aArgs, $bRetrofitParams);
  217. $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams);
  218. return "($sLeft $sOperator $sRight)";
  219. }
  220. public function Browse(Closure $callback)
  221. {
  222. $callback($this);
  223. $this->m_oLeftExpr->Browse($callback);
  224. $this->m_oRightExpr->Browse($callback);
  225. }
  226. public function ApplyParameters($aArgs)
  227. {
  228. if ($this->m_oLeftExpr instanceof VariableExpression)
  229. {
  230. $this->m_oLeftExpr = $this->m_oLeftExpr->GetAsScalar($aArgs);
  231. }
  232. else //if ($this->m_oLeftExpr instanceof Expression)
  233. {
  234. $this->m_oLeftExpr->ApplyParameters($aArgs);
  235. }
  236. if ($this->m_oRightExpr instanceof VariableExpression)
  237. {
  238. $this->m_oRightExpr = $this->m_oRightExpr->GetAsScalar($aArgs);
  239. }
  240. else //if ($this->m_oRightExpr instanceof Expression)
  241. {
  242. $this->m_oRightExpr->ApplyParameters($aArgs);
  243. }
  244. }
  245. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  246. {
  247. $this->GetLeftExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  248. $this->GetRightExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  249. }
  250. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  251. {
  252. $oLeft = $this->GetLeftExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  253. $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  254. return new BinaryExpression($oLeft, $this->GetOperator(), $oRight);
  255. }
  256. public function ListRequiredFields()
  257. {
  258. $aLeft = $this->GetLeftExpr()->ListRequiredFields();
  259. $aRight = $this->GetRightExpr()->ListRequiredFields();
  260. return array_merge($aLeft, $aRight);
  261. }
  262. public function CollectUsedParents(&$aTable)
  263. {
  264. $this->GetLeftExpr()->CollectUsedParents($aTable);
  265. $this->GetRightExpr()->CollectUsedParents($aTable);
  266. }
  267. /**
  268. * List all constant expression of the form <field> = <scalar> or <field> = :<variable>
  269. * Could be extended to support <field> = <function><constant_expression>
  270. */
  271. public function ListConstantFields()
  272. {
  273. $aResult = array();
  274. if ($this->m_sOperator == '=')
  275. {
  276. if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof ScalarExpression))
  277. {
  278. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  279. }
  280. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof ScalarExpression))
  281. {
  282. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  283. }
  284. else if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof VariableExpression))
  285. {
  286. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  287. }
  288. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof VariableExpression))
  289. {
  290. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  291. }
  292. }
  293. else if ($this->m_sOperator == 'AND')
  294. {
  295. // Strictly, this should be done only for the AND operator
  296. $aResult = array_merge_recursive($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields());
  297. }
  298. return $aResult;
  299. }
  300. public function RenameParam($sOldName, $sNewName)
  301. {
  302. $this->GetLeftExpr()->RenameParam($sOldName, $sNewName);
  303. $this->GetRightExpr()->RenameParam($sOldName, $sNewName);
  304. }
  305. public function RenameAlias($sOldName, $sNewName)
  306. {
  307. $this->GetLeftExpr()->RenameAlias($sOldName, $sNewName);
  308. $this->GetRightExpr()->RenameAlias($sOldName, $sNewName);
  309. }
  310. }
  311. class UnaryExpression extends Expression
  312. {
  313. protected $m_value;
  314. public function __construct($value)
  315. {
  316. $this->m_value = $value;
  317. }
  318. public function IsTrue()
  319. {
  320. // return true if we are certain that it will be true
  321. return ($this->m_value == 1);
  322. }
  323. public function GetValue()
  324. {
  325. return $this->m_value;
  326. }
  327. // recursive rendering
  328. public function Render(&$aArgs = null, $bRetrofitParams = false)
  329. {
  330. return CMDBSource::Quote($this->m_value);
  331. }
  332. public function Browse(Closure $callback)
  333. {
  334. $callback($this);
  335. }
  336. public function ApplyParameters($aArgs)
  337. {
  338. }
  339. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  340. {
  341. }
  342. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  343. {
  344. return clone $this;
  345. }
  346. public function ListRequiredFields()
  347. {
  348. return array();
  349. }
  350. public function CollectUsedParents(&$aTable)
  351. {
  352. }
  353. public function ListConstantFields()
  354. {
  355. return array();
  356. }
  357. public function RenameParam($sOldName, $sNewName)
  358. {
  359. // Do nothing
  360. // really ? what about :param{$iParamIndex} ??
  361. }
  362. public function RenameAlias($sOldName, $sNewName)
  363. {
  364. // Do nothing
  365. }
  366. }
  367. class ScalarExpression extends UnaryExpression
  368. {
  369. public function __construct($value)
  370. {
  371. if (!is_scalar($value) && !is_null($value) && (!$value instanceof OqlHexValue))
  372. {
  373. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  374. }
  375. parent::__construct($value);
  376. }
  377. // recursive rendering
  378. public function Render(&$aArgs = null, $bRetrofitParams = false)
  379. {
  380. if (is_null($this->m_value))
  381. {
  382. $sRet = 'NULL';
  383. }
  384. else
  385. {
  386. $sRet = CMDBSource::Quote($this->m_value);
  387. }
  388. return $sRet;
  389. }
  390. public function GetAsScalar($aArgs)
  391. {
  392. return clone $this;
  393. }
  394. }
  395. class TrueExpression extends ScalarExpression
  396. {
  397. public function __construct()
  398. {
  399. parent::__construct(1);
  400. }
  401. public function IsTrue()
  402. {
  403. return true;
  404. }
  405. }
  406. class FalseExpression extends ScalarExpression
  407. {
  408. public function __construct()
  409. {
  410. parent::__construct(0);
  411. }
  412. public function IsTrue()
  413. {
  414. return false;
  415. }
  416. }
  417. class FieldExpression extends UnaryExpression
  418. {
  419. protected $m_sParent;
  420. protected $m_sName;
  421. public function __construct($sName, $sParent = '')
  422. {
  423. parent::__construct("$sParent.$sName");
  424. $this->m_sParent = $sParent;
  425. $this->m_sName = $sName;
  426. }
  427. public function IsTrue()
  428. {
  429. // return true if we are certain that it will be true
  430. return false;
  431. }
  432. public function GetParent() {return $this->m_sParent;}
  433. public function GetName() {return $this->m_sName;}
  434. public function SetParent($sParent)
  435. {
  436. $this->m_sParent = $sParent;
  437. $this->m_value = $sParent.'.'.$this->m_sName;
  438. }
  439. // recursive rendering
  440. public function Render(&$aArgs = null, $bRetrofitParams = false)
  441. {
  442. if (empty($this->m_sParent))
  443. {
  444. return "`{$this->m_sName}`";
  445. }
  446. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  447. }
  448. public function ListRequiredFields()
  449. {
  450. return array($this->m_sParent.'.'.$this->m_sName);
  451. }
  452. public function CollectUsedParents(&$aTable)
  453. {
  454. $aTable[$this->m_sParent] = true;
  455. }
  456. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  457. {
  458. if ($this->m_sParent == $sAlias)
  459. {
  460. // Add a reference to the field
  461. $aUnresolved[$this->m_sName] = $this;
  462. }
  463. elseif ($sAlias == '')
  464. {
  465. // An empty alias means "any alias"
  466. // In such a case, the results are indexed differently
  467. $aUnresolved[$this->m_sParent][$this->m_sName] = $this;
  468. }
  469. }
  470. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  471. {
  472. if (!array_key_exists($this->m_sParent, $aTranslationData))
  473. {
  474. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  475. return clone $this;
  476. }
  477. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  478. {
  479. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  480. {
  481. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  482. if ($bMatchAll) throw new CoreException('Unknown name in translation table', array('name' => $this->m_sName, 'parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData[$this->m_sParent])));
  483. return clone $this;
  484. }
  485. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  486. $sNewName = $this->m_sName;
  487. if ($bMarkFieldsAsResolved)
  488. {
  489. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  490. }
  491. else
  492. {
  493. $oRet = new FieldExpression($sNewName, $sNewParent);
  494. }
  495. }
  496. else
  497. {
  498. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  499. }
  500. return $oRet;
  501. }
  502. /**
  503. * Make the most relevant label, given the value of the expression
  504. *
  505. * @param DBSearch oFilter The context in which this expression has been used
  506. * @param string sValue The value returned by the query, for this expression
  507. * @param string sDefault The default value if no relevant label could be computed
  508. * @return The label
  509. */
  510. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  511. {
  512. $sAttCode = $this->GetName();
  513. $sParentAlias = $this->GetParent();
  514. $aSelectedClasses = $oFilter->GetSelectedClasses();
  515. $sClass = $aSelectedClasses[$sParentAlias];
  516. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  517. // Set a default value for the general case
  518. $sRes = $oAttDef->GetAsHtml($sValue);
  519. // Exceptions...
  520. if ($oAttDef->IsExternalKey())
  521. {
  522. $sObjClass = $oAttDef->GetTargetClass();
  523. $iObjKey = (int)$sValue;
  524. if ($iObjKey > 0)
  525. {
  526. $oObject = MetaModel::GetObjectWithArchive($sObjClass, $iObjKey);
  527. $sRes = $oObject->GetHyperlink();
  528. }
  529. else
  530. {
  531. // Undefined
  532. $sRes = DBObject::MakeHyperLink($sObjClass, 0);
  533. }
  534. }
  535. elseif ($oAttDef->IsExternalField())
  536. {
  537. if (is_null($sValue))
  538. {
  539. $sRes = Dict::S('UI:UndefinedObject');
  540. }
  541. }
  542. return $sRes;
  543. }
  544. public function RenameAlias($sOldName, $sNewName)
  545. {
  546. if ($this->m_sParent == $sOldName)
  547. {
  548. $this->m_sParent = $sNewName;
  549. }
  550. }
  551. }
  552. // Has been resolved into an SQL expression
  553. class FieldExpressionResolved extends FieldExpression
  554. {
  555. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  556. {
  557. }
  558. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  559. {
  560. return clone $this;
  561. }
  562. }
  563. class VariableExpression extends UnaryExpression
  564. {
  565. protected $m_sName;
  566. public function __construct($sName)
  567. {
  568. parent::__construct($sName);
  569. $this->m_sName = $sName;
  570. }
  571. public function IsTrue()
  572. {
  573. // return true if we are certain that it will be true
  574. return false;
  575. }
  576. public function GetName() {return $this->m_sName;}
  577. // recursive rendering
  578. public function Render(&$aArgs = null, $bRetrofitParams = false)
  579. {
  580. if (is_null($aArgs))
  581. {
  582. return ':'.$this->m_sName;
  583. }
  584. elseif (array_key_exists($this->m_sName, $aArgs))
  585. {
  586. $res = CMDBSource::Quote($aArgs[$this->m_sName]);
  587. if (is_array($res))
  588. {
  589. $res = implode(', ', $res);
  590. }
  591. return $res;
  592. }
  593. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  594. {
  595. $sParamName = substr($this->m_sName, 0, $iPos);
  596. if (array_key_exists($sParamName.'->object()', $aArgs))
  597. {
  598. $sAttCode = substr($this->m_sName, $iPos + 2);
  599. $oObj = $aArgs[$sParamName.'->object()'];
  600. if ($sAttCode == 'id')
  601. {
  602. return CMDBSource::Quote($oObj->GetKey());
  603. }
  604. return CMDBSource::Quote($oObj->Get($sAttCode));
  605. }
  606. }
  607. if ($bRetrofitParams)
  608. {
  609. $aArgs[$this->m_sName] = null;
  610. return ':'.$this->m_sName;
  611. }
  612. else
  613. {
  614. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  615. }
  616. }
  617. public function RenameParam($sOldName, $sNewName)
  618. {
  619. if ($this->m_sName == $sOldName)
  620. {
  621. $this->m_sName = $sNewName;
  622. }
  623. }
  624. public function GetAsScalar($aArgs)
  625. {
  626. $oRet = null;
  627. if (array_key_exists($this->m_sName, $aArgs))
  628. {
  629. $oRet = new ScalarExpression($aArgs[$this->m_sName]);
  630. }
  631. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  632. {
  633. $sParamName = substr($this->m_sName, 0, $iPos);
  634. if (array_key_exists($sParamName.'->object()', $aArgs))
  635. {
  636. $sAttCode = substr($this->m_sName, $iPos + 2);
  637. $oObj = $aArgs[$sParamName.'->object()'];
  638. if ($sAttCode == 'id')
  639. {
  640. $oRet = new ScalarExpression($oObj->GetKey());
  641. }
  642. elseif (MetaModel::IsValidAttCode(get_class($oObj), $sAttCode))
  643. {
  644. $oRet = new ScalarExpression($oObj->Get($sAttCode));
  645. }
  646. else
  647. {
  648. throw new CoreException("Query argument {$this->m_sName} not matching any attribute of class ".get_class($oObj));
  649. }
  650. }
  651. }
  652. if (is_null($oRet))
  653. {
  654. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  655. }
  656. return $oRet;
  657. }
  658. }
  659. // Temporary, until we implement functions and expression casting!
  660. // ... or until we implement a real full text search based in the MATCH() expression
  661. class ListExpression extends Expression
  662. {
  663. protected $m_aExpressions;
  664. public function __construct($aExpressions)
  665. {
  666. $this->m_aExpressions = $aExpressions;
  667. }
  668. public static function FromScalars($aScalars)
  669. {
  670. $aExpressions = array();
  671. foreach($aScalars as $value)
  672. {
  673. $aExpressions[] = new ScalarExpression($value);
  674. }
  675. return new ListExpression($aExpressions);
  676. }
  677. public function IsTrue()
  678. {
  679. // return true if we are certain that it will be true
  680. return false;
  681. }
  682. public function GetItems()
  683. {
  684. return $this->m_aExpressions;
  685. }
  686. // recursive rendering
  687. public function Render(&$aArgs = null, $bRetrofitParams = false)
  688. {
  689. $aRes = array();
  690. foreach ($this->m_aExpressions as $oExpr)
  691. {
  692. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  693. }
  694. return '('.implode(', ', $aRes).')';
  695. }
  696. public function Browse(Closure $callback)
  697. {
  698. $callback($this);
  699. foreach ($this->m_aExpressions as $oExpr)
  700. {
  701. $oExpr->Browse($callback);
  702. }
  703. }
  704. public function ApplyParameters($aArgs)
  705. {
  706. $aRes = array();
  707. foreach ($this->m_aExpressions as $idx => $oExpr)
  708. {
  709. if ($oExpr instanceof VariableExpression)
  710. {
  711. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  712. }
  713. else
  714. {
  715. $oExpr->ApplyParameters($aArgs);
  716. }
  717. }
  718. }
  719. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  720. {
  721. foreach ($this->m_aExpressions as $oExpr)
  722. {
  723. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  724. }
  725. }
  726. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  727. {
  728. $aRes = array();
  729. foreach ($this->m_aExpressions as $oExpr)
  730. {
  731. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  732. }
  733. return new ListExpression($aRes);
  734. }
  735. public function ListRequiredFields()
  736. {
  737. $aRes = array();
  738. foreach ($this->m_aExpressions as $oExpr)
  739. {
  740. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  741. }
  742. return $aRes;
  743. }
  744. public function CollectUsedParents(&$aTable)
  745. {
  746. foreach ($this->m_aExpressions as $oExpr)
  747. {
  748. $oExpr->CollectUsedParents($aTable);
  749. }
  750. }
  751. public function ListConstantFields()
  752. {
  753. $aRes = array();
  754. foreach ($this->m_aExpressions as $oExpr)
  755. {
  756. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  757. }
  758. return $aRes;
  759. }
  760. public function RenameParam($sOldName, $sNewName)
  761. {
  762. $aRes = array();
  763. foreach ($this->m_aExpressions as $key => $oExpr)
  764. {
  765. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  766. }
  767. }
  768. public function RenameAlias($sOldName, $sNewName)
  769. {
  770. $aRes = array();
  771. foreach ($this->m_aExpressions as $key => $oExpr)
  772. {
  773. $oExpr->RenameAlias($sOldName, $sNewName);
  774. }
  775. }
  776. }
  777. class FunctionExpression extends Expression
  778. {
  779. protected $m_sVerb;
  780. protected $m_aArgs; // array of expressions
  781. public function __construct($sVerb, $aArgExpressions)
  782. {
  783. $this->m_sVerb = $sVerb;
  784. $this->m_aArgs = $aArgExpressions;
  785. }
  786. public function IsTrue()
  787. {
  788. // return true if we are certain that it will be true
  789. return false;
  790. }
  791. public function GetVerb()
  792. {
  793. return $this->m_sVerb;
  794. }
  795. public function GetArgs()
  796. {
  797. return $this->m_aArgs;
  798. }
  799. // recursive rendering
  800. public function Render(&$aArgs = null, $bRetrofitParams = false)
  801. {
  802. $aRes = array();
  803. foreach ($this->m_aArgs as $iPos => $oExpr)
  804. {
  805. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  806. }
  807. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  808. }
  809. public function Browse(Closure $callback)
  810. {
  811. $callback($this);
  812. foreach ($this->m_aArgs as $iPos => $oExpr)
  813. {
  814. $oExpr->Browse($callback);
  815. }
  816. }
  817. public function ApplyParameters($aArgs)
  818. {
  819. $aRes = array();
  820. foreach ($this->m_aArgs as $idx => $oExpr)
  821. {
  822. if ($oExpr instanceof VariableExpression)
  823. {
  824. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  825. }
  826. else
  827. {
  828. $oExpr->ApplyParameters($aArgs);
  829. }
  830. }
  831. }
  832. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  833. {
  834. foreach ($this->m_aArgs as $oExpr)
  835. {
  836. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  837. }
  838. }
  839. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  840. {
  841. $aRes = array();
  842. foreach ($this->m_aArgs as $oExpr)
  843. {
  844. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  845. }
  846. return new FunctionExpression($this->m_sVerb, $aRes);
  847. }
  848. public function ListRequiredFields()
  849. {
  850. $aRes = array();
  851. foreach ($this->m_aArgs as $oExpr)
  852. {
  853. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  854. }
  855. return $aRes;
  856. }
  857. public function CollectUsedParents(&$aTable)
  858. {
  859. foreach ($this->m_aArgs as $oExpr)
  860. {
  861. $oExpr->CollectUsedParents($aTable);
  862. }
  863. }
  864. public function ListConstantFields()
  865. {
  866. $aRes = array();
  867. foreach ($this->m_aArgs as $oExpr)
  868. {
  869. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  870. }
  871. return $aRes;
  872. }
  873. public function RenameParam($sOldName, $sNewName)
  874. {
  875. foreach ($this->m_aArgs as $key => $oExpr)
  876. {
  877. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  878. }
  879. }
  880. public function RenameAlias($sOldName, $sNewName)
  881. {
  882. foreach ($this->m_aArgs as $key => $oExpr)
  883. {
  884. $oExpr->RenameAlias($sOldName, $sNewName);
  885. }
  886. }
  887. /**
  888. * Make the most relevant label, given the value of the expression
  889. *
  890. * @param DBSearch oFilter The context in which this expression has been used
  891. * @param string sValue The value returned by the query, for this expression
  892. * @param string sDefault The default value if no relevant label could be computed
  893. * @return The label
  894. */
  895. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  896. {
  897. static $aWeekDayToString = null;
  898. if (is_null($aWeekDayToString))
  899. {
  900. // Init the correspondance table
  901. $aWeekDayToString = array(
  902. 0 => Dict::S('DayOfWeek-Sunday'),
  903. 1 => Dict::S('DayOfWeek-Monday'),
  904. 2 => Dict::S('DayOfWeek-Tuesday'),
  905. 3 => Dict::S('DayOfWeek-Wednesday'),
  906. 4 => Dict::S('DayOfWeek-Thursday'),
  907. 5 => Dict::S('DayOfWeek-Friday'),
  908. 6 => Dict::S('DayOfWeek-Saturday')
  909. );
  910. }
  911. static $aMonthToString = null;
  912. if (is_null($aMonthToString))
  913. {
  914. // Init the correspondance table
  915. $aMonthToString = array(
  916. 1 => Dict::S('Month-01'),
  917. 2 => Dict::S('Month-02'),
  918. 3 => Dict::S('Month-03'),
  919. 4 => Dict::S('Month-04'),
  920. 5 => Dict::S('Month-05'),
  921. 6 => Dict::S('Month-06'),
  922. 7 => Dict::S('Month-07'),
  923. 8 => Dict::S('Month-08'),
  924. 9 => Dict::S('Month-09'),
  925. 10 => Dict::S('Month-10'),
  926. 11 => Dict::S('Month-11'),
  927. 12 => Dict::S('Month-12'),
  928. );
  929. }
  930. $sRes = $sDefault;
  931. if (strtolower($this->m_sVerb) == 'date_format')
  932. {
  933. $oFormatExpr = $this->m_aArgs[1];
  934. if ($oFormatExpr->Render() == "'%w'")
  935. {
  936. if (isset($aWeekDayToString[(int)$sValue]))
  937. {
  938. $sRes = $aWeekDayToString[(int)$sValue];
  939. }
  940. }
  941. elseif ($oFormatExpr->Render() == "'%Y-%m'")
  942. {
  943. // yyyy-mm => "yyyy month"
  944. $iMonth = (int) substr($sValue, -2); // the two last chars
  945. $sRes = substr($sValue, 0, 4).' '.$aMonthToString[$iMonth];
  946. }
  947. elseif ($oFormatExpr->Render() == "'%Y-%m-%d'")
  948. {
  949. // yyyy-mm-dd => "month d"
  950. $iMonth = (int) substr($sValue, 5, 2);
  951. $sRes = $aMonthToString[$iMonth].' '.(int)substr($sValue, -2);
  952. }
  953. }
  954. return $sRes;
  955. }
  956. }
  957. class IntervalExpression extends Expression
  958. {
  959. protected $m_oValue; // expression
  960. protected $m_sUnit;
  961. public function __construct($oValue, $sUnit)
  962. {
  963. $this->m_oValue = $oValue;
  964. $this->m_sUnit = $sUnit;
  965. }
  966. public function IsTrue()
  967. {
  968. // return true if we are certain that it will be true
  969. return false;
  970. }
  971. public function GetValue()
  972. {
  973. return $this->m_oValue;
  974. }
  975. public function GetUnit()
  976. {
  977. return $this->m_sUnit;
  978. }
  979. // recursive rendering
  980. public function Render(&$aArgs = null, $bRetrofitParams = false)
  981. {
  982. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  983. }
  984. public function Browse(Closure $callback)
  985. {
  986. $callback($this);
  987. $this->m_oValue->Browse($callback);
  988. }
  989. public function ApplyParameters($aArgs)
  990. {
  991. if ($this->m_oValue instanceof VariableExpression)
  992. {
  993. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  994. }
  995. else
  996. {
  997. $this->m_oValue->ApplyParameters($aArgs);
  998. }
  999. }
  1000. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1001. {
  1002. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  1003. }
  1004. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1005. {
  1006. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  1007. }
  1008. public function ListRequiredFields()
  1009. {
  1010. return array();
  1011. }
  1012. public function CollectUsedParents(&$aTable)
  1013. {
  1014. }
  1015. public function ListConstantFields()
  1016. {
  1017. return array();
  1018. }
  1019. public function RenameParam($sOldName, $sNewName)
  1020. {
  1021. $this->m_oValue->RenameParam($sOldName, $sNewName);
  1022. }
  1023. public function RenameAlias($sOldName, $sNewName)
  1024. {
  1025. $this->m_oValue->RenameAlias($sOldName, $sNewName);
  1026. }
  1027. }
  1028. class CharConcatExpression extends Expression
  1029. {
  1030. protected $m_aExpressions;
  1031. public function __construct($aExpressions)
  1032. {
  1033. $this->m_aExpressions = $aExpressions;
  1034. }
  1035. public function IsTrue()
  1036. {
  1037. // return true if we are certain that it will be true
  1038. return false;
  1039. }
  1040. public function GetItems()
  1041. {
  1042. return $this->m_aExpressions;
  1043. }
  1044. // recursive rendering
  1045. public function Render(&$aArgs = null, $bRetrofitParams = false)
  1046. {
  1047. $aRes = array();
  1048. foreach ($this->m_aExpressions as $oExpr)
  1049. {
  1050. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  1051. // Concat will be globally NULL if one single argument is null !
  1052. $aRes[] = "COALESCE($sCol, '')";
  1053. }
  1054. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  1055. }
  1056. public function Browse(Closure $callback)
  1057. {
  1058. $callback($this);
  1059. foreach ($this->m_aExpressions as $oExpr)
  1060. {
  1061. $oExpr->Browse($callback);
  1062. }
  1063. }
  1064. public function ApplyParameters($aArgs)
  1065. {
  1066. $aRes = array();
  1067. foreach ($this->m_aExpressions as $idx => $oExpr)
  1068. {
  1069. if ($oExpr instanceof VariableExpression)
  1070. {
  1071. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  1072. }
  1073. else
  1074. {
  1075. $this->m_aExpressions->ApplyParameters($aArgs);
  1076. }
  1077. }
  1078. }
  1079. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1080. {
  1081. foreach ($this->m_aExpressions as $oExpr)
  1082. {
  1083. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1084. }
  1085. }
  1086. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1087. {
  1088. $aRes = array();
  1089. foreach ($this->m_aExpressions as $oExpr)
  1090. {
  1091. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1092. }
  1093. return new CharConcatExpression($aRes);
  1094. }
  1095. public function ListRequiredFields()
  1096. {
  1097. $aRes = array();
  1098. foreach ($this->m_aExpressions as $oExpr)
  1099. {
  1100. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  1101. }
  1102. return $aRes;
  1103. }
  1104. public function CollectUsedParents(&$aTable)
  1105. {
  1106. foreach ($this->m_aExpressions as $oExpr)
  1107. {
  1108. $oExpr->CollectUsedParents($aTable);
  1109. }
  1110. }
  1111. public function ListConstantFields()
  1112. {
  1113. $aRes = array();
  1114. foreach ($this->m_aExpressions as $oExpr)
  1115. {
  1116. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  1117. }
  1118. return $aRes;
  1119. }
  1120. public function RenameParam($sOldName, $sNewName)
  1121. {
  1122. foreach ($this->m_aExpressions as $key => $oExpr)
  1123. {
  1124. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  1125. }
  1126. }
  1127. public function RenameAlias($sOldName, $sNewName)
  1128. {
  1129. foreach ($this->m_aExpressions as $key => $oExpr)
  1130. {
  1131. $oExpr->RenameAlias($sOldName, $sNewName);
  1132. }
  1133. }
  1134. }
  1135. class CharConcatWSExpression extends CharConcatExpression
  1136. {
  1137. protected $m_separator;
  1138. public function __construct($separator, $aExpressions)
  1139. {
  1140. $this->m_separator = $separator;
  1141. parent::__construct($aExpressions);
  1142. }
  1143. // recursive rendering
  1144. public function Render(&$aArgs = null, $bRetrofitParams = false)
  1145. {
  1146. $aRes = array();
  1147. foreach ($this->m_aExpressions as $oExpr)
  1148. {
  1149. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  1150. // Concat will be globally NULL if one single argument is null !
  1151. $aRes[] = "COALESCE($sCol, '')";
  1152. }
  1153. $sSep = CMDBSource::Quote($this->m_separator);
  1154. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  1155. }
  1156. public function Browse(Closure $callback)
  1157. {
  1158. $callback($this);
  1159. foreach ($this->m_aExpressions as $oExpr)
  1160. {
  1161. $oExpr->Browse($callback);
  1162. }
  1163. }
  1164. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1165. {
  1166. $aRes = array();
  1167. foreach ($this->m_aExpressions as $oExpr)
  1168. {
  1169. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1170. }
  1171. return new CharConcatWSExpression($this->m_separator, $aRes);
  1172. }
  1173. }
  1174. class QueryBuilderExpressions
  1175. {
  1176. protected $m_oConditionExpr;
  1177. protected $m_aSelectExpr;
  1178. protected $m_aGroupByExpr;
  1179. protected $m_aJoinFields;
  1180. protected $m_aClassIds;
  1181. public function __construct(DBObjectSearch $oSearch, $aGroupByExpr = null)
  1182. {
  1183. if ($oSearch->GetShowObsoleteData() || !MetaModel::IsObsoletable($oSearch->GetClass()))
  1184. {
  1185. $this->m_oConditionExpr = $oSearch->GetCriteria();
  1186. }
  1187. else
  1188. {
  1189. $oNotObsolete = new BinaryExpression(new FieldExpression('obsolescence_flag', $oSearch->GetClassAlias()), '=', new ScalarExpression(0));
  1190. $this->m_oConditionExpr = new BinaryExpression($oSearch->GetCriteria(), 'AND', $oNotObsolete);
  1191. }
  1192. $this->m_aSelectExpr = array();
  1193. $this->m_aGroupByExpr = $aGroupByExpr;
  1194. $this->m_aJoinFields = array();
  1195. $this->m_aClassIds = array();
  1196. foreach($oSearch->GetJoinedClasses() as $sClassAlias => $sClass)
  1197. {
  1198. $this->m_aClassIds[$sClassAlias] = new FieldExpression('id', $sClassAlias);
  1199. }
  1200. }
  1201. public function GetSelect()
  1202. {
  1203. return $this->m_aSelectExpr;
  1204. }
  1205. public function GetGroupBy()
  1206. {
  1207. return $this->m_aGroupByExpr;
  1208. }
  1209. public function GetCondition()
  1210. {
  1211. return $this->m_oConditionExpr;
  1212. }
  1213. public function PopJoinField()
  1214. {
  1215. return array_pop($this->m_aJoinFields);
  1216. }
  1217. public function AddSelect($sAttAlias, $oExpression)
  1218. {
  1219. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  1220. }
  1221. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  1222. public function AddCondition($oExpression)
  1223. {
  1224. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  1225. }
  1226. public function PushJoinField($oExpression)
  1227. {
  1228. array_push($this->m_aJoinFields, $oExpression);
  1229. }
  1230. /**
  1231. * Get tables representing the queried objects
  1232. * Could be further optimized: when the first join is an outer join, then the rest can be omitted
  1233. */
  1234. public function GetMandatoryTables(&$aTables = null)
  1235. {
  1236. if (is_null($aTables)) $aTables = array();
  1237. foreach($this->m_aClassIds as $sClass => $oExpression)
  1238. {
  1239. $oExpression->CollectUsedParents($aTables);
  1240. }
  1241. }
  1242. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1243. {
  1244. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1245. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1246. {
  1247. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1248. }
  1249. if ($this->m_aGroupByExpr)
  1250. {
  1251. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1252. {
  1253. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1254. }
  1255. }
  1256. foreach($this->m_aJoinFields as $oExpression)
  1257. {
  1258. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  1259. }
  1260. }
  1261. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1262. {
  1263. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1264. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1265. {
  1266. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1267. }
  1268. if ($this->m_aGroupByExpr)
  1269. {
  1270. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1271. {
  1272. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1273. }
  1274. }
  1275. foreach($this->m_aJoinFields as $index => $oExpression)
  1276. {
  1277. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1278. }
  1279. foreach($this->m_aClassIds as $sClass => $oExpression)
  1280. {
  1281. $this->m_aClassIds[$sClass] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1282. }
  1283. }
  1284. public function RenameParam($sOldName, $sNewName)
  1285. {
  1286. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  1287. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1288. {
  1289. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1290. }
  1291. if ($this->m_aGroupByExpr)
  1292. {
  1293. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1294. {
  1295. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1296. }
  1297. }
  1298. foreach($this->m_aJoinFields as $index => $oExpression)
  1299. {
  1300. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  1301. }
  1302. }
  1303. }