expression.class.inc.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. public function GetAsScalar($aArgs)
  345. {
  346. return clone $this;
  347. }
  348. }
  349. class TrueExpression extends ScalarExpression
  350. {
  351. public function __construct()
  352. {
  353. parent::__construct(1);
  354. }
  355. public function IsTrue()
  356. {
  357. return true;
  358. }
  359. }
  360. class FalseExpression extends ScalarExpression
  361. {
  362. public function __construct()
  363. {
  364. parent::__construct(0);
  365. }
  366. public function IsTrue()
  367. {
  368. return false;
  369. }
  370. }
  371. class FieldExpression extends UnaryExpression
  372. {
  373. protected $m_sParent;
  374. protected $m_sName;
  375. public function __construct($sName, $sParent = '')
  376. {
  377. parent::__construct("$sParent.$sName");
  378. $this->m_sParent = $sParent;
  379. $this->m_sName = $sName;
  380. }
  381. public function IsTrue()
  382. {
  383. // return true if we are certain that it will be true
  384. return false;
  385. }
  386. public function GetParent() {return $this->m_sParent;}
  387. public function GetName() {return $this->m_sName;}
  388. // recursive rendering
  389. public function Render(&$aArgs = null, $bRetrofitParams = false)
  390. {
  391. if (empty($this->m_sParent))
  392. {
  393. return "`{$this->m_sName}`";
  394. }
  395. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  396. }
  397. public function ListRequiredFields()
  398. {
  399. return array($this->m_sParent.'.'.$this->m_sName);
  400. }
  401. public function CollectUsedParents(&$aTable)
  402. {
  403. $aTable[$this->m_sParent] = true;
  404. }
  405. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  406. {
  407. if ($this->m_sParent == $sAlias)
  408. {
  409. // Add a reference to the field
  410. $aUnresolved[$this->m_sName] = $this;
  411. }
  412. elseif ($sAlias == '')
  413. {
  414. // An empty alias means "any alias"
  415. // In such a case, the results are indexed differently
  416. $aUnresolved[$this->m_sParent][$this->m_sName] = $this;
  417. }
  418. }
  419. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  420. {
  421. if (!array_key_exists($this->m_sParent, $aTranslationData))
  422. {
  423. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  424. return clone $this;
  425. }
  426. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  427. {
  428. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  429. {
  430. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  431. 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])));
  432. return clone $this;
  433. }
  434. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  435. $sNewName = $this->m_sName;
  436. if ($bMarkFieldsAsResolved)
  437. {
  438. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  439. }
  440. else
  441. {
  442. $oRet = new FieldExpression($sNewName, $sNewParent);
  443. }
  444. }
  445. else
  446. {
  447. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  448. }
  449. return $oRet;
  450. }
  451. /**
  452. * Make the most relevant label, given the value of the expression
  453. *
  454. * @param DBObjectSearch oFilter The context in which this expression has been used
  455. * @param string sValue The value returned by the query, for this expression
  456. * @param string sDefault The default value if no relevant label could be computed
  457. * @return The label
  458. */
  459. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  460. {
  461. $sAttCode = $this->GetName();
  462. $sParentAlias = $this->GetParent();
  463. $aSelectedClasses = $oFilter->GetSelectedClasses();
  464. $sClass = $aSelectedClasses[$sParentAlias];
  465. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  466. // Set a default value for the general case
  467. $sRes = $oAttDef->GetAsHtml($sValue);
  468. // Exceptions...
  469. if ($oAttDef->IsExternalKey())
  470. {
  471. $sObjClass = $oAttDef->GetTargetClass();
  472. $iObjKey = (int)$sValue;
  473. if ($iObjKey > 0)
  474. {
  475. $oObject = MetaModel::GetObject($sObjClass, $iObjKey);
  476. $sRes = $oObject->GetHyperlink();
  477. }
  478. else
  479. {
  480. // Undefined
  481. $sRes = DBObject::MakeHyperLink($sObjClass, 0);
  482. }
  483. }
  484. elseif ($oAttDef->IsExternalField())
  485. {
  486. if (is_null($sValue))
  487. {
  488. $sRes = Dict::S('UI:UndefinedObject');
  489. }
  490. }
  491. return $sRes;
  492. }
  493. }
  494. // Has been resolved into an SQL expression
  495. class FieldExpressionResolved extends FieldExpression
  496. {
  497. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  498. {
  499. }
  500. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  501. {
  502. return clone $this;
  503. }
  504. }
  505. class VariableExpression extends UnaryExpression
  506. {
  507. protected $m_sName;
  508. public function __construct($sName)
  509. {
  510. parent::__construct($sName);
  511. $this->m_sName = $sName;
  512. }
  513. public function IsTrue()
  514. {
  515. // return true if we are certain that it will be true
  516. return false;
  517. }
  518. public function GetName() {return $this->m_sName;}
  519. // recursive rendering
  520. public function Render(&$aArgs = null, $bRetrofitParams = false)
  521. {
  522. if (is_null($aArgs))
  523. {
  524. return ':'.$this->m_sName;
  525. }
  526. elseif (array_key_exists($this->m_sName, $aArgs))
  527. {
  528. return CMDBSource::Quote($aArgs[$this->m_sName]);
  529. }
  530. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  531. {
  532. $sParamName = substr($this->m_sName, 0, $iPos);
  533. if (array_key_exists($sParamName.'->object()', $aArgs))
  534. {
  535. $sAttCode = substr($this->m_sName, $iPos + 2);
  536. $oObj = $aArgs[$sParamName.'->object()'];
  537. if ($sAttCode == 'id')
  538. {
  539. return CMDBSource::Quote($oObj->GetKey());
  540. }
  541. return CMDBSource::Quote($oObj->Get($sAttCode));
  542. }
  543. }
  544. if ($bRetrofitParams)
  545. {
  546. $aArgs[$this->m_sName] = null;
  547. return ':'.$this->m_sName;
  548. }
  549. else
  550. {
  551. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  552. }
  553. }
  554. public function RenameParam($sOldName, $sNewName)
  555. {
  556. if ($this->m_sName == $sOldName)
  557. {
  558. $this->m_sName = $sNewName;
  559. }
  560. }
  561. public function GetAsScalar($aArgs)
  562. {
  563. $value = null;
  564. if (array_key_exists($this->m_sName, $aArgs))
  565. {
  566. $value = $aArgs[$this->m_sName];
  567. }
  568. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  569. {
  570. $sParamName = substr($this->m_sName, 0, $iPos);
  571. if (array_key_exists($sParamName.'->object()', $aArgs))
  572. {
  573. $sAttCode = substr($this->m_sName, $iPos + 2);
  574. $oObj = $aArgs[$sParamName.'->object()'];
  575. if ($sAttCode == 'id')
  576. {
  577. $value = $oObj->GetKey();
  578. }
  579. else
  580. {
  581. $value = $oObj->Get($sAttCode);
  582. }
  583. }
  584. }
  585. if (is_null($value))
  586. {
  587. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  588. }
  589. return new ScalarExpression($value);
  590. }
  591. }
  592. // Temporary, until we implement functions and expression casting!
  593. // ... or until we implement a real full text search based in the MATCH() expression
  594. class ListExpression extends Expression
  595. {
  596. protected $m_aExpressions;
  597. public function __construct($aExpressions)
  598. {
  599. $this->m_aExpressions = $aExpressions;
  600. }
  601. public static function FromScalars($aScalars)
  602. {
  603. $aExpressions = array();
  604. foreach($aScalars as $value)
  605. {
  606. $aExpressions[] = new ScalarExpression($value);
  607. }
  608. return new ListExpression($aExpressions);
  609. }
  610. public function IsTrue()
  611. {
  612. // return true if we are certain that it will be true
  613. return false;
  614. }
  615. public function GetItems()
  616. {
  617. return $this->m_aExpressions;
  618. }
  619. // recursive rendering
  620. public function Render(&$aArgs = null, $bRetrofitParams = false)
  621. {
  622. $aRes = array();
  623. foreach ($this->m_aExpressions as $oExpr)
  624. {
  625. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  626. }
  627. return '('.implode(', ', $aRes).')';
  628. }
  629. public function ApplyParameters($aArgs)
  630. {
  631. $aRes = array();
  632. foreach ($this->m_aExpressions as $idx => $oExpr)
  633. {
  634. if ($oExpr instanceof VariableExpression)
  635. {
  636. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  637. }
  638. else
  639. {
  640. $oExpr->ApplyParameters($aArgs);
  641. }
  642. }
  643. }
  644. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  645. {
  646. foreach ($this->m_aExpressions as $oExpr)
  647. {
  648. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  649. }
  650. }
  651. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  652. {
  653. $aRes = array();
  654. foreach ($this->m_aExpressions as $oExpr)
  655. {
  656. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  657. }
  658. return new ListExpression($aRes);
  659. }
  660. public function ListRequiredFields()
  661. {
  662. $aRes = array();
  663. foreach ($this->m_aExpressions as $oExpr)
  664. {
  665. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  666. }
  667. return $aRes;
  668. }
  669. public function CollectUsedParents(&$aTable)
  670. {
  671. foreach ($this->m_aExpressions as $oExpr)
  672. {
  673. $oExpr->CollectUsedParents($aTable);
  674. }
  675. }
  676. public function ListConstantFields()
  677. {
  678. $aRes = array();
  679. foreach ($this->m_aExpressions as $oExpr)
  680. {
  681. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  682. }
  683. return $aRes;
  684. }
  685. public function RenameParam($sOldName, $sNewName)
  686. {
  687. $aRes = array();
  688. foreach ($this->m_aExpressions as $key => $oExpr)
  689. {
  690. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  691. }
  692. }
  693. }
  694. class FunctionExpression extends Expression
  695. {
  696. protected $m_sVerb;
  697. protected $m_aArgs; // array of expressions
  698. public function __construct($sVerb, $aArgExpressions)
  699. {
  700. $this->m_sVerb = $sVerb;
  701. $this->m_aArgs = $aArgExpressions;
  702. }
  703. public function IsTrue()
  704. {
  705. // return true if we are certain that it will be true
  706. return false;
  707. }
  708. public function GetVerb()
  709. {
  710. return $this->m_sVerb;
  711. }
  712. public function GetArgs()
  713. {
  714. return $this->m_aArgs;
  715. }
  716. // recursive rendering
  717. public function Render(&$aArgs = null, $bRetrofitParams = false)
  718. {
  719. $aRes = array();
  720. foreach ($this->m_aArgs as $oExpr)
  721. {
  722. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  723. }
  724. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  725. }
  726. public function ApplyParameters($aArgs)
  727. {
  728. $aRes = array();
  729. foreach ($this->m_aArgs as $idx => $oExpr)
  730. {
  731. if ($oExpr instanceof VariableExpression)
  732. {
  733. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  734. }
  735. else
  736. {
  737. $oExpr->ApplyParameters($aArgs);
  738. }
  739. }
  740. }
  741. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  742. {
  743. foreach ($this->m_aArgs as $oExpr)
  744. {
  745. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  746. }
  747. }
  748. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  749. {
  750. $aRes = array();
  751. foreach ($this->m_aArgs as $oExpr)
  752. {
  753. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  754. }
  755. return new FunctionExpression($this->m_sVerb, $aRes);
  756. }
  757. public function ListRequiredFields()
  758. {
  759. $aRes = array();
  760. foreach ($this->m_aArgs as $oExpr)
  761. {
  762. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  763. }
  764. return $aRes;
  765. }
  766. public function CollectUsedParents(&$aTable)
  767. {
  768. foreach ($this->m_aArgs as $oExpr)
  769. {
  770. $oExpr->CollectUsedParents($aTable);
  771. }
  772. }
  773. public function ListConstantFields()
  774. {
  775. $aRes = array();
  776. foreach ($this->m_aArgs as $oExpr)
  777. {
  778. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  779. }
  780. return $aRes;
  781. }
  782. public function RenameParam($sOldName, $sNewName)
  783. {
  784. foreach ($this->m_aArgs as $key => $oExpr)
  785. {
  786. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  787. }
  788. }
  789. /**
  790. * Make the most relevant label, given the value of the expression
  791. *
  792. * @param DBObjectSearch oFilter The context in which this expression has been used
  793. * @param string sValue The value returned by the query, for this expression
  794. * @param string sDefault The default value if no relevant label could be computed
  795. * @return The label
  796. */
  797. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  798. {
  799. static $aWeekDayToString = null;
  800. if (is_null($aWeekDayToString))
  801. {
  802. // Init the correspondance table
  803. $aWeekDayToString = array(
  804. 0 => Dict::S('DayOfWeek-Sunday'),
  805. 1 => Dict::S('DayOfWeek-Monday'),
  806. 2 => Dict::S('DayOfWeek-Tuesday'),
  807. 3 => Dict::S('DayOfWeek-Wednesday'),
  808. 4 => Dict::S('DayOfWeek-Thursday'),
  809. 5 => Dict::S('DayOfWeek-Friday'),
  810. 6 => Dict::S('DayOfWeek-Saturday')
  811. );
  812. }
  813. static $aMonthToString = null;
  814. if (is_null($aMonthToString))
  815. {
  816. // Init the correspondance table
  817. $aMonthToString = array(
  818. 1 => Dict::S('Month-01'),
  819. 2 => Dict::S('Month-02'),
  820. 3 => Dict::S('Month-03'),
  821. 4 => Dict::S('Month-04'),
  822. 5 => Dict::S('Month-05'),
  823. 6 => Dict::S('Month-06'),
  824. 7 => Dict::S('Month-07'),
  825. 8 => Dict::S('Month-08'),
  826. 9 => Dict::S('Month-09'),
  827. 10 => Dict::S('Month-10'),
  828. 11 => Dict::S('Month-11'),
  829. 12 => Dict::S('Month-12'),
  830. );
  831. }
  832. $sRes = $sDefault;
  833. if (strtolower($this->m_sVerb) == 'date_format')
  834. {
  835. $oFormatExpr = $this->m_aArgs[1];
  836. if ($oFormatExpr->Render() == "'%w'")
  837. {
  838. if (isset($aWeekDayToString[(int)$sValue]))
  839. {
  840. $sRes = $aWeekDayToString[(int)$sValue];
  841. }
  842. }
  843. elseif ($oFormatExpr->Render() == "'%Y-%m'")
  844. {
  845. // yyyy-mm => "yyyy month"
  846. $iMonth = (int) substr($sValue, -2); // the two last chars
  847. $sRes = substr($sValue, 0, 4).' '.$aMonthToString[$iMonth];
  848. }
  849. elseif ($oFormatExpr->Render() == "'%Y-%m-%d'")
  850. {
  851. // yyyy-mm-dd => "month d"
  852. $iMonth = (int) substr($sValue, 5, 2);
  853. $sRes = $aMonthToString[$iMonth].' '.(int)substr($sValue, -2);
  854. }
  855. }
  856. return $sRes;
  857. }
  858. }
  859. class IntervalExpression extends Expression
  860. {
  861. protected $m_oValue; // expression
  862. protected $m_sUnit;
  863. public function __construct($oValue, $sUnit)
  864. {
  865. $this->m_oValue = $oValue;
  866. $this->m_sUnit = $sUnit;
  867. }
  868. public function IsTrue()
  869. {
  870. // return true if we are certain that it will be true
  871. return false;
  872. }
  873. public function GetValue()
  874. {
  875. return $this->m_oValue;
  876. }
  877. public function GetUnit()
  878. {
  879. return $this->m_sUnit;
  880. }
  881. // recursive rendering
  882. public function Render(&$aArgs = null, $bRetrofitParams = false)
  883. {
  884. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  885. }
  886. public function ApplyParameters($aArgs)
  887. {
  888. if ($this->m_oValue instanceof VariableExpression)
  889. {
  890. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  891. }
  892. else
  893. {
  894. $this->m_oValue->ApplyParameters($aArgs);
  895. }
  896. }
  897. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  898. {
  899. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  900. }
  901. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  902. {
  903. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  904. }
  905. public function ListRequiredFields()
  906. {
  907. return array();
  908. }
  909. public function CollectUsedParents(&$aTable)
  910. {
  911. }
  912. public function ListConstantFields()
  913. {
  914. return array();
  915. }
  916. public function RenameParam($sOldName, $sNewName)
  917. {
  918. $this->m_oValue->RenameParam($sOldName, $sNewName);
  919. }
  920. }
  921. class CharConcatExpression extends Expression
  922. {
  923. protected $m_aExpressions;
  924. public function __construct($aExpressions)
  925. {
  926. $this->m_aExpressions = $aExpressions;
  927. }
  928. public function IsTrue()
  929. {
  930. // return true if we are certain that it will be true
  931. return false;
  932. }
  933. public function GetItems()
  934. {
  935. return $this->m_aExpressions;
  936. }
  937. // recursive rendering
  938. public function Render(&$aArgs = null, $bRetrofitParams = false)
  939. {
  940. $aRes = array();
  941. foreach ($this->m_aExpressions as $oExpr)
  942. {
  943. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  944. // Concat will be globally NULL if one single argument is null !
  945. $aRes[] = "COALESCE($sCol, '')";
  946. }
  947. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  948. }
  949. public function ApplyParameters($aArgs)
  950. {
  951. $aRes = array();
  952. foreach ($this->m_aExpressions as $idx => $oExpr)
  953. {
  954. if ($oExpr instanceof VariableExpression)
  955. {
  956. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  957. }
  958. else
  959. {
  960. $this->m_aExpressions->ApplyParameters($aArgs);
  961. }
  962. }
  963. }
  964. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  965. {
  966. foreach ($this->m_aExpressions as $oExpr)
  967. {
  968. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  969. }
  970. }
  971. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  972. {
  973. $aRes = array();
  974. foreach ($this->m_aExpressions as $oExpr)
  975. {
  976. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  977. }
  978. return new CharConcatExpression($aRes);
  979. }
  980. public function ListRequiredFields()
  981. {
  982. $aRes = array();
  983. foreach ($this->m_aExpressions as $oExpr)
  984. {
  985. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  986. }
  987. return $aRes;
  988. }
  989. public function CollectUsedParents(&$aTable)
  990. {
  991. foreach ($this->m_aExpressions as $oExpr)
  992. {
  993. $oExpr->CollectUsedParents($aTable);
  994. }
  995. }
  996. public function ListConstantFields()
  997. {
  998. $aRes = array();
  999. foreach ($this->m_aExpressions as $oExpr)
  1000. {
  1001. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  1002. }
  1003. return $aRes;
  1004. }
  1005. public function RenameParam($sOldName, $sNewName)
  1006. {
  1007. foreach ($this->m_aExpressions as $key => $oExpr)
  1008. {
  1009. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  1010. }
  1011. }
  1012. }
  1013. class CharConcatWSExpression extends CharConcatExpression
  1014. {
  1015. protected $m_separator;
  1016. public function __construct($separator, $aExpressions)
  1017. {
  1018. $this->m_separator = $separator;
  1019. parent::__construct($aExpressions);
  1020. }
  1021. // recursive rendering
  1022. public function Render(&$aArgs = null, $bRetrofitParams = false)
  1023. {
  1024. $aRes = array();
  1025. foreach ($this->m_aExpressions as $oExpr)
  1026. {
  1027. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  1028. // Concat will be globally NULL if one single argument is null !
  1029. $aRes[] = "COALESCE($sCol, '')";
  1030. }
  1031. $sSep = CMDBSource::Quote($this->m_separator);
  1032. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  1033. }
  1034. }
  1035. class QueryBuilderExpressions
  1036. {
  1037. protected $m_oConditionExpr;
  1038. protected $m_aSelectExpr;
  1039. protected $m_aGroupByExpr;
  1040. protected $m_aJoinFields;
  1041. protected $m_aClassIds;
  1042. public function __construct($oSearch, $aGroupByExpr = null)
  1043. {
  1044. $this->m_oConditionExpr = $oSearch->GetCriteria();
  1045. $this->m_aSelectExpr = array();
  1046. $this->m_aGroupByExpr = $aGroupByExpr;
  1047. $this->m_aJoinFields = array();
  1048. $this->m_aClassIds = array();
  1049. foreach($oSearch->GetJoinedClasses() as $sClassAlias => $sClass)
  1050. {
  1051. $this->m_aClassIds[$sClassAlias] = new FieldExpression('id', $sClassAlias);
  1052. }
  1053. }
  1054. public function GetSelect()
  1055. {
  1056. return $this->m_aSelectExpr;
  1057. }
  1058. public function GetGroupBy()
  1059. {
  1060. return $this->m_aGroupByExpr;
  1061. }
  1062. public function GetCondition()
  1063. {
  1064. return $this->m_oConditionExpr;
  1065. }
  1066. public function PopJoinField()
  1067. {
  1068. return array_pop($this->m_aJoinFields);
  1069. }
  1070. public function AddSelect($sAttAlias, $oExpression)
  1071. {
  1072. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  1073. }
  1074. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  1075. public function AddCondition($oExpression)
  1076. {
  1077. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  1078. }
  1079. public function PushJoinField($oExpression)
  1080. {
  1081. array_push($this->m_aJoinFields, $oExpression);
  1082. }
  1083. /**
  1084. * Get tables representing the queried objects
  1085. * Could be further optimized: when the first join is an outer join, then the rest can be omitted
  1086. */
  1087. public function GetMandatoryTables(&$aTables = null)
  1088. {
  1089. if (is_null($aTables)) $aTables = array();
  1090. foreach($this->m_aClassIds as $sClass => $oExpression)
  1091. {
  1092. $oExpression->CollectUsedParents($aTables);
  1093. }
  1094. }
  1095. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1096. {
  1097. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1098. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1099. {
  1100. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1101. }
  1102. if ($this->m_aGroupByExpr)
  1103. {
  1104. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1105. {
  1106. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1107. }
  1108. }
  1109. foreach($this->m_aJoinFields as $oExpression)
  1110. {
  1111. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  1112. }
  1113. }
  1114. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1115. {
  1116. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1117. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1118. {
  1119. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1120. }
  1121. if ($this->m_aGroupByExpr)
  1122. {
  1123. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1124. {
  1125. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1126. }
  1127. }
  1128. foreach($this->m_aJoinFields as $index => $oExpression)
  1129. {
  1130. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1131. }
  1132. foreach($this->m_aClassIds as $sClass => $oExpression)
  1133. {
  1134. $this->m_aClassIds[$sClass] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1135. }
  1136. }
  1137. public function RenameParam($sOldName, $sNewName)
  1138. {
  1139. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  1140. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1141. {
  1142. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1143. }
  1144. if ($this->m_aGroupByExpr)
  1145. {
  1146. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1147. {
  1148. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1149. }
  1150. }
  1151. foreach($this->m_aJoinFields as $index => $oExpression)
  1152. {
  1153. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  1154. }
  1155. }
  1156. }
  1157. ?>