Completed
Push — v2 ( f1b86f...9a6357 )
by Berend
01:53
created

Query::GreaterOrEqual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
10
namespace miBadger\Query;
11
12
/**
13
 * The query class.
14
 *
15
 * @since 1.0.0
16
 */
17
class Query implements QueryInterface
18
{
19
	/* @var \PDO The PDO. */
20
	private $pdo;
21
22
	/* @var array The bindings. */
23
	private $bindings;
24
25
	/* @var QueryBuilder The query builder. */
26
	private $queryBuilder;
27
28
	/**
29
	 * Construct a query object with the given pdo and table.
30
	 *
31
	 * @param \PDO $pdo
32
	 * @param string $table
33
	 */
34 21
	public function __construct(\PDO $pdo, $table)
35
	{
36 21
		$this->pdo = $pdo;
37 21
		$this->bindings = [];
38 21
		$this->queryBuilder = new QueryBuilder($table);
39 21
	}
40
41
	/**
42
	 * Returns a string representation of the query object.
43
	 *
44
	 * @return string a string representation of the query object.
45
	 */
46 21
	public function __toString()
47
	{
48 21
		return $this->queryBuilder->__toString();
49
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54 17
	public function select($columns = ['*'])
55
	{
56 17
		$this->queryBuilder->select(is_array($columns) ? $columns : func_get_args());
57
58 17
		return $this;
59
	}
60
61
	/**
62
	 * {@inheritdoc}
63
	 */
64 1
	public function insert(array $values)
65
	{
66 1
		$this->bindings['insert'] = [];
67 1
		$this->queryBuilder->insert($this->setBindings('insert', $values));
68
69 1
		return $this;
70
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75 1
	public function update(array $values)
76
	{
77 1
		$this->queryBuilder->update($this->setBindings('update', $values));
78
79 1
		return $this;
80
	}
81
82
	/**
83
	 * {@inheritdoc}
84
	 */
85 1
	public function delete()
86
	{
87 1
		$this->queryBuilder->delete();
88
89 1
		return $this;
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95 1
	public function join($table, $primary, $operator, $secondary)
96
	{
97 1
		$this->queryBuilder->join($table, $primary, $operator, $secondary);
98
99 1
		return $this;
100
	}
101
102
	/**
103
	 * {@inheritdoc}
104
	 */
105 1
	public function leftJoin($table, $primary, $operator, $secondary)
106
	{
107 1
		$this->queryBuilder->leftJoin($table, $primary, $operator, $secondary);
108
109 1
		return $this;
110
	}
111
112
	/**
113
	 * {@inheritdoc}
114
	 */
115 1
	public function rightJoin($table, $primary, $operator, $secondary)
116
	{
117 1
		$this->queryBuilder->rightJoin($table, $primary, $operator, $secondary);
118
119 1
		return $this;
120
	}
121
122
	/**
123
	 * {@inheritdoc}
124
	 */
125 1
	public function crossJoin($table, $primary, $operator, $secondary)
126
	{
127 1
		$this->queryBuilder->crossJoin($table, $primary, $operator, $secondary);
128
129 1
		return $this;
130
	}
131
132
	/**
133
	 * {@inheritdoc}
134
	 */
135 7
	public function where(QueryExpression $exp)
136
	{
137 7
		$conds = $exp->getFlattenedConditions();
138
139 7
		foreach ($conds as $cond) {
140 7
			$cond->bind($this);
141
		}
142
143 7
		$this->queryBuilder->where($exp);
144 7
		return $this;
145
	}
146
147
	public function having(QueryExpression $exp)
148
	{
149
		$conds = $exp->getFlattenedConditions();
150 1
151
		foreach ($conds as $cond) {
152 1
			$cond->bind($this, 'having');
153
		}
154 1
155
		$this->queryBuilder->having($exp);
156
		return $this;
157
	}
158
159
	/**
160 1
	 * {@inheritdoc}
161
	 */
162 1
	public function groupBy($column)
163
	{
164 1
		$this->queryBuilder->groupBy($column);
165
166
		return $this;
167
	}
168
169
	/**
170 2
	 * {@inheritdoc}
171
	 */
172 2
	public function orderBy($column, $order = null)
173
	{
174 2
		$this->queryBuilder->orderBy($column, $order);
175
176
		return $this;
177
	}
178
179
	/**
180 1
	 * {@inheritdoc}
181
	 */
182 1
	public function limit($limit)
183
	{
184 1
		$this->queryBuilder->limit($this->setBinding('limit', (int) $limit));
185
186
		return $this;
187
	}
188
189
	/**
190
	 * {@inheritdoc}
191
	 */
192 4
	public function offset($offset)
193
	{
194 4
		$this->queryBuilder->offset($this->setBinding('offset', (int) $offset));
195
196 4
		return $this;
197 1
	}
198 1
199
	/**
200
	 * Returns the result of the executed prepared query.
201
	 *
202 4
	 * @return QueryResult the result of the executed prepared query.
203
	 */
204 4
	public function execute()
205
	{
206
		$pdoStatement = $this->pdo->prepare((string) $this);
207
208
		foreach ($this->bindings as $clause => $predicate) {
209
			foreach ($predicate as $key => $value) {
210
				$pdoStatement->bindValue(sprintf(':%s%d', $clause, $key + 1), $value, $this->getPdoDataType($value));
211
			}
212
		}
213 1
214
		$pdoStatement->execute();
215 1
216
		return new QueryResult($pdoStatement);
0 ignored issues
show
Bug introduced by
It seems like $pdoStatement can also be of type boolean; however, parameter $pdoStatement of miBadger\Query\QueryResult::__construct() does only seem to accept PDOStatement, 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

216
		return new QueryResult(/** @scrutinizer ignore-type */ $pdoStatement);
Loading history...
217 1
	}
218 1
219 1
	/**
220 1
	 * Returns the data type of the given value.
221 1
	 *
222 1
	 * @param mixed $value
223
	 * @return int the data type of the given value.
224
	 */
225 1
	private function getPdoDataType($value)
226
	{
227
		$result = \PDO::PARAM_STR;
228
229
		if (is_bool($value)) {
230
			$result = \PDO::PARAM_BOOL;
231
		} elseif (is_null($value)) {
232
			$result = \PDO::PARAM_NULL;
233
		} elseif (is_int($value)) {
234
			$result = \PDO::PARAM_INT;
235 10
		}
236
237 10
		return $result;
238
	}
239 10
240
	/**
241
	 * Returns a binding for the given clause and value.
242
	 *
243
	 * @param string $clause
244
	 * @param string $value
245
	 * @return string a binding for the given clause and value.
246
	 */
247
	public function addBinding($clause, $value)
248
	{
249 3
		$this->bindings[$clause][] = $value;
250
251 3
		return sprintf(':%s%d', $clause, count($this->bindings[$clause]));
252
	}
253 3
254 3
	/**
255
	 * Returns bindings for the given clause and values.
256
	 *
257 3
	 * @param string $clause
258
	 * @param array $values
259
	 * @return array bindings for the given clause and values.
260
	 */
261
	public function addBindings($clause, array $values)
262
	{
263
		$result = [];
264
265
		foreach ($values as $key => $value) {
266
			$result[$key] = $this->addBinding($clause, $value);
267 2
		}
268
269 2
		return $result;
270
	}
271
272
	/**
273
	 * Returns a binding for the given clause and value.
274
	 *
275
	 * @param string $clause
276
	 * @param string $value
277
	 * @return string a binding for the given clause and value.
278
	 */
279 2
	private function setBinding($clause, $value)
280
	{
281 2
		return $this->removeBindings($clause)->addBinding($clause, $value);
282
	}
283
284
	/**
285
	 * Returns bindings for the given clause and values.
286
	 *
287
	 * @param string $clause
288
	 * @param array $values
289
	 * @return array bindings for the given clause and values.
290 4
	 */
291
	private function setBindings($clause, array $values)
292 4
	{
293
		return $this->removeBindings($clause)->addBindings($clause, $values);
294 4
	}
295
296
	/**
297
	 * Remove the bindings that are associated with the given clause.
298
	 *
299
	 * @param string $clause
300
	 * @return $this
301
	 */
302
	private function removeBindings($clause)
303
	{
304
		$this->bindings[$clause] = [];
305
306
		return $this;
307
	}
308
309
	/**
310
	 * Creates a Greater than Query condition, equivalent to mysql > operator
311
	 * @param string $left the lhs of the condition
312
	 * @param mixed $right the rhs of the condition
313
	 * @return QueryCondition the query condition
314
	 */
315
	public static function Greater($left, $right)
316
	{
317
		return new QueryCondition($left, '>', $right);
318
	}
319
320
	/**
321
	 * Creates a "Greater than or equal to" Query condition, equivalent to mysql >= operator
322
	 * @param string $left the lhs of the condition
323
	 * @param mixed $right the rhs of the condition
324
	 * @return QueryCondition the query condition
325
	 */
326
	public static function GreaterOrEqual($left, $right)
327
	{
328
		return new QueryCondition($left, '>=', $right);
329
	}
330
331
	/**
332
	 * Creates a "Less than" Query condition, equivalent to mysql < operator
333
	 * @param string $left the lhs of the condition
334
	 * @param mixed $right the rhs of the condition
335
	 * @return QueryCondition the query condition
336
	 */
337
	public static function Less($left, $right)
338
	{
339
		return new QueryCondition($left, '<', $right);
340
	}
341
342
	/**
343
	 * Creates a "Less than or equal to" Query condition, equivalent to mysql <= operator
344
	 * @param string $left the lhs of the condition
345
	 * @param mixed $right the rhs of the condition
346
	 * @return QueryCondition the query condition
347 7
	 */
348
	public static function LessOrEqual($left, $right)
349 7
	{
350
		return new QueryCondition($left, '<=', $right);
351
	}
352
353
	/**
354
	 * Creates an "equal to" Query condition, equivalent to mysql = operator
355
	 * @param string $left the lhs of the condition
356
	 * @param mixed $right the rhs of the condition
357
	 * @return QueryCondition the query condition
358
	 */
359
	public static function Equal($left, $right)
360
	{
361
		return new QueryCondition($left, '=', $right);
362
	}
363
364
	/**
365
	 * Creates a "Not equal to" Query condition, equivalent to mysql <> or != operators
366
	 * @param string $left the lhs of the condition
367
	 * @param mixed $right the rhs of the condition
368
	 * @return QueryCondition the query condition
369 1
	 */
370
	public static function NotEqual($left, $right)
371 1
	{
372
		return new QueryCondition($left, '<>', $right);
373
	}
374
375
	/**
376
	 * Creates a "Not Like" Query condition, equivalent to mysql NOT LIKE operator
377
	 * @param string $left the lhs of the condition
378
	 * @param mixed $right the rhs of the condition
379
	 * @return QueryCondition the query condition
380 8
	 */
381
	public static function NotLike($left, $right)
382 8
	{
383
		return new QueryCondition($left, 'NOT LIKE', $right);
384
	}
385
386
	/**
387
	 * Creates a "Like" Query condition, equivalent to mysql LIKE operator
388
	 * @param string $left the lhs of the condition
389
	 * @param mixed $right the rhs of the condition
390
	 * @return QueryCondition the query condition
391
	 */
392
	public static function Like($left, $right)
393
	{
394
		return new QueryCondition($left, 'LIKE', $right);
395
	}
396
397
	/**
398
	 * Creates an "Is" Query condition, equivalent to mysql IS operator
399
	 * @param string $left the lhs of the condition
400
	 * @param mixed $right the rhs of the condition
401
	 * @return QueryCondition the query condition
402 1
	 */
403
	public static function Is($left, $right)
404 1
	{
405
		return new QueryCondition($left, 'IS', $right);
406
	}
407
408
	/**
409
	 * Creates an "Is not" Query condition, equivalent to mysql IS NOT operator
410
	 * @param string $left the lhs of the condition
411
	 * @param mixed $right the rhs of the condition
412
	 * @return QueryCondition the query condition
413
	 */
414
	public static function IsNot($left, $right)
415
	{
416
		return new QueryCondition($left, 'IS NOT', $right);
417
	}
418
419
	/**
420
	 * Creates a "Not in" Query condition, equivalent to mysql NOT IN operator
421
	 * @param string $needle the parameter that cannot be present in the haystack
422
	 * @param string|Array $haystack the values that can be searched through
423
	 * @return QueryCondition the query condition
424 3
	 */
425
	public static function NotIn($needle, $haystack)
426 3
	{
427
		return new QueryCondition($needle, 'NOT IN', $haystack);
428
	}
429
430
	/**
431
	 * Creates a "In" Query condition, equivalent to mysql IN operator
432
	 * @param string $needle the parameter that has to be found
433 3
	 * @param string|Array $haystack the values that can be searched through
434
	 * @return QueryCondition the query condition
435 3
	 */
436
	public static function In($needle, $haystack)
437
	{
438
		return new QueryCondition($needle, 'IN', $haystack);
439
	}
440
441
	/**
442 1
	 * Creates an "AND" predicate from a variable number of expressions
443
	 * @return QueryPredicate the predicate expression
444 1
	 */
445
	public static function And(QueryExpression $left, QueryExpression ...$others)
446
	{
447
		return new QueryPredicate('AND', $left, ...$others);
448
	}
449
450
	/**
451
	 * Combines an array of QueryExpression clauses into an AND predicate
452 1
	 * @return miBadger\Query\QueryExpression|null Either null (if array contains no clauses), 
0 ignored issues
show
Bug introduced by
The type miBadger\Query\miBadger\Query\QueryExpression was not found. Did you mean miBadger\Query\QueryExpression? If so, make sure to prefix the type with \.
Loading history...
453
	 * 				the single clause in the input array, or a QueryPredicate combining the clauses
454 1
	 */
455
	public static function AndArray(Array $clauses)
456
	{
457
		if (count($clauses) == 0) {
458
			return null;
459
		} else if (count($clauses) == 1) 
460
		{
461
			return $clauses[0];
462
		} else {
463
			return new QueryPredicate('AND', $clauses[0], ...array_slice($clauses, 1));
464
		}
465
	}
466
467
	/**
468
	 * Creates an "OR" predicate from a variable number of expressions
469
	 * @return QueryPredicate the predicate expression
470
	 */
471
	public static function Or(QueryExpression $left, QueryExpression ...$others)
472
	{
473
		return new QueryPredicate('OR', $left, ...$others);
474
	}
475
476
	/**
477
	 * Combines an array of QueryExpression clauses into an OR predicate
478
	 * @return miBadger\Query\QueryExpression|null Either null (if array contains no clauses), 
479
	 * 				the single clause in the input array, or a QueryPredicate combining the clauses
480
	 */
481
	public static function OrArray(Array $clauses)
482
	{
483
		if (count($clauses) == 0) {
484
			return null;
485
		} else if (count($clauses) == 1)
486
		{
487
			return $clauses[0];
488
		} else {
489
			return new QueryPredicate('OR', $clauses[0], ...array_slice($clauses, 1));
490
		}
491
	}
492
493
	/**
494
	 * Creates a "NOT" predicate negating an expression
495
	 * @param QueryExpression The condition to be negated
0 ignored issues
show
Bug introduced by
The type miBadger\Query\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
496
	 * @return QueryPredicate the predicate expression
497
	 */
498
	public static function Not(QueryExpression $exp)
499
	{
500
		return new QueryPredicate('NOT', $exp);
501
	}
502
}
503