expression.class.inc.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * General definition of an expression tree (could be OQL, SQL or whatever)
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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. // recursively list field parents ($aTable = array of sParent => dummy)
  38. abstract public function CollectUsedParents(&$aTable);
  39. abstract public function IsTrue();
  40. // recursively builds an array of [classAlias][fieldName] => value
  41. abstract public function ListConstantFields();
  42. public function RequiresField($sClass, $sFieldName)
  43. {
  44. // #@# todo - optimize : this is called quite often when building a single query !
  45. $aRequired = $this->ListRequiredFields();
  46. if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false;
  47. return true;
  48. }
  49. public function serialize()
  50. {
  51. return base64_encode($this->Render());
  52. }
  53. static public function unserialize($sValue)
  54. {
  55. return self::FromOQL(base64_decode($sValue));
  56. }
  57. static public function FromOQL($sConditionExpr)
  58. {
  59. $oOql = new OqlInterpreter($sConditionExpr);
  60. $oExpression = $oOql->ParseExpression();
  61. return $oExpression;
  62. }
  63. static public function FromSQL($sSQL)
  64. {
  65. $oSql = new SQLExpression($sSQL);
  66. return $oSql;
  67. }
  68. public function LogAnd($oExpr)
  69. {
  70. if ($this->IsTrue()) return clone $oExpr;
  71. if ($oExpr->IsTrue()) return clone $this;
  72. return new BinaryExpression($this, 'AND', $oExpr);
  73. }
  74. public function LogOr($oExpr)
  75. {
  76. return new BinaryExpression($this, 'OR', $oExpr);
  77. }
  78. abstract public function RenameParam($sOldName, $sNewName);
  79. /**
  80. * Make the most relevant label, given the value of the expression
  81. *
  82. * @param DBObjectSearch oFilter The context in which this expression has been used
  83. * @param string sValue The value returned by the query, for this expression
  84. * @param string sDefault The default value if no relevant label could be computed
  85. * @return The label
  86. */
  87. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  88. {
  89. return $sDefault;
  90. }
  91. }
  92. class SQLExpression extends Expression
  93. {
  94. protected $m_sSQL;
  95. public function __construct($sSQL)
  96. {
  97. $this->m_sSQL = $sSQL;
  98. }
  99. public function IsTrue()
  100. {
  101. return false;
  102. }
  103. // recursive rendering
  104. public function Render(&$aArgs = null, $bRetrofitParams = false)
  105. {
  106. return $this->m_sSQL;
  107. }
  108. public function ApplyParameters($aArgs)
  109. {
  110. }
  111. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  112. {
  113. }
  114. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  115. {
  116. return clone $this;
  117. }
  118. public function ListRequiredFields()
  119. {
  120. return array();
  121. }
  122. public function CollectUsedParents(&$aTable)
  123. {
  124. }
  125. public function ListConstantFields()
  126. {
  127. return array();
  128. }
  129. public function RenameParam($sOldName, $sNewName)
  130. {
  131. // Do nothing, since there is nothing to rename
  132. }
  133. }
  134. class BinaryExpression extends Expression
  135. {
  136. protected $m_oLeftExpr; // filter code or an SQL expression (later?)
  137. protected $m_oRightExpr;
  138. protected $m_sOperator;
  139. public function __construct($oLeftExpr, $sOperator, $oRightExpr)
  140. {
  141. if (!is_object($oLeftExpr))
  142. {
  143. throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr)));
  144. }
  145. if (!is_object($oRightExpr))
  146. {
  147. throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr)));
  148. }
  149. if (!$oLeftExpr instanceof Expression)
  150. {
  151. throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr)));
  152. }
  153. if (!$oRightExpr instanceof Expression)
  154. {
  155. throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr)));
  156. }
  157. $this->m_oLeftExpr = $oLeftExpr;
  158. $this->m_oRightExpr = $oRightExpr;
  159. $this->m_sOperator = $sOperator;
  160. }
  161. public function IsTrue()
  162. {
  163. // return true if we are certain that it will be true
  164. if ($this->m_sOperator == 'AND')
  165. {
  166. if ($this->m_oLeftExpr->IsTrue() && $this->m_oRightExpr->IsTrue()) return true;
  167. }
  168. return false;
  169. }
  170. public function GetLeftExpr()
  171. {
  172. return $this->m_oLeftExpr;
  173. }
  174. public function GetRightExpr()
  175. {
  176. return $this->m_oRightExpr;
  177. }
  178. public function GetOperator()
  179. {
  180. return $this->m_sOperator;
  181. }
  182. // recursive rendering
  183. public function Render(&$aArgs = null, $bRetrofitParams = false)
  184. {
  185. $sOperator = $this->GetOperator();
  186. $sLeft = $this->GetLeftExpr()->Render($aArgs, $bRetrofitParams);
  187. $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams);
  188. return "($sLeft $sOperator $sRight)";
  189. }
  190. public function ApplyParameters($aArgs)
  191. {
  192. if ($this->m_oLeftExpr instanceof VariableExpression)
  193. {
  194. $this->m_oLeftExpr = $this->m_oLeftExpr->GetAsScalar($aArgs);
  195. }
  196. else //if ($this->m_oLeftExpr instanceof Expression)
  197. {
  198. $this->m_oLeftExpr->ApplyParameters($aArgs);
  199. }
  200. if ($this->m_oRightExpr instanceof VariableExpression)
  201. {
  202. $this->m_oRightExpr = $this->m_oRightExpr->GetAsScalar($aArgs);
  203. }
  204. else //if ($this->m_oRightExpr instanceof Expression)
  205. {
  206. $this->m_oRightExpr->ApplyParameters($aArgs);
  207. }
  208. }
  209. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  210. {
  211. $this->GetLeftExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  212. $this->GetRightExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  213. }
  214. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  215. {
  216. $oLeft = $this->GetLeftExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  217. $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  218. return new BinaryExpression($oLeft, $this->GetOperator(), $oRight);
  219. }
  220. public function ListRequiredFields()
  221. {
  222. $aLeft = $this->GetLeftExpr()->ListRequiredFields();
  223. $aRight = $this->GetRightExpr()->ListRequiredFields();
  224. return array_merge($aLeft, $aRight);
  225. }
  226. public function CollectUsedParents(&$aTable)
  227. {
  228. $this->GetLeftExpr()->CollectUsedParents($aTable);
  229. $this->GetRightExpr()->CollectUsedParents($aTable);
  230. }
  231. /**
  232. * List all constant expression of the form <field> = <scalar> or <field> = :<variable>
  233. * Could be extended to support <field> = <function><constant_expression>
  234. */
  235. public function ListConstantFields()
  236. {
  237. $aResult = array();
  238. if ($this->m_sOperator == '=')
  239. {
  240. if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof ScalarExpression))
  241. {
  242. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  243. }
  244. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof ScalarExpression))
  245. {
  246. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  247. }
  248. else if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof VariableExpression))
  249. {
  250. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  251. }
  252. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof VariableExpression))
  253. {
  254. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  255. }
  256. else
  257. {
  258. $aResult = array_merge($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields()) ;
  259. }
  260. }
  261. else
  262. {
  263. $aResult = array_merge($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields()) ;
  264. }
  265. return $aResult;
  266. }
  267. public function RenameParam($sOldName, $sNewName)
  268. {
  269. $this->GetLeftExpr()->RenameParam($sOldName, $sNewName);
  270. $this->GetRightExpr()->RenameParam($sOldName, $sNewName);
  271. }
  272. }
  273. class UnaryExpression extends Expression
  274. {
  275. protected $m_value;
  276. public function __construct($value)
  277. {
  278. $this->m_value = $value;
  279. }
  280. public function IsTrue()
  281. {
  282. // return true if we are certain that it will be true
  283. return ($this->m_value == 1);
  284. }
  285. public function GetValue()
  286. {
  287. return $this->m_value;
  288. }
  289. // recursive rendering
  290. public function Render(&$aArgs = null, $bRetrofitParams = false)
  291. {
  292. return CMDBSource::Quote($this->m_value);
  293. }
  294. public function ApplyParameters($aArgs)
  295. {
  296. }
  297. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  298. {
  299. }
  300. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  301. {
  302. return clone $this;
  303. }
  304. public function ListRequiredFields()
  305. {
  306. return array();
  307. }
  308. public function CollectUsedParents(&$aTable)
  309. {
  310. }
  311. public function ListConstantFields()
  312. {
  313. return array();
  314. }
  315. public function RenameParam($sOldName, $sNewName)
  316. {
  317. // Do nothing
  318. // really ? what about :param{$iParamIndex} ??
  319. }
  320. }
  321. class ScalarExpression extends UnaryExpression
  322. {
  323. public function __construct($value)
  324. {
  325. if (!is_scalar($value) && !is_null($value))
  326. {
  327. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  328. }
  329. parent::__construct($value);
  330. }
  331. // recursive rendering
  332. public function Render(&$aArgs = null, $bRetrofitParams = false)
  333. {
  334. if (is_null($this->m_value))
  335. {
  336. $sRet = 'NULL';
  337. }
  338. else
  339. {
  340. $sRet = CMDBSource::Quote($this->m_value);
  341. }
  342. return $sRet;
  343. }
  344. }
  345. class TrueExpression extends ScalarExpression
  346. {
  347. public function __construct()
  348. {
  349. parent::__construct(1);
  350. }
  351. public function IsTrue()
  352. {
  353. return true;
  354. }
  355. }
  356. class FalseExpression extends ScalarExpression
  357. {
  358. public function __construct()
  359. {
  360. parent::__construct(0);
  361. }
  362. public function IsTrue()
  363. {
  364. return false;
  365. }
  366. }
  367. class FieldExpression extends UnaryExpression
  368. {
  369. protected $m_sParent;
  370. protected $m_sName;
  371. public function __construct($sName, $sParent = '')
  372. {
  373. parent::__construct("$sParent.$sName");
  374. $this->m_sParent = $sParent;
  375. $this->m_sName = $sName;
  376. }
  377. public function IsTrue()
  378. {
  379. // return true if we are certain that it will be true
  380. return false;
  381. }
  382. public function GetParent() {return $this->m_sParent;}
  383. public function GetName() {return $this->m_sName;}
  384. // recursive rendering
  385. public function Render(&$aArgs = null, $bRetrofitParams = false)
  386. {
  387. if (empty($this->m_sParent))
  388. {
  389. return "`{$this->m_sName}`";
  390. }
  391. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  392. }
  393. public function ListRequiredFields()
  394. {
  395. return array($this->m_sParent.'.'.$this->m_sName);
  396. }
  397. public function CollectUsedParents(&$aTable)
  398. {
  399. $aTable[$this->m_sParent] = true;
  400. }
  401. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  402. {
  403. if ($this->m_sParent == $sAlias)
  404. {
  405. // Add a reference to the field
  406. $aUnresolved[$this->m_sName] = $this;
  407. }
  408. elseif ($sAlias == '')
  409. {
  410. // An empty alias means "any alias"
  411. // In such a case, the results are indexed differently
  412. $aUnresolved[$this->m_sParent][$this->m_sName] = $this;
  413. }
  414. }
  415. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  416. {
  417. if (!array_key_exists($this->m_sParent, $aTranslationData))
  418. {
  419. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  420. return clone $this;
  421. }
  422. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  423. {
  424. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  425. {
  426. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  427. 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])));
  428. return clone $this;
  429. }
  430. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  431. $sNewName = $this->m_sName;
  432. if ($bMarkFieldsAsResolved)
  433. {
  434. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  435. }
  436. else
  437. {
  438. $oRet = new FieldExpression($sNewName, $sNewParent);
  439. }
  440. }
  441. else
  442. {
  443. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  444. }
  445. return $oRet;
  446. }
  447. /**
  448. * Make the most relevant label, given the value of the expression
  449. *
  450. * @param DBObjectSearch oFilter The context in which this expression has been used
  451. * @param string sValue The value returned by the query, for this expression
  452. * @param string sDefault The default value if no relevant label could be computed
  453. * @return The label
  454. */
  455. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  456. {
  457. $sAttCode = $this->GetName();
  458. $sParentAlias = $this->GetParent();
  459. $aSelectedClasses = $oFilter->GetSelectedClasses();
  460. $sClass = $aSelectedClasses[$sParentAlias];
  461. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  462. // Set a default value for the general case
  463. $sRes = $oAttDef->GetAsHtml($sValue);
  464. // Exceptions...
  465. if ($oAttDef->IsExternalKey())
  466. {
  467. $sObjClass = $oAttDef->GetTargetClass();
  468. $iObjKey = (int)$sValue;
  469. if ($iObjKey > 0)
  470. {
  471. $oObject = MetaModel::GetObject($sObjClass, $iObjKey);
  472. $sRes = $oObject->GetHyperlink();
  473. }
  474. else
  475. {
  476. // Undefined
  477. $sRes = DBObject::MakeHyperLink($sObjClass, 0);
  478. }
  479. }
  480. elseif ($oAttDef->IsExternalField())
  481. {
  482. if (is_null($sValue))
  483. {
  484. $sRes = Dict::S('UI:UndefinedObject');
  485. }
  486. }
  487. return $sRes;
  488. }
  489. }
  490. // Has been resolved into an SQL expression
  491. class FieldExpressionResolved extends FieldExpression
  492. {
  493. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  494. {
  495. }
  496. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  497. {
  498. return clone $this;
  499. }
  500. }
  501. class VariableExpression extends UnaryExpression
  502. {
  503. protected $m_sName;
  504. public function __construct($sName)
  505. {
  506. parent::__construct($sName);
  507. $this->m_sName = $sName;
  508. }
  509. public function IsTrue()
  510. {
  511. // return true if we are certain that it will be true
  512. return false;
  513. }
  514. public function GetName() {return $this->m_sName;}
  515. // recursive rendering
  516. public function Render(&$aArgs = null, $bRetrofitParams = false)
  517. {
  518. if (is_null($aArgs))
  519. {
  520. return ':'.$this->m_sName;
  521. }
  522. elseif (array_key_exists($this->m_sName, $aArgs))
  523. {
  524. return CMDBSource::Quote($aArgs[$this->m_sName]);
  525. }
  526. elseif ($bRetrofitParams)
  527. {
  528. $aArgs[$this->m_sName] = null;
  529. return ':'.$this->m_sName;
  530. }
  531. else
  532. {
  533. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
  534. }
  535. }
  536. public function RenameParam($sOldName, $sNewName)
  537. {
  538. if ($this->m_sName == $sOldName)
  539. {
  540. $this->m_sName = $sNewName;
  541. }
  542. }
  543. public function GetAsScalar($aArgs)
  544. {
  545. $value = '';
  546. if (array_key_exists($this->m_sName, $aArgs))
  547. {
  548. $value = $aArgs[$this->m_sName];
  549. }
  550. else
  551. {
  552. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  553. }
  554. return new ScalarExpression($value);
  555. }
  556. }
  557. // Temporary, until we implement functions and expression casting!
  558. // ... or until we implement a real full text search based in the MATCH() expression
  559. class ListExpression extends Expression
  560. {
  561. protected $m_aExpressions;
  562. public function __construct($aExpressions)
  563. {
  564. $this->m_aExpressions = $aExpressions;
  565. }
  566. public static function FromScalars($aScalars)
  567. {
  568. $aExpressions = array();
  569. foreach($aScalars as $value)
  570. {
  571. $aExpressions[] = new ScalarExpression($value);
  572. }
  573. return new ListExpression($aExpressions);
  574. }
  575. public function IsTrue()
  576. {
  577. // return true if we are certain that it will be true
  578. return false;
  579. }
  580. public function GetItems()
  581. {
  582. return $this->m_aExpressions;
  583. }
  584. // recursive rendering
  585. public function Render(&$aArgs = null, $bRetrofitParams = false)
  586. {
  587. $aRes = array();
  588. foreach ($this->m_aExpressions as $oExpr)
  589. {
  590. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  591. }
  592. return '('.implode(', ', $aRes).')';
  593. }
  594. public function ApplyParameters($aArgs)
  595. {
  596. $aRes = array();
  597. foreach ($this->m_aExpressions as $idx => $oExpr)
  598. {
  599. if ($oExpr instanceof VariableExpression)
  600. {
  601. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  602. }
  603. else
  604. {
  605. $oExpr->ApplyParameters($aArgs);
  606. }
  607. }
  608. }
  609. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  610. {
  611. foreach ($this->m_aExpressions 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_aExpressions as $oExpr)
  620. {
  621. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  622. }
  623. return new ListExpression($aRes);
  624. }
  625. public function ListRequiredFields()
  626. {
  627. $aRes = array();
  628. foreach ($this->m_aExpressions as $oExpr)
  629. {
  630. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  631. }
  632. return $aRes;
  633. }
  634. public function CollectUsedParents(&$aTable)
  635. {
  636. foreach ($this->m_aExpressions as $oExpr)
  637. {
  638. $oExpr->CollectUsedParents($aTable);
  639. }
  640. }
  641. public function ListConstantFields()
  642. {
  643. $aRes = array();
  644. foreach ($this->m_aExpressions as $oExpr)
  645. {
  646. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  647. }
  648. return $aRes;
  649. }
  650. public function RenameParam($sOldName, $sNewName)
  651. {
  652. $aRes = array();
  653. foreach ($this->m_aExpressions as $key => $oExpr)
  654. {
  655. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  656. }
  657. }
  658. }
  659. class FunctionExpression extends Expression
  660. {
  661. protected $m_sVerb;
  662. protected $m_aArgs; // array of expressions
  663. public function __construct($sVerb, $aArgExpressions)
  664. {
  665. $this->m_sVerb = $sVerb;
  666. $this->m_aArgs = $aArgExpressions;
  667. }
  668. public function IsTrue()
  669. {
  670. // return true if we are certain that it will be true
  671. return false;
  672. }
  673. public function GetVerb()
  674. {
  675. return $this->m_sVerb;
  676. }
  677. public function GetArgs()
  678. {
  679. return $this->m_aArgs;
  680. }
  681. // recursive rendering
  682. public function Render(&$aArgs = null, $bRetrofitParams = false)
  683. {
  684. $aRes = array();
  685. foreach ($this->m_aArgs as $oExpr)
  686. {
  687. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  688. }
  689. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  690. }
  691. public function ApplyParameters($aArgs)
  692. {
  693. $aRes = array();
  694. foreach ($this->m_aArgs as $idx => $oExpr)
  695. {
  696. if ($oExpr instanceof VariableExpression)
  697. {
  698. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  699. }
  700. else
  701. {
  702. $oExpr->ApplyParameters($aArgs);
  703. }
  704. }
  705. }
  706. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  707. {
  708. foreach ($this->m_aArgs as $oExpr)
  709. {
  710. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  711. }
  712. }
  713. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  714. {
  715. $aRes = array();
  716. foreach ($this->m_aArgs as $oExpr)
  717. {
  718. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  719. }
  720. return new FunctionExpression($this->m_sVerb, $aRes);
  721. }
  722. public function ListRequiredFields()
  723. {
  724. $aRes = array();
  725. foreach ($this->m_aArgs as $oExpr)
  726. {
  727. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  728. }
  729. return $aRes;
  730. }
  731. public function CollectUsedParents(&$aTable)
  732. {
  733. foreach ($this->m_aArgs as $oExpr)
  734. {
  735. $oExpr->CollectUsedParents($aTable);
  736. }
  737. }
  738. public function ListConstantFields()
  739. {
  740. $aRes = array();
  741. foreach ($this->m_aArgs as $oExpr)
  742. {
  743. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  744. }
  745. return $aRes;
  746. }
  747. public function RenameParam($sOldName, $sNewName)
  748. {
  749. foreach ($this->m_aArgs as $key => $oExpr)
  750. {
  751. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  752. }
  753. }
  754. /**
  755. * Make the most relevant label, given the value of the expression
  756. *
  757. * @param DBObjectSearch oFilter The context in which this expression has been used
  758. * @param string sValue The value returned by the query, for this expression
  759. * @param string sDefault The default value if no relevant label could be computed
  760. * @return The label
  761. */
  762. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  763. {
  764. $sRes = $sDefault;
  765. if (strtolower($this->m_sVerb) == 'date_format')
  766. {
  767. $oFormatExpr = $this->m_aArgs[1];
  768. if ($oFormatExpr->Render() == "'%w'")
  769. {
  770. static $aWeekDayToString = null;
  771. if (is_null($aWeekDayToString))
  772. {
  773. // Init the correspondance table
  774. $aWeekDayToString = array(
  775. 0 => Dict::S('DayOfWeek-Sunday'),
  776. 1 => Dict::S('DayOfWeek-Monday'),
  777. 2 => Dict::S('DayOfWeek-Tuesday'),
  778. 3 => Dict::S('DayOfWeek-Wednesday'),
  779. 4 => Dict::S('DayOfWeek-Thursday'),
  780. 5 => Dict::S('DayOfWeek-Friday'),
  781. 6 => Dict::S('DayOfWeek-Saturday')
  782. );
  783. }
  784. if (isset($aWeekDayToString[(int)$sValue]))
  785. {
  786. $sRes = $aWeekDayToString[(int)$sValue];
  787. }
  788. }
  789. }
  790. return $sRes;
  791. }
  792. }
  793. class IntervalExpression extends Expression
  794. {
  795. protected $m_oValue; // expression
  796. protected $m_sUnit;
  797. public function __construct($oValue, $sUnit)
  798. {
  799. $this->m_oValue = $oValue;
  800. $this->m_sUnit = $sUnit;
  801. }
  802. public function IsTrue()
  803. {
  804. // return true if we are certain that it will be true
  805. return false;
  806. }
  807. public function GetValue()
  808. {
  809. return $this->m_oValue;
  810. }
  811. public function GetUnit()
  812. {
  813. return $this->m_sUnit;
  814. }
  815. // recursive rendering
  816. public function Render(&$aArgs = null, $bRetrofitParams = false)
  817. {
  818. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  819. }
  820. public function ApplyParameters($aArgs)
  821. {
  822. if ($this->m_oValue instanceof VariableExpression)
  823. {
  824. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  825. }
  826. else
  827. {
  828. $this->m_oValue->ApplyParameters($aArgs);
  829. }
  830. }
  831. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  832. {
  833. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  834. }
  835. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  836. {
  837. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  838. }
  839. public function ListRequiredFields()
  840. {
  841. return array();
  842. }
  843. public function CollectUsedParents(&$aTable)
  844. {
  845. }
  846. public function ListConstantFields()
  847. {
  848. return array();
  849. }
  850. public function RenameParam($sOldName, $sNewName)
  851. {
  852. $this->m_oValue->RenameParam($sOldName, $sNewName);
  853. }
  854. }
  855. class CharConcatExpression extends Expression
  856. {
  857. protected $m_aExpressions;
  858. public function __construct($aExpressions)
  859. {
  860. $this->m_aExpressions = $aExpressions;
  861. }
  862. public function IsTrue()
  863. {
  864. // return true if we are certain that it will be true
  865. return false;
  866. }
  867. public function GetItems()
  868. {
  869. return $this->m_aExpressions;
  870. }
  871. // recursive rendering
  872. public function Render(&$aArgs = null, $bRetrofitParams = false)
  873. {
  874. $aRes = array();
  875. foreach ($this->m_aExpressions as $oExpr)
  876. {
  877. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  878. // Concat will be globally NULL if one single argument is null !
  879. $aRes[] = "COALESCE($sCol, '')";
  880. }
  881. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  882. }
  883. public function ApplyParameters($aArgs)
  884. {
  885. $aRes = array();
  886. foreach ($this->m_aExpressions as $idx => $oExpr)
  887. {
  888. if ($oExpr instanceof VariableExpression)
  889. {
  890. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  891. }
  892. else
  893. {
  894. $this->m_aExpressions->ApplyParameters($aArgs);
  895. }
  896. }
  897. }
  898. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  899. {
  900. foreach ($this->m_aExpressions as $oExpr)
  901. {
  902. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  903. }
  904. }
  905. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  906. {
  907. $aRes = array();
  908. foreach ($this->m_aExpressions as $oExpr)
  909. {
  910. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  911. }
  912. return new CharConcatExpression($aRes);
  913. }
  914. public function ListRequiredFields()
  915. {
  916. $aRes = array();
  917. foreach ($this->m_aExpressions as $oExpr)
  918. {
  919. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  920. }
  921. return $aRes;
  922. }
  923. public function CollectUsedParents(&$aTable)
  924. {
  925. foreach ($this->m_aExpressions as $oExpr)
  926. {
  927. $oExpr->CollectUsedParents($aTable);
  928. }
  929. }
  930. public function ListConstantFields()
  931. {
  932. $aRes = array();
  933. foreach ($this->m_aExpressions as $oExpr)
  934. {
  935. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  936. }
  937. return $aRes;
  938. }
  939. public function RenameParam($sOldName, $sNewName)
  940. {
  941. foreach ($this->m_aExpressions as $key => $oExpr)
  942. {
  943. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  944. }
  945. }
  946. }
  947. class CharConcatWSExpression extends CharConcatExpression
  948. {
  949. protected $m_separator;
  950. public function __construct($separator, $aExpressions)
  951. {
  952. $this->m_separator = $separator;
  953. parent::__construct($aExpressions);
  954. }
  955. // recursive rendering
  956. public function Render(&$aArgs = null, $bRetrofitParams = false)
  957. {
  958. $aRes = array();
  959. foreach ($this->m_aExpressions as $oExpr)
  960. {
  961. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  962. // Concat will be globally NULL if one single argument is null !
  963. $aRes[] = "COALESCE($sCol, '')";
  964. }
  965. $sSep = CMDBSource::Quote($this->m_separator);
  966. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  967. }
  968. }
  969. class QueryBuilderExpressions
  970. {
  971. protected $m_oConditionExpr;
  972. protected $m_aSelectExpr;
  973. protected $m_aGroupByExpr;
  974. protected $m_aJoinFields;
  975. public function __construct($oCondition, $aGroupByExpr = null)
  976. {
  977. $this->m_oConditionExpr = $oCondition;
  978. $this->m_aSelectExpr = array();
  979. $this->m_aGroupByExpr = $aGroupByExpr;
  980. $this->m_aJoinFields = array();
  981. }
  982. public function GetSelect()
  983. {
  984. return $this->m_aSelectExpr;
  985. }
  986. public function GetGroupBy()
  987. {
  988. return $this->m_aGroupByExpr;
  989. }
  990. public function GetCondition()
  991. {
  992. return $this->m_oConditionExpr;
  993. }
  994. public function PopJoinField()
  995. {
  996. return array_pop($this->m_aJoinFields);
  997. }
  998. public function AddSelect($sAttAlias, $oExpression)
  999. {
  1000. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  1001. }
  1002. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  1003. public function AddCondition($oExpression)
  1004. {
  1005. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  1006. }
  1007. public function PushJoinField($oExpression)
  1008. {
  1009. array_push($this->m_aJoinFields, $oExpression);
  1010. }
  1011. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1012. {
  1013. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1014. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1015. {
  1016. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1017. }
  1018. if ($this->m_aGroupByExpr)
  1019. {
  1020. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1021. {
  1022. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1023. }
  1024. }
  1025. foreach($this->m_aJoinFields as $oExpression)
  1026. {
  1027. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  1028. }
  1029. }
  1030. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1031. {
  1032. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1033. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1034. {
  1035. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1036. }
  1037. if ($this->m_aGroupByExpr)
  1038. {
  1039. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1040. {
  1041. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1042. }
  1043. }
  1044. foreach($this->m_aJoinFields as $index => $oExpression)
  1045. {
  1046. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1047. }
  1048. }
  1049. public function RenameParam($sOldName, $sNewName)
  1050. {
  1051. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  1052. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1053. {
  1054. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1055. }
  1056. if ($this->m_aGroupByExpr)
  1057. {
  1058. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1059. {
  1060. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1061. }
  1062. }
  1063. foreach($this->m_aJoinFields as $index => $oExpression)
  1064. {
  1065. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  1066. }
  1067. }
  1068. }
  1069. ?>