ExpressionBuilder.php 9.3 KB

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