expression.class.inc.php 31 KB

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