ExpressionBuilder.php 9.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21 22 23 24 25 26
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Query\Expression;

use Doctrine\DBAL\Connection;

/**
 * ExpressionBuilder class is responsible to dynamically create SQL query parts.
 *
Benjamin Morel's avatar
Benjamin Morel committed
27 28 29 30
 * @link   www.doctrine-project.org
 * @since  2.1
 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 32 33 34 35 36 37 38 39
 */
class ExpressionBuilder
{
    const EQ  = '=';
    const NEQ = '<>';
    const LT  = '<';
    const LTE = '<=';
    const GT  = '>';
    const GTE = '>=';
40

41
    /**
Benjamin Morel's avatar
Benjamin Morel committed
42 43 44
     * The DBAL Connection.
     *
     * @var \Doctrine\DBAL\Connection
45
     */
Benjamin Morel's avatar
Benjamin Morel committed
46
    private $connection;
47 48 49 50

    /**
     * Initializes a new <tt>ExpressionBuilder</tt>.
     *
Benjamin Morel's avatar
Benjamin Morel committed
51
     * @param \Doctrine\DBAL\Connection $connection The DBAL Connection.
52 53 54 55 56
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }
57

58 59 60 61 62 63 64 65 66 67 68
    /**
     * Creates a conjunction of the given boolean expressions.
     *
     * Example:
     *
     *     [php]
     *     // (u.type = ?) AND (u.role = ?)
     *     $expr->andX('u.type = ?', 'u.role = ?'));
     *
     * @param mixed $x Optional clause. Defaults = null, but requires
     *                 at least one defined when converting to string.
Benjamin Morel's avatar
Benjamin Morel committed
69 70
     *
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
     */
    public function andX($x = null)
    {
        return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
    }

    /**
     * Creates a disjunction of the given boolean expressions.
     *
     * Example:
     *
     *     [php]
     *     // (u.type = ?) OR (u.role = ?)
     *     $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
     *
     * @param mixed $x Optional clause. Defaults = null, but requires
     *                 at least one defined when converting to string.
Benjamin Morel's avatar
Benjamin Morel committed
88 89
     *
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
90 91 92 93 94 95 96 97
     */
    public function orX($x = null)
    {
        return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
    }

    /**
     * Creates a comparison expression.
98
     *
Benjamin Morel's avatar
Benjamin Morel committed
99
     * @param mixed  $x        The left expression.
100
     * @param string $operator One of the ExpressionBuilder::* constants.
Benjamin Morel's avatar
Benjamin Morel committed
101 102
     * @param mixed  $y        The right expression.
     *
103 104 105 106 107 108
     * @return string
     */
    public function comparison($x, $operator, $y)
    {
        return $x . ' ' . $operator . ' ' . $y;
    }
109

110 111 112 113 114 115 116 117 118 119
    /**
     * Creates an equality comparison expression with the given arguments.
     *
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> = <right expr>. Example:
     *
     *     [php]
     *     // u.id = ?
     *     $expr->eq('u.id', '?');
     *
Benjamin Morel's avatar
Benjamin Morel committed
120 121 122
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
     * @return string
     */
    public function eq($x, $y)
    {
        return $this->comparison($x, self::EQ, $y);
    }

    /**
     * Creates a non equality comparison expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
     *
     *     [php]
     *     // u.id <> 1
     *     $q->where($q->expr()->neq('u.id', '1'));
     *
Benjamin Morel's avatar
Benjamin Morel committed
139 140 141
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
     * @return string
     */
    public function neq($x, $y)
    {
        return $this->comparison($x, self::NEQ, $y);
    }

    /**
     * Creates a lower-than comparison expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
     *
     *     [php]
     *     // u.id < ?
     *     $q->where($q->expr()->lt('u.id', '?'));
     *
Benjamin Morel's avatar
Benjamin Morel committed
158 159 160
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
     * @return string
     */
    public function lt($x, $y)
    {
        return $this->comparison($x, self::LT, $y);
    }

