expression.class.inc.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. elseif ($sAlias == '')
  392. {
  393. // An empty alias means "any alias"
  394. // In such a case, the results are indexed differently
  395. $aUnresolved[$this->m_sParent][$this->m_sName] = $this;
  396. }
  397. }
  398. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  399. {
  400. if (!array_key_exists($this->m_sParent, $aTranslationData))
  401. {
  402. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  403. return clone $this;
  404. }
  405. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  406. {
  407. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  408. {
  409. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  410. 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])));
  411. return clone $this;
  412. }
  413. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  414. $sNewName = $this->m_sName;
  415. if ($bMarkFieldsAsResolved)
  416. {
  417. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  418. }
  419. else
  420. {
  421. $oRet = new FieldExpression($sNewName, $sNewParent);
  422. }
  423. }
  424. else
  425. {
  426. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  427. }
  428. return $oRet;
  429. }
  430. /**
  431. * Make the most relevant label, given the value of the expression
  432. *
  433. * @param DBObjectSearch oFilter The context in which this expression has been used
  434. * @param string sValue The value returned by the query, for this expression
  435. * @param string sDefault The default value if no relevant label could be computed
  436. * @return The label
  437. */
  438. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  439. {
  440. $sAttCode = $this->GetName();
  441. $sParentAlias = $this->GetParent();
  442. $aSelectedClasses = $oFilter->GetSelectedClasses();
  443. $sClass = $aSelectedClasses[$sParentAlias];
  444. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  445. // Set a default value for the general case
  446. $sRes = $oAttDef->GetAsHtml($sValue);
  447. // Exceptions...
  448. if ($oAttDef->IsExternalKey())
  449. {
  450. $sObjClass = $oAttDef->GetTargetClass();
  451. $iObjKey = (int)$sValue;
  452. if ($iObjKey > 0)
  453. {
  454. $oObject = MetaModel::GetObject($sObjClass, $iObjKey);
  455. $sRes = $oObject->GetHyperlink();
  456. }
  457. else
  458. {
  459. // Undefined
  460. $sRes = DBObject::MakeHyperLink($sObjClass, 0);
  461. }
  462. }
  463. elseif ($oAttDef->IsExternalField())
  464. {
  465. if (is_null($sValue))
  466. {
  467. $sRes = Dict::S('UI:UndefinedObject');
  468. }
  469. }
  470. return $sRes;
  471. }
  472. }
  473. // Has been resolved into an SQL expression
  474. class FieldExpressionResolved extends FieldExpression
  475. {
  476. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  477. {
  478. }
  479. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  480. {
  481. return clone $this;
  482. }
  483. }
  484. class VariableExpression extends UnaryExpression
  485. {
  486. protected $m_sName;
  487. public function __construct($sName)
  488. {
  489. parent::__construct($sName);
  490. $this->m_sName = $sName;
  491. }
  492. public function IsTrue()
  493. {
  494. // return true if we are certain that it will be true
  495. return false;
  496. }
  497. public function GetName() {return $this->m_sName;}
  498. // recursive rendering
  499. public function Render(&$aArgs = null, $bRetrofitParams = false)
  500. {
  501. if (is_null($aArgs))
  502. {
  503. return ':'.$this->m_sName;
  504. }
  505. elseif (array_key_exists($this->m_sName, $aArgs))
  506. {
  507. return CMDBSource::Quote($aArgs[$this->m_sName]);
  508. }
  509. elseif ($bRetrofitParams)
  510. {
  511. $aArgs[$this->m_sName] = null;
  512. return ':'.$this->m_sName;
  513. }
  514. else
  515. {
  516. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
  517. }
  518. }
  519. public function RenameParam($sOldName, $sNewName)
  520. {
  521. if ($this->m_sName == $sOldName)
  522. {
  523. $this->m_sName = $sNewName;
  524. }
  525. }
  526. public function GetAsScalar($aArgs)
  527. {
  528. $value = '';
  529. if (array_key_exists($this->m_sName, $aArgs))
  530. {
  531. $value = $aArgs[$this->m_sName];
  532. }
  533. else
  534. {
  535. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  536. }
  537. return new ScalarExpression($value);
  538. }
  539. }
  540. // Temporary, until we implement functions and expression casting!
  541. // ... or until we implement a real full text search based in the MATCH() expression
  542. class ListExpression extends Expression
  543. {
  544. protected $m_aExpressions;
  545. public function __construct($aExpressions)
  546. {
  547. $this->m_aExpressions = $aExpressions;
  548. }
  549. public static function FromScalars($aScalars)
  550. {
  551. $aExpressions = array();
  552. foreach($aScalars as $value)
  553. {
  554. $aExpressions[] = new ScalarExpression($value);
  555. }
  556. return new ListExpression($aExpressions);
  557. }
  558. public function IsTrue()
  559. {
  560. // return true if we are certain that it will be true
  561. return false;
  562. }
  563. public function GetItems()
  564. {
  565. return $this->m_aExpressions;
  566. }
  567. // recursive rendering
  568. public function Render(&$aArgs = null, $bRetrofitParams = false)
  569. {
  570. $aRes = array();
  571. foreach ($this->m_aExpressions as $oExpr)
  572. {
  573. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  574. }
  575. return '('.implode(', ', $aRes).')';
  576. }
  577. public function ApplyParameters($aArgs)
  578. {
  579. $aRes = array();
  580. foreach ($this->m_aExpressions as $idx => $oExpr)
  581. {
  582. if ($oExpr instanceof VariableExpression)
  583. {
  584. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  585. }
  586. else
  587. {
  588. $oExpr->ApplyParameters($aArgs);
  589. }
  590. }
  591. }
  592. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  593. {
  594. foreach ($this->m_aExpressions as $oExpr)
  595. {
  596. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  597. }
  598. }
  599. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  600. {
  601. $aRes = array();
  602. foreach ($this->m_aExpressions as $oExpr)
  603. {
  604. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  605. }
  606. return new ListExpression($aRes);
  607. }
  608. public function ListRequiredFields()
  609. {
  610. $aRes = array();
  611. foreach ($this->m_aExpressions as $oExpr)
  612. {
  613. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  614. }
  615. return $aRes;
  616. }
  617. public function ListConstantFields()
  618. {
  619. $aRes = array();
  620. foreach ($this->m_aExpressions as $oExpr)
  621. {
  622. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  623. }
  624. return $aRes;
  625. }
  626. public function RenameParam($sOldName, $sNewName)
  627. {
  628. $aRes = array();
  629. foreach ($this->m_aExpressions as $key => $oExpr)
  630. {
  631. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  632. }
  633. }
  634. }
  635. class FunctionExpression extends Expression
  636. {
  637. protected $m_sVerb;
  638. protected $m_aArgs; // array of expressions
  639. public function __construct($sVerb, $aArgExpressions)
  640. {
  641. $this->m_sVerb = $sVerb;
  642. $this->m_aArgs = $aArgExpressions;
  643. }
  644. public function IsTrue()
  645. {
  646. // return true if we are certain that it will be true
  647. return false;
  648. }
  649. public function GetVerb()
  650. {
  651. return $this->m_sVerb;
  652. }
  653. public function GetArgs()
  654. {
  655. return $this->m_aArgs;
  656. }
  657. // recursive rendering
  658. public function Render(&$aArgs = null, $bRetrofitParams = false)
  659. {
  660. $aRes = array();
  661. foreach ($this->m_aArgs as $oExpr)
  662. {
  663. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  664. }
  665. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  666. }
  667. public function ApplyParameters($aArgs)
  668. {
  669. $aRes = array();
  670. foreach ($this->m_aArgs as $idx => $oExpr)
  671. {
  672. if ($oExpr instanceof VariableExpression)
  673. {
  674. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  675. }
  676. else
  677. {
  678. $oExpr->ApplyParameters($aArgs);
  679. }
  680. }
  681. }
  682. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  683. {
  684. foreach ($this->m_aArgs as $oExpr)
  685. {
  686. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  687. }
  688. }
  689. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  690. {
  691. $aRes = array();
  692. foreach ($this->m_aArgs as $oExpr)
  693. {
  694. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  695. }
  696. return new FunctionExpression($this->m_sVerb, $aRes);
  697. }
  698. public function ListRequiredFields()
  699. {
  700. $aRes = array();
  701. foreach ($this->m_aArgs as $oExpr)
  702. {
  703. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  704. }
  705. return $aRes;
  706. }
  707. public function ListConstantFields()
  708. {
  709. $aRes = array();
  710. foreach ($this->m_aArgs as $oExpr)
  711. {
  712. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  713. }
  714. return $aRes;
  715. }
  716. public function RenameParam($sOldName, $sNewName)
  717. {
  718. foreach ($this->m_aArgs as $key => $oExpr)
  719. {
  720. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  721. }
  722. }
  723. /**
  724. * Make the most relevant label, given the value of the expression
  725. *
  726. * @param DBObjectSearch oFilter The context in which this expression has been used
  727. * @param string sValue The value returned by the query, for this expression
  728. * @param string sDefault The default value if no relevant label could be computed
  729. * @return The label
  730. */
  731. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  732. {
  733. $sRes = $sDefault;
  734. if (strtolower($this->m_sVerb) == 'date_format')
  735. {
  736. $oFormatExpr = $this->m_aArgs[1];
  737. if ($oFormatExpr->Render() == "'%w'")
  738. {
  739. static $aWeekDayToString = array(
  740. 0 => 'Sunday',
  741. 1 => 'Monday',
  742. 2 => 'Tuesday',
  743. 3 => 'Wednesday',
  744. 4 => 'Thursday',
  745. 5 => 'Friday',
  746. 6 => 'Saturday',
  747. );
  748. if (isset($aWeekDayToString[(int)$sValue]))
  749. {
  750. $sRes = $aWeekDayToString[(int)$sValue];
  751. }
  752. }
  753. }
  754. return $sRes;
  755. }
  756. }
  757. class IntervalExpression extends Expression
  758. {
  759. protected $m_oValue; // expression
  760. protected $m_sUnit;
  761. public function __construct($oValue, $sUnit)
  762. {
  763. $this->m_oValue = $oValue;
  764. $this->m_sUnit = $sUnit;
  765. }
  766. public function IsTrue()
  767. {
  768. // return true if we are certain that it will be true
  769. return false;
  770. }
  771. public function GetValue()
  772. {
  773. return $this->m_oValue;
  774. }
  775. public function GetUnit()
  776. {
  777. return $this->m_sUnit;
  778. }
  779. // recursive rendering
  780. public function Render(&$aArgs = null, $bRetrofitParams = false)
  781. {
  782. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  783. }
  784. public function ApplyParameters($aArgs)
  785. {
  786. if ($this->m_oValue instanceof VariableExpression)
  787. {
  788. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  789. }
  790. else
  791. {
  792. $this->m_oValue->ApplyParameters($aArgs);
  793. }
  794. }
  795. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  796. {
  797. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  798. }
  799. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  800. {
  801. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  802. }
  803. public function ListRequiredFields()
  804. {
  805. return array();
  806. }
  807. public function ListConstantFields()
  808. {
  809. return array();
  810. }
  811. public function RenameParam($sOldName, $sNewName)
  812. {
  813. $this->m_oValue->RenameParam($sOldName, $sNewName);
  814. }
  815. }
  816. class CharConcatExpression extends Expression
  817. {
  818. protected $m_aExpressions;
  819. public function __construct($aExpressions)
  820. {
  821. $this->m_aExpressions = $aExpressions;
  822. }
  823. public function IsTrue()
  824. {
  825. // return true if we are certain that it will be true
  826. return false;
  827. }
  828. public function GetItems()
  829. {
  830. return $this->m_aExpressions;
  831. }
  832. // recursive rendering
  833. public function Render(&$aArgs = null, $bRetrofitParams = false)
  834. {
  835. $aRes = array();
  836. foreach ($this->m_aExpressions as $oExpr)
  837. {
  838. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  839. // Concat will be globally NULL if one single argument is null !
  840. $aRes[] = "COALESCE($sCol, '')";
  841. }
  842. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  843. }
  844. public function ApplyParameters($aArgs)
  845. {
  846. $aRes = array();
  847. foreach ($this->m_aExpressions as $idx => $oExpr)
  848. {
  849. if ($oExpr instanceof VariableExpression)
  850. {
  851. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  852. }
  853. else
  854. {
  855. $this->m_aExpressions->ApplyParameters($aArgs);
  856. }
  857. }
  858. }
  859. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  860. {
  861. foreach ($this->m_aExpressions as $oExpr)
  862. {
  863. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  864. }
  865. }
  866. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  867. {
  868. $aRes = array();
  869. foreach ($this->m_aExpressions as $oExpr)
  870. {
  871. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  872. }
  873. return new CharConcatExpression($aRes);
  874. }
  875. public function ListRequiredFields()
  876. {
  877. $aRes = array();
  878. foreach ($this->m_aExpressions as $oExpr)
  879. {
  880. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  881. }
  882. return $aRes;
  883. }
  884. public function ListConstantFields()
  885. {
  886. $aRes = array();
  887. foreach ($this->m_aExpressions as $oExpr)
  888. {
  889. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  890. }
  891. return $aRes;
  892. }
  893. public function RenameParam($sOldName, $sNewName)
  894. {
  895. foreach ($this->m_aExpressions as $key => $oExpr)
  896. {
  897. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  898. }
  899. }
  900. }
  901. class CharConcatWSExpression extends CharConcatExpression
  902. {
  903. protected $m_separator;
  904. public function __construct($separator, $aExpressions)
  905. {
  906. $this->m_separator = $separator;
  907. parent::__construct($aExpressions);
  908. }
  909. // recursive rendering
  910. public function Render(&$aArgs = null, $bRetrofitParams = false)
  911. {
  912. $aRes = array();
  913. foreach ($this->m_aExpressions as $oExpr)
  914. {
  915. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  916. // Concat will be globally NULL if one single argument is null !
  917. $aRes[] = "COALESCE($sCol, '')";
  918. }
  919. $sSep = CMDBSource::Quote($this->m_separator);
  920. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  921. }
  922. }
  923. class QueryBuilderExpressions
  924. {
  925. protected $m_oConditionExpr;
  926. protected $m_aSelectExpr;
  927. protected $m_aGroupByExpr;
  928. protected $m_aJoinFields;
  929. public function __construct($oCondition, $aGroupByExpr = null)
  930. {
  931. $this->m_oConditionExpr = $oCondition;
  932. $this->m_aSelectExpr = array();
  933. $this->m_aGroupByExpr = $aGroupByExpr;
  934. $this->m_aJoinFields = array();
  935. }
  936. public function GetSelect()
  937. {
  938. return $this->m_aSelectExpr;
  939. }
  940. public function GetGroupBy()
  941. {
  942. return $this->m_aGroupByExpr;
  943. }
  944. public function GetCondition()
  945. {
  946. return $this->m_oConditionExpr;
  947. }
  948. public function PopJoinField()
  949. {
  950. return array_pop($this->m_aJoinFields);
  951. }
  952. public function AddSelect($sAttAlias, $oExpression)
  953. {
  954. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  955. }
  956. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  957. public function AddCondition($oExpression)
  958. {
  959. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  960. }
  961. public function PushJoinField($oExpression)
  962. {
  963. array_push($this->m_aJoinFields, $oExpression);
  964. }
  965. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  966. {
  967. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  968. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  969. {
  970. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  971. }
  972. if ($this->m_aGroupByExpr)
  973. {
  974. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  975. {
  976. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  977. }
  978. }
  979. foreach($this->m_aJoinFields as $oExpression)
  980. {
  981. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  982. }
  983. }
  984. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  985. {
  986. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  987. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  988. {
  989. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  990. }
  991. if ($this->m_aGroupByExpr)
  992. {
  993. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  994. {
  995. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  996. }
  997. }
  998. foreach($this->m_aJoinFields as $index => $oExpression)
  999. {
  1000. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1001. }
  1002. }
  1003. public function RenameParam($sOldName, $sNewName)
  1004. {
  1005. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  1006. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1007. {
  1008. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1009. }
  1010. if ($this->m_aGroupByExpr)
  1011. {
  1012. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1013. {
  1014. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1015. }
  1016. }
  1017. foreach($this->m_aJoinFields as $index => $oExpression)
  1018. {
  1019. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  1020. }
  1021. }
  1022. }
  1023. ?>