expression.class.inc.php 37 KB

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