expression.class.inc.php 26 KB

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