expression.class.inc.php 13 KB

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