ExpressionBuilder.php 8.5 KB

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