expression.class.inc.php 33 KB

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