expression.class.inc.php 27 KB

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