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.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Query\Expression;
22 use Doctrine\DBAL\Connection;
25 * ExpressionBuilder class is responsible to dynamically create SQL query parts.
27 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28 * @link www.doctrine-project.com
30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 class ExpressionBuilder
43 * @var Doctrine\DBAL\Connection DBAL Connection
45 private $connection = null;
48 * Initializes a new <tt>ExpressionBuilder</tt>.
50 * @param \Doctrine\DBAL\Connection $connection DBAL Connection
52 public function __construct(Connection $connection)
54 $this->connection = $connection;
58 * Creates a conjunction of the given boolean expressions.
63 * // (u.type = ?) AND (u.role = ?)
64 * $expr->andX('u.type = ?', 'u.role = ?'));
66 * @param mixed $x Optional clause. Defaults = null, but requires
67 * at least one defined when converting to string.
68 * @return CompositeExpression
70 public function andX($x = null)
72 return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
76 * Creates a disjunction of the given boolean expressions.
81 * // (u.type = ?) OR (u.role = ?)
82 * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
84 * @param mixed $x Optional clause. Defaults = null, but requires
85 * at least one defined when converting to string.
86 * @return CompositeExpression
88 public function orX($x = null)
90 return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
94 * Creates a comparison expression.
96 * @param mixed $x Left expression
97 * @param string $operator One of the ExpressionBuilder::* constants.
98 * @param mixed $y Right expression
101 public function comparison($x, $operator, $y)
103 return $x . ' ' . $operator . ' ' . $y;
107 * Creates an equality comparison expression with the given arguments.
109 * First argument is considered the left expression and the second is the right expression.
110 * When converted to string, it will generated a <left expr> = <right expr>. Example:
114 * $expr->eq('u.id', '?');
116 * @param mixed $x Left expression
117 * @param mixed $y Right expression
120 public function eq($x, $y)
122 return $this->comparison($x, self::EQ, $y);
126 * Creates a non equality comparison expression with the given arguments.
127 * First argument is considered the left expression and the second is the right expression.
128 * When converted to string, it will generated a <left expr> <> <right expr>. Example:
132 * $q->where($q->expr()->neq('u.id', '1'));
134 * @param mixed $x Left expression
135 * @param mixed $y Right expression
138 public function neq($x, $y)
140 return $this->comparison($x, self::NEQ, $y);
144 * Creates a lower-than comparison expression with the given arguments.
145 * First argument is considered the left expression and the second is the right expression.
146 * When converted to string, it will generated a <left expr> < <right expr>. Example:
150 * $q->where($q->expr()->lt('u.id', '?'));
152 * @param mixed $x Left expression
153 * @param mixed $y Right expression
156 public function lt($x, $y)
158 return $this->comparison($x, self::LT, $y);
162 * Creates a lower-than-equal comparison expression with the given arguments.
163 * First argument is considered the left expression and the second is the right expression.
164 * When converted to string, it will generated a <left expr> <= <right expr>. Example:
168 * $q->where($q->expr()->lte('u.id', '?'));
170 * @param mixed $x Left expression
171 * @param mixed $y Right expression
174 public function lte($x, $y)
176 return $this->comparison($x, self::LTE, $y);
180 * Creates a greater-than comparison expression with the given arguments.
181 * First argument is considered the left expression and the second is the right expression.
182 * When converted to string, it will generated a <left expr> > <right expr>. Example:
186 * $q->where($q->expr()->gt('u.id', '?'));
188 * @param mixed $x Left expression
189 * @param mixed $y Right expression
192 public function gt($x, $y)
194 return $this->comparison($x, self::GT, $y);
198 * Creates a greater-than-equal comparison expression with the given arguments.
199 * First argument is considered the left expression and the second is the right expression.
200 * When converted to string, it will generated a <left expr> >= <right expr>. Example:
204 * $q->where($q->expr()->gte('u.id', '?'));
206 * @param mixed $x Left expression
207 * @param mixed $y Right expression
210 public function gte($x, $y)
212 return $this->comparison($x, self::GTE, $y);
216 * Creates an IS NULL expression with the given arguments.
218 * @param string $x Field in string format to be restricted by IS NULL
222 public function isNull($x)
224 return $x . ' IS NULL';
228 * Creates an IS NOT NULL expression with the given arguments.
230 * @param string $x Field in string format to be restricted by IS NOT NULL
234 public function isNotNull($x)
236 return $x . ' IS NOT NULL';
240 * Creates a LIKE() comparison expression with the given arguments.
242 * @param string $x Field in string format to be inspected by LIKE() comparison.
243 * @param mixed $y Argument to be used in LIKE() comparison.
247 public function like($x, $y)
249 return $this->comparison($x, 'LIKE', $y);
253 * Quotes a given input parameter.
255 * @param mixed $input Parameter to be quoted.
256 * @param string $type Type of the parameter.
260 public function literal($input, $type = null)
262 return $this->connection->quote($input, $type);