/
Builder.cfc
281 lines (224 loc) · 7.23 KB
/
Builder.cfc
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
component displayname='Builder' {
property name='grammar' inject='Grammar@Quick';
property name='utils' inject='QueryUtils@Quick';
property name='wirebox' inject='wirebox';
property name='distinct' type='boolean' default='false';
property name='columns' type='array';
property name='from' type='string';
property name='joins' type='array';
property name='wheres' type='array';
variables.operators = [
'=', '<', '>', '<=', '>=', '<>', '!=',
'like', 'like binary', 'not like', 'between', 'ilike',
'&', '|', '^', '<<', '>>',
'rlike', 'regexp', 'not regexp',
'~', '~*', '!~', '!~*', 'similar to',
'not similar to',
];
variables.combinators = [
'AND', 'OR'
];
variables.bindings = {
'join' = [],
'where' = []
};
public Builder function init() {
setDefaultValues();
return this;
}
private void function setDefaultValues() {
variables.distinct = false;
variables.columns = ['*'];
variables.joins = [];
variables.from = '';
variables.wheres = [];
}
// API
// select methods
public Builder function distinct() {
variables.distinct = true;
return this;
}
public Builder function select(required any columns) {
variables.columns = normalizeToArray(argumentCollection = arguments);
return this;
}
public Builder function addSelect(required any columns) {
arrayAppend(variables.columns, normalizeToArray(argumentCollection = arguments), true);
return this;
}
// from methods
public Builder function from(required string from) {
variables.from = arguments.from;
return this;
}
// join methods
public Builder function join(
required string table,
any first,
string operator,
string second,
string type = 'inner',
any conditions
) {
var joinClause = wirebox.getInstance(name = 'JoinClause@Quick', initArguments = {
type = arguments.type,
table = arguments.table
});
if (structKeyExists(arguments, 'first') && isClosure(arguments.first)) {
arguments.conditions = arguments.first;
}
if (structKeyExists(arguments, 'conditions') && isClosure(arguments.conditions)) {
conditions(joinClause);
}
else {
joinClause.on(
first = arguments.first,
operator = arguments.operator,
second = arguments.second,
combinator = 'and'
);
}
arrayAppend(variables.joins, joinClause);
arrayAppend(bindings.join, joinClause.getBindings(), true);
return this;
}
public Builder function leftJoin(
required string table,
string first,
string operator,
string second,
any conditions
) {
arguments.type = 'left';
return join(argumentCollection = arguments);
}
public Builder function rightJoin(
required string table,
string first,
string operator,
string second,
any conditions
) {
arguments.type = 'right';
return join(argumentCollection = arguments);
}
// where methods
public Builder function where(column, operator, value, combinator) {
var argCount = argumentCount(arguments);
if (isNull(arguments.combinator)) {
arguments.combinator = 'AND';
}
else if (isInvalidCombinator(arguments.combinator)) {
throw(
type = 'InvalidSQLType',
message = 'Illegal combinator'
);
}
if (argCount == 2) {
arguments.value = arguments.operator;
arguments.operator = '=';
}
else if (isInvalidOperator(arguments.operator)) {
throw(
type = 'InvalidSQLType',
message = 'Illegal operator'
);
}
var binding = utils.extractBinding(arguments.value);
arrayAppend(variables.wheres, {
column = arguments.column,
operator = arguments.operator,
value = arguments.value,
combinator = arguments.combinator
});
arrayAppend(bindings.where, binding);
return this;
}
// Accessors
public boolean function getDistinct() {
return distinct;
}
public array function getColumns() {
return columns;
}
public string function getFrom() {
return from;
}
public array function getJoins() {
return joins;
}
public array function getWheres() {
return wheres;
}
public array function getBindings() {
var bindingOrder = ['join', 'where'];
var flatBindings = [];
for (var key in bindingOrder) {
if (structKeyExists(bindings, key)) {
arrayAppend(flatBindings, bindings[key], true);
}
}
return flatBindings;
}
public struct function getRawBindings() {
return bindings;
}
// Collaborators
public string function toSQL() {
return grammar.compileSelect(this);
}
public query function get(struct options = {}) {
return queryExecute(this.toSQL(), this.getBindings(), options);
}
// Unused(?)
private array function normalizeToArray() {
if (isVariadicFunction(args = arguments)) {
return normalizeVariadicArgumentsToArray(args = arguments);
}
var arg = arguments[1];
if (! isArray(arg)) {
return normalizeListArgumentsToArray(arg);
}
return arg;
}
private boolean function isVariadicFunction(required struct args) {
return structCount(args) > 1;
}
private array function normalizeVariadicArgumentsToArray(required struct args) {
return arrayMap(structKeyArray(args), function(arg) {
return args[arg];
});
}
private array function normalizeListArgumentsToArray(required string list) {
return arrayMap(listToArray(list, ','), function(column) {
return trim(column);
});
}
private boolean function isInvalidOperator(required string operator) {
return ! arrayContains(operators, operator);
}
private boolean function isInvalidCombinator(required string combinator) {
return ! arrayContainsNoCase(combinators, combinator);
}
private function argumentCount(args) {
var count = 0;
for (var key in args) {
if (! isNull(args[key]) && ! isEmpty(args[key])) {
count++;
}
}
return count;
}
public any function onMissingMethod(string missingMethodName, struct missingMethodArguments) {
if (! arrayIsEmpty(REMatchNoCase('^where(.+)', missingMethodName))) {
var args = { '1' = mid(missingMethodName, 6) };
for (var key in missingMethodArguments) {
args[key + 1] = missingMethodArguments[key];
}
where(argumentCollection = args);
return;
}
throw("Method does not exist [#missingMethodName#]");
}
}