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

namespace Doctrine\DBAL\Query\Expression;

use Doctrine\DBAL\Connection;
6
use function func_get_arg;
7
use function func_get_args;
8
use function func_num_args;
9
use function implode;
10
use function sprintf;
11 12 13 14 15 16

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

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

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

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

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

93 94 95 96 97 98 99 100 101 102
    /**
     * 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
103 104 105
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
     * @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
122 123 124
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
     * @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
141 142 143
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
     * @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
160 161 162
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
     * @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
179 180 181
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
     * @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
198 199 200
     * @param mixed $x The left expression.
     * @param mixed $y The right expression.
     *
201 202 203 204 205 206 207 208 209 210
     * @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
211
     * @param string $x The field in string format to be restricted by IS NULL.
212
     *
213 214 215 216 217 218 219 220 221 222
     * @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
223
     * @param string $x The field in string format to be restricted by IS NOT NULL.
224
     *
225 226 227 228 229 230 231 232 233 234 235
     * @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
236
     * @param mixed  $y Argument to be used in LIKE() comparison.
237
     *
238 239
     * @return string
     */
240
    public function like($x, $y/*, ?string $escapeChar = null */)
241
    {
242
        return $this->comparison($x, 'LIKE', $y) .
243
            (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
244
    }
Steve Müller's avatar
Steve Müller committed
245

246 247 248 249
    /**
     * Creates a NOT LIKE() comparison expression with the given arguments.
     *
     * @param string $x Field in string format to be inspected by NOT LIKE() comparison.
250
     * @param mixed  $y Argument to be used in NOT LIKE() comparison.
251 252 253
     *
     * @return string
     */
254
    public function notLike($x, $y/*, ?string $escapeChar = null */)
255
    {
256
        return $this->comparison($x, 'NOT LIKE', $y) .
257
            (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
258
    }
259

260 261 262
    /**
     * Creates a IN () comparison expression with the given arguments.
     *
263 264
     * @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.
265 266 267
     *
     * @return string
     */
268
    public function in($x, $y)
269
    {
270
        return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')');
271 272 273 274 275
    }

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

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