expression.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * General definition of an expression tree (could be OQL, SQL or whatever)
  4. *
  5. * @package iTopORM
  6. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  7. * @author Denis Flaven <denisflave@free.fr>
  8. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  9. * @link www.itop.com
  10. * @since 1.0
  11. * @version 1.1.1.1 $
  12. */
  13. abstract class Expression
  14. {
  15. // recursive translation of identifiers
  16. abstract public function Translate($aTranslationData, $bMatchAll = true);
  17. // recursive rendering (aArgs used as input by default, or used as output if bRetrofitParams set to True
  18. abstract public function Render(&$aArgs = null, $bRetrofitParams = false);
  19. // recursively builds an array of class => fieldname
  20. abstract public function ListRequiredFields();
  21. abstract public function IsTrue();
  22. public function RequiresField($sClass, $sFieldName)
  23. {
  24. // #@# todo - optimize : this is called quite often when building a single query !
  25. $aRequired = $this->ListRequiredFields();
  26. if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false;
  27. return true;
  28. }
  29. public function serialize()
  30. {
  31. return base64_encode($this->Render());
  32. }
  33. static public function unserialize($sValue)
  34. {
  35. return self::FromOQL(base64_decode($sValue));
  36. }
  37. static public function FromOQL($sConditionExpr)
  38. {
  39. $oOql = new OqlInterpreter($sConditionExpr);
  40. $oExpression = $oOql->ParseExpression();
  41. return $oExpression;
  42. }
  43. public function LogAnd($oExpr)
  44. {
  45. if ($this->IsTrue()) return clone $oExpr;
  46. if ($oExpr->IsTrue()) return clone $this;
  47. return new BinaryExpression($this, 'AND', $oExpr);
  48. }
  49. public function LogOr($oExpr)
  50. {
  51. return new BinaryExpression($this, 'OR', $oExpr);
  52. }
  53. }
  54. class BinaryExpression extends Expression
  55. {
  56. protected $m_oLeftExpr; // filter code or an SQL expression (later?)
  57. protected $m_oRightExpr;
  58. protected $m_sOperator;
  59. public function __construct($oLeftExpr, $sOperator, $oRightExpr)
  60. {
  61. if (!is_object($oLeftExpr))
  62. {
  63. throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr)));
  64. }
  65. if (!is_object($oRightExpr))
  66. {
  67. throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr)));
  68. }
  69. if (!$oLeftExpr instanceof Expression)
  70. {
  71. throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr)));
  72. }
  73. if (!$oRightExpr instanceof Expression)
  74. {
  75. throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr)));
  76. }
  77. $this->m_oLeftExpr = $oLeftExpr;
  78. $this->m_oRightExpr = $oRightExpr;
  79. $this->m_sOperator = $sOperator;
  80. }
  81. public function IsTrue()
  82. {
  83. // return true if we are certain that it will be true
  84. if ($this->m_sOperator == 'AND')
  85. {
  86. if ($this->m_oLeftExpr->IsTrue() && $this->m_oLeftExpr->IsTrue()) return true;
  87. }
  88. return false;
  89. }
  90. public function GetLeftExpr()
  91. {
  92. return $this->m_oLeftExpr;
  93. }
  94. public function GetRightExpr()
  95. {
  96. return $this->m_oRightExpr;
  97. }
  98. public function GetOperator()
  99. {
  100. return $this->m_sOperator;
  101. }
  102. // recursive rendering
  103. public function Render(&$aArgs = null, $bRetrofitParams = false)
  104. {
  105. $sOperator = $this->GetOperator();
  106. $sLeft = $this->GetLeftExpr()->Render($aArgs, $bRetrofitParams);
  107. $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams);
  108. return "($sLeft $sOperator $sRight)";
  109. }
  110. public function Translate($aTranslationData, $bMatchAll = true)
  111. {
  112. $oLeft = $this->GetLeftExpr()->Translate($aTranslationData, $bMatchAll);
  113. $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll);
  114. return new BinaryExpression($oLeft, $this->GetOperator(), $oRight);
  115. }
  116. public function ListRequiredFields()
  117. {
  118. $aLeft = $this->GetLeftExpr()->ListRequiredFields();
  119. $aRight = $this->GetRightExpr()->ListRequiredFields();
  120. return array_merge($aLeft, $aRight);
  121. }
  122. }
  123. class UnaryExpression extends Expression
  124. {
  125. protected $m_value;
  126. public function __construct($value)
  127. {
  128. $this->m_value = $value;
  129. }
  130. public function IsTrue()
  131. {
  132. // return true if we are certain that it will be true
  133. return ($this->m_value == 1);
  134. }
  135. public function GetValue()
  136. {
  137. return $this->m_value;
  138. }
  139. // recursive rendering
  140. public function Render(&$aArgs = null, $bRetrofitParams = false)
  141. {
  142. if ($bRetrofitParams)
  143. {
  144. $iParamIndex = count($aArgs) + 1; // 1-based indexation
  145. $aArgs['param'.$iParamIndex] = $this->m_value;
  146. return ':param'.$iParamIndex;
  147. }
  148. else
  149. {
  150. return CMDBSource::Quote($this->m_value);
  151. }
  152. }
  153. public function Translate($aTranslationData, $bMatchAll = true)
  154. {
  155. return clone $this;
  156. }
  157. public function ListRequiredFields()
  158. {
  159. return array();
  160. }
  161. }
  162. class ScalarExpression extends UnaryExpression
  163. {
  164. public function __construct($value)
  165. {
  166. if (!is_scalar($value))
  167. {
  168. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  169. }
  170. parent::__construct($value);
  171. }
  172. }
  173. class TrueExpression extends ScalarExpression
  174. {
  175. public function __construct()
  176. {
  177. parent::__construct(1);
  178. }
  179. public function IsTrue()
  180. {
  181. return true;
  182. }
  183. }
  184. class FieldExpression extends UnaryExpression
  185. {
  186. protected $m_sParent;
  187. protected $m_sName;
  188. public function __construct($sName, $sParent = '')
  189. {
  190. parent::__construct("$sParent.$sName");
  191. $this->m_sParent = $sParent;
  192. $this->m_sName = $sName;
  193. }
  194. public function IsTrue()
  195. {
  196. // return true if we are certain that it will be true
  197. return false;
  198. }
  199. public function GetParent() {return $this->m_sParent;}
  200. public function GetName() {return $this->m_sName;}
  201. // recursive rendering
  202. public function Render(&$aArgs = null, $bRetrofitParams = false)
  203. {
  204. if (empty($this->m_sParent))
  205. {
  206. return "`{$this->m_sName}`";
  207. }
  208. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  209. }
  210. public function Translate($aTranslationData, $bMatchAll = true)
  211. {
  212. if (!array_key_exists($this->m_sParent, $aTranslationData))
  213. {
  214. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  215. return clone $this;
  216. }
  217. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  218. {
  219. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  220. {
  221. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  222. 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])));
  223. return clone $this;
  224. }
  225. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  226. $sNewName = $this->m_sName;
  227. }
  228. else
  229. {
  230. $sNewParent = $aTranslationData[$this->m_sParent][$this->m_sName][0];
  231. $sNewName = $aTranslationData[$this->m_sParent][$this->m_sName][1];
  232. }
  233. return new FieldExpression($sNewName, $sNewParent);
  234. }
  235. public function ListRequiredFields()
  236. {
  237. return array($this->m_sParent.'.'.$this->m_sName);
  238. }
  239. }
  240. class VariableExpression extends UnaryExpression
  241. {
  242. protected $m_sName;
  243. public function __construct($sName)
  244. {
  245. parent::__construct($sName);
  246. $this->m_sName = $sName;
  247. }
  248. public function IsTrue()
  249. {
  250. // return true if we are certain that it will be true
  251. return false;
  252. }
  253. public function GetName() {return $this->m_sName;}
  254. // recursive rendering
  255. public function Render(&$aArgs = null, $bRetrofitParams = false)
  256. {
  257. if (is_null($aArgs) || $bRetrofitParams)
  258. {
  259. return ':'.$this->m_sName;
  260. }
  261. elseif (array_key_exists($this->m_sName, $aArgs))
  262. {
  263. return CMDBSource::Quote($aArgs[$this->m_sName]);
  264. }
  265. else
  266. {
  267. throw new CoreException('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
  268. }
  269. }
  270. }
  271. // Temporary, until we implement functions and expression casting!
  272. // ... or until we implement a real full text search based in the MATCH() expression
  273. class ListExpression extends Expression
  274. {
  275. protected $m_aExpressions;
  276. public function __construct($aExpressions)
  277. {
  278. $this->m_aExpressions = $aExpressions;
  279. }
  280. public function IsTrue()
  281. {
  282. // return true if we are certain that it will be true
  283. return false;
  284. }
  285. public function GetItems()
  286. {
  287. return $this->m_aExpressions;
  288. }
  289. // recursive rendering
  290. public function Render(&$aArgs = null, $bRetrofitParams = false)
  291. {
  292. $aRes = array();
  293. foreach ($this->m_aExpressions as $oExpr)
  294. {
  295. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  296. }
  297. return '('.implode(', ', $aRes).')';
  298. }
  299. public function Translate($aTranslationData, $bMatchAll = true)
  300. {
  301. $aRes = array();
  302. foreach ($this->m_aExpressions as $oExpr)
  303. {
  304. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  305. }
  306. return new ListExpression($aRes);
  307. }
  308. public function ListRequiredFields()
  309. {
  310. $aRes = array();
  311. foreach ($this->m_aExpressions as $oExpr)
  312. {
  313. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  314. }
  315. return $aRes;
  316. }
  317. }
  318. class FunctionExpression extends Expression
  319. {
  320. protected $m_sVerb;
  321. protected $m_aArgs; // array of expressions
  322. public function __construct($sVerb, $aArgExpressions)
  323. {
  324. $this->m_sVerb = $sVerb;
  325. $this->m_aArgs = $aArgExpressions;
  326. }
  327. public function IsTrue()
  328. {
  329. // return true if we are certain that it will be true
  330. return false;
  331. }
  332. public function GetVerb()
  333. {
  334. return $this->m_sVerb;
  335. }
  336. public function GetArgs()
  337. {
  338. return $this->m_aArgs;
  339. }
  340. // recursive rendering
  341. public function Render(&$aArgs = null, $bRetrofitParams = false)
  342. {
  343. $aRes = array();
  344. foreach ($this->m_aArgs as $oExpr)
  345. {
  346. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  347. }
  348. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  349. }
  350. public function Translate($aTranslationData, $bMatchAll = true)
  351. {
  352. $aRes = array();
  353. foreach ($this->m_aArgs as $oExpr)
  354. {
  355. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  356. }
  357. return new FunctionExpression($this->m_sVerb, $aRes);
  358. }
  359. public function ListRequiredFields()
  360. {
  361. $aRes = array();
  362. foreach ($this->m_aArgs as $oExpr)
  363. {
  364. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  365. }
  366. return $aRes;
  367. }
  368. }
  369. class IntervalExpression extends Expression
  370. {
  371. protected $m_oValue; // expression
  372. protected $m_sUnit;
  373. public function __construct($oValue, $sUnit)
  374. {
  375. $this->m_oValue = $oValue;
  376. $this->m_sUnit = $sUnit;
  377. }
  378. public function IsTrue()
  379. {
  380. // return true if we are certain that it will be true
  381. return false;
  382. }
  383. public function GetValue()
  384. {
  385. return $this->m_oValue;
  386. }
  387. public function GetUnit()
  388. {
  389. return $this->m_sUnit;
  390. }
  391. // recursive rendering
  392. public function Render(&$aArgs = null, $bRetrofitParams = false)
  393. {
  394. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  395. }
  396. public function Translate($aTranslationData, $bMatchAll = true)
  397. {
  398. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll), $this->m_sUnit);
  399. }
  400. public function ListRequiredFields()
  401. {
  402. return array();
  403. }
  404. }
  405. class CharConcatExpression extends Expression
  406. {
  407. protected $m_aExpressions;
  408. public function __construct($aExpressions)
  409. {
  410. $this->m_aExpressions = $aExpressions;
  411. }
  412. public function IsTrue()
  413. {
  414. // return true if we are certain that it will be true
  415. return false;
  416. }
  417. public function GetItems()
  418. {
  419. return $this->m_aExpressions;
  420. }
  421. // recursive rendering
  422. public function Render(&$aArgs = null, $bRetrofitParams = false)
  423. {
  424. $aRes = array();
  425. foreach ($this->m_aExpressions as $oExpr)
  426. {
  427. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  428. // Concat will be globally NULL if one single argument is null !
  429. $aRes[] = "COALESCE($sCol, '')";
  430. }
  431. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  432. }
  433. public function Translate($aTranslationData, $bMatchAll = true)
  434. {
  435. $aRes = array();
  436. foreach ($this->m_aExpressions as $oExpr)
  437. {
  438. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  439. }
  440. return new CharConcatExpression($aRes);
  441. }
  442. public function ListRequiredFields()
  443. {
  444. $aRes = array();
  445. foreach ($this->m_aExpressions as $oExpr)
  446. {
  447. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  448. }
  449. return $aRes;
  450. }
  451. }
  452. ?>