PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/php/storage/mdb2/condition_translator.class.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 282 lines | 195 code | 34 blank | 53 comment | 18 complexity | 5045dad9b2a49d38c229de3eef1d8438 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. namespace common\libraries;
  3. use Exception;
  4. /**
  5. * @package common.libraries.storage.mdb2
  6. * @author Hans De Bisschop
  7. */
  8. class Mdb2ConditionTranslator extends ConditionTranslator
  9. {
  10. /**
  11. * Translates an aggregate condition to a SQL WHERE clause.
  12. * @param AggregateCondition $condition The AggregateCondition object.
  13. * @param array $parameters A reference to the query's parameter list.
  14. * @param boolean $storage_unit Whether or not to
  15. * prefix learning
  16. * object properties
  17. * to avoid collisions.
  18. * @return string The WHERE clause.
  19. */
  20. function translate_aggregate_condition($aggregate_condition)
  21. {
  22. $string = '';
  23. if ($aggregate_condition instanceof AndCondition || $aggregate_condition instanceof OrCondition)
  24. {
  25. $condition_translations = array();
  26. $count = 0;
  27. foreach ($aggregate_condition->get_conditions() as $key => $condition)
  28. {
  29. $count ++;
  30. $translation = $this->translate($condition);
  31. if (! empty($translation))
  32. {
  33. $condition_translations[] = $translation;
  34. }
  35. }
  36. if (count($condition_translations) > 0)
  37. {
  38. $string = '(' . implode($aggregate_condition->get_operator(), $condition_translations) . ')';
  39. }
  40. }
  41. elseif ($aggregate_condition instanceof NotCondition)
  42. {
  43. $string .= 'NOT (';
  44. $string .= $this->translate($aggregate_condition->get_condition());
  45. $string .= $this->strings[] = ')';
  46. }
  47. else
  48. {
  49. die('Cannot translate aggregate condition');
  50. }
  51. return $string;
  52. }
  53. /**
  54. * Translates an in condition to a SQL WHERE clause.
  55. * @param InCondition $condition The InCondition object.
  56. * @param array $parameters A reference to the query's parameter list.
  57. * @param boolean $storage_unit Whether or not to
  58. * prefix learning
  59. * object properties
  60. * to avoid collisions.
  61. * @return string The WHERE clause.
  62. */
  63. function translate_in_condition($condition)
  64. {
  65. $storage_unit = $this->get_storage_unit();
  66. $condition_storage_unit = $condition->get_storage_unit();
  67. if (! is_null($condition_storage_unit))
  68. {
  69. if ($condition->is_alias())
  70. {
  71. $storage_unit = $condition_storage_unit;
  72. }
  73. else
  74. {
  75. $storage_unit = $this->get_data_manager()->get_alias($condition_storage_unit);
  76. }
  77. }
  78. if ($condition instanceof InCondition)
  79. {
  80. $name = $condition->get_name();
  81. $values = $condition->get_values();
  82. if (! is_array($values))
  83. {
  84. $values = array($values);
  85. }
  86. if (count($values) > 0)
  87. {
  88. $where_clause = array();
  89. $where_clause[] = $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' IN (';
  90. $placeholders = array();
  91. foreach ($values as $value)
  92. {
  93. $placeholders[] = $this->get_data_manager()->quote($value);
  94. }
  95. $where_clause[] = implode(',', $placeholders);
  96. $where_clause[] = ')';
  97. return implode('', $where_clause);
  98. }
  99. else
  100. {
  101. return 'true=false';
  102. }
  103. }
  104. else
  105. {
  106. die('Cannot translate in condition');
  107. }
  108. }
  109. function translate_subselect_condition($condition)
  110. {
  111. if ($condition instanceof SubselectCondition)
  112. {
  113. $storage_unit = $this->get_storage_unit();
  114. $name = $condition->get_name();
  115. $value = $condition->get_value();
  116. $table = $condition->get_storage_unit_value();
  117. $name_table = $condition->get_storage_unit_name();
  118. if ($condition->get_data_manager())
  119. {
  120. $etable = $condition->get_data_manager()->escape_table_name($table);
  121. }
  122. else
  123. {
  124. $etable = $this->get_data_manager()->escape_table_name($table);
  125. }
  126. $sub_condition = $condition->get_condition();
  127. $alias = $this->get_data_manager()->get_alias($table);
  128. $alias_name = null;
  129. if ($name_table)
  130. {
  131. $alias_name = $this->get_data_manager()->get_alias($name_table);
  132. }
  133. $this->set_storage_unit($alias);
  134. $string = $this->get_data_manager()->escape_column_name($name, $alias_name) . ' IN ( SELECT ' . $this->get_data_manager()->escape_column_name($value, $alias) . ' FROM ' . $etable . ' AS ' . $alias;
  135. if ($sub_condition)
  136. {
  137. $string .= ' WHERE ';
  138. $string .= $this->translate($sub_condition);
  139. }
  140. $string .= ')';
  141. $this->set_storage_unit($storage_unit);
  142. }
  143. else
  144. {
  145. die('Cannot translate in condition');
  146. }
  147. return $string;
  148. }
  149. /**
  150. * Translates a simple condition to a SQL WHERE clause.
  151. * @param Condition $condition The Condition object.
  152. * @param array $parameters A reference to the query's parameter list.
  153. * @param boolean $storage_unit Whether or not to
  154. * prefix learning
  155. * object properties
  156. * to avoid collisions.
  157. * @return string The WHERE clause.
  158. */
  159. function translate_simple_condition($condition)
  160. {
  161. $storage_unit = $this->get_storage_unit();
  162. $data_manager = $this->get_data_manager();
  163. $name = $condition->get_name();
  164. $condition_storage_unit = $condition->get_storage_unit();
  165. if (! is_null($condition_storage_unit))
  166. {
  167. if ($condition->is_alias())
  168. {
  169. $storage_unit = $condition_storage_unit;
  170. }
  171. else
  172. {
  173. $storage_unit = $this->get_data_manager()->get_alias($condition_storage_unit);
  174. }
  175. }
  176. if ($condition instanceof EqualityCondition)
  177. {
  178. $value = $condition->get_value();
  179. if (is_null($value))
  180. {
  181. return $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' IS NULL';
  182. }
  183. return $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' = ' . $this->get_data_manager()->quote($value);
  184. }
  185. elseif ($condition instanceof InequalityCondition)
  186. {
  187. $value = $condition->get_value();
  188. switch ($condition->get_operator())
  189. {
  190. case InequalityCondition :: GREATER_THAN :
  191. $operator = '>';
  192. break;
  193. case InequalityCondition :: GREATER_THAN_OR_EQUAL :
  194. $operator = '>=';
  195. break;
  196. case InequalityCondition :: LESS_THAN :
  197. $operator = '<';
  198. break;
  199. case InequalityCondition :: LESS_THAN_OR_EQUAL :
  200. $operator = '<=';
  201. break;
  202. default :
  203. dump($condition);
  204. die('Unknown operator for inequality condition');
  205. }
  206. return $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' ' . $operator . ' ' . $this->get_data_manager()->quote($value);
  207. }
  208. elseif ($condition instanceof PatternMatchCondition)
  209. {
  210. return $this->get_data_manager()->escape_column_name($condition->get_name(), $storage_unit) . ' LIKE ' . $this->get_data_manager()->quote($this->translate_search_string($condition->get_pattern()));
  211. }
  212. else
  213. {
  214. return $condition; //die('Cannot translate condition');
  215. }
  216. }
  217. /**
  218. * Translates a string with wildcard characters "?" (single character)
  219. * and "*" (any character sequence) to a SQL pattern for use in a LIKE
  220. * condition. Should be suitable for any SQL flavor.
  221. * @param string $string The string that contains wildcard characters.
  222. * @return string The escaped string.
  223. */
  224. function translate_search_string($string)
  225. {
  226. /*
  227. ======================================================================
  228. * A brief explanation of these regexps:
  229. * - The first one escapes SQL wildcard characters, thus prefixing
  230. * %, ', \ and _ with a backslash.
  231. * - The second one replaces asterisks that are not prefixed with a
  232. * backslash (which escapes them) with the SQL equivalent, namely a
  233. * percent sign.
  234. * - The third one is similar to the second: it replaces question
  235. * marks that are not escaped with the SQL equivalent _.
  236. ======================================================================
  237. */
  238. return preg_replace(array('/([%\'\\\\_])/e', '/(?<!\\\\)\*/', '/(?<!\\\\)\?/'), array("'\\\\\\\\' . '\\1'",
  239. '%', '_'), $string);
  240. }
  241. function render_query($condition, $add_where = true)
  242. {
  243. return ($add_where ? ' WHERE ' : '') . $this->translate($condition);
  244. }
  245. }
  246. ?>