Completed
Push — master ( 69742a...ea7657 )
by Marco
13s
created

ExpressionBuilder::like()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
/*
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.
14
 *
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>.
18
 */
19
20
namespace Doctrine\DBAL\Query\Expression;
21
22
use Doctrine\DBAL\Connection;
23
use function func_get_arg;
24
use function func_num_args;
25
use function sprintf;
26
27
/**
28
 * ExpressionBuilder class is responsible to dynamically create SQL query parts.
29
 *
30
 * @link   www.doctrine-project.org
31
 * @since  2.1
32
 * @author Guilherme Blanco <[email protected]>
33
 * @author Benjamin Eberlei <[email protected]>
34
 */
35
class ExpressionBuilder
36
{
37
    const EQ  = '=';
38
    const NEQ = '<>';
39
    const LT  = '<';
40
    const LTE = '<=';
41
    const GT  = '>';
42
    const GTE = '>=';
43
44
    /**
45
     * The DBAL Connection.
46
     *
47
     * @var \Doctrine\DBAL\Connection
48
     */
49
    private $connection;
50
51
    /**
52
     * Initializes a new <tt>ExpressionBuilder</tt>.
53
     *
54
     * @param \Doctrine\DBAL\Connection $connection The DBAL Connection.
55
     */
56 231
    public function __construct(Connection $connection)
57
    {
58 231
        $this->connection = $connection;
59 231
    }
60
61
    /**
62
     * Creates a conjunction of the given boolean expressions.
63
     *
64
     * Example:
65
     *
66
     *     [php]
67
     *     // (u.type = ?) AND (u.role = ?)
68
     *     $expr->andX('u.type = ?', 'u.role = ?'));
69
     *
70
     * @param mixed $x Optional clause. Defaults = null, but requires
71
     *                 at least one defined when converting to string.
72
     *
73
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
74
     */
75 7
    public function andX($x = null)
76
    {
77 7
        return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
78
    }
79
80
    /**
81
     * Creates a disjunction of the given boolean expressions.
82
     *
83
     * Example:
84
     *
85
     *     [php]
86
     *     // (u.type = ?) OR (u.role = ?)
87
     *     $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
88
     *
89
     * @param mixed $x Optional clause. Defaults = null, but requires
90
     *                 at least one defined when converting to string.
91
     *
92
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
93
     */
94 6
    public function orX($x = null)
95
    {
96 6
        return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
97
    }
98
99
    /**
100
     * Creates a comparison expression.
101
     *
102
     * @param mixed  $x        The left expression.
103
     * @param string $operator One of the ExpressionBuilder::* constants.
104
     * @param mixed  $y        The right expression.
105
     *
106
     * @return string
107
     */
108 28
    public function comparison($x, $operator, $y)
109
    {
110 28
        return $x . ' ' . $operator . ' ' . $y;
111
    }
112
113
    /**
114
     * Creates an equality comparison expression with the given arguments.
115
     *
116
     * First argument is considered the left expression and the second is the right expression.
117
     * When converted to string, it will generated a <left expr> = <right expr>. Example:
118
     *
119
     *     [php]
120
     *     // u.id = ?
121
     *     $expr->eq('u.id', '?');
122
     *
123
     * @param mixed $x The left expression.
124
     * @param mixed $y The right expression.
125
     *
126
     * @return string
127
     */
128 9
    public function eq($x, $y)
129
    {
130 9
        return $this->comparison($x, self::EQ, $y);
131
    }
132
133
    /**
134
     * Creates a non equality comparison expression with the given arguments.
135
     * First argument is considered the left expression and the second is the right expression.
136
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
137
     *
138
     *     [php]
139
     *     // u.id <> 1
140
     *     $q->where($q->expr()->neq('u.id', '1'));
141
     *
142
     * @param mixed $x The left expression.
143
     * @param mixed $y The right expression.
144
     *
145
     * @return string
146
     */
147 1
    public function neq($x, $y)
148
    {
149 1
        return $this->comparison($x, self::NEQ, $y);
150
    }
151
152
    /**
153
     * Creates a lower-than comparison expression with the given arguments.
154
     * First argument is considered the left expression and the second is the right expression.
155
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
156
     *
157
     *     [php]
158
     *     // u.id < ?
159
     *     $q->where($q->expr()->lt('u.id', '?'));
160
     *
161
     * @param mixed $x The left expression.
162
     * @param mixed $y The right expression.
163
     *
164
     * @return string
165
     */
166 1
    public function lt($x, $y)
167
    {
168 1
        return $this->comparison($x, self::LT, $y);
169
    }
170
171
    /**
172
     * Creates a lower-than-equal comparison expression with the given arguments.
173
     * First argument is considered the left expression and the second is the right expression.
174
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
175
     *
176
     *     [php]
177
     *     // u.id <= ?
178
     *     $q->where($q->expr()->lte('u.id', '?'));
179
     *
180
     * @param mixed $x The left expression.
181
     * @param mixed $y The right expression.
182
     *
183
     * @return string
184
     */
185 1
    public function lte($x, $y)
186
    {
187 1
        return $this->comparison($x, self::LTE, $y);
188
    }
189
190
    /**
191
     * Creates a greater-than comparison expression with the given arguments.
192
     * First argument is considered the left expression and the second is the right expression.
193
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
194
     *
195
     *     [php]
196
     *     // u.id > ?
197
     *     $q->where($q->expr()->gt('u.id', '?'));
198
     *
199
     * @param mixed $x The left expression.
200
     * @param mixed $y The right expression.
201
     *
202
     * @return string
203
     */
204 1
    public function gt($x, $y)
205
    {
206 1
        return $this->comparison($x, self::GT, $y);
207
    }
208
209
    /**
210
     * Creates a greater-than-equal comparison expression with the given arguments.
211
     * First argument is considered the left expression and the second is the right expression.
212
     * When converted to string, it will generated a <left expr> >= <right expr>. Example:
213
     *
214
     *     [php]
215
     *     // u.id >= ?
216
     *     $q->where($q->expr()->gte('u.id', '?'));
217
     *
218
     * @param mixed $x The left expression.
219
     * @param mixed $y The right expression.
220
     *
221
     * @return string
222
     */
223 1
    public function gte($x, $y)
224
    {
225 1
        return $this->comparison($x, self::GTE, $y);
226
    }
227
228
    /**
229
     * Creates an IS NULL expression with the given arguments.
230
     *
231
     * @param string $x The field in string format to be restricted by IS NULL.
232
     *
233
     * @return string
234
     */
235 1
    public function isNull($x)
236
    {
237 1
        return $x . ' IS NULL';
238
    }
239
240
    /**
241
     * Creates an IS NOT NULL expression with the given arguments.
242
     *
243
     * @param string $x The field in string format to be restricted by IS NOT NULL.
244
     *
245
     * @return string
246
     */
247 1
    public function isNotNull($x)
248
    {
249 1
        return $x . ' IS NOT NULL';
250
    }
251
252
    /**
253
     * Creates a LIKE() comparison expression with the given arguments.
254
     *
255
     * @param string $x Field in string format to be inspected by LIKE() comparison.
256
     * @param mixed  $y Argument to be used in LIKE() comparison.
257
     *
258
     * @return string
259
     */
260 2
    public function like($x, $y/*, ?string $escapeChar = null */)
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
261
    {
262 2
        return $this->comparison($x, 'LIKE', $y) .
263 2
            (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
264
    }
265
266
    /**
267
     * Creates a NOT LIKE() comparison expression with the given arguments.
268
     *
269
     * @param string $x Field in string format to be inspected by NOT LIKE() comparison.
270
     * @param mixed  $y Argument to be used in NOT LIKE() comparison.
271
     *
272
     * @return string
273
     */
274 2
    public function notLike($x, $y/*, ?string $escapeChar = null */)
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
275
    {
276 2
        return $this->comparison($x, 'NOT LIKE', $y) .
277 2
            (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
278
    }
279
280
    /**
281
     * Creates a IN () comparison expression with the given arguments.
282
     *
283
     * @param string       $x The field in string format to be inspected by IN() comparison.
284
     * @param string|array $y The placeholder or the array of values to be used by IN() comparison.
285
     *
286
     * @return string
287
     */
288 2
    public function in($x, $y)
289
    {
290 2
        return $this->comparison($x, 'IN', '('.implode(', ', (array) $y).')');
291
    }
292
293
    /**
294
     * Creates a NOT IN () comparison expression with the given arguments.
295
     *
296
     * @param string       $x The field in string format to be inspected by NOT IN() comparison.
297
     * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison.
298
     *
299
     * @return string
300
     */
301 2
    public function notIn($x, $y)
302
    {
303 2
        return $this->comparison($x, 'NOT IN', '('.implode(', ', (array) $y).')');
304
    }
305
306
    /**
307
     * Quotes a given input parameter.
308
     *
309
     * @param mixed       $input The parameter to be quoted.
310
     * @param string|null $type  The type of the parameter.
311
     *
312
     * @return string
313
     */
314
    public function literal($input, $type = null)
315
    {
316
        return $this->connection->quote($input, $type);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type string; however, parameter $type of Doctrine\DBAL\Connection::quote() does only seem to accept null|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

316
        return $this->connection->quote($input, /** @scrutinizer ignore-type */ $type);
Loading history...
317
    }
318
}
319