-
Notifications
You must be signed in to change notification settings - Fork 5
/
pudlHelpers.php
289 lines (210 loc) · 7.49 KB
/
pudlHelpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
////////////////////////////////////////////////////////////////////////////////
// DO NOTHING
// THIS IS USED TO CREATE FAKE VERSIONS OF SOME CLASSES THAT DO NOTHING
////////////////////////////////////////////////////////////////////////////////
class pudlVoid implements pudlHelper {
public function __call($name, $arguments) {
return false;
}
}
////////////////////////////////////////////////////////////////////////////////
// FLOATING POINT COMPARISON
// WE CANNOT COMPARE FLOATS DIRECTLY, SO INSTEAD WE COMPARE THE DIFFERENCE OF
// TWO FLOATS AND SEE IF THAT VALUE IS WITHIN A PARTICULAR MARGIN FOR ERROR
////////////////////////////////////////////////////////////////////////////////
class pudlFloat extends pudlEquals {
public function __construct($value, $precision=10) {
parent::__construct($value);
if ($precision < 1) {
$this->precision = '1';
} else {
$this->precision = '0.' . str_repeat('0', $precision-1) . '1';
}
}
public function pudlValue(pudl $pudl, $quote=true) {
return 'ABS(' . $pudl->identifier($this->column)
. '-' . $pudl->_value($this->value, $quote)
. ')<' . $this->precision;
}
public $precision;
public $column;
}
////////////////////////////////////////////////////////////////////////////////
// USED WHERE PUDL EXPECTS A STRING BUT WE NEED A SQL TABLE'S COLUMN INSTEAD
////////////////////////////////////////////////////////////////////////////////
class pudlColumn extends pudlEquals {
public function __construct($column, $value=false) {
parent::__construct($value);
$this->args = func_num_args() > 1;
if (!pudl_array($column)) {
$this->column = $column;
} else if (count($column) > 1 && is_int($column[0])) {
$this->column = $column[1];
} else {
$this->column = implode('.', $column);
}
}
public function pudlValue(pudl $pudl, $quote=true) {
return $pudl->identifiers($this->column);
}
public $column;
public $args;
}
////////////////////////////////////////////////////////////////////////////////
// SQL COUNT() FUNCTION
////////////////////////////////////////////////////////////////////////////////
class pudlCount extends pudlColumn {
public function __construct($column='*') {
parent::__construct($column);
}
public function pudlValue(pudl $pudl, $quote=true) {
return 'COUNT(' . $pudl->identifiers($this->column) . ')';
}
}
////////////////////////////////////////////////////////////////////////////////
// SQL ALIAS
////////////////////////////////////////////////////////////////////////////////
class pudlAs extends pudlColumn {
public function __construct($column, $alias, $length=false) {
parent::__construct($column);
$this->alias = $alias;
$this->length = $length;
}
public function pudlValue(pudl $pudl, $quote=true) {
return $pudl->_value($this->column) .
' AS ' . $pudl->identifier($this->alias) .
($this->length === false ? '' : ('('.$this->length.')'));
}
public $alias;
public $length;
}
////////////////////////////////////////////////////////////////////////////////
// SQL "BETWEEN" TWO DIFFERENT VALUES
////////////////////////////////////////////////////////////////////////////////
class pudlBetween extends pudlEquals {
public function __construct($v1, $v2, $v3=false) {
if ($v3 === false) {
parent::__construct([$v1,$v2], false, ' BETWEEN ');
} else {
parent::__construct($v1, [$v2,$v3], ' BETWEEN ');
}
}
public function __toString() {
return (string) $this->value[0] . ', ' . $this->value[1];
}
public function pudlValue(pudl $pudl, $quote=true) {
return $pudl->_value($this->value[0], $quote)
. ' AND '
. $pudl->_value($this->value[1], $quote);
}
}
////////////////////////////////////////////////////////////////////////////////
// USE SQL STRING "LIKE" COMPARISON
////////////////////////////////////////////////////////////////////////////////
class pudlLike extends pudlEquals {
public function __construct($value, $compare, $side) {
parent::__construct($value, $compare, ' LIKE ');
$this->left = ($side & PUDL_START) ? '%' : '';
$this->right = ($side & PUDL_END) ? '%' : '';
$this->raw = ($side === PUDL_NONE);
}
public function pudlValue(pudl $pudl, $quote=true) {
if (!is_object($this->value)) {
return "'" . $this->left
. $pudl->likeEscape($this->value, $this->raw)
. $this->right . "'";
}
return "CONCAT('" . $this->left . "',"
. $pudl->_value($this->value)
. ",'" . $this->right . "')";
}
public $left;
public $right;
public $raw;
}
////////////////////////////////////////////////////////////////////////////////
// USE SQL STRING "REGEXP" COMPARISON
////////////////////////////////////////////////////////////////////////////////
class pudlRegexp extends pudlEquals {
public function __construct($value) {
parent::__construct(
func_num_args() === 1 ? $value : func_get_args(),
false, ' REGEXP '
);
}
public function pudlValue(pudl $pudl, $quote=true) {
$query = '';
if (!pudl_array($this->value)) $this->value = [$this->value];
foreach ($this->value as $item) {
$query .= is_string($item) ?
$pudl->escape(preg_quote($item)) :
$pudl->_value($item, false);
}
return "'" . $query . "'";
}
}
////////////////////////////////////////////////////////////////////////////////
// ADD AN ITEM TO A SET VALUE
////////////////////////////////////////////////////////////////////////////////
class pudlAppendSet extends pudlEquals {}
////////////////////////////////////////////////////////////////////////////////
// REMOVE AN ITEM FROM A SET VALUE
////////////////////////////////////////////////////////////////////////////////
class pudlRemoveSet extends pudlEquals {}
////////////////////////////////////////////////////////////////////////////////
// ???
////////////////////////////////////////////////////////////////////////////////
class pudlSort extends pudlEquals {
public function __construct($sort, $column=false) {
parent::__construct($sort, false, ' ');
$this->column = $column;
}
public function pudlValue(pudl $pudl, $quote=true) {
return $this->value;
}
public $column;
}
////////////////////////////////////////////////////////////////////////////////
// FORCE "AND" JOINING OF COMPARISON CLAUSE GROUPS
////////////////////////////////////////////////////////////////////////////////
class pudlAnd implements pudlHelper {
public function __construct($clause) {
$this->clause = $clause;
$this->joiner = ' AND ';
}
public $clause;
public $joiner;
}
////////////////////////////////////////////////////////////////////////////////
// FORCE "OR" JOINING OF COMPARIOSON CLAUSE GROUPS
////////////////////////////////////////////////////////////////////////////////
class pudlOr extends pudlAnd {
public function __construct($clause) {
parent::__construct($clause);
$this->joiner = ' OR ';
}
}
////////////////////////////////////////////////////////////////////////////////
// FORCE A STRING COMPARISON WHERE PUDL EXPECTS A COLUMN OR OTHER VALUE
////////////////////////////////////////////////////////////////////////////////
class pudlString {
public function __construct($value='') {
$this->value = (string) $value;
}
public function __toString() {
return (string) $this->value;
}
public $value;
}
////////////////////////////////////////////////////////////////////////////////
// COMPLEX DATA TYPES FOR CREATE TABLE SYNTAX GENERATOR
////////////////////////////////////////////////////////////////////////////////
class pudlType {
public function __construct($type, $value) {
$this->type = $type;
$this->value = $value;
}
public $type;
public $value;
}