expression.class.inc.php 32 KB

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