Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

DBAL/Query/Expression/ExpressionBuilder.php (1 issue)

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
24
/**
25
 * ExpressionBuilder class is responsible to dynamically create SQL query parts.
26
 *
27
 * @link   www.doctrine-project.org
28
 * @since  2.1
29
 * @author Guilherme Blanco <[email protected]>
30
 * @author Benjamin Eberlei <[email protected]>
31
 */
32
class ExpressionBuilder
33
{
34
    const EQ  = '=';
35
    const NEQ = '<>';
36
    const LT  = '<';
37
    const LTE = '<=';
38
    const GT  = '>';
39
    const GTE = '>=';
40
41
    /**
42
     * The DBAL Connection.
43
     *
44
     * @var \Doctrine\DBAL\Connection
45
     */
46
    private $connection;
47
48
    /**
49
     * Initializes a new <tt>ExpressionBuilder</tt>.
50
     *
51
     * @param \Doctrine\DBAL\Connection $connection The DBAL Connection.
52
     */
53 227
    public function __construct(Connection $connection)
54
    {
55 227
        $this->connection = $connection;
56 227
    }
57
58
    /**
59
     * Creates a conjunction of the given boolean expressions.
60
     *
61
     * Example:
62
     *
63
     *     [php]
64
     *     // (u.type = ?) AND (u.role = ?)
65
     *     $expr->andX('u.type = ?', 'u.role = ?'));
66
     *
67
     * @param mixed $x Optional clause. Defaults = null, but requires
68
     *                 at least one defined when converting to string.
69
     *
70
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
71
     */
72 7
    public function andX($x = null)
73
    {
74 7
        return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
75
    }
76
77
    /**
78
     * Creates a disjunction of the given boolean expressions.
79
     *
80
     * Example:
81
     *
82
     *     [php]
83
     *     // (u.type = ?) OR (u.role = ?)
84
     *     $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
85
     *
86
     * @param mixed $x Optional clause. Defaults = null, but requires
87
     *                 at least one defined when converting to string.
88
     *
89
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
90
     */
91 6
    public function orX($x = null)
92
    {
93 6
        return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
94
    }
95
96
    /**
97
     * Creates a comparison expression.
98
     *
99
     * @param mixed  $x        The left expression.
100
     * @param string $operator One of the ExpressionBuilder::* constants.
101
     * @param mixed  $y        The right expression.
102
     *
103
     * @return string
104
     */
105 24
    public function comparison($x, $operator, $y)
106
    {
107 24
        return $x . ' ' . $operator . ' ' . $y;
108
    }
109
110
    /**
111
     * Creates an equality comparison expression with the given arguments.
112
     *
113
     * First argument is considered the left expression and the second is the right expression.
114
     * When converted to string, it will generated a <left expr> = <right expr>. Example:
115
     *
116
     *     [php]
117
     *     // u.id = ?
118
     *     $expr->eq('u.id', '?');
119
     *
120
     * @param mixed $x The left expression.
121
     * @param mixed $y The right expression.
122
     *
123
     * @return string
124
     */
125 9
    public function eq($x, $y)
126
    {
127 9
        return $this->comparison($x, self::EQ, $y);
128
    }
129
130
    /**
131
     * Creates a non equality comparison expression with the given arguments.
132
     * First argument is considered the left expression and the second is the right expression.
133
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
134
     *
135
     *     [php]
136
     *     // u.id <> 1
137
     *     $q->where($q->expr()->neq('u.id', '1'));
138
     *
139
     * @param mixed $x The left expression.
140
     * @param mixed $y The right expression.
141
     *
142
     * @return string
143
     */
144 1
    public function neq($x, $y)
145
    {
146 1
        return $this->comparison($x, self::NEQ, $y);
147
    }
148
149
    /**
150
     * Creates a lower-than comparison expression with the given arguments.
151
     * First argument is considered the left expression and the second is the right expression.
152
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
153
     *
154
     *     [php]
155
     *     // u.id < ?
156
     *     $q->where($q->expr()->lt('u.id', '?'));
157
     *
158
     * @param mixed $x The left expression.
159
     * @param mixed $y The right expression.
160
     *
161
     * @return string
162
     */
163 1
    public function lt($x, $y)
164
    {
165 1
        return $this->comparison($x, self::LT, $y);
166
    }
167
168
    /**
169
     * Creates a lower-than-equal comparison expression with the given arguments.
170
     * First argument is considered the left expression and the second is the right expression.
171
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
172
     *
173
     *     [php]
174
     *     // u.id <= ?
175
     *     $q->where($q->expr()->lte('u.id', '?'));
176
     *
177
     * @param mixed $x The left expression.
178
     * @param mixed $y The right expression.
179
     *
180
     * @return string
181
     */
182 1
    public function lte($x, $y)
183
    {
184 1
        return $this->comparison($x, self::LTE, $y);
185
    }
186
187
    /**
188
     * Creates a greater-than comparison expression with the given arguments.
189
     * First argument is considered the left expression and the second is the right expression.
190
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
191
     *
192
     *     [php]
193
     *     // u.id > ?
194
     *     $q->where($q->expr()->gt('u.id', '?'));
195
     *
196
     * @param mixed $x The left expression.
197
     * @param mixed $y The right expression.
198
     *
199
     * @return string
200
     */
201 1
    public function gt($x, $y)
202
    {
203 1
        return $this->comparison($x, self::GT, $y);
204
    }
205
206
    /**
207
     * Creates a greater-than-equal comparison expression with the given arguments.
208
     * First argument is considered the left expression and the second is the right expression.
209
     * When converted to string, it will generated a <left expr> >= <right expr>. Example:
210
     *
211
     *     [php]
212
     *     // u.id >= ?
213
     *     $q->where($q->expr()->gte('u.id', '?'));
214
     *
215
     * @param mixed $x The left expression.
216
     * @param mixed $y The right expression.
217
     *
218
     * @return string
219
     */
220 1
    public function gte($x, $y)
221
    {
222 1
        return $this->comparison($x, self::GTE, $y);
223
    }
224
225
    /**
226
     * Creates an IS NULL expression with the given arguments.
227
     *
228
     * @param string $x The field in string format to be restricted by IS NULL.
229
     *
230
     * @return string
231
     */
232 1
    public function isNull($x)
233
    {
234 1
        return $x . ' IS NULL';
235
    }
236
237
    /**
238
     * Creates an IS NOT NULL expression with the given arguments.
239
     *
240
     * @param string $x The field in string format to be restricted by IS NOT NULL.
241
     *
242
     * @return string
243
     */
244 1
    public function isNotNull($x)
245
    {
246 1
        return $x . ' IS NOT NULL';
247
    }
248
249
    /**
250
     * Creates a LIKE() comparison expression with the given arguments.
251
     *
252
     * @param string $x Field in string format to be inspected by LIKE() comparison.
253
     * @param mixed  $y Argument to be used in LIKE() comparison.
254
     *
255
     * @return string
256
     */
257
    public function like($x, $y)
258
    {
259
        return $this->comparison($x, 'LIKE', $y);
260
    }
261
262
    /**
263
     * Creates a NOT LIKE() comparison expression with the given arguments.
264
     *
265
     * @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
     * @return string
269
     */
270
    public function notLike($x, $y)
271
    {
272
        return $this->comparison($x, 'NOT LIKE', $y);
273
    }
274
275
    /**
276
     * Creates a IN () comparison expression with the given arguments.
277
     *
278
     * @param string       $x The field in string format to be inspected by IN() comparison.
279
     * @param string|array $y The placeholder or the array of values to be used by IN() comparison.
280
     *
281
     * @return string
282
     */
283 2
    public function in($x, $y)
284
    {
285 2
        return $this->comparison($x, 'IN', '('.implode(', ', (array) $y).')');
286
    }
287
288
    /**
289
     * Creates a NOT IN () comparison expression with the given arguments.
290
     *
291
     * @param string       $x The field in string format to be inspected by NOT IN() comparison.
292
     * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison.
293
     *
294
     * @return string
295
     */
296 2
    public function notIn($x, $y)
297
    {
298 2
        return $this->comparison($x, 'NOT IN', '('.implode(', ', (array) $y).')');
299
    }
300
301
    /**
302
     * Quotes a given input parameter.
303
     *
304
     * @param mixed       $input The parameter to be quoted.
305
     * @param string|null $type  The type of the parameter.
306
     *
307
     * @return string
308
     */
309
    public function literal($input, $type = null)
310
    {
311
        return $this->connection->quote($input, $type);
0 ignored issues
show
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

311
        return $this->connection->quote($input, /** @scrutinizer ignore-type */ $type);
Loading history...
312
    }
313
}
314