expression.class.inc.php 33 KB

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