expression.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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
  18. abstract public function Render();
  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()
  104. {
  105. $sOperator = $this->GetOperator();
  106. $sLeft = $this->GetLeftExpr()->Render();
  107. $sRight = $this->GetRightExpr()->Render();
  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()
  141. {
  142. return CMDBSource::Quote($this->m_value);
  143. }
  144. public function Translate($aTranslationData, $bMatchAll = true)
  145. {
  146. return clone $this;
  147. }
  148. public function ListRequiredFields()
  149. {
  150. return array();
  151. }
  152. }
  153. class ScalarExpression extends UnaryExpression
  154. {
  155. public function __construct($value)
  156. {
  157. if (!is_scalar($value))
  158. {
  159. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  160. }
  161. parent::__construct($value);
  162. }
  163. }
  164. class TrueExpression extends ScalarExpression
  165. {
  166. public function __construct()
  167. {
  168. parent::__construct(1);
  169. }
  170. public function IsTrue()
  171. {
  172. return true;
  173. }
  174. }
  175. class FieldExpression extends UnaryExpression
  176. {
  177. protected $m_sParent;
  178. protected $m_sName;
  179. public function __construct($sName, $sParent = '')
  180. {
  181. parent::__construct("$sParent.$sName");
  182. $this->m_sParent = $sParent;
  183. $this->m_sName = $sName;
  184. }
  185. public function IsTrue()
  186. {
  187. // return true if we are certain that it will be true
  188. return false;
  189. }
  190. public function GetParent() {return $this->m_sParent;}
  191. public function GetName() {return $this->m_sName;}
  192. // recursive rendering
  193. public function Render()
  194. {
  195. if (empty($this->m_sParent))
  196. {
  197. return "`{$this->m_sName}`";
  198. }
  199. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  200. }
  201. public function Translate($aTranslationData, $bMatchAll = true)
  202. {
  203. if (!array_key_exists($this->m_sParent, $aTranslationData))
  204. {
  205. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  206. return clone $this;
  207. }
  208. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  209. {
  210. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  211. {
  212. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  213. 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])));
  214. return clone $this;
  215. }
  216. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  217. $sNewName = $this->m_sName;
  218. }
  219. else
  220. {
  221. $sNewParent = $aTranslationData[$this->m_sParent][$this->m_sName][0];
  222. $sNewName = $aTranslationData[$this->m_sParent][$this->m_sName][1];
  223. }
  224. return new FieldExpression($sNewName, $sNewParent);
  225. }
  226. public function ListRequiredFields()
  227. {
  228. return array($this->m_sParent.'.'.$this->m_sName);
  229. }
  230. }
  231. // Temporary, until we implement functions and expression casting!
  232. // ... or until we implement a real full text search based in the MATCH() expression
  233. class ListExpression extends Expression
  234. {
  235. protected $m_aExpressions;
  236. public function __construct($aExpressions)
  237. {
  238. $this->m_aExpressions = $aExpressions;
  239. }
  240. public function IsTrue()
  241. {
  242. // return true if we are certain that it will be true
  243. return false;
  244. }
  245. public function GetItems()
  246. {
  247. return $this->m_aExpressions;
  248. }
  249. // recursive rendering
  250. public function Render()
  251. {
  252. $aRes = array();
  253. foreach ($this->m_aExpressions as $oExpr)
  254. {
  255. $aRes[] = $oExpr->Render();
  256. }
  257. return '('.implode(', ', $aRes).')';
  258. }
  259. public function Translate($aTranslationData, $bMatchAll = true)
  260. {
  261. $aRes = array();
  262. foreach ($this->m_aExpressions as $oExpr)
  263. {
  264. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  265. }
  266. return new ListExpression($aRes);
  267. }
  268. public function ListRequiredFields()
  269. {
  270. $aRes = array();
  271. foreach ($this->m_aExpressions as $oExpr)
  272. {
  273. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  274. }
  275. return $aRes;
  276. }
  277. }
  278. class FunctionExpression extends Expression
  279. {
  280. protected $m_sVerb;
  281. protected $m_aArgs; // array of expressions
  282. public function __construct($sVerb, $aArgExpressions)
  283. {
  284. $this->m_sVerb = $sVerb;
  285. $this->m_aArgs = $aArgExpressions;
  286. }
  287. public function IsTrue()
  288. {
  289. // return true if we are certain that it will be true
  290. return false;
  291. }
  292. public function GetVerb()
  293. {
  294. return $this->m_sVerb;
  295. }
  296. public function GetArgs()
  297. {
  298. return $this->m_aArgs;
  299. }
  300. // recursive rendering
  301. public function Render()
  302. {
  303. $aRes = array();
  304. foreach ($this->m_aArgs as $oExpr)
  305. {
  306. $aRes[] = $oExpr->Render();
  307. }
  308. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  309. }
  310. public function Translate($aTranslationData, $bMatchAll = true)
  311. {
  312. $aRes = array();
  313. foreach ($this->m_aArgs as $oExpr)
  314. {
  315. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  316. }
  317. return new FunctionExpression($this->m_sVerb, $aRes);
  318. }
  319. public function ListRequiredFields()
  320. {
  321. $aRes = array();
  322. foreach ($this->m_aArgs as $oExpr)
  323. {
  324. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  325. }
  326. return $aRes;
  327. }
  328. }
  329. class IntervalExpression extends Expression
  330. {
  331. protected $m_oValue; // expression
  332. protected $m_sUnit;
  333. public function __construct($oValue, $sUnit)
  334. {
  335. $this->m_oValue = $oValue;
  336. $this->m_sUnit = $sUnit;
  337. }
  338. public function IsTrue()
  339. {
  340. // return true if we are certain that it will be true
  341. return false;
  342. }
  343. public function GetValue()
  344. {
  345. return $this->m_oValue;
  346. }
  347. public function GetUnit()
  348. {
  349. return $this->m_sUnit;
  350. }
  351. // recursive rendering
  352. public function Render()
  353. {
  354. return 'INTERVAL '.$this->m_oValue->Render().' '.$this->m_sUnit;
  355. }
  356. public function Translate($aTranslationData, $bMatchAll = true)
  357. {
  358. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll), $this->m_sUnit);
  359. }
  360. public function ListRequiredFields()
  361. {
  362. return array();
  363. }
  364. }
  365. class CharConcatExpression extends Expression
  366. {
  367. protected $m_aExpressions;
  368. public function __construct($aExpressions)
  369. {
  370. $this->m_aExpressions = $aExpressions;
  371. }
  372. public function IsTrue()
  373. {
  374. // return true if we are certain that it will be true
  375. return false;
  376. }
  377. public function GetItems()
  378. {
  379. return $this->m_aExpressions;
  380. }
  381. // recursive rendering
  382. public function Render()
  383. {
  384. $aRes = array();
  385. foreach ($this->m_aExpressions as $oExpr)
  386. {
  387. $sCol = $oExpr->Render();
  388. // Concat will be globally NULL if one single argument is null !
  389. $aRes[] = "COALESCE($sCol, '')";
  390. }
  391. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  392. }
  393. public function Translate($aTranslationData, $bMatchAll = true)
  394. {
  395. $aRes = array();
  396. foreach ($this->m_aExpressions as $oExpr)
  397. {
  398. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  399. }
  400. return new CharConcatExpression($aRes);
  401. }
  402. public function ListRequiredFields()
  403. {
  404. $aRes = array();
  405. foreach ($this->m_aExpressions as $oExpr)
  406. {
  407. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  408. }
  409. return $aRes;
  410. }
  411. }
  412. ?>