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