expression.class.inc.php 35 KB

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