Completed
Push — master ( ca3aef...5b2d6b )
by Morris
15:49
created

ExpressionBuilder::comparison()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\DB\QueryBuilder\ExpressionBuilder;
26
27
use Doctrine\DBAL\Query\Expression\ExpressionBuilder as DoctrineExpressionBuilder;
28
use OC\DB\QueryBuilder\CompositeExpression;
29
use OC\DB\QueryBuilder\Literal;
30
use OC\DB\QueryBuilder\QueryFunction;
31
use OC\DB\QueryBuilder\QuoteHelper;
32
use OCP\DB\QueryBuilder\IExpressionBuilder;
33
use OCP\DB\QueryBuilder\ILiteral;
34
use OCP\DB\QueryBuilder\IQueryBuilder;
35
use OCP\DB\QueryBuilder\IQueryFunction;
36
use OCP\IDBConnection;
37
38
class ExpressionBuilder implements IExpressionBuilder {
39
	/** @var \Doctrine\DBAL\Query\Expression\ExpressionBuilder */
40
	protected $expressionBuilder;
41
42
	/** @var QuoteHelper */
43
	protected $helper;
44
45
	/** @var IDBConnection */
46
	protected $connection;
47
48
	/**
49
	 * Initializes a new <tt>ExpressionBuilder</tt>.
50
	 *
51
	 * @param \OCP\IDBConnection $connection
52
	 */
53
	public function __construct(IDBConnection $connection) {
54
		$this->connection = $connection;
55
		$this->helper = new QuoteHelper();
56
		$this->expressionBuilder = new DoctrineExpressionBuilder($connection);
57
	}
58
59
	/**
60
	 * Creates a conjunction of the given boolean expressions.
61
	 *
62
	 * Example:
63
	 *
64
	 *     [php]
65
	 *     // (u.type = ?) AND (u.role = ?)
66
	 *     $expr->andX('u.type = ?', 'u.role = ?'));
67
	 *
68
	 * @param mixed $x Optional clause. Defaults = null, but requires
69
	 *                 at least one defined when converting to string.
70
	 *
71
	 * @return \OCP\DB\QueryBuilder\ICompositeExpression
72
	 */
73
	public function andX($x = null) {
74
		$arguments = func_get_args();
75
		$compositeExpression = call_user_func_array([$this->expressionBuilder, 'andX'], $arguments);
76
		return new CompositeExpression($compositeExpression);
77
	}
78
79
	/**
80
	 * Creates a disjunction of the given boolean expressions.
81
	 *
82
	 * Example:
83
	 *
84
	 *     [php]
85
	 *     // (u.type = ?) OR (u.role = ?)
86
	 *     $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
87
	 *
88
	 * @param mixed $x Optional clause. Defaults = null, but requires
89
	 *                 at least one defined when converting to string.
90
	 *
91
	 * @return \OCP\DB\QueryBuilder\ICompositeExpression
92
	 */
93
	public function orX($x = null) {
94
		$arguments = func_get_args();
95
		$compositeExpression = call_user_func_array([$this->expressionBuilder, 'orX'], $arguments);
96
		return new CompositeExpression($compositeExpression);
97
	}
98
99
	/**
100
	 * Creates a comparison expression.
101
	 *
102
	 * @param mixed $x The left expression.
103
	 * @param string $operator One of the IExpressionBuilder::* constants.
104
	 * @param mixed $y The right expression.
105
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
106
	 *                  required when comparing text fields for oci compatibility
107
	 *
108
	 * @return string
109
	 */
110
	public function comparison($x, $operator, $y, $type = null) {
111
		$x = $this->helper->quoteColumnName($x);
112
		$y = $this->helper->quoteColumnName($y);
113
		return $this->expressionBuilder->comparison($x, $operator, $y);
114
	}
115
116
	/**
117
	 * Creates an equality comparison expression with the given arguments.
118
	 *
119
	 * First argument is considered the left expression and the second is the right expression.
120
	 * When converted to string, it will generated a <left expr> = <right expr>. Example:
121
	 *
122
	 *     [php]
123
	 *     // u.id = ?
124
	 *     $expr->eq('u.id', '?');
125
	 *
126
	 * @param mixed $x The left expression.
127
	 * @param mixed $y The right expression.
128
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
129
	 *                  required when comparing text fields for oci compatibility
130
	 *
131
	 * @return string
132
	 */
133
	public function eq($x, $y, $type = null) {
134
		$x = $this->helper->quoteColumnName($x);
135
		$y = $this->helper->quoteColumnName($y);
136
		return $this->expressionBuilder->eq($x, $y);
137
	}
138
139
	/**
140
	 * Creates a non equality comparison expression with the given arguments.
141
	 * First argument is considered the left expression and the second is the right expression.
142
	 * When converted to string, it will generated a <left expr> <> <right expr>. Example:
143
	 *
144
	 *     [php]
145
	 *     // u.id <> 1
146
	 *     $q->where($q->expr()->neq('u.id', '1'));
147
	 *
148
	 * @param mixed $x The left expression.
149
	 * @param mixed $y The right expression.
150
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
151
	 *                  required when comparing text fields for oci compatibility
152
	 *
153
	 * @return string
154
	 */
155
	public function neq($x, $y, $type = null) {
156
		$x = $this->helper->quoteColumnName($x);
157
		$y = $this->helper->quoteColumnName($y);
158
		return $this->expressionBuilder->neq($x, $y);
159
	}
160
161
	/**
162
	 * Creates a lower-than 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
	 *
166
	 *     [php]
167
	 *     // u.id < ?
168
	 *     $q->where($q->expr()->lt('u.id', '?'));
169
	 *
170
	 * @param mixed $x The left expression.
171
	 * @param mixed $y The right expression.
172
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
173
	 *                  required when comparing text fields for oci compatibility
174
	 *
175
	 * @return string
176
	 */
177
	public function lt($x, $y, $type = null) {
178
		$x = $this->helper->quoteColumnName($x);
179
		$y = $this->helper->quoteColumnName($y);
180
		return $this->expressionBuilder->lt($x, $y);
181
	}
182
183
	/**
184
	 * Creates a lower-than-equal comparison expression with the given arguments.
185
	 * First argument is considered the left expression and the second is the right expression.
186
	 * When converted to string, it will generated a <left expr> <= <right expr>. Example:
187
	 *
188
	 *     [php]
189
	 *     // u.id <= ?
190
	 *     $q->where($q->expr()->lte('u.id', '?'));
191
	 *
192
	 * @param mixed $x The left expression.
193
	 * @param mixed $y The right expression.
194
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
195
	 *                  required when comparing text fields for oci compatibility
196
	 *
197
	 * @return string
198
	 */
199
	public function lte($x, $y, $type = null) {
200
		$x = $this->helper->quoteColumnName($x);
201
		$y = $this->helper->quoteColumnName($y);
202
		return $this->expressionBuilder->lte($x, $y);
203
	}
204
205
	/**
206
	 * Creates a greater-than comparison expression with the given arguments.
207
	 * First argument is considered the left expression and the second is the right expression.
208
	 * When converted to string, it will generated a <left expr> > <right expr>. Example:
209
	 *
210
	 *     [php]
211
	 *     // u.id > ?
212
	 *     $q->where($q->expr()->gt('u.id', '?'));
213
	 *
214
	 * @param mixed $x The left expression.
215
	 * @param mixed $y The right expression.
216
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
217
	 *                  required when comparing text fields for oci compatibility
218
	 *
219
	 * @return string
220
	 */
221
	public function gt($x, $y, $type = null) {
222
		$x = $this->helper->quoteColumnName($x);
223
		$y = $this->helper->quoteColumnName($y);
224
		return $this->expressionBuilder->gt($x, $y);
225
	}
226
227
	/**
228
	 * Creates a greater-than-equal comparison expression with the given arguments.
229
	 * First argument is considered the left expression and the second is the right expression.
230
	 * When converted to string, it will generated a <left expr> >= <right expr>. Example:
231
	 *
232
	 *     [php]
233
	 *     // u.id >= ?
234
	 *     $q->where($q->expr()->gte('u.id', '?'));
235
	 *
236
	 * @param mixed $x The left expression.
237
	 * @param mixed $y The right expression.
238
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
239
	 *                  required when comparing text fields for oci compatibility
240
	 *
241
	 * @return string
242
	 */
243
	public function gte($x, $y, $type = null) {
244
		$x = $this->helper->quoteColumnName($x);
245
		$y = $this->helper->quoteColumnName($y);
246
		return $this->expressionBuilder->gte($x, $y);
247
	}
248
249
	/**
250
	 * Creates an IS NULL expression with the given arguments.
251
	 *
252
	 * @param string $x The field in string format to be restricted by IS NULL.
253
	 *
254
	 * @return string
255
	 */
256
	public function isNull($x) {
257
		$x = $this->helper->quoteColumnName($x);
258
		return $this->expressionBuilder->isNull($x);
259
	}
260
261
	/**
262
	 * Creates an IS NOT NULL expression with the given arguments.
263
	 *
264
	 * @param string $x The field in string format to be restricted by IS NOT NULL.
265
	 *
266
	 * @return string
267
	 */
268
	public function isNotNull($x) {
269
		$x = $this->helper->quoteColumnName($x);
270
		return $this->expressionBuilder->isNotNull($x);
271
	}
272
273
	/**
274
	 * Creates a LIKE() comparison expression with the given arguments.
275
	 *
276
	 * @param string $x Field in string format to be inspected by LIKE() comparison.
277
	 * @param mixed $y Argument to be used in LIKE() comparison.
278
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
279
	 *                  required when comparing text fields for oci compatibility
280
	 *
281
	 * @return string
282
	 */
283
	public function like($x, $y, $type = null) {
284
		$x = $this->helper->quoteColumnName($x);
285
		$y = $this->helper->quoteColumnName($y);
286
		return $this->expressionBuilder->like($x, $y);
287
	}
288
289
	/**
290
	 * Creates a ILIKE() comparison expression with the given arguments.
291
	 *
292
	 * @param string $x Field in string format to be inspected by ILIKE() comparison.
293
	 * @param mixed $y Argument to be used in ILIKE() comparison.
294
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
295
	 *                  required when comparing text fields for oci compatibility
296
	 *
297
	 * @return string
298
	 * @since 9.0.0
299
	 */
300
	public function iLike($x, $y, $type = null) {
301
		$x = $this->helper->quoteColumnName($x);
302
		$y = $this->helper->quoteColumnName($y);
303
		return $this->expressionBuilder->comparison("LOWER($x)", 'LIKE', "LOWER($y)");
304
	}
305
306
	/**
307
	 * Creates a NOT LIKE() comparison expression with the given arguments.
308
	 *
309
	 * @param string $x Field in string format to be inspected by NOT LIKE() comparison.
310
	 * @param mixed $y Argument to be used in NOT LIKE() comparison.
311
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
312
	 *                  required when comparing text fields for oci compatibility
313
	 *
314
	 * @return string
315
	 */
316
	public function notLike($x, $y, $type = null) {
317
		$x = $this->helper->quoteColumnName($x);
318
		$y = $this->helper->quoteColumnName($y);
319
		return $this->expressionBuilder->notLike($x, $y);
320
	}
321
322
	/**
323
	 * Creates a IN () comparison expression with the given arguments.
324
	 *
325
	 * @param string $x The field in string format to be inspected by IN() comparison.
326
	 * @param string|array $y The placeholder or the array of values to be used by IN() comparison.
327
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
328
	 *                  required when comparing text fields for oci compatibility
329
	 *
330
	 * @return string
331
	 */
332
	public function in($x, $y, $type = null) {
333
		$x = $this->helper->quoteColumnName($x);
334
		$y = $this->helper->quoteColumnNames($y);
335
		return $this->expressionBuilder->in($x, $y);
336
	}
337
338
	/**
339
	 * Creates a NOT IN () comparison expression with the given arguments.
340
	 *
341
	 * @param string $x The field in string format to be inspected by NOT IN() comparison.
342
	 * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison.
343
	 * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
344
	 *                  required when comparing text fields for oci compatibility
345
	 *
346
	 * @return string
347
	 */
348
	public function notIn($x, $y, $type = null) {
349
		$x = $this->helper->quoteColumnName($x);
350
		$y = $this->helper->quoteColumnNames($y);
351
		return $this->expressionBuilder->notIn($x, $y);
352
	}
353
354
	/**
355
	 * Creates a $x = '' statement, because Oracle needs a different check
356
	 *
357
	 * @param string $x The field in string format to be inspected by the comparison.
358
	 * @return string
359
	 * @since 13.0.0
360
	 */
361
	public function emptyString($x) {
362
		return $this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR));
