expression.class.inc.php 33 KB

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