expression.class.inc.php 29 KB

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