1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\SQL; |
4
|
|
|
|
5
|
|
|
use Helix\DB\Select; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Represents a logical expression. Produces more predicates. |
9
|
|
|
*/ |
10
|
|
|
class Predicate extends Expression { |
11
|
|
|
|
12
|
|
|
use PredicateTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* `(... AND ...)` |
16
|
|
|
* |
17
|
|
|
* @param string[] $conditions |
18
|
|
|
* @return Predicate |
19
|
|
|
*/ |
20
|
|
|
public static function all (array $conditions) { |
21
|
|
|
if (count($conditions) === 1) { |
22
|
|
|
return reset($conditions); |
23
|
|
|
} |
24
|
|
|
return new static('(' . implode(' AND ', $conditions) . ')'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* `(... OR ...)` |
29
|
|
|
* |
30
|
|
|
* @param string[] $conditions |
31
|
|
|
* @return Predicate |
32
|
|
|
*/ |
33
|
|
|
public static function any (array $conditions) { |
34
|
|
|
if (count($conditions) === 1) { |
35
|
|
|
return reset($conditions); |
36
|
|
|
} |
37
|
|
|
return new static('(' . implode(' OR ', $conditions) . ')'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Used to generate a comparison. Arguments are not quoted. |
42
|
|
|
* |
43
|
|
|
* `"$a $oper $b"` |
44
|
|
|
* |
45
|
|
|
* If `$b` is an array, all items are listed within parenthesis. |
46
|
|
|
* |
47
|
|
|
* `"$a $listOper (...$b)"` |
48
|
|
|
* |
49
|
|
|
* If `$b` is a `{@link Select}, it's used as a subquery. |
50
|
|
|
* |
51
|
|
|
* `"$a $operator $subOper $b->toSql()"` |
52
|
|
|
* |
53
|
|
|
* @param string $a |
54
|
|
|
* @param mixed $b |
55
|
|
|
* @param string $oper |
56
|
|
|
* @param string $subOper |
57
|
|
|
* @param string $listOper |
58
|
|
|
* @return Predicate |
59
|
|
|
*/ |
60
|
|
|
public static function compare ($a, $b = null, $oper = '=', $subOper = 'ANY', $listOper = 'IN') { |
61
|
|
|
if (is_array($b)) { |
62
|
|
|
return new static("{$a} {$listOper} (" . implode(',', $b) . ")"); |
63
|
|
|
} |
64
|
|
|
elseif ($b instanceof Select) { |
65
|
|
|
return new static("{$a} {$oper} {$subOper} ({$b->toSql()})"); |
66
|
|
|
} |
67
|
|
|
return new static("{$a} {$oper} {$b}"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns an array of predicates, using keys and values for `$a` and `$b`. |
72
|
|
|
* Keys are preserved. |
73
|
|
|
* |
74
|
|
|
* @see Predicate::compare() |
75
|
|
|
* |
76
|
|
|
* @param array $values |
77
|
|
|
* @param string $oper |
78
|
|
|
* @param string $subOper |
79
|
|
|
* @param string $listOper |
80
|
|
|
* @return Predicate[] |
81
|
|
|
*/ |
82
|
|
|
public static function compareArray (array $values, $oper = '=', $subOper = 'ANY', $listOper = 'IN') { |
83
|
|
|
foreach ($values as $a => $b) { |
84
|
|
|
$values[$a] = static::compare($a, $b, $oper, $subOper, $listOper); |
85
|
|
|
} |
86
|
|
|
return $values; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* `NOT($this)` |
91
|
|
|
* |
92
|
|
|
* @return Predicate |
93
|
|
|
*/ |
94
|
|
|
public function invert () { |
95
|
|
|
return new static("NOT({$this})"); |
96
|
|
|
} |
97
|
|
|
} |