ExpressionBuilder.php 8.49 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\DBAL\Query\Expression;

use Doctrine\DBAL\Connection;
6

7
use function func_get_arg;
8
use function func_get_args;
9
use function func_num_args;
10
use function implode;
11
use function sprintf;
12 13 14 15 16 17

/**
 * ExpressionBuilder class is responsible to dynamically create SQL query parts.
 */
class ExpressionBuilder
{
18 19 20 21 22 23
    public const EQ  = '=';
    public const NEQ = '<>';
    public const LT  = '<';
    public const LTE = '<=';
    public const GT  = '>';
    public const GTE = '>=';
24

25
    /**
Benjamin Morel's avatar
Benjamin Morel committed
26 27
     * The DBAL Connection.
     *
28
     * @var Connection
29
     */
Benjamin Morel's avatar
Benjamin Morel committed
30
    private $connection;
31 32 33 34

    /**
     * Initializes a new <tt>ExpressionBuilder</tt>.
     *
35
     * @param Connection $connection The DBAL Connection.
36 37 38 39 40
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }
41

42 43 44 45 46 47 48 49 50 51 52
    /**
     * 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
53
     *
54
     * @return CompositeExpression
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
     */
    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
72
     *
73
     * @return CompositeExpression
74 75 76 77 78 79 80 81
     */
    public function orX($x = null)
    {
        return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
    }

    /**
     * Creates a comparison expression.
82
     *
Benjamin Morel's avatar
Benjamin Morel committed
83
     * @param mixed  $x        The left expression.
84
     * @param string $operator One of the ExpressionBuilder::* constants.
Benjamin Morel's avatar
Benjamin Morel committed
85 86
     * @param mixed  $y        The right expression.
     *
87 88 89 90 91 92
     * @return string
     */
    public function comparison($x, $operator, $y)
    {
        return $x . ' ' . $operator . ' ' . $y;
    }
93

94 95 96 97 98 99 100 101 102 103
    /**
     * 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
104 105 106
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
     * @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
123 124 125
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
     * @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
142 143 144
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
     * @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
161 162 163
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
     * @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
180 181 182
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
     * @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
199 200 201
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
202 203 204 205 206 207 208 209 210 211
     * @return string
     */
    public function gte($x, $y)
    {
        return $this->comparison($x, self::GTE, $y);
    }

    /**
     * Creates an IS NULL expression with the given arguments.
     *
212
     * @param string $x The expression to be restricted by IS NULL.
213
     *
214 215 216 217 218 219 220 221 222 223
     * @return string
     */
    public function isNull($x)
    {
        return $x . ' IS NULL';
    }

    /**
     * Creates an IS NOT NULL expression with the given arguments.
     *
224
     * @param string $x The expression to be restricted by IS NOT NULL.
225
     *
226 227 228 229 230 231 232 233 234 235 236
     * @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
237
     * @param mixed  $y Argument to be used in LIKE() comparison.
238
     *
239 240
     * @return string
     */
241
    public function like($x, $y/*, ?string $escapeChar = null */)
242
    {
243
        return $this->comparison($x, 'LIKE', $y) .
244
            (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
245
    }
Steve Müller's avatar
Steve Müller committed
246

247 248 249 250
    /**
     * Creates a NOT LIKE() comparison expression with the given arguments.
     *
     * @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 254
     *
     * @return string
     */
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 262 263
    /**
     * Creates a IN () comparison expression with the given arguments.
     *
264 265
     * @param string          $x The field in string format to be inspected by IN() comparison.
     * @param string|string[] $y The placeholder or the array of values to be used by IN() comparison.
266 267 268
     *
     * @return string
     */
269
    public function in($x, $y)
270
    {
271
        return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')');
272 273 274 275 276
    }

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

287 288
    /**
     * Quotes a given input parameter.
289
     *
Sergei Morozov's avatar
Sergei Morozov committed
290 291
     * @param mixed    $input The parameter to be quoted.
     * @param int|null $type  The type of the parameter.
292
     *
293 294 295 296 297 298
     * @return string
     */
    public function literal($input, $type = null)
    {
        return $this->connection->quote($input, $type);
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
299
}