ExpressionBuilder.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace Doctrine\DBAL\Query\Expression;
  3. use Doctrine\DBAL\Connection;
  4. use function func_get_arg;
  5. use function func_get_args;
  6. use function func_num_args;
  7. use function implode;
  8. use function sprintf;
  9. /**
  10. * ExpressionBuilder class is responsible to dynamically create SQL query parts.
  11. */
  12. class ExpressionBuilder
  13. {
  14. public const EQ = '=';
  15. public const NEQ = '<>';
  16. public const LT = '<';
  17. public const LTE = '<=';
  18. public const GT = '>';
  19. public const GTE = '>=';
  20. /**
  21. * The DBAL Connection.
  22. *
  23. * @var Connection
  24. */
  25. private $connection;
  26. /**
  27. * Initializes a new <tt>ExpressionBuilder</tt>.
  28. *
  29. * @param Connection $connection The DBAL Connection.
  30. */
  31. public function __construct(Connection $connection)
  32. {
  33. $this->connection = $connection;
  34. }
  35. /**
  36. * Creates a conjunction of the given expressions.
  37. *
  38. * @param string|CompositeExpression $expression
  39. * @param string|CompositeExpression ...$expressions
  40. */
  41. public function and($expression, ...$expressions): CompositeExpression
  42. {
  43. return CompositeExpression::and($expression, ...$expressions);
  44. }
  45. /**
  46. * Creates a disjunction of the given expressions.
  47. *
  48. * @param string|CompositeExpression $expression
  49. * @param string|CompositeExpression ...$expressions
  50. */
  51. public function or($expression, ...$expressions): CompositeExpression
  52. {
  53. return CompositeExpression::or($expression, ...$expressions);
  54. }
  55. /**
  56. * @deprecated Use `and()` instead.
  57. *
  58. * @param mixed $x Optional clause. Defaults = null, but requires
  59. * at least one defined when converting to string.
  60. *
  61. * @return CompositeExpression
  62. */
  63. public function andX($x = null)
  64. {
  65. return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
  66. }
  67. /**
  68. * @deprecated Use `or()` instead.
  69. *
  70. * @param mixed $x Optional clause. Defaults = null, but requires
  71. * at least one defined when converting to string.
  72. *
  73. * @return CompositeExpression
  74. */
  75. public function orX($x = null)
  76. {
  77. return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
  78. }
  79. /**
  80. * Creates a comparison expression.
  81. *
  82. * @param mixed $x The left expression.
  83. * @param string $operator One of the ExpressionBuilder::* constants.
  84. * @param mixed $y The right expression.
  85. *
  86. * @return string
  87. */
  88. public function comparison($x, $operator, $y)
  89. {
  90. return $x . ' ' . $operator . ' ' . $y;
  91. }
  92. /**
  93. * Creates an equality comparison expression with the given arguments.
  94. *
  95. * First argument is considered the left expression and the second is the right expression.
  96. * When converted to string, it will generated a <left expr> = <right expr>. Example:
  97. *
  98. * [php]
  99. * // u.id = ?
  100. * $expr->eq('u.id', '?');
  101. *
  102. * @param mixed $x The left expression.
  103. * @param mixed $y The right expression.
  104. *
  105. * @return string
  106. */
  107. public function eq($x, $y)
  108. {
  109. return $this->comparison($x, self::EQ, $y);
  110. }
  111. /**
  112. * Creates a non equality comparison expression with the given arguments.
  113. * First argument is considered the left expression and the second is the right expression.
  114. * When converted to string, it will generated a <left expr> <> <right expr>. Example:
  115. *
  116. * [php]
  117. * // u.id <> 1
  118. * $q->where($q->expr()->neq('u.id', '1'));
  119. *
  120. * @param mixed $x The left expression.
  121. * @param mixed $y The right expression.
  122. *
  123. * @return string
  124. */
  125. public function neq($x, $y)
  126. {
  127. return $this->comparison($x, self::NEQ, $y);
  128. }
  129. /**
  130. * Creates a lower-than comparison expression with the given arguments.
  131. * First argument is considered the left expression and the second is the right expression.
  132. * When converted to string, it will generated a <left expr> < <right expr>. Example:
  133. *
  134. * [php]
  135. * // u.id < ?
  136. * $q->where($q->expr()->lt('u.id', '?'));
  137. *
  138. * @param mixed $x The left expression.
  139. * @param mixed $y The right expression.
  140. *
  141. * @return string
  142. */
  143. public function lt($x, $y)
  144. {
  145. return $this->comparison($x, self::LT, $y);
  146. }
  147. /**
  148. * Creates a lower-than-equal comparison expression with the given arguments.
  149. * First argument is considered the left expression and the second is the right expression.
  150. * When converted to string, it will generated a <left expr> <= <right expr>. Example:
  151. *
  152. * [php]
  153. * // u.id <= ?
  154. * $q->where($q->expr()->lte('u.id', '?'));
  155. *
  156. * @param mixed $x The left expression.
  157. * @param mixed $y The right expression.
  158. *
  159. * @return string
  160. */
  161. public function lte($x, $y)
  162. {
  163. return $this->comparison($x, self::LTE, $y);
  164. }
  165. /**
  166. * Creates a greater-than comparison expression with the given arguments.
  167. * First argument is considered the left expression and the second is the right expression.
  168. * When converted to string, it will generated a <left expr> > <right expr>. Example:
  169. *
  170. * [php]
  171. * // u.id > ?
  172. * $q->where($q->expr()->gt('u.id', '?'));
  173. *
  174. * @param mixed $x The left expression.
  175. * @param mixed $y The right expression.
  176. *
  177. * @return string
  178. */
  179. public function gt($x, $y)
  180. {
  181. return $this->comparison($x, self::GT, $y);
  182. }
  183. /**
  184. * Creates a greater-than-equal comparison expression with the given arguments.
  185. * First argument is considered the left expression and the second is the right expression.
  186. * When converted to string, it will generated a <left expr> >= <right expr>. Example:
  187. *
  188. * [php]
  189. * // u.id >= ?
  190. * $q->where($q->expr()->gte('u.id', '?'));
  191. *
  192. * @param mixed $x The left expression.
  193. * @param mixed $y The right expression.
  194. *
  195. * @return string
  196. */
  197. public function gte($x, $y)
  198. {
  199. return $this->comparison($x, self::GTE, $y);
  200. }
  201. /**
  202. * Creates an IS NULL expression with the given arguments.
  203. *
  204. * @param string $x The expression to be restricted by IS NULL.
  205. *
  206. * @return string
  207. */
  208. public function isNull($x)
  209. {
  210. return $x . ' IS NULL';
  211. }
  212. /**
  213. * Creates an IS NOT NULL expression with the given arguments.
  214. *
  215. * @param string $x The expression to be restricted by IS NOT NULL.
  216. *
  217. * @return string
  218. */
  219. public function isNotNull($x)
  220. {
  221. return $x . ' IS NOT NULL';
  222. }
  223. /**
  224. * Creates a LIKE() comparison expression with the given arguments.
  225. *
  226. * @param string $x Field in string format to be inspected by LIKE() comparison.
  227. * @param mixed $y Argument to be used in LIKE() comparison.
  228. *
  229. * @return string
  230. */
  231. public function like($x, $y/*, ?string $escapeChar = null */)
  232. {
  233. return $this->comparison($x, 'LIKE', $y) .
  234. (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
  235. }
  236. /**
  237. * Creates a NOT LIKE() comparison expression with the given arguments.
  238. *
  239. * @param string $x Field in string format to be inspected by NOT LIKE() comparison.
  240. * @param mixed $y Argument to be used in NOT LIKE() comparison.
  241. *
  242. * @return string
  243. */
  244. public function notLike($x, $y/*, ?string $escapeChar = null */)
  245. {
  246. return $this->comparison($x, 'NOT LIKE', $y) .
  247. (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
  248. }
  249. /**
  250. * Creates a IN () comparison expression with the given arguments.
  251. *
  252. * @param string $x The field in string format to be inspected by IN() comparison.
  253. * @param string|string[] $y The placeholder or the array of values to be used by IN() comparison.
  254. *
  255. * @return string
  256. */
  257. public function in($x, $y)
  258. {
  259. return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')');
  260. }
  261. /**
  262. * Creates a NOT IN () comparison expression with the given arguments.
  263. *
  264. * @param string $x The expression to be inspected by NOT IN() comparison.
  265. * @param string|string[] $y The placeholder or the array of values to be used by NOT IN() comparison.
  266. *
  267. * @return string
  268. */
  269. public function notIn($x, $y)
  270. {
  271. return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')');
  272. }
  273. /**
  274. * Quotes a given input parameter.
  275. *
  276. * @param mixed $input The parameter to be quoted.
  277. * @param int|null $type The type of the parameter.
  278. *
  279. * @return string
  280. */
  281. public function literal($input, $type = null)
  282. {
  283. return $this->connection->quote($input, $type);
  284. }
  285. }