363
	}
364
365
	/**
366
	 * Creates a `$x <> ''` statement, because Oracle needs a different check
367
	 *
368
	 * @param string $x The field in string format to be inspected by the comparison.
369
	 * @return string
370
	 * @since 13.0.0
371
	 */
372
	public function nonEmptyString($x) {
373
		return $this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR));
374
	}
375
376
	/**
377
	 * Binary AND Operator copies a bit to the result if it exists in both operands.
378
	 *
379
	 * @param string|ILiteral $x The field or value to check
380
	 * @param int $y Bitmap that must be set
381
	 * @return IQueryFunction
382
	 * @since 12.0.0
383
	 */
384
	public function bitwiseAnd($x, $y) {
385
		return new QueryFunction($this->connection->getDatabasePlatform()->getBitAndComparisonExpression(
386
			$this->helper->quoteColumnName($x),
387
			$y
388
		));
389
	}
390
391
	/**
392
	 * Binary OR Operator copies a bit if it exists in either operand.
393
	 *
394
	 * @param string|ILiteral $x The field or value to check
395
	 * @param int $y Bitmap that must be set
396
	 * @return IQueryFunction
397
	 * @since 12.0.0
398
	 */
399
	public function bitwiseOr($x, $y) {
400
		return new QueryFunction($this->connection->getDatabasePlatform()->getBitOrComparisonExpression(
401
			$this->helper->quoteColumnName($x),
402
			$y
403
		));
404
	}
405
406
	/**
407
	 * Quotes a given input parameter.
408
	 *
409
	 * @param mixed $input The parameter to be quoted.
410
	 * @param mixed|null $type One of the IQueryBuilder::PARAM_* constants
411
	 *
412
	 * @return ILiteral
413
	 */
414
	public function literal($input, $type = null) {
415
		return new Literal($this->expressionBuilder->literal($input, $type));
416
	}
417
418
	/**
419
	 * Returns a IQueryFunction that casts the column to the given type
420
	 *
421
	 * @param string $column
422
	 * @param mixed $type One of IQueryBuilder::PARAM_*
423
	 * @return string
424
	 */
425
	public function castColumn($column, $type) {
426
		return new QueryFunction(
427
			$this->helper->quoteColumnName($column)
428
		);
429
	}
430
}
431