expression.class.inc.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. <?php
  2. // Copyright (C) 2010-2016 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-2016 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. /**
  30. * Perform a deep clone (as opposed to "clone" which does copy a reference to the underlying objects)
  31. **/
  32. public function DeepClone()
  33. {
  34. return unserialize(serialize($this));
  35. }
  36. // recursive translation of identifiers
  37. abstract public function GetUnresolvedFields($sAlias, &$aUnresolved);
  38. abstract public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true);
  39. // recursive rendering (aArgs used as input by default, or used as output if bRetrofitParams set to True
  40. abstract public function Render(&$aArgs = null, $bRetrofitParams = false);
  41. abstract public function ApplyParameters($aArgs);
  42. // recursively builds an array of class => fieldname
  43. abstract public function ListRequiredFields();
  44. // recursively list field parents ($aTable = array of sParent => dummy)
  45. abstract public function CollectUsedParents(&$aTable);
  46. abstract public function IsTrue();
  47. // recursively builds an array of [classAlias][fieldName] => value
  48. abstract public function ListConstantFields();
  49. public function RequiresField($sClass, $sFieldName)
  50. {
  51. // #@# todo - optimize : this is called quite often when building a single query !
  52. $aRequired = $this->ListRequiredFields();
  53. if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false;
  54. return true;
  55. }
  56. public function serialize()
  57. {
  58. return base64_encode($this->Render());
  59. }
  60. static public function unserialize($sValue)
  61. {
  62. return self::FromOQL(base64_decode($sValue));
  63. }
  64. static public function FromOQL($sConditionExpr)
  65. {
  66. $oOql = new OqlInterpreter($sConditionExpr);
  67. $oExpression = $oOql->ParseExpression();
  68. return $oExpression;
  69. }
  70. static public function FromSQL($sSQL)
  71. {
  72. $oSql = new SQLExpression($sSQL);
  73. return $oSql;
  74. }
  75. public function LogAnd($oExpr)
  76. {
  77. if ($this->IsTrue()) return clone $oExpr;
  78. if ($oExpr->IsTrue()) return clone $this;
  79. return new BinaryExpression($this, 'AND', $oExpr);
  80. }
  81. public function LogOr($oExpr)
  82. {
  83. return new BinaryExpression($this, 'OR', $oExpr);
  84. }
  85. abstract public function RenameParam($sOldName, $sNewName);
  86. abstract public function RenameAlias($sOldName, $sNewName);
  87. /**
  88. * Make the most relevant label, given the value of the expression
  89. *
  90. * @param DBSearch oFilter The context in which this expression has been used
  91. * @param string sValue The value returned by the query, for this expression
  92. * @param string sDefault The default value if no relevant label could be computed
  93. * @return The label
  94. */
  95. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  96. {
  97. return $sDefault;
  98. }
  99. }
  100. class SQLExpression extends Expression
  101. {
  102. protected $m_sSQL;
  103. public function __construct($sSQL)
  104. {
  105. $this->m_sSQL = $sSQL;
  106. }
  107. public function IsTrue()
  108. {
  109. return false;
  110. }
  111. // recursive rendering
  112. public function Render(&$aArgs = null, $bRetrofitParams = false)
  113. {
  114. return $this->m_sSQL;
  115. }
  116. public function ApplyParameters($aArgs)
  117. {
  118. }
  119. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  120. {
  121. }
  122. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  123. {
  124. return clone $this;
  125. }
  126. public function ListRequiredFields()
  127. {
  128. return array();
  129. }
  130. public function CollectUsedParents(&$aTable)
  131. {
  132. }
  133. public function ListConstantFields()
  134. {
  135. return array();
  136. }
  137. public function RenameParam($sOldName, $sNewName)
  138. {
  139. // Do nothing, since there is nothing to rename
  140. }
  141. public function RenameAlias($sOldName, $sNewName)
  142. {
  143. // Do nothing, since there is nothing to rename
  144. }
  145. }
  146. class BinaryExpression extends Expression
  147. {
  148. protected $m_oLeftExpr; // filter code or an SQL expression (later?)
  149. protected $m_oRightExpr;
  150. protected $m_sOperator;
  151. public function __construct($oLeftExpr, $sOperator, $oRightExpr)
  152. {
  153. if (!is_object($oLeftExpr))
  154. {
  155. throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr)));
  156. }
  157. if (!is_object($oRightExpr))
  158. {
  159. throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr)));
  160. }
  161. if (!$oLeftExpr instanceof Expression)
  162. {
  163. throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr)));
  164. }
  165. if (!$oRightExpr instanceof Expression)
  166. {
  167. throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr)));
  168. }
  169. $this->m_oLeftExpr = $oLeftExpr;
  170. $this->m_oRightExpr = $oRightExpr;
  171. $this->m_sOperator = $sOperator;
  172. }
  173. public function IsTrue()
  174. {
  175. // return true if we are certain that it will be true
  176. if ($this->m_sOperator == 'AND')
  177. {
  178. if ($this->m_oLeftExpr->IsTrue() && $this->m_oRightExpr->IsTrue()) return true;
  179. }
  180. elseif ($this->m_sOperator == 'OR')
  181. {
  182. if ($this->m_oLeftExpr->IsTrue() || $this->m_oRightExpr->IsTrue()) return true;
  183. }
  184. return false;
  185. }
  186. public function GetLeftExpr()
  187. {
  188. return $this->m_oLeftExpr;
  189. }
  190. public function GetRightExpr()
  191. {
  192. return $this->m_oRightExpr;
  193. }
  194. public function GetOperator()
  195. {
  196. return $this->m_sOperator;
  197. }
  198. // recursive rendering
  199. public function Render(&$aArgs = null, $bRetrofitParams = false)
  200. {
  201. $sOperator = $this->GetOperator();
  202. $sLeft = $this->GetLeftExpr()->Render($aArgs, $bRetrofitParams);
  203. $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams);
  204. return "($sLeft $sOperator $sRight)";
  205. }
  206. public function ApplyParameters($aArgs)
  207. {
  208. if ($this->m_oLeftExpr instanceof VariableExpression)
  209. {
  210. $this->m_oLeftExpr = $this->m_oLeftExpr->GetAsScalar($aArgs);
  211. }
  212. else //if ($this->m_oLeftExpr instanceof Expression)
  213. {
  214. $this->m_oLeftExpr->ApplyParameters($aArgs);
  215. }
  216. if ($this->m_oRightExpr instanceof VariableExpression)
  217. {
  218. $this->m_oRightExpr = $this->m_oRightExpr->GetAsScalar($aArgs);
  219. }
  220. else //if ($this->m_oRightExpr instanceof Expression)
  221. {
  222. $this->m_oRightExpr->ApplyParameters($aArgs);
  223. }
  224. }
  225. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  226. {
  227. $this->GetLeftExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  228. $this->GetRightExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  229. }
  230. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  231. {
  232. $oLeft = $this->GetLeftExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  233. $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  234. return new BinaryExpression($oLeft, $this->GetOperator(), $oRight);
  235. }
  236. public function ListRequiredFields()
  237. {
  238. $aLeft = $this->GetLeftExpr()->ListRequiredFields();
  239. $aRight = $this->GetRightExpr()->ListRequiredFields();
  240. return array_merge($aLeft, $aRight);
  241. }
  242. public function CollectUsedParents(&$aTable)
  243. {
  244. $this->GetLeftExpr()->CollectUsedParents($aTable);
  245. $this->GetRightExpr()->CollectUsedParents($aTable);
  246. }
  247. /**
  248. * List all constant expression of the form <field> = <scalar> or <field> = :<variable>
  249. * Could be extended to support <field> = <function><constant_expression>
  250. */
  251. public function ListConstantFields()
  252. {
  253. $aResult = array();
  254. if ($this->m_sOperator == '=')
  255. {
  256. if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof ScalarExpression))
  257. {
  258. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  259. }
  260. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof ScalarExpression))
  261. {
  262. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  263. }
  264. else if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof VariableExpression))
  265. {
  266. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  267. }
  268. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof VariableExpression))
  269. {
  270. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  271. }
  272. }
  273. else if ($this->m_sOperator == 'AND')
  274. {
  275. // Strictly, this should be done only for the AND operator
  276. $aResult = array_merge_recursive($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields());
  277. }
  278. return $aResult;
  279. }
  280. public function RenameParam($sOldName, $sNewName)
  281. {
  282. $this->GetLeftExpr()->RenameParam($sOldName, $sNewName);
  283. $this->GetRightExpr()->RenameParam($sOldName, $sNewName);
  284. }
  285. public function RenameAlias($sOldName, $sNewName)
  286. {
  287. $this->GetLeftExpr()->RenameAlias($sOldName, $sNewName);
  288. $this->GetRightExpr()->RenameAlias($sOldName, $sNewName);
  289. }
  290. }
  291. class UnaryExpression extends Expression
  292. {
  293. protected $m_value;
  294. public function __construct($value)
  295. {
  296. $this->m_value = $value;
  297. }
  298. public function IsTrue()
  299. {
  300. // return true if we are certain that it will be true
  301. return ($this->m_value == 1);
  302. }
  303. public function GetValue()
  304. {
  305. return $this->m_value;
  306. }
  307. // recursive rendering
  308. public function Render(&$aArgs = null, $bRetrofitParams = false)
  309. {
  310. return CMDBSource::Quote($this->m_value);
  311. }
  312. public function ApplyParameters($aArgs)
  313. {
  314. }
  315. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  316. {
  317. }
  318. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  319. {
  320. return clone $this;
  321. }
  322. public function ListRequiredFields()
  323. {
  324. return array();
  325. }
  326. public function CollectUsedParents(&$aTable)
  327. {
  328. }
  329. public function ListConstantFields()
  330. {
  331. return array();
  332. }
  333. public function RenameParam($sOldName, $sNewName)
  334. {
  335. // Do nothing
  336. // really ? what about :param{$iParamIndex} ??
  337. }
  338. public function RenameAlias($sOldName, $sNewName)
  339. {
  340. // Do nothing
  341. }
  342. }
  343. class ScalarExpression extends UnaryExpression
  344. {
  345. public function __construct($value)
  346. {
  347. if (!is_scalar($value) && !is_null($value) && (!$value instanceof OqlHexValue))
  348. {
  349. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  350. }
  351. parent::__construct($value);
  352. }
  353. // recursive rendering
  354. public function Render(&$aArgs = null, $bRetrofitParams = false)
  355. {
  356. if (is_null($this->m_value))
  357. {
  358. $sRet = 'NULL';
  359. }
  360. else
  361. {
  362. $sRet = CMDBSource::Quote($this->m_value);
  363. }
  364. return $sRet;
  365. }
  366. public function GetAsScalar($aArgs)
  367. {
  368. return clone $this;
  369. }
  370. }
  371. class TrueExpression extends ScalarExpression
  372. {
  373. public function __construct()
  374. {
  375. parent::__construct(1);
  376. }
  377. public function IsTrue()
  378. {
  379. return true;
  380. }
  381. }
  382. class FalseExpression extends ScalarExpression
  383. {
  384. public function __construct()
  385. {
  386. parent::__construct(0);
  387. }
  388. public function IsTrue()
  389. {
  390. return false;
  391. }
  392. }
  393. class FieldExpression extends UnaryExpression
  394. {
  395. protected $m_sParent;
  396. protected $m_sName;
  397. public function __construct($sName, $sParent = '')
  398. {
  399. parent::__construct("$sParent.$sName");
  400. $this->m_sParent = $sParent;
  401. $this->m_sName = $sName;
  402. }
  403. public function IsTrue()
  404. {
  405. // return true if we are certain that it will be true
  406. return false;
  407. }
  408. public function GetParent() {return $this->m_sParent;}
  409. public function GetName() {return $this->m_sName;}
  410. // recursive rendering
  411. public function Render(&$aArgs = null, $bRetrofitParams = false)
  412. {
  413. if (empty($this->m_sParent))
  414. {
  415. return "`{$this->m_sName}`";
  416. }
  417. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  418. }
  419. public function ListRequiredFields()
  420. {
  421. return array($this->m_sParent.'.'.$this->m_sName);
  422. }
  423. public function CollectUsedParents(&$aTable)
  424. {
  425. $aTable[$this->m_sParent] = true;
  426. }
  427. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  428. {
  429. if ($this->m_sParent == $sAlias)
  430. {
  431. // Add a reference to the field
  432. $aUnresolved[$this->m_sName] = $this;
  433. }
  434. elseif ($sAlias == '')
  435. {
  436. // An empty alias means "any alias"
  437. // In such a case, the results are indexed differently
  438. $aUnresolved[$this->m_sParent][$this->m_sName] = $this;
  439. }
  440. }
  441. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  442. {
  443. if (!array_key_exists($this->m_sParent, $aTranslationData))
  444. {
  445. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  446. return clone $this;
  447. }
  448. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  449. {
  450. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  451. {
  452. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  453. 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])));
  454. return clone $this;
  455. }
  456. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  457. $sNewName = $this->m_sName;
  458. if ($bMarkFieldsAsResolved)
  459. {
  460. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  461. }
  462. else
  463. {
  464. $oRet = new FieldExpression($sNewName, $sNewParent);
  465. }
  466. }
  467. else
  468. {
  469. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  470. }
  471. return $oRet;
  472. }
  473. /**
  474. * Make the most relevant label, given the value of the expression
  475. *
  476. * @param DBSearch oFilter The context in which this expression has been used
  477. * @param string sValue The value returned by the query, for this expression
  478. * @param string sDefault The default value if no relevant label could be computed
  479. * @return The label
  480. */
  481. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  482. {
  483. $sAttCode = $this->GetName();
  484. $sParentAlias = $this->GetParent();
  485. $aSelectedClasses = $oFilter->GetSelectedClasses();
  486. $sClass = $aSelectedClasses[$sParentAlias];
  487. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  488. // Set a default value for the general case
  489. $sRes = $oAttDef->GetAsHtml($sValue);
  490. // Exceptions...
  491. if ($oAttDef->IsExternalKey())
  492. {
  493. $sObjClass = $oAttDef->GetTargetClass();
  494. $iObjKey = (int)$sValue;
  495. if ($iObjKey > 0)
  496. {
  497. $oObject = MetaModel::GetObject($sObjClass, $iObjKey);
  498. $sRes = $oObject->GetHyperlink();
  499. }
  500. else
  501. {
  502. // Undefined
  503. $sRes = DBObject::MakeHyperLink($sObjClass, 0);
  504. }
  505. }
  506. elseif ($oAttDef->IsExternalField())
  507. {
  508. if (is_null($sValue))
  509. {
  510. $sRes = Dict::S('UI:UndefinedObject');
  511. }
  512. }
  513. return $sRes;
  514. }
  515. public function RenameAlias($sOldName, $sNewName)
  516. {
  517. if ($this->m_sParent == $sOldName)
  518. {
  519. $this->m_sParent = $sNewName;
  520. }
  521. }
  522. }
  523. // Has been resolved into an SQL expression
  524. class FieldExpressionResolved extends FieldExpression
  525. {
  526. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  527. {
  528. }
  529. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  530. {
  531. return clone $this;
  532. }
  533. }
  534. class VariableExpression extends UnaryExpression
  535. {
  536. protected $m_sName;
  537. public function __construct($sName)
  538. {
  539. parent::__construct($sName);
  540. $this->m_sName = $sName;
  541. }
  542. public function IsTrue()
  543. {
  544. // return true if we are certain that it will be true
  545. return false;
  546. }
  547. public function GetName() {return $this->m_sName;}
  548. // recursive rendering
  549. public function Render(&$aArgs = null, $bRetrofitParams = false)
  550. {
  551. if (is_null($aArgs))
  552. {
  553. return ':'.$this->m_sName;
  554. }
  555. elseif (array_key_exists($this->m_sName, $aArgs))
  556. {
  557. return CMDBSource::Quote($aArgs[$this->m_sName]);
  558. }
  559. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  560. {
  561. $sParamName = substr($this->m_sName, 0, $iPos);
  562. if (array_key_exists($sParamName.'->object()', $aArgs))
  563. {
  564. $sAttCode = substr($this->m_sName, $iPos + 2);
  565. $oObj = $aArgs[$sParamName.'->object()'];
  566. if ($sAttCode == 'id')
  567. {
  568. return CMDBSource::Quote($oObj->GetKey());
  569. }
  570. return CMDBSource::Quote($oObj->Get($sAttCode));
  571. }
  572. }
  573. if ($bRetrofitParams)
  574. {
  575. $aArgs[$this->m_sName] = null;
  576. return ':'.$this->m_sName;
  577. }
  578. else
  579. {
  580. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  581. }
  582. }
  583. public function RenameParam($sOldName, $sNewName)
  584. {
  585. if ($this->m_sName == $sOldName)
  586. {
  587. $this->m_sName = $sNewName;
  588. }
  589. }
  590. public function GetAsScalar($aArgs)
  591. {
  592. $oRet = null;
  593. if (array_key_exists($this->m_sName, $aArgs))
  594. {
  595. $oRet = new ScalarExpression($aArgs[$this->m_sName]);
  596. }
  597. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  598. {
  599. $sParamName = substr($this->m_sName, 0, $iPos);
  600. if (array_key_exists($sParamName.'->object()', $aArgs))
  601. {
  602. $sAttCode = substr($this->m_sName, $iPos + 2);
  603. $oObj = $aArgs[$sParamName.'->object()'];
  604. if ($sAttCode == 'id')
  605. {
  606. $oRet = new ScalarExpression($oObj->GetKey());
  607. }
  608. elseif (MetaModel::IsValidAttCode(get_class($oObj), $sAttCode))
  609. {
  610. $oRet = new ScalarExpression($oObj->Get($sAttCode));
  611. }
  612. else
  613. {
  614. throw new CoreException("Query argument {$this->m_sName} not matching any attribute of class ".get_class($oObj));
  615. }
  616. }
  617. }
  618. if (is_null($oRet))
  619. {
  620. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  621. }
  622. return $oRet;
  623. }
  624. }
  625. // Temporary, until we implement functions and expression casting!
  626. // ... or until we implement a real full text search based in the MATCH() expression
  627. class ListExpression extends Expression
  628. {
  629. protected $m_aExpressions;
  630. public function __construct($aExpressions)
  631. {
  632. $this->m_aExpressions = $aExpressions;
  633. }
  634. public static function FromScalars($aScalars)
  635. {
  636. $aExpressions = array();
  637. foreach($aScalars as $value)
  638. {
  639. $aExpressions[] = new ScalarExpression($value);
  640. }
  641. return new ListExpression($aExpressions);
  642. }
  643. public function IsTrue()
  644. {
  645. // return true if we are certain that it will be true
  646. return false;
  647. }
  648. public function GetItems()
  649. {
  650. return $this->m_aExpressions;
  651. }
  652. // recursive rendering
  653. public function Render(&$aArgs = null, $bRetrofitParams = false)
  654. {
  655. $aRes = array();
  656. foreach ($this->m_aExpressions as $oExpr)
  657. {
  658. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  659. }
  660. return '('.implode(', ', $aRes).')';
  661. }
  662. public function ApplyParameters($aArgs)
  663. {
  664. $aRes = array();
  665. foreach ($this->m_aExpressions as $idx => $oExpr)
  666. {
  667. if ($oExpr instanceof VariableExpression)
  668. {
  669. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  670. }
  671. else
  672. {
  673. $oExpr->ApplyParameters($aArgs);
  674. }
  675. }
  676. }
  677. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  678. {
  679. foreach ($this->m_aExpressions as $oExpr)
  680. {
  681. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  682. }
  683. }
  684. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  685. {
  686. $aRes = array();
  687. foreach ($this->m_aExpressions as $oExpr)
  688. {
  689. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  690. }
  691. return new ListExpression($aRes);
  692. }
  693. public function ListRequiredFields()
  694. {
  695. $aRes = array();
  696. foreach ($this->m_aExpressions as $oExpr)
  697. {
  698. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  699. }
  700. return $aRes;
  701. }
  702. public function CollectUsedParents(&$aTable)
  703. {
  704. foreach ($this->m_aExpressions as $oExpr)
  705. {
  706. $oExpr->CollectUsedParents($aTable);
  707. }
  708. }
  709. public function ListConstantFields()
  710. {
  711. $aRes = array();
  712. foreach ($this->m_aExpressions as $oExpr)
  713. {
  714. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  715. }
  716. return $aRes;
  717. }
  718. public function RenameParam($sOldName, $sNewName)
  719. {
  720. $aRes = array();
  721. foreach ($this->m_aExpressions as $key => $oExpr)
  722. {
  723. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  724. }
  725. }
  726. public function RenameAlias($sOldName, $sNewName)
  727. {
  728. $aRes = array();
  729. foreach ($this->m_aExpressions as $key => $oExpr)
  730. {
  731. $oExpr->RenameAlias($sOldName, $sNewName);
  732. }
  733. }
  734. }
  735. class FunctionExpression extends Expression
  736. {
  737. protected $m_sVerb;
  738. protected $m_aArgs; // array of expressions
  739. public function __construct($sVerb, $aArgExpressions)
  740. {
  741. $this->m_sVerb = $sVerb;
  742. $this->m_aArgs = $aArgExpressions;
  743. }
  744. public function IsTrue()
  745. {
  746. // return true if we are certain that it will be true
  747. return false;
  748. }
  749. public function GetVerb()
  750. {
  751. return $this->m_sVerb;
  752. }
  753. public function GetArgs()
  754. {
  755. return $this->m_aArgs;
  756. }
  757. // recursive rendering
  758. public function Render(&$aArgs = null, $bRetrofitParams = false)
  759. {
  760. $aRes = array();
  761. foreach ($this->m_aArgs as $iPos => $oExpr)
  762. {
  763. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  764. }
  765. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  766. }
  767. public function ApplyParameters($aArgs)
  768. {
  769. $aRes = array();
  770. foreach ($this->m_aArgs as $idx => $oExpr)
  771. {
  772. if ($oExpr instanceof VariableExpression)
  773. {
  774. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  775. }
  776. else
  777. {
  778. $oExpr->ApplyParameters($aArgs);
  779. }
  780. }
  781. }
  782. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  783. {
  784. foreach ($this->m_aArgs as $oExpr)
  785. {
  786. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  787. }
  788. }
  789. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  790. {
  791. $aRes = array();
  792. foreach ($this->m_aArgs as $oExpr)
  793. {
  794. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  795. }
  796. return new FunctionExpression($this->m_sVerb, $aRes);
  797. }
  798. public function ListRequiredFields()
  799. {
  800. $aRes = array();
  801. foreach ($this->m_aArgs as $oExpr)
  802. {
  803. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  804. }
  805. return $aRes;
  806. }
  807. public function CollectUsedParents(&$aTable)
  808. {
  809. foreach ($this->m_aArgs as $oExpr)
  810. {
  811. $oExpr->CollectUsedParents($aTable);
  812. }
  813. }
  814. public function ListConstantFields()
  815. {
  816. $aRes = array();
  817. foreach ($this->m_aArgs as $oExpr)
  818. {
  819. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  820. }
  821. return $aRes;
  822. }
  823. public function RenameParam($sOldName, $sNewName)
  824. {
  825. foreach ($this->m_aArgs as $key => $oExpr)
  826. {
  827. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  828. }
  829. }
  830. public function RenameAlias($sOldName, $sNewName)
  831. {
  832. foreach ($this->m_aArgs as $key => $oExpr)
  833. {
  834. $oExpr->RenameAlias($sOldName, $sNewName);
  835. }
  836. }
  837. /**
  838. * Make the most relevant label, given the value of the expression
  839. *
  840. * @param DBSearch oFilter The context in which this expression has been used
  841. * @param string sValue The value returned by the query, for this expression
  842. * @param string sDefault The default value if no relevant label could be computed
  843. * @return The label
  844. */
  845. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  846. {
  847. static $aWeekDayToString = null;
  848. if (is_null($aWeekDayToString))
  849. {
  850. // Init the correspondance table
  851. $aWeekDayToString = array(
  852. 0 => Dict::S('DayOfWeek-Sunday'),
  853. 1 => Dict::S('DayOfWeek-Monday'),
  854. 2 => Dict::S('DayOfWeek-Tuesday'),
  855. 3 => Dict::S('DayOfWeek-Wednesday'),
  856. 4 => Dict::S('DayOfWeek-Thursday'),
  857. 5 => Dict::S('DayOfWeek-Friday'),
  858. 6 => Dict::S('DayOfWeek-Saturday')
  859. );
  860. }
  861. static $aMonthToString = null;
  862. if (is_null($aMonthToString))
  863. {
  864. // Init the correspondance table
  865. $aMonthToString = array(
  866. 1 => Dict::S('Month-01'),
  867. 2 => Dict::S('Month-02'),
  868. 3 => Dict::S('Month-03'),
  869. 4 => Dict::S('Month-04'),
  870. 5 => Dict::S('Month-05'),
  871. 6 => Dict::S('Month-06'),
  872. 7 => Dict::S('Month-07'),
  873. 8 => Dict::S('Month-08'),
  874. 9 => Dict::S('Month-09'),
  875. 10 => Dict::S('Month-10'),
  876. 11 => Dict::S('Month-11'),
  877. 12 => Dict::S('Month-12'),
  878. );
  879. }
  880. $sRes = $sDefault;
  881. if (strtolower($this->m_sVerb) == 'date_format')
  882. {
  883. $oFormatExpr = $this->m_aArgs[1];
  884. if ($oFormatExpr->Render() == "'%w'")
  885. {
  886. if (isset($aWeekDayToString[(int)$sValue]))
  887. {
  888. $sRes = $aWeekDayToString[(int)$sValue];
  889. }
  890. }
  891. elseif ($oFormatExpr->Render() == "'%Y-%m'")
  892. {
  893. // yyyy-mm => "yyyy month"
  894. $iMonth = (int) substr($sValue, -2); // the two last chars
  895. $sRes = substr($sValue, 0, 4).' '.$aMonthToString[$iMonth];
  896. }
  897. elseif ($oFormatExpr->Render() == "'%Y-%m-%d'")
  898. {
  899. // yyyy-mm-dd => "month d"
  900. $iMonth = (int) substr($sValue, 5, 2);
  901. $sRes = $aMonthToString[$iMonth].' '.(int)substr($sValue, -2);
  902. }
  903. }
  904. return $sRes;
  905. }
  906. }
  907. class IntervalExpression extends Expression
  908. {
  909. protected $m_oValue; // expression
  910. protected $m_sUnit;
  911. public function __construct($oValue, $sUnit)
  912. {
  913. $this->m_oValue = $oValue;
  914. $this->m_sUnit = $sUnit;
  915. }
  916. public function IsTrue()
  917. {
  918. // return true if we are certain that it will be true
  919. return false;
  920. }
  921. public function GetValue()
  922. {
  923. return $this->m_oValue;
  924. }
  925. public function GetUnit()
  926. {
  927. return $this->m_sUnit;
  928. }
  929. // recursive rendering
  930. public function Render(&$aArgs = null, $bRetrofitParams = false)
  931. {
  932. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  933. }
  934. public function ApplyParameters($aArgs)
  935. {
  936. if ($this->m_oValue instanceof VariableExpression)
  937. {
  938. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  939. }
  940. else
  941. {
  942. $this->m_oValue->ApplyParameters($aArgs);
  943. }
  944. }
  945. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  946. {
  947. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  948. }
  949. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  950. {
  951. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  952. }
  953. public function ListRequiredFields()
  954. {
  955. return array();
  956. }
  957. public function CollectUsedParents(&$aTable)
  958. {
  959. }
  960. public function ListConstantFields()
  961. {
  962. return array();
  963. }
  964. public function RenameParam($sOldName, $sNewName)
  965. {
  966. $this->m_oValue->RenameParam($sOldName, $sNewName);
  967. }
  968. public function RenameAlias($sOldName, $sNewName)
  969. {
  970. $this->m_oValue->RenameAlias($sOldName, $sNewName);
  971. }
  972. }
  973. class CharConcatExpression extends Expression
  974. {
  975. protected $m_aExpressions;
  976. public function __construct($aExpressions)
  977. {
  978. $this->m_aExpressions = $aExpressions;
  979. }
  980. public function IsTrue()
  981. {
  982. // return true if we are certain that it will be true
  983. return false;
  984. }
  985. public function GetItems()
  986. {
  987. return $this->m_aExpressions;
  988. }
  989. // recursive rendering
  990. public function Render(&$aArgs = null, $bRetrofitParams = false)
  991. {
  992. $aRes = array();
  993. foreach ($this->m_aExpressions as $oExpr)
  994. {
  995. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  996. // Concat will be globally NULL if one single argument is null !
  997. $aRes[] = "COALESCE($sCol, '')";
  998. }
  999. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  1000. }
  1001. public function ApplyParameters($aArgs)
  1002. {
  1003. $aRes = array();
  1004. foreach ($this->m_aExpressions as $idx => $oExpr)
  1005. {
  1006. if ($oExpr instanceof VariableExpression)
  1007. {
  1008. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  1009. }
  1010. else
  1011. {
  1012. $this->m_aExpressions->ApplyParameters($aArgs);
  1013. }
  1014. }
  1015. }
  1016. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1017. {
  1018. foreach ($this->m_aExpressions as $oExpr)
  1019. {
  1020. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1021. }
  1022. }
  1023. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1024. {
  1025. $aRes = array();
  1026. foreach ($this->m_aExpressions as $oExpr)
  1027. {
  1028. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1029. }
  1030. return new CharConcatExpression($aRes);
  1031. }
  1032. public function ListRequiredFields()
  1033. {
  1034. $aRes = array();
  1035. foreach ($this->m_aExpressions as $oExpr)
  1036. {
  1037. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  1038. }
  1039. return $aRes;
  1040. }
  1041. public function CollectUsedParents(&$aTable)
  1042. {
  1043. foreach ($this->m_aExpressions as $oExpr)
  1044. {
  1045. $oExpr->CollectUsedParents($aTable);
  1046. }
  1047. }
  1048. public function ListConstantFields()
  1049. {
  1050. $aRes = array();
  1051. foreach ($this->m_aExpressions as $oExpr)
  1052. {
  1053. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  1054. }
  1055. return $aRes;
  1056. }
  1057. public function RenameParam($sOldName, $sNewName)
  1058. {
  1059. foreach ($this->m_aExpressions as $key => $oExpr)
  1060. {
  1061. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  1062. }
  1063. }
  1064. public function RenameAlias($sOldName, $sNewName)
  1065. {
  1066. foreach ($this->m_aExpressions as $key => $oExpr)
  1067. {
  1068. $oExpr->RenameAlias($sOldName, $sNewName);
  1069. }
  1070. }
  1071. }
  1072. class CharConcatWSExpression extends CharConcatExpression
  1073. {
  1074. protected $m_separator;
  1075. public function __construct($separator, $aExpressions)
  1076. {
  1077. $this->m_separator = $separator;
  1078. parent::__construct($aExpressions);
  1079. }
  1080. // recursive rendering
  1081. public function Render(&$aArgs = null, $bRetrofitParams = false)
  1082. {
  1083. $aRes = array();
  1084. foreach ($this->m_aExpressions as $oExpr)
  1085. {
  1086. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  1087. // Concat will be globally NULL if one single argument is null !
  1088. $aRes[] = "COALESCE($sCol, '')";
  1089. }
  1090. $sSep = CMDBSource::Quote($this->m_separator);
  1091. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  1092. }
  1093. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1094. {
  1095. $aRes = array();
  1096. foreach ($this->m_aExpressions as $oExpr)
  1097. {
  1098. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1099. }
  1100. return new CharConcatWSExpression($this->m_separator, $aRes);
  1101. }
  1102. }
  1103. class QueryBuilderExpressions
  1104. {
  1105. protected $m_oConditionExpr;
  1106. protected $m_aSelectExpr;
  1107. protected $m_aGroupByExpr;
  1108. protected $m_aJoinFields;
  1109. protected $m_aClassIds;
  1110. public function __construct($oSearch, $aGroupByExpr = null)
  1111. {
  1112. $this->m_oConditionExpr = $oSearch->GetCriteria();
  1113. $this->m_aSelectExpr = array();
  1114. $this->m_aGroupByExpr = $aGroupByExpr;
  1115. $this->m_aJoinFields = array();
  1116. $this->m_aClassIds = array();
  1117. foreach($oSearch->GetJoinedClasses() as $sClassAlias => $sClass)
  1118. {
  1119. $this->m_aClassIds[$sClassAlias] = new FieldExpression('id', $sClassAlias);
  1120. }
  1121. }
  1122. public function GetSelect()
  1123. {
  1124. return $this->m_aSelectExpr;
  1125. }
  1126. public function GetGroupBy()
  1127. {
  1128. return $this->m_aGroupByExpr;
  1129. }
  1130. public function GetCondition()
  1131. {
  1132. return $this->m_oConditionExpr;
  1133. }
  1134. public function PopJoinField()
  1135. {
  1136. return array_pop($this->m_aJoinFields);
  1137. }
  1138. public function AddSelect($sAttAlias, $oExpression)
  1139. {
  1140. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  1141. }
  1142. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  1143. public function AddCondition($oExpression)
  1144. {
  1145. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  1146. }
  1147. public function PushJoinField($oExpression)
  1148. {
  1149. array_push($this->m_aJoinFields, $oExpression);
  1150. }
  1151. /**
  1152. * Get tables representing the queried objects
  1153. * Could be further optimized: when the first join is an outer join, then the rest can be omitted
  1154. */
  1155. public function GetMandatoryTables(&$aTables = null)
  1156. {
  1157. if (is_null($aTables)) $aTables = array();
  1158. foreach($this->m_aClassIds as $sClass => $oExpression)
  1159. {
  1160. $oExpression->CollectUsedParents($aTables);
  1161. }
  1162. }
  1163. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1164. {
  1165. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1166. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1167. {
  1168. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1169. }
  1170. if ($this->m_aGroupByExpr)
  1171. {
  1172. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1173. {
  1174. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1175. }
  1176. }
  1177. foreach($this->m_aJoinFields as $oExpression)
  1178. {
  1179. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  1180. }
  1181. }
  1182. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1183. {
  1184. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1185. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1186. {
  1187. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1188. }
  1189. if ($this->m_aGroupByExpr)
  1190. {
  1191. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1192. {
  1193. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1194. }
  1195. }
  1196. foreach($this->m_aJoinFields as $index => $oExpression)
  1197. {
  1198. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1199. }
  1200. foreach($this->m_aClassIds as $sClass => $oExpression)
  1201. {
  1202. $this->m_aClassIds[$sClass] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1203. }
  1204. }
  1205. public function RenameParam($sOldName, $sNewName)
  1206. {
  1207. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  1208. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1209. {
  1210. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1211. }
  1212. if ($this->m_aGroupByExpr)
  1213. {
  1214. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1215. {
  1216. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1217. }
  1218. }
  1219. foreach($this->m_aJoinFields as $index => $oExpression)
  1220. {
  1221. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  1222. }
  1223. }
  1224. }
  1225. ?>