1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB; |
4
|
|
|
|
5
|
|
|
use Helix\DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Immutable column access. |
9
|
|
|
* |
10
|
|
|
* Unlike {@link SQL}, the SQL generation functions here will quote arguments. |
11
|
|
|
* |
12
|
|
|
* @immutable |
13
|
|
|
* |
14
|
|
|
* @method ExpressionInterface avg($aggregate = 'ALL') |
15
|
|
|
* @method ExpressionInterface count($aggregate = 'ALL') |
16
|
|
|
* @method ExpressionInterface max() |
17
|
|
|
* @method ExpressionInterface min() |
18
|
|
|
* @method ExpressionInterface sum($aggregate = 'ALL') |
19
|
|
|
*/ |
20
|
|
|
class Column extends AbstractAccess implements ExpressionInterface { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $name; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $qualifier; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param DB $db |
34
|
|
|
* @param string $name |
35
|
|
|
* @param string $qualifier |
36
|
|
|
*/ |
37
|
|
|
public function __construct (DB $db, string $name, string $qualifier = '') { |
38
|
|
|
parent::__construct($db); |
39
|
|
|
$this->name = $name; |
40
|
|
|
$this->qualifier = $qualifier; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Appends the instance to the arguments, |
45
|
|
|
* and forwards the call to {@link Expression::__callStatic()} |
46
|
|
|
* |
47
|
|
|
* @see Expression::__callStatic() |
48
|
|
|
* |
49
|
|
|
* @param string $name |
50
|
|
|
* @param array $arguments |
51
|
|
|
* @return ExpressionInterface |
52
|
|
|
*/ |
53
|
|
|
public function __call (string $name, array $arguments): ExpressionInterface { |
54
|
|
|
array_push($arguments, $this); |
55
|
|
|
return Expression::__callStatic($name, $arguments); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the qualified name. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function __toString (): string { |
64
|
|
|
if (strlen($this->qualifier)) { |
65
|
|
|
return "{$this->qualifier}.{$this->name}"; |
66
|
|
|
} |
67
|
|
|
return $this->name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
final public function getName (): string { |
74
|
|
|
return $this->name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
final public function getQualifier (): string { |
81
|
|
|
return $this->qualifier; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Null-safe equality. |
86
|
|
|
* |
87
|
|
|
* - Mysql: `$this <=> $arg`, or `$this <=> ANY ($arg)` |
88
|
|
|
* - Sqlite: `$this IS $arg`, or `$this IS ANY ($arg)` |
89
|
|
|
* |
90
|
|
|
* @param null|bool|string|Select $arg |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function is ($arg): string { |
94
|
|
|
if ($arg === null or is_bool($arg)) { |
95
|
|
|
$arg = ['' => 'NULL', 1 => 'TRUE', 0 => 'FALSE'][$arg]; |
96
|
|
|
} |
97
|
|
|
else { |
98
|
|
|
$arg = $this->db->quote($arg); |
99
|
|
|
} |
100
|
|
|
$operator = ['mysql' => '<=>', 'sqlite' => 'IS'][$this->db->getDriver()]; |
101
|
|
|
return SQL::compare($this, $operator, $arg, 'ANY'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* `$this = $arg`, or `$this IN (...$arg)`, or `$this = ANY ($arg)` |
106
|
|
|
* |
107
|
|
|
* @param string|array|Select $arg |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function isEqual ($arg): string { |
111
|
|
|
return SQL::isEqual($this, $this->db->quote($arg)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* `$this > $arg`, or `$this > ALL ($arg)` |
116
|
|
|
* |
117
|
|
|
* @param string|Select $arg |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function isGreater ($arg): string { |
121
|
|
|
return SQL::isGreater($this, $this->db->quote($arg)); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* `$this >= $arg`, or `$this >= ALL ($arg)` |
126
|
|
|
* |
127
|
|
|
* @param string|Select $arg |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function isGreaterOrEqual ($arg): string { |
131
|
|
|
return SQL::isGreaterOrEqual($this, $this->db->quote($arg)); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* `$this < $arg`, or `$this < ALL ($arg)` |
136
|
|
|
* |
137
|
|
|
* @param string|Select $arg |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function isLess ($arg): string { |
141
|
|
|
return SQL::isLess($this, $this->db->quote($arg)); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* `$this <= $arg`, or `$this <= ALL ($arg)` |
146
|
|
|
* |
147
|
|
|
* @param string|Select $arg |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function isLessOrEqual ($arg): string { |
151
|
|
|
return SQL::isLessOrEqual($this, $this->db->quote($arg)); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* `$this LIKE $pattern` |
156
|
|
|
* |
157
|
|
|
* @param string $pattern |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function isLike (string $pattern): string { |
161
|
|
|
return SQL::isLike($this, $this->db->quote($pattern)); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Null-safe inequality. |
166
|
|
|
* |
167
|
|
|
* @see is() |
168
|
|
|
* |
169
|
|
|
* @param null|bool|string|Select $arg |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function isNot ($arg): string { |
173
|
|
|
return SQL::not($this->is($arg)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* `$this <> $arg`, or `$this NOT IN (...$arg)`, or `$this <> ALL ($arg)` |
178
|
|
|
* |
179
|
|
|
* @param string|array|Select $arg |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function isNotEqual ($arg): string { |
183
|
|
|
return SQL::isNotEqual($this, $this->db->quote($arg)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* `$this NOT LIKE $pattern` |
188
|
|
|
* |
189
|
|
|
* @param string $pattern |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function isNotLike (string $pattern): string { |
193
|
|
|
return SQL::isNotLike($this, $this->db->quote($pattern)); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* `$this IS NOT NULL` |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function isNotNull (): string { |
202
|
|
|
return SQL::isNotNull($this); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* `$this NOT REGEXP $pattern` |
207
|
|
|
* |
208
|
|
|
* @param string $pattern |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function isNotRegExp (string $pattern): string { |
212
|
|
|
return SQL::isNotRegExp($this, $this->db->quote($pattern)); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* `$this REGEXP $pattern` |
217
|
|
|
* |
218
|
|
|
* @param string $pattern |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function isRegExp (string $pattern): string { |
222
|
|
|
return SQL::isRegExp($this, $this->db->quote($pattern)); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $name |
227
|
|
|
* @return $this |
228
|
|
|
*/ |
229
|
|
|
public function setName (string $name) { |
230
|
|
|
$clone = clone $this; |
231
|
|
|
$clone->name = $name; |
232
|
|
|
return $clone; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $qualifier |
237
|
|
|
* @return $this |
238
|
|
|
*/ |
239
|
|
|
public function setQualifier (string $qualifier) { |
240
|
|
|
$clone = clone $this; |
241
|
|
|
$clone->qualifier = $qualifier; |
242
|
|
|
return $clone; |
243
|
|
|
} |
244
|
|
|
} |