expression.class.inc.php 34 KB

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