expression.class.inc.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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) && (!$value instanceof OqlHexValue))
  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 (($iPos = strpos($this->m_sName, '->')) !== false)
  527. {
  528. $sParamName = substr($this->m_sName, 0, $iPos);
  529. if (array_key_exists($sParamName.'->object()', $aArgs))
  530. {
  531. $sAttCode = substr($this->m_sName, $iPos + 2);
  532. $oObj = $aArgs[$sParamName.'->object()'];
  533. if ($sAttCode == 'id')
  534. {
  535. return CMDBSource::Quote($oObj->GetKey());
  536. }
  537. return CMDBSource::Quote($oObj->Get($sAttCode));
  538. }
  539. }
  540. if ($bRetrofitParams)
  541. {
  542. $aArgs[$this->m_sName] = null;
  543. return ':'.$this->m_sName;
  544. }
  545. else
  546. {
  547. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  548. }
  549. }
  550. public function RenameParam($sOldName, $sNewName)
  551. {
  552. if ($this->m_sName == $sOldName)
  553. {
  554. $this->m_sName = $sNewName;
  555. }
  556. }
  557. public function GetAsScalar($aArgs)
  558. {
  559. $value = '';
  560. if (array_key_exists($this->m_sName, $aArgs))
  561. {
  562. $value = $aArgs[$this->m_sName];
  563. }
  564. else
  565. {
  566. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  567. }
  568. return new ScalarExpression($value);
  569. }
  570. }
  571. // Temporary, until we implement functions and expression casting!
  572. // ... or until we implement a real full text search based in the MATCH() expression
  573. class ListExpression extends Expression
  574. {
  575. protected $m_aExpressions;
  576. public function __construct($aExpressions)
  577. {
  578. $this->m_aExpressions = $aExpressions;
  579. }
  580. public static function FromScalars($aScalars)
  581. {
  582. $aExpressions = array();
  583. foreach($aScalars as $value)
  584. {
  585. $aExpressions[] = new ScalarExpression($value);
  586. }
  587. return new ListExpression($aExpressions);
  588. }
  589. public function IsTrue()
  590. {
  591. // return true if we are certain that it will be true
  592. return false;
  593. }
  594. public function GetItems()
  595. {
  596. return $this->m_aExpressions;
  597. }
  598. // recursive rendering
  599. public function Render(&$aArgs = null, $bRetrofitParams = false)
  600. {
  601. $aRes = array();
  602. foreach ($this->m_aExpressions as $oExpr)
  603. {
  604. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  605. }
  606. return '('.implode(', ', $aRes).')';
  607. }
  608. public function ApplyParameters($aArgs)
  609. {
  610. $aRes = array();
  611. foreach ($this->m_aExpressions as $idx => $oExpr)
  612. {
  613. if ($oExpr instanceof VariableExpression)
  614. {
  615. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  616. }
  617. else
  618. {
  619. $oExpr->ApplyParameters($aArgs);
  620. }
  621. }
  622. }
  623. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  624. {
  625. foreach ($this->m_aExpressions as $oExpr)
  626. {
  627. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  628. }
  629. }
  630. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  631. {
  632. $aRes = array();
  633. foreach ($this->m_aExpressions as $oExpr)
  634. {
  635. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  636. }
  637. return new ListExpression($aRes);
  638. }
  639. public function ListRequiredFields()
  640. {
  641. $aRes = array();
  642. foreach ($this->m_aExpressions as $oExpr)
  643. {
  644. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  645. }
  646. return $aRes;
  647. }
  648. public function CollectUsedParents(&$aTable)
  649. {
  650. foreach ($this->m_aExpressions as $oExpr)
  651. {
  652. $oExpr->CollectUsedParents($aTable);
  653. }
  654. }
  655. public function ListConstantFields()
  656. {
  657. $aRes = array();
  658. foreach ($this->m_aExpressions as $oExpr)
  659. {
  660. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  661. }
  662. return $aRes;
  663. }
  664. public function RenameParam($sOldName, $sNewName)
  665. {
  666. $aRes = array();
  667. foreach ($this->m_aExpressions as $key => $oExpr)
  668. {
  669. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  670. }
  671. }
  672. }
  673. class FunctionExpression extends Expression
  674. {
  675. protected $m_sVerb;
  676. protected $m_aArgs; // array of expressions
  677. public function __construct($sVerb, $aArgExpressions)
  678. {
  679. $this->m_sVerb = $sVerb;
  680. $this->m_aArgs = $aArgExpressions;
  681. }
  682. public function IsTrue()
  683. {
  684. // return true if we are certain that it will be true
  685. return false;
  686. }
  687. public function GetVerb()
  688. {
  689. return $this->m_sVerb;
  690. }
  691. public function GetArgs()
  692. {
  693. return $this->m_aArgs;
  694. }
  695. // recursive rendering
  696. public function Render(&$aArgs = null, $bRetrofitParams = false)
  697. {
  698. $aRes = array();
  699. foreach ($this->m_aArgs as $oExpr)
  700. {
  701. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  702. }
  703. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  704. }
  705. public function ApplyParameters($aArgs)
  706. {
  707. $aRes = array();
  708. foreach ($this->m_aArgs as $idx => $oExpr)
  709. {
  710. if ($oExpr instanceof VariableExpression)
  711. {
  712. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  713. }
  714. else
  715. {
  716. $oExpr->ApplyParameters($aArgs);
  717. }
  718. }
  719. }
  720. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  721. {
  722. foreach ($this->m_aArgs as $oExpr)
  723. {
  724. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  725. }
  726. }
  727. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  728. {
  729. $aRes = array();
  730. foreach ($this->m_aArgs as $oExpr)
  731. {
  732. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  733. }
  734. return new FunctionExpression($this->m_sVerb, $aRes);
  735. }
  736. public function ListRequiredFields()
  737. {
  738. $aRes = array();
  739. foreach ($this->m_aArgs as $oExpr)
  740. {
  741. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  742. }
  743. return $aRes;
  744. }
  745. public function CollectUsedParents(&$aTable)
  746. {
  747. foreach ($this->m_aArgs as $oExpr)
  748. {
  749. $oExpr->CollectUsedParents($aTable);
  750. }
  751. }
  752. public function ListConstantFields()
  753. {
  754. $aRes = array();
  755. foreach ($this->m_aArgs as $oExpr)
  756. {
  757. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  758. }
  759. return $aRes;
  760. }
  761. public function RenameParam($sOldName, $sNewName)
  762. {
  763. foreach ($this->m_aArgs as $key => $oExpr)
  764. {
  765. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  766. }
  767. }
  768. /**
  769. * Make the most relevant label, given the value of the expression
  770. *
  771. * @param DBObjectSearch oFilter The context in which this expression has been used
  772. * @param string sValue The value returned by the query, for this expression
  773. * @param string sDefault The default value if no relevant label could be computed
  774. * @return The label
  775. */
  776. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  777. {
  778. static $aWeekDayToString = null;
  779. if (is_null($aWeekDayToString))
  780. {
  781. // Init the correspondance table
  782. $aWeekDayToString = array(
  783. 0 => Dict::S('DayOfWeek-Sunday'),
  784. 1 => Dict::S('DayOfWeek-Monday'),
  785. 2 => Dict::S('DayOfWeek-Tuesday'),
  786. 3 => Dict::S('DayOfWeek-Wednesday'),
  787. 4 => Dict::S('DayOfWeek-Thursday'),
  788. 5 => Dict::S('DayOfWeek-Friday'),
  789. 6 => Dict::S('DayOfWeek-Saturday')
  790. );
  791. }
  792. static $aMonthToString = null;
  793. if (is_null($aMonthToString))
  794. {
  795. // Init the correspondance table
  796. $aMonthToString = array(
  797. 1 => Dict::S('Month-01'),
  798. 2 => Dict::S('Month-02'),
  799. 3 => Dict::S('Month-03'),
  800. 4 => Dict::S('Month-04'),
  801. 5 => Dict::S('Month-05'),
  802. 6 => Dict::S('Month-06'),
  803. 7 => Dict::S('Month-07'),
  804. 8 => Dict::S('Month-08'),
  805. 9 => Dict::S('Month-09'),
  806. 10 => Dict::S('Month-10'),
  807. 11 => Dict::S('Month-11'),
  808. 12 => Dict::S('Month-12'),
  809. );
  810. }
  811. $sRes = $sDefault;
  812. if (strtolower($this->m_sVerb) == 'date_format')
  813. {
  814. $oFormatExpr = $this->m_aArgs[1];
  815. if ($oFormatExpr->Render() == "'%w'")
  816. {
  817. if (isset($aWeekDayToString[(int)$sValue]))
  818. {
  819. $sRes = $aWeekDayToString[(int)$sValue];
  820. }
  821. }
  822. elseif ($oFormatExpr->Render() == "'%Y-%m'")
  823. {
  824. // yyyy-mm => "yyyy month"
  825. $iMonth = (int) substr($sValue, -2); // the two last chars
  826. $sRes = substr($sValue, 0, 4).' '.$aMonthToString[$iMonth];
  827. }
  828. elseif ($oFormatExpr->Render() == "'%Y-%m-%d'")
  829. {
  830. // yyyy-mm-dd => "month d"
  831. $iMonth = (int) substr($sValue, 5, 2);
  832. $sRes = $aMonthToString[$iMonth].' '.(int)substr($sValue, -2);
  833. }
  834. }
  835. return $sRes;
  836. }
  837. }
  838. class IntervalExpression extends Expression
  839. {
  840. protected $m_oValue; // expression
  841. protected $m_sUnit;
  842. public function __construct($oValue, $sUnit)
  843. {
  844. $this->m_oValue = $oValue;
  845. $this->m_sUnit = $sUnit;
  846. }
  847. public function IsTrue()
  848. {
  849. // return true if we are certain that it will be true
  850. return false;
  851. }
  852. public function GetValue()
  853. {
  854. return $this->m_oValue;
  855. }
  856. public function GetUnit()
  857. {
  858. return $this->m_sUnit;
  859. }
  860. // recursive rendering
  861. public function Render(&$aArgs = null, $bRetrofitParams = false)
  862. {
  863. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  864. }
  865. public function ApplyParameters($aArgs)
  866. {
  867. if ($this->m_oValue instanceof VariableExpression)
  868. {
  869. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  870. }
  871. else
  872. {
  873. $this->m_oValue->ApplyParameters($aArgs);
  874. }
  875. }
  876. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  877. {
  878. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  879. }
  880. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  881. {
  882. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  883. }
  884. public function ListRequiredFields()
  885. {
  886. return array();
  887. }
  888. public function CollectUsedParents(&$aTable)
  889. {
  890. }
  891. public function ListConstantFields()
  892. {
  893. return array();
  894. }
  895. public function RenameParam($sOldName, $sNewName)
  896. {
  897. $this->m_oValue->RenameParam($sOldName, $sNewName);
  898. }
  899. }
  900. class CharConcatExpression extends Expression
  901. {
  902. protected $m_aExpressions;
  903. public function __construct($aExpressions)
  904. {
  905. $this->m_aExpressions = $aExpressions;
  906. }
  907. public function IsTrue()
  908. {
  909. // return true if we are certain that it will be true
  910. return false;
  911. }
  912. public function GetItems()
  913. {
  914. return $this->m_aExpressions;
  915. }
  916. // recursive rendering
  917. public function Render(&$aArgs = null, $bRetrofitParams = false)
  918. {
  919. $aRes = array();
  920. foreach ($this->m_aExpressions as $oExpr)
  921. {
  922. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  923. // Concat will be globally NULL if one single argument is null !
  924. $aRes[] = "COALESCE($sCol, '')";
  925. }
  926. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  927. }
  928. public function ApplyParameters($aArgs)
  929. {
  930. $aRes = array();
  931. foreach ($this->m_aExpressions as $idx => $oExpr)
  932. {
  933. if ($oExpr instanceof VariableExpression)
  934. {
  935. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  936. }
  937. else
  938. {
  939. $this->m_aExpressions->ApplyParameters($aArgs);
  940. }
  941. }
  942. }
  943. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  944. {
  945. foreach ($this->m_aExpressions as $oExpr)
  946. {
  947. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  948. }
  949. }
  950. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  951. {
  952. $aRes = array();
  953. foreach ($this->m_aExpressions as $oExpr)
  954. {
  955. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  956. }
  957. return new CharConcatExpression($aRes);
  958. }
  959. public function ListRequiredFields()
  960. {
  961. $aRes = array();
  962. foreach ($this->m_aExpressions as $oExpr)
  963. {
  964. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  965. }
  966. return $aRes;
  967. }
  968. public function CollectUsedParents(&$aTable)
  969. {
  970. foreach ($this->m_aExpressions as $oExpr)
  971. {
  972. $oExpr->CollectUsedParents($aTable);
  973. }
  974. }
  975. public function ListConstantFields()
  976. {
  977. $aRes = array();
  978. foreach ($this->m_aExpressions as $oExpr)
  979. {
  980. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  981. }
  982. return $aRes;
  983. }
  984. public function RenameParam($sOldName, $sNewName)
  985. {
  986. foreach ($this->m_aExpressions as $key => $oExpr)
  987. {
  988. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  989. }
  990. }
  991. }
  992. class CharConcatWSExpression extends CharConcatExpression
  993. {
  994. protected $m_separator;
  995. public function __construct($separator, $aExpressions)
  996. {
  997. $this->m_separator = $separator;
  998. parent::__construct($aExpressions);
  999. }
  1000. // recursive rendering
  1001. public function Render(&$aArgs = null, $bRetrofitParams = false)
  1002. {
  1003. $aRes = array();
  1004. foreach ($this->m_aExpressions as $oExpr)
  1005. {
  1006. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  1007. // Concat will be globally NULL if one single argument is null !
  1008. $aRes[] = "COALESCE($sCol, '')";
  1009. }
  1010. $sSep = CMDBSource::Quote($this->m_separator);
  1011. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  1012. }
  1013. }
  1014. class QueryBuilderExpressions
  1015. {
  1016. protected $m_oConditionExpr;
  1017. protected $m_aSelectExpr;
  1018. protected $m_aGroupByExpr;
  1019. protected $m_aJoinFields;
  1020. protected $m_aClassIds;
  1021. public function __construct($oSearch, $aGroupByExpr = null)
  1022. {
  1023. $this->m_oConditionExpr = $oSearch->GetCriteria();
  1024. $this->m_aSelectExpr = array();
  1025. $this->m_aGroupByExpr = $aGroupByExpr;
  1026. $this->m_aJoinFields = array();
  1027. $this->m_aClassIds = array();
  1028. foreach($oSearch->GetJoinedClasses() as $sClassAlias => $sClass)
  1029. {
  1030. $this->m_aClassIds[$sClassAlias] = new FieldExpression('id', $sClassAlias);
  1031. }
  1032. }
  1033. public function GetSelect()
  1034. {
  1035. return $this->m_aSelectExpr;
  1036. }
  1037. public function GetGroupBy()
  1038. {
  1039. return $this->m_aGroupByExpr;
  1040. }
  1041. public function GetCondition()
  1042. {
  1043. return $this->m_oConditionExpr;
  1044. }
  1045. public function PopJoinField()
  1046. {
  1047. return array_pop($this->m_aJoinFields);
  1048. }
  1049. public function AddSelect($sAttAlias, $oExpression)
  1050. {
  1051. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  1052. }
  1053. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  1054. public function AddCondition($oExpression)
  1055. {
  1056. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  1057. }
  1058. public function PushJoinField($oExpression)
  1059. {
  1060. array_push($this->m_aJoinFields, $oExpression);
  1061. }
  1062. /**
  1063. * Get tables representing the queried objects
  1064. * Could be further optimized: when the first join is an outer join, then the rest can be omitted
  1065. */
  1066. public function GetMandatoryTables(&$aTables = null)
  1067. {
  1068. if (is_null($aTables)) $aTables = array();
  1069. foreach($this->m_aClassIds as $sClass => $oExpression)
  1070. {
  1071. $oExpression->CollectUsedParents($aTables);
  1072. }
  1073. }
  1074. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1075. {
  1076. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1077. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1078. {
  1079. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1080. }
  1081. if ($this->m_aGroupByExpr)
  1082. {
  1083. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1084. {
  1085. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1086. }
  1087. }
  1088. foreach($this->m_aJoinFields as $oExpression)
  1089. {
  1090. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  1091. }
  1092. }
  1093. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1094. {
  1095. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1096. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1097. {
  1098. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1099. }
  1100. if ($this->m_aGroupByExpr)
  1101. {
  1102. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1103. {
  1104. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1105. }
  1106. }
  1107. foreach($this->m_aJoinFields as $index => $oExpression)
  1108. {
  1109. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1110. }
  1111. foreach($this->m_aClassIds as $sClass => $oExpression)
  1112. {
  1113. $this->m_aClassIds[$sClass] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1114. }
  1115. }
  1116. public function RenameParam($sOldName, $sNewName)
  1117. {
  1118. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  1119. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1120. {
  1121. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1122. }
  1123. if ($this->m_aGroupByExpr)
  1124. {
  1125. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1126. {
  1127. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1128. }
  1129. }
  1130. foreach($this->m_aJoinFields as $index => $oExpression)
  1131. {
  1132. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  1133. }
  1134. }
  1135. }
  1136. ?>