expression.class.inc.php 23 KB

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