    /**
     * Creates a lower-than-equal comparison expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
     *
     *     [php]
     *     // u.id <= ?
     *     $q->where($q->expr()->lte('u.id', '?'));
     *
Benjamin Morel's avatar
Benjamin Morel committed
177 178 179
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
     * @return string
     */
    public function lte($x, $y)
    {
        return $this->comparison($x, self::LTE, $y);
    }

    /**
     * Creates a greater-than comparison expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
     *
     *     [php]
     *     // u.id > ?
     *     $q->where($q->expr()->gt('u.id', '?'));
     *
Benjamin Morel's avatar
Benjamin Morel committed
196 197 198
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
     * @return string
     */
    public function gt($x, $y)
    {
        return $this->comparison($x, self::GT, $y);
    }

    /**
     * Creates a greater-than-equal comparison expression with the given arguments.
     * First argument is considered the left expression and the second is the right expression.
     * When converted to string, it will generated a <left expr> >= <right expr>. Example:
     *
     *     [php]
     *     // u.id >= ?
     *     $q->where($q->expr()->gte('u.id', '?'));
     *
Benjamin Morel's avatar
Benjamin Morel committed
215 216 217
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
218 219 220 221 222 223 224 225 226 227
     * @return string
     */
    public function gte($x, $y)
    {
        return $this->comparison($x, self::GTE, $y);
    }

    /**
     * Creates an IS NULL expression with the given arguments.
     *
Benjamin Morel's avatar
Benjamin Morel committed
228
     * @param string $x The field in string format to be restricted by IS NULL.
229
     *
230 231 232 233 234 235 236 237 238 239
     * @return string
     */
    public function isNull($x)
    {
        return $x . ' IS NULL';
    }

    /**
     * Creates an IS NOT NULL expression with the given arguments.
     *
Benjamin Morel's avatar
Benjamin Morel committed
240
     * @param string $x The field in string format to be restricted by IS NOT NULL.
241
     *
242 243 244 245 246 247 248 249 250 251 252
     * @return string
     */
    public function isNotNull($x)
    {
        return $x . ' IS NOT NULL';
    }

    /**
     * Creates a LIKE() comparison expression with the given arguments.
     *
     * @param string $x Field in string format to be inspected by LIKE() comparison.
Benjamin Morel's avatar
Benjamin Morel committed
253
     * @param mixed  $y Argument to be used in LIKE() comparison.
254
     *
255 256 257 258 259 260
     * @return string
     */
    public function like($x, $y)
    {
        return $this->comparison($x, 'LIKE', $y);
    }
Steve Müller's avatar
Steve Müller committed
261

262 263 264 265
    /**
     * Creates a NOT LIKE() comparison expression with the given arguments.
     *
     * @param string $x Field in string format to be inspected by NOT LIKE() comparison.
266
     * @param mixed  $y Argument to be used in NOT LIKE() comparison.
267 268 269
     *
     * @return string
     */
nomack84's avatar
nomack84 committed
270
    public function notLike($x, $y)
271 272 273
    {
        return $this->comparison($x, 'NOT LIKE', $y);
    }
274

275 276 277
    /**
     * Creates a IN () comparison expression with the given arguments.
     *
278 279
     * @param string       $x The field in string format to be inspected by IN() comparison.
     * @param string|array $y The placeholder or the array of values to be used by IN() comparison.
280 281 282
     *
     * @return string
     */
283
    public function in($x, $y)
284
    {
285
        return $this->comparison($x, 'IN', '('.implode(', ', (array) $y).')');
286 287 288 289 290
    }

    /**
     * Creates a NOT IN () comparison expression with the given arguments.
     *
291 292
     * @param string       $x The field in string format to be inspected by NOT IN() comparison.
     * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison.
293 294 295
     *
     * @return string
     */
296
    public function notIn($x, $y)
297
    {
298
        return $this->comparison($x, 'NOT IN', '('.implode(', ', (array) $y).')');
299 300
    }

301 302
    /**
     * Quotes a given input parameter.
303
     *
Benjamin Morel's avatar
Benjamin Morel committed
304 305
     * @param mixed       $input The parameter to be quoted.
     * @param string|null $type  The type of the parameter.
306
     *
307 308 309 310 311 312
     * @return string
     */
    public function literal($input, $type = null)
    {
        return $this->connection->quote($input, $type);
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
313
}