expression.class.inc.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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) && !is_null($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. // recursive rendering
  319. public function Render(&$aArgs = null, $bRetrofitParams = false)
  320. {
  321. if (is_null($this->m_value))
  322. {
  323. $sRet = 'NULL';
  324. }
  325. else
  326. {
  327. $sRet = CMDBSource::Quote($this->m_value);
  328. }
  329. return $sRet;
  330. }
  331. }
  332. class TrueExpression extends ScalarExpression
  333. {
  334. public function __construct()
  335. {
  336. parent::__construct(1);
  337. }
  338. public function IsTrue()
  339. {
  340. return true;
  341. }
  342. }
  343. class FalseExpression extends ScalarExpression
  344. {
  345. public function __construct()
  346. {
  347. parent::__construct(0);
  348. }
  349. public function IsTrue()
  350. {
  351. return false;
  352. }
  353. }
  354. class FieldExpression extends UnaryExpression
  355. {
  356. protected $m_sParent;
  357. protected $m_sName;
  358. public function __construct($sName, $sParent = '')
  359. {
  360. parent::__construct("$sParent.$sName");
  361. $this->m_sParent = $sParent;
  362. $this->m_sName = $sName;
  363. }
  364. public function IsTrue()
  365. {
  366. // return true if we are certain that it will be true
  367. return false;
  368. }
  369. public function GetParent() {return $this->m_sParent;}
  370. public function GetName() {return $this->m_sName;}
  371. // recursive rendering
  372. public function Render(&$aArgs = null, $bRetrofitParams = false)
  373. {
  374. if (empty($this->m_sParent))
  375. {
  376. return "`{$this->m_sName}`";
  377. }
  378. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  379. }
  380. public function ListRequiredFields()
  381. {
  382. return array($this->m_sParent.'.'.$this->m_sName);
  383. }
  384. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  385. {
  386. if ($this->m_sParent == $sAlias)
  387. {
  388. // Add a reference to the field
  389. $aUnresolved[$this->m_sName] = $this;
  390. }
  391. }
  392. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  393. {
  394. if (!array_key_exists($this->m_sParent, $aTranslationData))
  395. {
  396. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  397. return clone $this;
  398. }
  399. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  400. {
  401. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  402. {
  403. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  404. 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])));
  405. return clone $this;
  406. }
  407. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  408. $sNewName = $this->m_sName;
  409. if ($bMarkFieldsAsResolved)
  410. {
  411. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  412. }
  413. else
  414. {
  415. $oRet = new FieldExpression($sNewName, $sNewParent);
  416. }
  417. }
  418. else
  419. {
  420. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  421. }
  422. return $oRet;
  423. }
  424. /**
  425. * Make the most relevant label, given the value of the expression
  426. *
  427. * @param DBObjectSearch oFilter The context in which this expression has been used
  428. * @param string sValue The value returned by the query, for this expression
  429. * @param string sDefault The default value if no relevant label could be computed
  430. * @return The label
  431. */
  432. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  433. {
  434. $sAttCode = $this->GetName();
  435. $sParentAlias = $this->GetParent();
  436. $aSelectedClasses = $oFilter->GetSelectedClasses();
  437. $sClass = $aSelectedClasses[$sParentAlias];
  438. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  439. // Set a default value for the general case
  440. $sRes = $oAttDef->GetAsHtml($sValue);
  441. // Exceptions...
  442. if ($oAttDef->IsExternalKey())
  443. {
  444. $sObjClass = $oAttDef->GetTargetClass();
  445. $iObjKey = (int)$sValue;
  446. if ($iObjKey > 0)
  447. {
  448. $oObject = MetaModel::GetObject($sObjClass, $iObjKey);
  449. $sRes = $oObject->GetHyperlink();
  450. }
  451. else
  452. {
  453. // Undefined
  454. $sRes = DBObject::MakeHyperLink($sObjClass, 0);
  455. }
  456. }
  457. elseif ($oAttDef->IsExternalField())
  458. {
  459. if (is_null($sValue))
  460. {
  461. $sRes = Dict::S('UI:UndefinedObject');
  462. }
  463. }
  464. return $sRes;
  465. }
  466. }
  467. // Has been resolved into an SQL expression
  468. class FieldExpressionResolved extends FieldExpression
  469. {
  470. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  471. {
  472. }
  473. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  474. {
  475. return clone $this;
  476. }
  477. }
  478. class VariableExpression extends UnaryExpression
  479. {
  480. protected $m_sName;
  481. public function __construct($sName)
  482. {
  483. parent::__construct($sName);
  484. $this->m_sName = $sName;
  485. }
  486. public function IsTrue()
  487. {
  488. // return true if we are certain that it will be true
  489. return false;
  490. }
  491. public function GetName() {return $this->m_sName;}
  492. // recursive rendering
  493. public function Render(&$aArgs = null, $bRetrofitParams = false)
  494. {
  495. if (is_null($aArgs))
  496. {
  497. return ':'.$this->m_sName;
  498. }
  499. elseif (array_key_exists($this->m_sName, $aArgs))
  500. {
  501. return CMDBSource::Quote($aArgs[$this->m_sName]);
  502. }
  503. elseif ($bRetrofitParams)
  504. {
  505. $aArgs[$this->m_sName] = null;
  506. return ':'.$this->m_sName;
  507. }
  508. else
  509. {
  510. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
  511. }
  512. }
  513. public function RenameParam($sOldName, $sNewName)
  514. {
  515. if ($this->m_sName == $sOldName)
  516. {
  517. $this->m_sName = $sNewName;
  518. }
  519. }
  520. public function GetAsScalar($aArgs)
  521. {
  522. $value = '';
  523. if (array_key_exists($this->m_sName, $aArgs))
  524. {
  525. $value = $aArgs[$this->m_sName];
  526. }
  527. else
  528. {
  529. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  530. }
  531. return new ScalarExpression($value);
  532. }
  533. }
  534. // Temporary, until we implement functions and expression casting!
  535. // ... or until we implement a real full text search based in the MATCH() expression
  536. class ListExpression extends Expression
  537. {
  538. protected $m_aExpressions;
  539. public function __construct($aExpressions)
  540. {
  541. $this->m_aExpressions = $aExpressions;
  542. }
  543. public static function FromScalars($aScalars)
  544. {
  545. $aExpressions = array();
  546. foreach($aScalars as $value)
  547. {
  548. $aExpressions[] = new ScalarExpression($value);
  549. }
  550. return new ListExpression($aExpressions);
  551. }
  552. public function IsTrue()
  553. {
  554. // return true if we are certain that it will be true
  555. return false;
  556. }
  557. public function GetItems()
  558. {
  559. return $this->m_aExpressions;
  560. }
  561. // recursive rendering
  562. public function Render(&$aArgs = null, $bRetrofitParams = false)
  563. {
  564. $aRes = array();
  565. foreach ($this->m_aExpressions as $oExpr)
  566. {
  567. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  568. }
  569. return '('.implode(', ', $aRes).')';
  570. }
  571. public function ApplyParameters($aArgs)
  572. {
  573. $aRes = array();
  574. foreach ($this->m_aExpressions as $idx => $oExpr)
  575. {
  576. if ($oExpr instanceof VariableExpression)
  577. {
  578. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  579. }
  580. else
  581. {
  582. $oExpr->ApplyParameters($aArgs);
  583. }
  584. }
  585. }
  586. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  587. {
  588. foreach ($this->m_aExpressions as $oExpr)
  589. {
  590. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  591. }
  592. }
  593. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  594. {
  595. $aRes = array();
  596. foreach ($this->m_aExpressions as $oExpr)
  597. {
  598. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  599. }
  600. return new ListExpression($aRes);
  601. }
  602. public function ListRequiredFields()
  603. {
  604. $aRes = array();
  605. foreach ($this->m_aExpressions as $oExpr)
  606. {
  607. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  608. }
  609. return $aRes;
  610. }
  611. public function ListConstantFields()
  612. {
  613. $aRes = array();
  614. foreach ($this->m_aExpressions as $oExpr)
  615. {
  616. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  617. }
  618. return $aRes;
  619. }
  620. public function RenameParam($sOldName, $sNewName)
  621. {
  622. $aRes = array();
  623. foreach ($this->m_aExpressions as $key => $oExpr)
  624. {
  625. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  626. }
  627. }
  628. }
  629. class FunctionExpression extends Expression
  630. {
  631. protected $m_sVerb;
  632. protected $m_aArgs; // array of expressions
  633. public function __construct($sVerb, $aArgExpressions)
  634. {
  635. $this->m_sVerb = $sVerb;
  636. $this->m_aArgs = $aArgExpressions;
  637. }
  638. public function IsTrue()
  639. {
  640. // return true if we are certain that it will be true
  641. return false;
  642. }
  643. public function GetVerb()
  644. {
  645. return $this->m_sVerb;
  646. }
  647. public function GetArgs()
  648. {
  649. return $this->m_aArgs;
  650. }
  651. // recursive rendering
  652. public function Render(&$aArgs = null, $bRetrofitParams = false)
  653. {
  654. $aRes = array();
  655. foreach ($this->m_aArgs as $oExpr)
  656. {
  657. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  658. }
  659. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  660. }
  661. public function ApplyParameters($aArgs)
  662. {
  663. $aRes = array();
  664. foreach ($this->m_aArgs as $idx => $oExpr)
  665. {
  666. if ($oExpr instanceof VariableExpression)
  667. {
  668. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  669. }
  670. else
  671. {
  672. $oExpr->ApplyParameters($aArgs);
  673. }
  674. }
  675. }
  676. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  677. {
  678. foreach ($this->m_aArgs as $oExpr)
  679. {
  680. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  681. }
  682. }
  683. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  684. {
  685. $aRes = array();
  686. foreach ($this->m_aArgs as $oExpr)
  687. {
  688. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  689. }
  690. return new FunctionExpression($this->m_sVerb, $aRes);
  691. }
  692. public function ListRequiredFields()
  693. {
  694. $aRes = array();
  695. foreach ($this->m_aArgs as $oExpr)
  696. {
  697. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  698. }
  699. return $aRes;
  700. }
  701. public function ListConstantFields()
  702. {
  703. $aRes = array();
  704. foreach ($this->m_aArgs as $oExpr)
  705. {
  706. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  707. }
  708. return $aRes;
  709. }
  710. public function RenameParam($sOldName, $sNewName)
  711. {
  712. foreach ($this->m_aArgs as $key => $oExpr)
  713. {
  714. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  715. }
  716. }
  717. }
  718. class IntervalExpression extends Expression
  719. {
  720. protected $m_oValue; // expression
  721. protected $m_sUnit;
  722. public function __construct($oValue, $sUnit)
  723. {
  724. $this->m_oValue = $oValue;
  725. $this->m_sUnit = $sUnit;
  726. }
  727. public function IsTrue()
  728. {
  729. // return true if we are certain that it will be true
  730. return false;
  731. }
  732. public function GetValue()
  733. {
  734. return $this->m_oValue;
  735. }
  736. public function GetUnit()
  737. {
  738. return $this->m_sUnit;
  739. }
  740. // recursive rendering
  741. public function Render(&$aArgs = null, $bRetrofitParams = false)
  742. {
  743. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  744. }
  745. public function ApplyParameters($aArgs)
  746. {
  747. if ($this->m_oValue instanceof VariableExpression)
  748. {
  749. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  750. }
  751. else
  752. {
  753. $this->m_oValue->ApplyParameters($aArgs);
  754. }
  755. }
  756. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  757. {
  758. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  759. }
  760. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  761. {
  762. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  763. }
  764. public function ListRequiredFields()
  765. {
  766. return array();
  767. }
  768. public function ListConstantFields()
  769. {
  770. return array();
  771. }
  772. public function RenameParam($sOldName, $sNewName)
  773. {
  774. $this->m_oValue->RenameParam($sOldName, $sNewName);
  775. }
  776. }
  777. class CharConcatExpression extends Expression
  778. {
  779. protected $m_aExpressions;
  780. public function __construct($aExpressions)
  781. {
  782. $this->m_aExpressions = $aExpressions;
  783. }
  784. public function IsTrue()
  785. {
  786. // return true if we are certain that it will be true
  787. return false;
  788. }
  789. public function GetItems()
  790. {
  791. return $this->m_aExpressions;
  792. }
  793. // recursive rendering
  794. public function Render(&$aArgs = null, $bRetrofitParams = false)
  795. {
  796. $aRes = array();
  797. foreach ($this->m_aExpressions as $oExpr)
  798. {
  799. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  800. // Concat will be globally NULL if one single argument is null !
  801. $aRes[] = "COALESCE($sCol, '')";
  802. }
  803. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  804. }
  805. public function ApplyParameters($aArgs)
  806. {
  807. $aRes = array();
  808. foreach ($this->m_aExpressions as $idx => $oExpr)
  809. {
  810. if ($oExpr instanceof VariableExpression)
  811. {
  812. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  813. }
  814. else
  815. {
  816. $this->m_aExpressions->ApplyParameters($aArgs);
  817. }
  818. }
  819. }
  820. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  821. {
  822. foreach ($this->m_aExpressions as $oExpr)
  823. {
  824. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  825. }
  826. }
  827. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  828. {
  829. $aRes = array();
  830. foreach ($this->m_aExpressions as $oExpr)
  831. {
  832. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  833. }
  834. return new CharConcatExpression($aRes);
  835. }
  836. public function ListRequiredFields()
  837. {
  838. $aRes = array();
  839. foreach ($this->m_aExpressions as $oExpr)
  840. {
  841. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  842. }
  843. return $aRes;
  844. }
  845. public function ListConstantFields()
  846. {
  847. $aRes = array();
  848. foreach ($this->m_aExpressions as $oExpr)
  849. {
  850. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  851. }
  852. return $aRes;
  853. }
  854. public function RenameParam($sOldName, $sNewName)
  855. {
  856. foreach ($this->m_aExpressions as $key => $oExpr)
  857. {
  858. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  859. }
  860. }
  861. }
  862. class CharConcatWSExpression extends CharConcatExpression
  863. {
  864. protected $m_separator;
  865. public function __construct($separator, $aExpressions)
  866. {
  867. $this->m_separator = $separator;
  868. parent::__construct($aExpressions);
  869. }
  870. // recursive rendering
  871. public function Render(&$aArgs = null, $bRetrofitParams = false)
  872. {
  873. $aRes = array();
  874. foreach ($this->m_aExpressions as $oExpr)
  875. {
  876. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  877. // Concat will be globally NULL if one single argument is null !
  878. $aRes[] = "COALESCE($sCol, '')";
  879. }
  880. $sSep = CMDBSource::Quote($this->m_separator);
  881. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  882. }
  883. }
  884. class QueryBuilderExpressions
  885. {
  886. protected $m_oConditionExpr;
  887. protected $m_aSelectExpr;
  888. protected $m_aGroupByExpr;
  889. protected $m_aJoinFields;
  890. public function __construct($oCondition, $aGroupByExpr = null)
  891. {
  892. $this->m_oConditionExpr = $oCondition;
  893. $this->m_aSelectExpr = array();
  894. $this->m_aGroupByExpr = $aGroupByExpr;
  895. $this->m_aJoinFields = array();
  896. }
  897. public function GetSelect()
  898. {
  899. return $this->m_aSelectExpr;
  900. }
  901. public function GetGroupBy()
  902. {
  903. return $this->m_aGroupByExpr;
  904. }
  905. public function GetCondition()
  906. {
  907. return $this->m_oConditionExpr;
  908. }
  909. public function PopJoinField()
  910. {
  911. return array_pop($this->m_aJoinFields);
  912. }
  913. public function AddSelect($sAttAlias, $oExpression)
  914. {
  915. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  916. }
  917. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  918. public function AddCondition($oExpression)
  919. {
  920. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  921. }
  922. public function PushJoinField($oExpression)
  923. {
  924. array_push($this->m_aJoinFields, $oExpression);
  925. }
  926. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  927. {
  928. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  929. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  930. {
  931. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  932. }
  933. if ($this->m_aGroupByExpr)
  934. {
  935. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  936. {
  937. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  938. }
  939. }
  940. foreach($this->m_aJoinFields as $oExpression)
  941. {
  942. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  943. }
  944. }
  945. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  946. {
  947. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  948. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  949. {
  950. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  951. }
  952. if ($this->m_aGroupByExpr)
  953. {
  954. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  955. {
  956. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  957. }
  958. }
  959. foreach($this->m_aJoinFields as $index => $oExpression)
  960. {
  961. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  962. }
  963. }
  964. public function RenameParam($sOldName, $sNewName)
  965. {
  966. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  967. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  968. {
  969. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  970. }
  971. if ($this->m_aGroupByExpr)
  972. {
  973. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  974. {
  975. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  976. }
  977. }
  978. foreach($this->m_aJoinFields as $index => $oExpression)
  979. {
  980. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  981. }
  982. }
  983. }
  984. ?>