expression.class.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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($aArgs = array());
  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 = array())
  104. {
  105. $sOperator = $this->GetOperator();
  106. $sLeft = $this->GetLeftExpr()->Render($aArgs);
  107. $sRight = $this->GetRightExpr()->Render($aArgs);
  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 = array())
  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($aArgs = array())
  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. class VariableExpression extends UnaryExpression
  232. {
  233. protected $m_sName;
  234. public function __construct($sName)
  235. {
  236. parent::__construct($sName);
  237. $this->m_sName = $sName;
  238. }
  239. public function IsTrue()
  240. {
  241. // return true if we are certain that it will be true
  242. return false;
  243. }
  244. public function GetName() {return $this->m_sName;}
  245. // recursive rendering
  246. public function Render($aArgs = array())
  247. {
  248. if (array_key_exists($this->m_sName, $aArgs))
  249. {
  250. return $aArgs[$this->m_sName];
  251. }
  252. elseif (is_null($aArgs))
  253. {
  254. return ':'.$this->m_sName;
  255. }
  256. else
  257. {
  258. throw new CoreException('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
  259. }
  260. }
  261. }
  262. // Temporary, until we implement functions and expression casting!
  263. // ... or until we implement a real full text search based in the MATCH() expression
  264. class ListExpression extends Expression
  265. {
  266. protected $m_aExpressions;
  267. public function __construct($aExpressions)
  268. {
  269. $this->m_aExpressions = $aExpressions;
  270. }
  271. public function IsTrue()
  272. {
  273. // return true if we are certain that it will be true
  274. return false;
  275. }
  276. public function GetItems()
  277. {
  278. return $this->m_aExpressions;
  279. }
  280. // recursive rendering
  281. public function Render($aArgs = array())
  282. {
  283. $aRes = array();
  284. foreach ($this->m_aExpressions as $oExpr)
  285. {
  286. $aRes[] = $oExpr->Render($aArgs);
  287. }
  288. return '('.implode(', ', $aRes).')';
  289. }
  290. public function Translate($aTranslationData, $bMatchAll = true)
  291. {
  292. $aRes = array();
  293. foreach ($this->m_aExpressions as $oExpr)
  294. {
  295. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  296. }
  297. return new ListExpression($aRes);
  298. }
  299. public function ListRequiredFields()
  300. {
  301. $aRes = array();
  302. foreach ($this->m_aExpressions as $oExpr)
  303. {
  304. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  305. }
  306. return $aRes;
  307. }
  308. }
  309. class FunctionExpression extends Expression
  310. {
  311. protected $m_sVerb;
  312. protected $m_aArgs; // array of expressions
  313. public function __construct($sVerb, $aArgExpressions)
  314. {
  315. $this->m_sVerb = $sVerb;
  316. $this->m_aArgs = $aArgExpressions;
  317. }
  318. public function IsTrue()
  319. {
  320. // return true if we are certain that it will be true
  321. return false;
  322. }
  323. public function GetVerb()
  324. {
  325. return $this->m_sVerb;
  326. }
  327. public function GetArgs()
  328. {
  329. return $this->m_aArgs;
  330. }
  331. // recursive rendering
  332. public function Render($aArgs = array())
  333. {
  334. $aRes = array();
  335. foreach ($this->m_aArgs as $oExpr)
  336. {
  337. $aRes[] = $oExpr->Render($aArgs);
  338. }
  339. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  340. }
  341. public function Translate($aTranslationData, $bMatchAll = true)
  342. {
  343. $aRes = array();
  344. foreach ($this->m_aArgs as $oExpr)
  345. {
  346. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  347. }
  348. return new FunctionExpression($this->m_sVerb, $aRes);
  349. }
  350. public function ListRequiredFields()
  351. {
  352. $aRes = array();
  353. foreach ($this->m_aArgs as $oExpr)
  354. {
  355. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  356. }
  357. return $aRes;
  358. }
  359. }
  360. class IntervalExpression extends Expression
  361. {
  362. protected $m_oValue; // expression
  363. protected $m_sUnit;
  364. public function __construct($oValue, $sUnit)
  365. {
  366. $this->m_oValue = $oValue;
  367. $this->m_sUnit = $sUnit;
  368. }
  369. public function IsTrue()
  370. {
  371. // return true if we are certain that it will be true
  372. return false;
  373. }
  374. public function GetValue()
  375. {
  376. return $this->m_oValue;
  377. }
  378. public function GetUnit()
  379. {
  380. return $this->m_sUnit;
  381. }
  382. // recursive rendering
  383. public function Render($aArgs = array())
  384. {
  385. return 'INTERVAL '.$this->m_oValue->Render($aArgs).' '.$this->m_sUnit;
  386. }
  387. public function Translate($aTranslationData, $bMatchAll = true)
  388. {
  389. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll), $this->m_sUnit);
  390. }
  391. public function ListRequiredFields()
  392. {
  393. return array();
  394. }
  395. }
  396. class CharConcatExpression extends Expression
  397. {
  398. protected $m_aExpressions;
  399. public function __construct($aExpressions)
  400. {
  401. $this->m_aExpressions = $aExpressions;
  402. }
  403. public function IsTrue()
  404. {
  405. // return true if we are certain that it will be true
  406. return false;
  407. }
  408. public function GetItems()
  409. {
  410. return $this->m_aExpressions;
  411. }
  412. // recursive rendering
  413. public function Render($aArgs = array())
  414. {
  415. $aRes = array();
  416. foreach ($this->m_aExpressions as $oExpr)
  417. {
  418. $sCol = $oExpr->Render($aArgs);
  419. // Concat will be globally NULL if one single argument is null !
  420. $aRes[] = "COALESCE($sCol, '')";
  421. }
  422. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  423. }
  424. public function Translate($aTranslationData, $bMatchAll = true)
  425. {
  426. $aRes = array();
  427. foreach ($this->m_aExpressions as $oExpr)
  428. {
  429. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll);
  430. }
  431. return new CharConcatExpression($aRes);
  432. }
  433. public function ListRequiredFields()
  434. {
  435. $aRes = array();
  436. foreach ($this->m_aExpressions as $oExpr)
  437. {
  438. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  439. }
  440. return $aRes;
  441. }
  442. }
  443. ?>