expression.class.inc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. if ($bRetrofitParams)
  240. {
  241. $iParamIndex = count($aArgs) + 1; // 1-based indexation
  242. $aArgs['param'.$iParamIndex] = $this->m_value;
  243. return ':param'.$iParamIndex;
  244. }
  245. else
  246. {
  247. return CMDBSource::Quote($this->m_value);
  248. }
  249. }
  250. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  251. {
  252. }
  253. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  254. {
  255. return clone $this;
  256. }
  257. public function ListRequiredFields()
  258. {
  259. return array();
  260. }
  261. public function ListConstantFields()
  262. {
  263. return array();
  264. }
  265. public function RenameParam($sOldName, $sNewName)
  266. {
  267. // Do nothing
  268. // really ? what about :param{$iParamIndex} ??
  269. }
  270. }
  271. class ScalarExpression extends UnaryExpression
  272. {
  273. public function __construct($value)
  274. {
  275. if (!is_scalar($value))
  276. {
  277. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  278. }
  279. parent::__construct($value);
  280. }
  281. }
  282. class TrueExpression extends ScalarExpression
  283. {
  284. public function __construct()
  285. {
  286. parent::__construct(1);
  287. }
  288. public function IsTrue()
  289. {
  290. return true;
  291. }
  292. }
  293. class FalseExpression extends ScalarExpression
  294. {
  295. public function __construct()
  296. {
  297. parent::__construct(0);
  298. }
  299. public function IsTrue()
  300. {
  301. return false;
  302. }
  303. }
  304. class FieldExpression extends UnaryExpression
  305. {
  306. protected $m_sParent;
  307. protected $m_sName;
  308. public function __construct($sName, $sParent = '')
  309. {
  310. parent::__construct("$sParent.$sName");
  311. $this->m_sParent = $sParent;
  312. $this->m_sName = $sName;
  313. }
  314. public function IsTrue()
  315. {
  316. // return true if we are certain that it will be true
  317. return false;
  318. }
  319. public function GetParent() {return $this->m_sParent;}
  320. public function GetName() {return $this->m_sName;}
  321. // recursive rendering
  322. public function Render(&$aArgs = null, $bRetrofitParams = false)
  323. {
  324. if (empty($this->m_sParent))
  325. {
  326. return "`{$this->m_sName}`";
  327. }
  328. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  329. }
  330. public function ListRequiredFields()
  331. {
  332. return array($this->m_sParent.'.'.$this->m_sName);
  333. }
  334. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  335. {
  336. if ($this->m_sParent == $sAlias)
  337. {
  338. // Add a reference to the field
  339. $aUnresolved[$this->m_sName] = $this;
  340. }
  341. }
  342. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  343. {
  344. if (!array_key_exists($this->m_sParent, $aTranslationData))
  345. {
  346. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  347. return clone $this;
  348. }
  349. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  350. {
  351. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  352. {
  353. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  354. 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])));
  355. return clone $this;
  356. }
  357. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  358. $sNewName = $this->m_sName;
  359. if ($bMarkFieldsAsResolved)
  360. {
  361. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  362. }
  363. else
  364. {
  365. $oRet = new FieldExpression($sNewName, $sNewParent);
  366. }
  367. }
  368. else
  369. {
  370. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  371. }
  372. return $oRet;
  373. }
  374. }
  375. // Has been resolved into an SQL expression
  376. class FieldExpressionResolved extends FieldExpression
  377. {
  378. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  379. {
  380. }
  381. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  382. {
  383. return clone $this;
  384. }
  385. }
  386. class VariableExpression extends UnaryExpression
  387. {
  388. protected $m_sName;
  389. public function __construct($sName)
  390. {
  391. parent::__construct($sName);
  392. $this->m_sName = $sName;
  393. }
  394. public function IsTrue()
  395. {
  396. // return true if we are certain that it will be true
  397. return false;
  398. }
  399. public function GetName() {return $this->m_sName;}
  400. // recursive rendering
  401. public function Render(&$aArgs = null, $bRetrofitParams = false)
  402. {
  403. if (is_null($aArgs))
  404. {
  405. return ':'.$this->m_sName;
  406. }
  407. elseif (array_key_exists($this->m_sName, $aArgs))
  408. {
  409. return CMDBSource::Quote($aArgs[$this->m_sName]);
  410. }
  411. elseif ($bRetrofitParams)
  412. {
  413. //$aArgs[$this->m_sName] = null;
  414. return ':'.$this->m_sName;
  415. }
  416. else
  417. {
  418. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
  419. }
  420. }
  421. public function RenameParam($sOldName, $sNewName)
  422. {
  423. if ($this->m_sName == $sOldName)
  424. {
  425. $this->m_sName = $sNewName;
  426. }
  427. }
  428. }
  429. // Temporary, until we implement functions and expression casting!
  430. // ... or until we implement a real full text search based in the MATCH() expression
  431. class ListExpression extends Expression
  432. {
  433. protected $m_aExpressions;
  434. public function __construct($aExpressions)
  435. {
  436. $this->m_aExpressions = $aExpressions;
  437. }
  438. public static function FromScalars($aScalars)
  439. {
  440. $aExpressions = array();
  441. foreach($aScalars as $value)
  442. {
  443. $aExpressions[] = new ScalarExpression($value);
  444. }
  445. return new ListExpression($aExpressions);
  446. }
  447. public function IsTrue()
  448. {
  449. // return true if we are certain that it will be true
  450. return false;
  451. }
  452. public function GetItems()
  453. {
  454. return $this->m_aExpressions;
  455. }
  456. // recursive rendering
  457. public function Render(&$aArgs = null, $bRetrofitParams = false)
  458. {
  459. $aRes = array();
  460. foreach ($this->m_aExpressions as $oExpr)
  461. {
  462. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  463. }
  464. return '('.implode(', ', $aRes).')';
  465. }
  466. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  467. {
  468. foreach ($this->m_aExpressions as $oExpr)
  469. {
  470. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  471. }
  472. }
  473. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  474. {
  475. $aRes = array();
  476. foreach ($this->m_aExpressions as $oExpr)
  477. {
  478. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  479. }
  480. return new ListExpression($aRes);
  481. }
  482. public function ListRequiredFields()
  483. {
  484. $aRes = array();
  485. foreach ($this->m_aExpressions as $oExpr)
  486. {
  487. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  488. }
  489. return $aRes;
  490. }
  491. public function ListConstantFields()
  492. {
  493. $aRes = array();
  494. foreach ($this->m_aExpressions as $oExpr)
  495. {
  496. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  497. }
  498. return $aRes;
  499. }
  500. public function RenameParam($sOldName, $sNewName)
  501. {
  502. $aRes = array();
  503. foreach ($this->m_aExpressions as $key => $oExpr)
  504. {
  505. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  506. }
  507. }
  508. }
  509. class FunctionExpression extends Expression
  510. {
  511. protected $m_sVerb;
  512. protected $m_aArgs; // array of expressions
  513. public function __construct($sVerb, $aArgExpressions)
  514. {
  515. $this->m_sVerb = $sVerb;
  516. $this->m_aArgs = $aArgExpressions;
  517. }
  518. public function IsTrue()
  519. {
  520. // return true if we are certain that it will be true
  521. return false;
  522. }
  523. public function GetVerb()
  524. {
  525. return $this->m_sVerb;
  526. }
  527. public function GetArgs()
  528. {
  529. return $this->m_aArgs;
  530. }
  531. // recursive rendering
  532. public function Render(&$aArgs = null, $bRetrofitParams = false)
  533. {
  534. $aRes = array();
  535. foreach ($this->m_aArgs as $oExpr)
  536. {
  537. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  538. }
  539. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  540. }
  541. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  542. {
  543. foreach ($this->m_aArgs as $oExpr)
  544. {
  545. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  546. }
  547. }
  548. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  549. {
  550. $aRes = array();
  551. foreach ($this->m_aArgs as $oExpr)
  552. {
  553. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  554. }
  555. return new FunctionExpression($this->m_sVerb, $aRes);
  556. }
  557. public function ListRequiredFields()
  558. {
  559. $aRes = array();
  560. foreach ($this->m_aArgs as $oExpr)
  561. {
  562. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  563. }
  564. return $aRes;
  565. }
  566. public function ListConstantFields()
  567. {
  568. $aRes = array();
  569. foreach ($this->m_aArgs as $oExpr)
  570. {
  571. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  572. }
  573. return $aRes;
  574. }
  575. public function RenameParam($sOldName, $sNewName)
  576. {
  577. foreach ($this->m_aArgs as $key => $oExpr)
  578. {
  579. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  580. }
  581. }
  582. }
  583. class IntervalExpression extends Expression
  584. {
  585. protected $m_oValue; // expression
  586. protected $m_sUnit;
  587. public function __construct($oValue, $sUnit)
  588. {
  589. $this->m_oValue = $oValue;
  590. $this->m_sUnit = $sUnit;
  591. }
  592. public function IsTrue()
  593. {
  594. // return true if we are certain that it will be true
  595. return false;
  596. }
  597. public function GetValue()
  598. {
  599. return $this->m_oValue;
  600. }
  601. public function GetUnit()
  602. {
  603. return $this->m_sUnit;
  604. }
  605. // recursive rendering
  606. public function Render(&$aArgs = null, $bRetrofitParams = false)
  607. {
  608. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  609. }
  610. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  611. {
  612. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  613. }
  614. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  615. {
  616. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  617. }
  618. public function ListRequiredFields()
  619. {
  620. return array();
  621. }
  622. public function ListConstantFields()
  623. {
  624. return array();
  625. }
  626. public function RenameParam($sOldName, $sNewName)
  627. {
  628. $this->m_oValue->RenameParam($sOldName, $sNewName);
  629. }
  630. }
  631. class CharConcatExpression extends Expression
  632. {
  633. protected $m_aExpressions;
  634. public function __construct($aExpressions)
  635. {
  636. $this->m_aExpressions = $aExpressions;
  637. }
  638. public function IsTrue()
  639. {
  640. // return true if we are certain that it will be true
  641. return false;
  642. }
  643. public function GetItems()
  644. {
  645. return $this->m_aExpressions;
  646. }
  647. // recursive rendering
  648. public function Render(&$aArgs = null, $bRetrofitParams = false)
  649. {
  650. $aRes = array();
  651. foreach ($this->m_aExpressions as $oExpr)
  652. {
  653. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  654. // Concat will be globally NULL if one single argument is null !
  655. $aRes[] = "COALESCE($sCol, '')";
  656. }
  657. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  658. }
  659. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  660. {
  661. foreach ($this->m_aExpressions as $oExpr)
  662. {
  663. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  664. }
  665. }
  666. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  667. {
  668. $aRes = array();
  669. foreach ($this->m_aExpressions as $oExpr)
  670. {
  671. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  672. }
  673. return new CharConcatExpression($aRes);
  674. }
  675. public function ListRequiredFields()
  676. {
  677. $aRes = array();
  678. foreach ($this->m_aExpressions as $oExpr)
  679. {
  680. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  681. }
  682. return $aRes;
  683. }
  684. public function ListConstantFields()
  685. {
  686. $aRes = array();
  687. foreach ($this->m_aExpressions as $oExpr)
  688. {
  689. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  690. }
  691. return $aRes;
  692. }
  693. public function RenameParam($sOldName, $sNewName)
  694. {
  695. foreach ($this->m_aExpressions as $key => $oExpr)
  696. {
  697. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  698. }
  699. }
  700. }
  701. class CharConcatWSExpression extends CharConcatExpression
  702. {
  703. protected $m_separator;
  704. public function __construct($separator, $aExpressions)
  705. {
  706. $this->m_separator = $separator;
  707. parent::__construct($aExpressions);
  708. }
  709. // recursive rendering
  710. public function Render(&$aArgs = null, $bRetrofitParams = false)
  711. {
  712. $aRes = array();
  713. foreach ($this->m_aExpressions as $oExpr)
  714. {
  715. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  716. // Concat will be globally NULL if one single argument is null !
  717. $aRes[] = "COALESCE($sCol, '')";
  718. }
  719. $sSep = CMDBSource::Quote($this->m_separator);
  720. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  721. }
  722. }
  723. class QueryBuilderExpressions
  724. {
  725. protected $m_oConditionExpr;
  726. protected $m_aSelectExpr;
  727. protected $m_aJoinFields;
  728. public function __construct($aSelect, $oCondition)
  729. {
  730. $this->m_oConditionExpr = $oCondition;
  731. $this->m_aSelectExpr = $aSelect;
  732. $this->m_aJoinFields = array();
  733. }
  734. public function GetSelect()
  735. {
  736. return $this->m_aSelectExpr;
  737. }
  738. public function GetCondition()
  739. {
  740. return $this->m_oConditionExpr;
  741. }
  742. public function PopJoinField()
  743. {
  744. return array_pop($this->m_aJoinFields);
  745. }
  746. public function AddSelect($sAttAlias, $oExpression)
  747. {
  748. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  749. }
  750. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  751. public function AddCondition($oExpression)
  752. {
  753. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  754. }
  755. public function PushJoinField($oExpression)
  756. {
  757. array_push($this->m_aJoinFields, $oExpression);
  758. }
  759. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  760. {
  761. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  762. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  763. {
  764. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  765. }
  766. foreach($this->m_aJoinFields as $oExpression)
  767. {
  768. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  769. }
  770. }
  771. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  772. {
  773. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  774. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  775. {
  776. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  777. }
  778. foreach($this->m_aJoinFields as $index => $oExpression)
  779. {
  780. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  781. }
  782. }
  783. public function RenameParam($sOldName, $sNewName)
  784. {
  785. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  786. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  787. {
  788. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  789. }
  790. foreach($this->m_aJoinFields as $index => $oExpression)
  791. {
  792. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  793. }
  794. }
  795. }
  796. ?>