expression.class.inc.php 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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_aJoinFields;
  822. public function __construct($oCondition)
  823. {
  824. $this->m_oConditionExpr = $oCondition;
  825. $this->m_aSelectExpr = array();
  826. $this->m_aJoinFields = array();
  827. }
  828. public function GetSelect()
  829. {
  830. return $this->m_aSelectExpr;
  831. }
  832. public function GetCondition()
  833. {
  834. return $this->m_oConditionExpr;
  835. }
  836. public function PopJoinField()
  837. {
  838. return array_pop($this->m_aJoinFields);
  839. }
  840. public function AddSelect($sAttAlias, $oExpression)
  841. {
  842. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  843. }
  844. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  845. public function AddCondition($oExpression)
  846. {
  847. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  848. }
  849. public function PushJoinField($oExpression)
  850. {
  851. array_push($this->m_aJoinFields, $oExpression);
  852. }
  853. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  854. {
  855. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  856. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  857. {
  858. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  859. }
  860. foreach($this->m_aJoinFields as $oExpression)
  861. {
  862. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  863. }
  864. }
  865. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  866. {
  867. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  868. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  869. {
  870. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  871. }
  872. foreach($this->m_aJoinFields as $index => $oExpression)
  873. {
  874. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  875. }
  876. }
  877. public function RenameParam($sOldName, $sNewName)
  878. {
  879. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  880. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  881. {
  882. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  883. }
  884. foreach($this->m_aJoinFields as $index => $oExpression)
  885. {
  886. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  887. }
  888. }
  889. }
  890. ?>