expression.class.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * General definition of an expression tree (could be OQL, SQL or whatever)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class MissingQueryArgument extends CoreException
  25. {
  26. }
  27. abstract class Expression
  28. {
  29. // recursive translation of identifiers
  30. abstract public function GetUnresolvedFields($sAlias, &$aUnresolved);
  31. abstract public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true);
  32. // recursive rendering (aArgs used as input by default, or used as output if bRetrofitParams set to True
  33. abstract public function Render(&$aArgs = null, $bRetrofitParams = false);
  34. // recursively builds an array of class => fieldname
  35. abstract public function ListRequiredFields();
  36. abstract public function IsTrue();
  37. public function RequiresField($sClass, $sFieldName)
  38. {
  39. // #@# todo - optimize : this is called quite often when building a single query !
  40. $aRequired = $this->ListRequiredFields();
  41. if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false;
  42. return true;
  43. }
  44. public function serialize()
  45. {
  46. return base64_encode($this->Render());
  47. }
  48. static public function unserialize($sValue)
  49. {
  50. return self::FromOQL(base64_decode($sValue));
  51. }
  52. static public function FromOQL($sConditionExpr)
  53. {
  54. $oOql = new OqlInterpreter($sConditionExpr);
  55. $oExpression = $oOql->ParseExpression();
  56. return $oExpression;
  57. }
  58. static public function FromSQL($sSQL)
  59. {
  60. $oSql = new SQLExpression($sSQL);
  61. return $oSql;
  62. }
  63. public function LogAnd($oExpr)
  64. {
  65. if ($this->IsTrue()) return clone $oExpr;
  66. if ($oExpr->IsTrue()) return clone $this;
  67. return new BinaryExpression($this, 'AND', $oExpr);
  68. }
  69. public function LogOr($oExpr)
  70. {
  71. return new BinaryExpression($this, 'OR', $oExpr);
  72. }
  73. abstract public function RenameParam($sOldName, $sNewName);
  74. }
  75. class SQLExpression extends Expression
  76. {
  77. protected $m_sSQL;
  78. public function __construct($sSQL)
  79. {
  80. $this->m_sSQL = $sSQL;
  81. }
  82. public function IsTrue()
  83. {
  84. return false;
  85. }
  86. // recursive rendering
  87. public function Render(&$aArgs = null, $bRetrofitParams = false)
  88. {
  89. return $this->m_sSQL;
  90. }
  91. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  92. {
  93. }
  94. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  95. {
  96. return clone $this;
  97. }
  98. public function ListRequiredFields()
  99. {
  100. return array();
  101. }
  102. public function RenameParam($sOldName, $sNewName)
  103. {
  104. // Do nothing, since there is nothing to rename
  105. }
  106. }
  107. class BinaryExpression extends Expression
  108. {
  109. protected $m_oLeftExpr; // filter code or an SQL expression (later?)
  110. protected $m_oRightExpr;
  111. protected $m_sOperator;
  112. public function __construct($oLeftExpr, $sOperator, $oRightExpr)
  113. {
  114. if (!is_object($oLeftExpr))
  115. {
  116. throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr)));
  117. }
  118. if (!is_object($oRightExpr))
  119. {
  120. throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr)));
  121. }
  122. if (!$oLeftExpr instanceof Expression)
  123. {
  124. throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr)));
  125. }
  126. if (!$oRightExpr instanceof Expression)
  127. {
  128. throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr)));
  129. }
  130. $this->m_oLeftExpr = $oLeftExpr;
  131. $this->m_oRightExpr = $oRightExpr;
  132. $this->m_sOperator = $sOperator;
  133. }
  134. public function IsTrue()
  135. {
  136. // return true if we are certain that it will be true
  137. if ($this->m_sOperator == 'AND')
  138. {
  139. if ($this->m_oLeftExpr->IsTrue() && $this->m_oLeftExpr->IsTrue()) return true;
  140. }
  141. return false;
  142. }
  143. public function GetLeftExpr()
  144. {
  145. return $this->m_oLeftExpr;
  146. }
  147. public function GetRightExpr()
  148. {
  149. return $this->m_oRightExpr;
  150. }
  151. public function GetOperator()
  152. {
  153. return $this->m_sOperator;
  154. }
  155. // recursive rendering
  156. public function Render(&$aArgs = null, $bRetrofitParams = false)
  157. {
  158. $sOperator = $this->GetOperator();
  159. $sLeft = $this->GetLeftExpr()->Render($aArgs, $bRetrofitParams);
  160. $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams);
  161. return "($sLeft $sOperator $sRight)";
  162. }
  163. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  164. {
  165. $this->GetLeftExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  166. $this->GetRightExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  167. }
  168. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  169. {
  170. $oLeft = $this->GetLeftExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  171. $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  172. return new BinaryExpression($oLeft, $this->GetOperator(), $oRight);
  173. }
  174. public function ListRequiredFields()
  175. {
  176. $aLeft = $this->GetLeftExpr()->ListRequiredFields();
  177. $aRight = $this->GetRightExpr()->ListRequiredFields();
  178. return array_merge($aLeft, $aRight);
  179. }
  180. public function RenameParam($sOldName, $sNewName)
  181. {
  182. $this->GetLeftExpr()->RenameParam($sOldName, $sNewName);
  183. $this->GetRightExpr()->RenameParam($sOldName, $sNewName);
  184. }
  185. }
  186. class UnaryExpression extends Expression
  187. {
  188. protected $m_value;
  189. public function __construct($value)
  190. {
  191. $this->m_value = $value;
  192. }
  193. public function IsTrue()
  194. {
  195. // return true if we are certain that it will be true
  196. return ($this->m_value == 1);
  197. }
  198. public function GetValue()
  199. {
  200. return $this->m_value;
  201. }
  202. // recursive rendering
  203. public function Render(&$aArgs = null, $bRetrofitParams = false)
  204. {
  205. if ($bRetrofitParams)
  206. {
  207. $iParamIndex = count($aArgs) + 1; // 1-based indexation
  208. $aArgs['param'.$iParamIndex] = $this->m_value;
  209. return ':param'.$iParamIndex;
  210. }
  211. else
  212. {
  213. return CMDBSource::Quote($this->m_value);
  214. }
  215. }
  216. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  217. {
  218. }
  219. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  220. {
  221. return clone $this;
  222. }
  223. public function ListRequiredFields()
  224. {
  225. return array();
  226. }
  227. public function RenameParam($sOldName, $sNewName)
  228. {
  229. // Do nothing
  230. // really ? what about :param{$iParamIndex} ??
  231. }
  232. }
  233. class ScalarExpression extends UnaryExpression
  234. {
  235. public function __construct($value)
  236. {
  237. if (!is_scalar($value))
  238. {
  239. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  240. }
  241. parent::__construct($value);
  242. }
  243. }
  244. class TrueExpression extends ScalarExpression
  245. {
  246. public function __construct()
  247. {
  248. parent::__construct(1);
  249. }
  250. public function IsTrue()
  251. {
  252. return true;
  253. }
  254. }
  255. class FalseExpression extends ScalarExpression
  256. {
  257. public function __construct()
  258. {
  259. parent::__construct(0);
  260. }
  261. public function IsTrue()
  262. {
  263. return false;
  264. }
  265. }
  266. class FieldExpression extends UnaryExpression
  267. {
  268. protected $m_sParent;
  269. protected $m_sName;
  270. public function __construct($sName, $sParent = '')
  271. {
  272. parent::__construct("$sParent.$sName");
  273. $this->m_sParent = $sParent;
  274. $this->m_sName = $sName;
  275. }
  276. public function IsTrue()
  277. {
  278. // return true if we are certain that it will be true
  279. return false;
  280. }
  281. public function GetParent() {return $this->m_sParent;}
  282. public function GetName() {return $this->m_sName;}
  283. // recursive rendering
  284. public function Render(&$aArgs = null, $bRetrofitParams = false)
  285. {
  286. if (empty($this->m_sParent))
  287. {
  288. return "`{$this->m_sName}`";
  289. }
  290. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  291. }
  292. public function ListRequiredFields()
  293. {
  294. return array($this->m_sParent.'.'.$this->m_sName);
  295. }
  296. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  297. {
  298. if ($this->m_sParent == $sAlias)
  299. {
  300. // Add a reference to the field
  301. $aUnresolved[$this->m_sName] = $this;
  302. }
  303. }
  304. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  305. {
  306. if (!array_key_exists($this->m_sParent, $aTranslationData))
  307. {
  308. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  309. return clone $this;
  310. }
  311. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  312. {
  313. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  314. {
  315. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  316. 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])));
  317. return clone $this;
  318. }
  319. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  320. $sNewName = $this->m_sName;
  321. if ($bMarkFieldsAsResolved)
  322. {
  323. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  324. }
  325. else
  326. {
  327. $oRet = new FieldExpression($sNewName, $sNewParent);
  328. }
  329. }
  330. else
  331. {
  332. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  333. }
  334. return $oRet;
  335. }
  336. }
  337. // Has been resolved into an SQL expression
  338. class FieldExpressionResolved extends FieldExpression
  339. {
  340. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  341. {
  342. }
  343. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  344. {
  345. return clone $this;
  346. }
  347. }
  348. class VariableExpression extends UnaryExpression
  349. {
  350. protected $m_sName;
  351. public function __construct($sName)
  352. {
  353. parent::__construct($sName);
  354. $this->m_sName = $sName;
  355. }
  356. public function IsTrue()
  357. {
  358. // return true if we are certain that it will be true
  359. return false;
  360. }
  361. public function GetName() {return $this->m_sName;}
  362. // recursive rendering
  363. public function Render(&$aArgs = null, $bRetrofitParams = false)
  364. {
  365. if (is_null($aArgs))
  366. {
  367. return ':'.$this->m_sName;
  368. }
  369. elseif (array_key_exists($this->m_sName, $aArgs))
  370. {
  371. return CMDBSource::Quote($aArgs[$this->m_sName]);
  372. }
  373. elseif ($bRetrofitParams)
  374. {
  375. //$aArgs[$this->m_sName] = null;
  376. return ':'.$this->m_sName;
  377. }
  378. else
  379. {
  380. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
  381. }
  382. }
  383. public function RenameParam($sOldName, $sNewName)
  384. {
  385. if ($this->m_sName == $sOldName)
  386. {
  387. $this->m_sName = $sNewName;
  388. }
  389. }
  390. }
  391. // Temporary, until we implement functions and expression casting!
  392. // ... or until we implement a real full text search based in the MATCH() expression
  393. class ListExpression extends Expression
  394. {
  395. protected $m_aExpressions;
  396. public function __construct($aExpressions)
  397. {
  398. $this->m_aExpressions = $aExpressions;
  399. }
  400. public static function FromScalars($aScalars)
  401. {
  402. $aExpressions = array();
  403. foreach($aScalars as $value)
  404. {
  405. $aExpressions[] = new ScalarExpression($value);
  406. }
  407. return new ListExpression($aExpressions);
  408. }
  409. public function IsTrue()
  410. {
  411. // return true if we are certain that it will be true
  412. return false;
  413. }
  414. public function GetItems()
  415. {
  416. return $this->m_aExpressions;
  417. }
  418. // recursive rendering
  419. public function Render(&$aArgs = null, $bRetrofitParams = false)
  420. {
  421. $aRes = array();
  422. foreach ($this->m_aExpressions as $oExpr)
  423. {
  424. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  425. }
  426. return '('.implode(', ', $aRes).')';
  427. }
  428. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  429. {
  430. foreach ($this->m_aExpressions as $oExpr)
  431. {
  432. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  433. }
  434. }
  435. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  436. {
  437. $aRes = array();
  438. foreach ($this->m_aExpressions as $oExpr)
  439. {
  440. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  441. }
  442. return new ListExpression($aRes);
  443. }
  444. public function ListRequiredFields()
  445. {
  446. $aRes = array();
  447. foreach ($this->m_aExpressions as $oExpr)
  448. {
  449. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  450. }
  451. return $aRes;
  452. }
  453. public function RenameParam($sOldName, $sNewName)
  454. {
  455. $aRes = array();
  456. foreach ($this->m_aExpressions as $key => $oExpr)
  457. {
  458. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  459. }
  460. }
  461. }
  462. class FunctionExpression extends Expression
  463. {
  464. protected $m_sVerb;
  465. protected $m_aArgs; // array of expressions
  466. public function __construct($sVerb, $aArgExpressions)
  467. {
  468. $this->m_sVerb = $sVerb;
  469. $this->m_aArgs = $aArgExpressions;
  470. }
  471. public function IsTrue()
  472. {
  473. // return true if we are certain that it will be true
  474. return false;
  475. }
  476. public function GetVerb()
  477. {
  478. return $this->m_sVerb;
  479. }
  480. public function GetArgs()
  481. {
  482. return $this->m_aArgs;
  483. }
  484. // recursive rendering
  485. public function Render(&$aArgs = null, $bRetrofitParams = false)
  486. {
  487. $aRes = array();
  488. foreach ($this->m_aArgs as $oExpr)
  489. {
  490. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  491. }
  492. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  493. }
  494. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  495. {
  496. foreach ($this->m_aArgs as $oExpr)
  497. {
  498. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  499. }
  500. }
  501. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  502. {
  503. $aRes = array();
  504. foreach ($this->m_aArgs as $oExpr)
  505. {
  506. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  507. }
  508. return new FunctionExpression($this->m_sVerb, $aRes);
  509. }
  510. public function ListRequiredFields()
  511. {
  512. $aRes = array();
  513. foreach ($this->m_aArgs as $oExpr)
  514. {
  515. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  516. }
  517. return $aRes;
  518. }
  519. public function RenameParam($sOldName, $sNewName)
  520. {
  521. foreach ($this->m_aArgs as $key => $oExpr)
  522. {
  523. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  524. }
  525. }
  526. }
  527. class IntervalExpression extends Expression
  528. {
  529. protected $m_oValue; // expression
  530. protected $m_sUnit;
  531. public function __construct($oValue, $sUnit)
  532. {
  533. $this->m_oValue = $oValue;
  534. $this->m_sUnit = $sUnit;
  535. }
  536. public function IsTrue()
  537. {
  538. // return true if we are certain that it will be true
  539. return false;
  540. }
  541. public function GetValue()
  542. {
  543. return $this->m_oValue;
  544. }
  545. public function GetUnit()
  546. {
  547. return $this->m_sUnit;
  548. }
  549. // recursive rendering
  550. public function Render(&$aArgs = null, $bRetrofitParams = false)
  551. {
  552. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  553. }
  554. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  555. {
  556. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  557. }
  558. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  559. {
  560. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  561. }
  562. public function ListRequiredFields()
  563. {
  564. return array();
  565. }
  566. public function RenameParam($sOldName, $sNewName)
  567. {
  568. $this->m_oValue->RenameParam($sOldName, $sNewName);
  569. }
  570. }
  571. class CharConcatExpression extends Expression
  572. {
  573. protected $m_aExpressions;
  574. public function __construct($aExpressions)
  575. {
  576. $this->m_aExpressions = $aExpressions;
  577. }
  578. public function IsTrue()
  579. {
  580. // return true if we are certain that it will be true
  581. return false;
  582. }
  583. public function GetItems()
  584. {
  585. return $this->m_aExpressions;
  586. }
  587. // recursive rendering
  588. public function Render(&$aArgs = null, $bRetrofitParams = false)
  589. {
  590. $aRes = array();
  591. foreach ($this->m_aExpressions as $oExpr)
  592. {
  593. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  594. // Concat will be globally NULL if one single argument is null !
  595. $aRes[] = "COALESCE($sCol, '')";
  596. }
  597. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  598. }
  599. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  600. {
  601. foreach ($this->m_aExpressions as $oExpr)
  602. {
  603. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  604. }
  605. }
  606. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  607. {
  608. $aRes = array();
  609. foreach ($this->m_aExpressions as $oExpr)
  610. {
  611. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  612. }
  613. return new CharConcatExpression($aRes);
  614. }
  615. public function ListRequiredFields()
  616. {
  617. $aRes = array();
  618. foreach ($this->m_aExpressions as $oExpr)
  619. {
  620. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  621. }
  622. return $aRes;
  623. }
  624. public function RenameParam($sOldName, $sNewName)
  625. {
  626. foreach ($this->m_aExpressions as $key => $oExpr)
  627. {
  628. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  629. }
  630. }
  631. }
  632. class CharConcatWSExpression extends CharConcatExpression
  633. {
  634. protected $m_separator;
  635. public function __construct($separator, $aExpressions)
  636. {
  637. $this->m_separator = $separator;
  638. parent::__construct($aExpressions);
  639. }
  640. // recursive rendering
  641. public function Render(&$aArgs = null, $bRetrofitParams = false)
  642. {
  643. $aRes = array();
  644. foreach ($this->m_aExpressions as $oExpr)
  645. {
  646. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  647. // Concat will be globally NULL if one single argument is null !
  648. $aRes[] = "COALESCE($sCol, '')";
  649. }
  650. $sSep = CMDBSource::Quote($this->m_separator);
  651. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  652. }
  653. }
  654. class QueryBuilderExpressions
  655. {
  656. protected $m_oConditionExpr;
  657. protected $m_aSelectExpr;
  658. protected $m_aJoinFields;
  659. public function __construct($aSelect, $oCondition)
  660. {
  661. $this->m_oConditionExpr = $oCondition;
  662. $this->m_aSelectExpr = $aSelect;
  663. $this->m_aJoinFields = array();
  664. }
  665. public function GetSelect()
  666. {
  667. return $this->m_aSelectExpr;
  668. }
  669. public function GetCondition()
  670. {
  671. return $this->m_oConditionExpr;
  672. }
  673. public function PopJoinField()
  674. {
  675. return array_pop($this->m_aJoinFields);
  676. }
  677. public function AddSelect($sAttAlias, $oExpression)
  678. {
  679. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  680. }
  681. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  682. public function AddCondition($oExpression)
  683. {
  684. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  685. }
  686. public function PushJoinField($oExpression)
  687. {
  688. array_push($this->m_aJoinFields, $oExpression);
  689. }
  690. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  691. {
  692. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  693. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  694. {
  695. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  696. }
  697. foreach($this->m_aJoinFields as $oExpression)
  698. {
  699. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  700. }
  701. }
  702. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  703. {
  704. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  705. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  706. {
  707. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  708. }
  709. foreach($this->m_aJoinFields as $index => $oExpression)
  710. {
  711. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  712. }
  713. }
  714. public function RenameParam($sOldName, $sNewName)
  715. {
  716. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  717. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  718. {
  719. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  720. }
  721. foreach($this->m_aJoinFields as $index => $oExpression)
  722. {
  723. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  724. }
  725. }
  726. }
  727. ?>