Completed
Push — master ( f076cc...ed4ac2 )
by Sergei
63:27 queued 11s
created

ExpressionBuilder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
eloc 28
c 0
b 0
f 0
dl 0
loc 235
rs 10
ccs 34
cts 34
cp 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A and() 0 3 1
A or() 0 3 1
A comparison() 0 3 1
A lte() 0 3 1
A neq() 0 3 1
A gt() 0 3 1
A notIn() 0 3 1
A isNull() 0 3 1
A like() 0 4 2
A gte() 0 3 1
A lt() 0 3 1
A in() 0 3 1
A literal() 0 3 1
A eq() 0 3 1
A notLike() 0 4 2
A isNotNull() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Query\Expression;
6
7
use Doctrine\DBAL\Connection;
8
use function array_merge;
9
use function implode;
10
use function sprintf;
11
12
/**
13
 * ExpressionBuilder class is responsible to dynamically create SQL query parts.
14
 */
15
class ExpressionBuilder
16
{
17
    public const EQ  = '=';
18
    public const NEQ = '<>';
19
    public const LT  = '<';
20
    public const LTE = '<=';
21
    public const GT  = '>';
22
    public const GTE = '>=';
23
24
    /**
25
     * The DBAL Connection.
26
     *
27
     * @var Connection
28
     */
29
    private $connection;
30
31
    /**
32
     * Initializes a new <tt>ExpressionBuilder</tt>.
33
     *
34
     * @param Connection $connection The DBAL Connection.
35 5571
     */
36
    public function __construct(Connection $connection)
37 5571
    {
38 5571
        $this->connection = $connection;
39
    }
40
41
    /**
42
     * Creates a conjunction of the given expressions.
43
     *
44
     * @param string|CompositeExpression $expression
45
     * @param string|CompositeExpression ...$expressions
46
     */
47
    public function and($expression, ...$expressions) : CompositeExpression
48
    {
49
        return new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$expression], $expressions));
50
    }
51 189
52
    /**
53 189
     * Creates a disjunction of the given expressions.
54
     *
55
     * @param string|CompositeExpression $expression
56
     * @param string|CompositeExpression ...$expressions
57
     */
58
    public function or($expression, ...$expressions) : CompositeExpression
59
    {
60
        return new CompositeExpression(CompositeExpression::TYPE_OR, array_merge([$expression], $expressions));
61
    }
62
63
    /**
64
     * Creates a comparison expression.
65
     *
66
     * @param string $x        The left expression.
67 162
     * @param string $operator The comparison operator.
68
     * @param string $y        The right expression.
69 162
     */
70
    public function comparison(string $x, string $operator, string $y) : string
71
    {
72
        return $x . ' ' . $operator . ' ' . $y;
73
    }
74
75
    /**
76
     * Creates an equality comparison expression with the given arguments.
77
     *
78
     * First argument is considered the left expression and the second is the right expression.
79 756
     * When converted to string, it will generated a <left expr> = <right expr>. Example:
80
     *
81 756
     *     [php]
82
     *     // u.id = ?
83
     *     $expr->eq('u.id', '?');
84
     *
85
     * @param string $x The left expression.
86
     * @param string $y The right expression.
87
     */
88
    public function eq(string $x, string $y) : string
89
    {
90
        return $this->comparison($x, self::EQ, $y);
91
    }
92
93
    /**
94
     * Creates a non equality comparison expression with the given arguments.
95
     * First argument is considered the left expression and the second is the right expression.
96
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
97 243
     *
98
     *     [php]
99 243
     *     // u.id <> 1
100
     *     $q->where($q->expr()->neq('u.id', '1'));
101
     *
102
     * @param string $x The left expression.
103
     * @param string $y The right expression.
104
     */
105
    public function neq(string $x, string $y) : string
106
    {
107
        return $this->comparison($x, self::NEQ, $y);
108
    }
109
110
    /**
111
     * Creates a lower-than comparison expression with the given arguments.
112
     * First argument is considered the left expression and the second is the right expression.
113
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
114 27
     *
115
     *     [php]
116 27
     *     // u.id < ?
117
     *     $q->where($q->expr()->lt('u.id', '?'));
118
     *
119
     * @param string $x The left expression.
120
     * @param string $y The right expression.
121
     */
