ExpressionBuilder.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Query\Expression;
  20. use Doctrine\DBAL\Connection;
  21. /**
  22. * ExpressionBuilder class is responsible to dynamically create SQL query parts.
  23. *
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @link www.doctrine-project.com
  26. * @since 2.1
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. */
  30. class ExpressionBuilder
  31. {
  32. const EQ = '=';
  33. const NEQ = '<>';
  34. const LT = '<';
  35. const LTE = '<=';
  36. const GT = '>';
  37. const GTE = '>=';
  38. /**
  39. * @var Doctrine\DBAL\Connection DBAL Connection
  40. */
  41. private $connection = null;
  42. /**
  43. * Initializes a new <tt>ExpressionBuilder</tt>.
  44. *
  45. * @param Doctrine\DBAL\Connection $connection DBAL Connection
  46. */
  47. public function __construct(Connection $connection)
  48. {
  49. $this->connection = $connection;
  50. }
  51. /**
  52. * Creates a conjunction of the given boolean expressions.
  53. *
  54. * Example:
  55. *
  56. * [php]
  57. * // (u.type = ?) AND (u.role = ?)
  58. * $expr->andX('u.type = ?', 'u.role = ?'));
  59. *
  60. * @param mixed $x Optional clause. Defaults = null, but requires
  61. * at least one defined when converting to string.
  62. * @return CompositeExpression
  63. */
  64. public function andX($x = null)
  65. {
  66. return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
  67. }
  68. /**
  69. * Creates a disjunction of the given boolean expressions.
  70. *
  71. * Example:
  72. *
  73. * [php]
  74. * // (u.type = ?) OR (u.role = ?)
  75. * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
  76. *
  77. * @param mixed $x Optional clause. Defaults = null, but requires
  78. * at least one defined when converting to string.
  79. * @return CompositeExpression
  80. */
  81. public function orX($x = null)
  82. {
  83. return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
  84. }
  85. /**
  86. * Creates a comparison expression.
  87. *
  88. * @param mixed $x Left expression
  89. * @param string $operator One of the ExpressionBuikder::* constants.
  90. * @param mixed $y Right expression
  91. * @return string
  92. */
  93. public function comparison($x, $operator, $y)
  94. {
  95. return $x . ' ' . $operator . ' ' . $y;
  96. }
  97. /**
  98. * Creates an equality comparison expression with the given arguments.
  99. *
  100. * First argument is considered the left expression and the second is the right expression.
  101. * When converted to string, it will generated a <left expr> = <right expr>. Example:
  102. *
  103. * [php]
  104. * // u.id = ?
  105. * $expr->eq('u.id', '?');
  106. *
  107. * @param mixed $x Left expression
  108. * @param mixed $y Right expression
  109. * @return string
  110. */
  111. public function eq($x, $y)
  112. {
  113. return $this->comparison($x, self::EQ, $y);
  114. }
  115. /**
  116. * Creates a non equality comparison expression with the given arguments.
  117. * First argument is considered the left expression and the second is the right expression.
  118. * When converted to string, it will generated a <left expr> <> <right expr>. Example:
  119. *
  120. * [php]
  121. * // u.id <> 1
  122. * $q->where($q->expr()->neq('u.id', '1'));
  123. *
  124. * @param mixed $x Left expression
  125. * @param mixed $y Right expression
  126. * @return string
  127. */
  128. public function neq($x, $y)
  129. {
  130. return $this->comparison($x, self::NEQ, $y);
  131. }
  132. /**
  133. * Creates a lower-than comparison expression with the given arguments.
  134. * First argument is considered the left expression and the second is the right expression.
  135. * When converted to string, it will generated a <left expr> < <right expr>. Example:
  136. *
  137. * [php]
  138. * // u.id < ?
  139. * $q->where($q->expr()->lt('u.id', '?'));
  140. *
  141. * @param mixed $x Left expression
  142. * @param mixed $y Right expression
  143. * @return string
  144. */
  145. public function lt($x, $y)
  146. {
  147. return $this->comparison($x, self::LT, $y);
  148. }
  149. /**
  150. * Creates a lower-than-equal comparison expression with the given arguments.
  151. * First argument is considered the left expression and the second is the right expression.
  152. * When converted to string, it will generated a <left expr> <= <right expr>. Example:
  153. *
  154. * [php]
  155. * // u.id <= ?
  156. * $q->where($q->expr()->lte('u.id', '?'));
  157. *
  158. * @param mixed $x Left expression
  159. * @param mixed $y Right expression
  160. * @return string
  161. */
  162. public function lte($x, $y)
  163. {
  164. return $this->comparison($x, self::LTE, $y);
  165. }
  166. /**
  167. * Creates a greater-than comparison expression with the given arguments.
  168. * First argument is considered the left expression and the second is the right expression.
  169. * When converted to string, it will generated a <left expr> > <right expr>. Example:
  170. *
  171. * [php]
  172. * // u.id > ?
  173. * $q->where($q->expr()->gt('u.id', '?'));
  174. *
  175. * @param mixed $x Left expression
  176. * @param mixed $y Right expression
  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 Left expression
  193. * @param mixed $y Right expression
  194. * @return string
  195. */
  196. public function gte($x, $y)
  197. {
  198. return $this->comparison($x, self::GTE, $y);
  199. }
  200. /**
  201. * Creates an IS NULL expression with the given arguments.
  202. *
  203. * @param string $x Field in string format to be restricted by IS NULL
  204. *
  205. * @return string
  206. */
  207. public function isNull($x)
  208. {
  209. return $x . ' IS NULL';
  210. }
  211. /**
  212. * Creates an IS NOT NULL expression with the given arguments.
  213. *
  214. * @param string $x Field in string format to be restricted by IS NOT NULL
  215. *
  216. * @return string
  217. */
  218. public function isNotNull($x)
  219. {
  220. return $x . ' IS NOT NULL';
  221. }
  222. /**
  223. * Creates a LIKE() comparison expression with the given arguments.
  224. *
  225. * @param string $x Field in string format to be inspected by LIKE() comparison.
  226. * @param mixed $y Argument to be used in LIKE() comparison.
  227. *
  228. * @return string
  229. */
  230. public function like($x, $y)
  231. {
  232. return $this->comparison($x, 'LIKE', $y);
  233. }
  234. /**
  235. * Quotes a given input parameter.
  236. *
  237. * @param mixed $input Parameter to be quoted.
  238. * @param string $type Type of the parameter.
  239. *
  240. * @return string
  241. */
  242. public function literal($input, $type = null)
  243. {
  244. return $this->connection->quote($input, $type);
  245. }
  246. }