122
    public function lt(string $x, string $y) : string
123
    {
124
        return $this->comparison($x, self::LT, $y);
125
    }
126
127
    /**
128
     * Creates a lower-than-equal comparison expression with the given arguments.
129
     * First argument is considered the left expression and the second is the right expression.
130
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
131 27
     *
132
     *     [php]
133 27
     *     // u.id <= ?
134
     *     $q->where($q->expr()->lte('u.id', '?'));
135
     *
136
     * @param string $x The left expression.
137
     * @param string $y The right expression.
138
     */
139
    public function lte(string $x, string $y) : string
140
    {
141
        return $this->comparison($x, self::LTE, $y);
142
    }
143
144
    /**
145
     * Creates a greater-than comparison expression with the given arguments.
146
     * First argument is considered the left expression and the second is the right expression.
147
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
148 27
     *
149
     *     [php]
150 27
     *     // u.id > ?
151
     *     $q->where($q->expr()->gt('u.id', '?'));
152
     *
153
     * @param string $x The left expression.
154
     * @param string $y The right expression.
155
     */
156
    public function gt(string $x, string $y) : string
157
    {
158
        return $this->comparison($x, self::GT, $y);
159
    }
160
161
    /**
162
     * Creates a greater-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:
165 27
     *
166
     *     [php]
167 27
     *     // u.id >= ?
168
     *     $q->where($q->expr()->gte('u.id', '?'));
169
     *
170
     * @param string $x The left expression.
171
     * @param string $y The right expression.
172
     */
173
    public function gte(string $x, string $y) : string
174
    {
175
        return $this->comparison($x, self::GTE, $y);
176
    }
177
178
    /**
179
     * Creates an IS NULL expression with the given arguments.
180
     *
181
     * @param string $x The field in string format to be restricted by IS NULL.
182 27
     */
183
    public function isNull(string $x) : string
184 27
    {
185
        return $x . ' IS NULL';
186
    }
187
188
    /**
189
     * Creates an IS NOT NULL expression with the given arguments.
190
     *
191
     * @param string $x The field in string format to be restricted by IS NOT NULL.
192 27
     */
193
    public function isNotNull(string $x) : string
194 27
    {
195
        return $x . ' IS NOT NULL';
196
    }
197
198
    /**
199
     * Creates a LIKE comparison expression.
200
     *
201
     * @param string $expression The expression to be inspected by the LIKE comparison
202 27
     * @param string $pattern    The pattern to compare against
203
     */
204 27
    public function like(string $expression, string $pattern, ?string $escapeChar = null) : string
205
    {
206
        return $this->comparison($expression, 'LIKE', $pattern) .
207
            ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : '');
208
    }
209
210
    /**
211
     * Creates a NOT LIKE comparison expression
212
     *
213 54
     * @param string $expression The expression to be inspected by the NOT LIKE comparison
214
     * @param string $pattern    The pattern to compare against
215 54
     */
216 54
    public function notLike(string $expression, string $pattern, ?string $escapeChar = null) : string
217
    {
218
        return $this->comparison($expression, 'NOT LIKE', $pattern) .
219
            ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : '');
220
    }
221
222
    /**
223
     * Creates a IN () comparison expression with the given arguments.
224
     *
225 54
     * @param string          $x The field in string format to be inspected by IN() comparison.
226
     * @param string|string[] $y The placeholder or the array of values to be used by IN() comparison.
227 54
     */
228 54
    public function in(string $x, $y) : string
229
    {
230
        return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')');
231
    }
232
233
    /**
234
     * Creates a NOT IN () comparison expression with the given arguments.
235
     *
236
     * @param string          $x The field in string format to be inspected by NOT IN() comparison.
237 54
     * @param string|string[] $y The placeholder or the array of values to be used by NOT IN() comparison.
238
     */
239 54
    public function notIn(string $x, $y) : string
240
    {
241
        return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')');
242
    }
243
244
    /**
245
     * Creates an SQL literal expression from the string.
246
     */
247
    public function literal(string $input) : string
248 54
    {
249
        return $this->connection->quote($input);
250 54
    }
251
}
252