forked from brianc/node-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.js
More file actions
344 lines (305 loc) · 8.35 KB
/
query.js
File metadata and controls
344 lines (305 loc) · 8.35 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'use strict';
var _ = require('lodash');
var assert = require('assert');
var sliced = require('sliced');
var util = require('util');
var Node = require('./');
var Select = require('./select');
var From = require('./from');
var Where = require('./where');
var OrderBy = require('./orderBy');
var GroupBy = require('./groupBy');
var Having = require('./having');
var Insert = require('./insert');
var Update = require('./update');
var Delete = require('./delete');
var Returning = require('./returning');
var Create = require('./create');
var Drop = require('./drop');
var Alter = require('./alter');
var AddColumn = require('./addColumn');
var DropColumn = require('./dropColumn');
var RenameColumn = require('./renameColumn');
var Rename = require('./rename');
var Column = require('../column');
var ParameterNode = require('./parameter');
var PrefixUnaryNode = require('./prefixUnary');
var IfExists = require('./ifExists');
var IfNotExists = require('./ifNotExists');
var Indexes = require('./indexes');
var CreateIndex = require('./createIndex');
var DropIndex = require('./dropIndex');
var Modifier = Node.define({
constructor: function(table, type, count) {
this.table = table;
this.type = type;
this.count = count;
}
});
var ensureSubquery = function(node, methodName) {
assert(node.type === 'SUBQUERY', methodName + ' can only be used on a subQuery');
};
// get the first element of an arguments if it is an array, else return arguments as an array
var getArrayOrArgsAsArray = function(args) {
if (util.isArray(args[0])) {
return args[0];
}
return sliced(args);
};
var Query = Node.define({
type: 'QUERY',
constructor: function(table) {
Node.call(this);
this.table = table;
if (table) {
this.sql = table.sql;
}
},
select: function() {
var select;
if (this._select) {
select = this._select;
} else {
select = this._select = new Select();
this.add(select);
}
var args = getArrayOrArgsAsArray(arguments);
select.addAll(args);
// if this is a subquery then add reference to this column
if (this.type === 'SUBQUERY') {
for (var j = 0; j < select.nodes.length; j++) {
var name = select.nodes[j].alias || select.nodes[j].name;
var col = new Column(select.nodes[j]);
col.name = name;
col.table = this;
if (this[name] === undefined) {
this[name] = col;
}
}
}
return this;
},
star: function() {
ensureSubquery(this, 'star()');
return new Column({
table: this,
star: true
});
},
from: function(tableNode) {
var from = new From().add(tableNode);
return this.add(from);
},
where: function(node) {
if (arguments.length > 1) {
// allow multiple where clause arguments
var args = sliced(arguments);
for (var i = 0; i < args.length; i++) {
this.where(args[i]);
}
return this;
}
// calling #where twice functions like calling #where & then #and
if (this.whereClause) {
return this.and(node);
}
this.whereClause = new Where(this.table);
this.whereClause = this.whereClause.add(node);
return this.add(this.whereClause);
},
or: function(node) {
var index = _.indexOf(this.nodes, this.whereClause);
this.whereClause = this.whereClause.or(node);
this.nodes[index] = this.whereClause;
return this;
},
and: function(node) {
var index = _.indexOf(this.nodes, this.whereClause);
this.whereClause = this.whereClause.and(node);
this.nodes[index] = this.whereClause;
return this;
},
order: function() {
var args = getArrayOrArgsAsArray(arguments);
var orderBy;
if (this._orderBy) {
orderBy = this._orderBy;
} else {
orderBy = this._orderBy = new OrderBy();
this.add(orderBy);
}
orderBy.addAll(args);
return this;
},
group: function() {
var args = getArrayOrArgsAsArray(arguments);
var groupBy = new GroupBy().addAll(args);
return this.add(groupBy);
},
having: function() {
var args = getArrayOrArgsAsArray(arguments);
var having = new Having().addAll(args);
return this.add(having);
},
insert: function(o) {
var self = this;
var args = sliced(arguments);
// object literal
if (arguments.length === 1 && !o.toNode && !o.forEach) {
args = Object.keys(o).map(function(key) {
return self.table.get(key).value(o[key]);
});
} else if (o.forEach) {
o.forEach(function(arg) {
return self.insert.call(self, arg);
});
return self;
}
if (self.insertClause) {
self.insertClause = self.insertClause.add(args);
self.nodes[0] = self.insertClause;
return self;
} else {
self.insertClause = new Insert();
self.insertClause = self.insertClause.add(args);
return self.add(self.insertClause);
}
},
update: function(o) {
var self = this;
var update = new Update();
Object.keys(o).forEach(function(key) {
var val = o[key];
update.add(self.table.get(key).value(ParameterNode.getNodeOrParameterNode(val)));
});
return this.add(update);
},
delete: function(params) {
var result = this.add(new Delete());
if (params) {
result = this.where(params);
}
return result;
},
returning: function() {
var args = getArrayOrArgsAsArray(arguments);
var returning = new Returning();
returning.addAll(args);
return this.add(returning);
},
create: function(indexName) {
if (this.indexesClause) {
var createIndex = new CreateIndex(this.table, indexName);
this.add(createIndex);
return createIndex;
} else {
return this.add(new Create());
}
},
drop: function() {
if (this.indexesClause) {
var args = sliced(arguments);
var dropIndex = new DropIndex(this.table, args);
this.add(dropIndex);
return dropIndex;
} else {
return this.add(new Drop());
}
},
alter: function() {
return this.add(new Alter());
},
rename: function(newName) {
var renameClause = new Rename();
if (!newName.toNode) {
newName = new Column({
name: newName,
table: this.table
});
}
renameClause.add(newName.toNode());
this.nodes[0].add(renameClause);
return this;
},
addColumn: function(column, dataType) {
var addClause = new AddColumn();
if (!column.toNode) {
column = new Column({
name: column,
table: this.table
});
}
if (dataType) {
column.dataType = dataType;
}
addClause.add(column.toNode());
this.nodes[0].add(addClause);
return this;
},
dropColumn: function(column) {
var dropClause = new DropColumn();
if (!column.toNode) {
column = new Column({
name: column,
table: this.table
});
}
dropClause.add(column.toNode());
this.nodes[0].add(dropClause);
return this;
},
renameColumn: function(oldColumn, newColumn) {
var renameClause = new RenameColumn();
if (!oldColumn.toNode) {
oldColumn = new Column({
name: oldColumn,
table: this.table
});
}
if (!newColumn.toNode) {
newColumn = new Column({
name: newColumn,
table: this.table
});
}
renameClause.add(oldColumn.toNode());
renameClause.add(newColumn.toNode());
this.nodes[0].add(renameClause);
return this;
},
limit: function(count) {
return this.add(new Modifier(this, 'LIMIT', count));
},
offset: function(count) {
return this.add(new Modifier(this, 'OFFSET', count));
},
exists: function() {
ensureSubquery(this, 'exists()');
return new PrefixUnaryNode({
left: this,
operator: "EXISTS"
});
},
notExists: function() {
ensureSubquery(this, 'notExists()');
return new PrefixUnaryNode({
left: this,
operator: "NOT EXISTS"
});
},
ifExists: function() {
this.nodes[0].add(new IfExists());
return this;
},
ifNotExists: function() {
this.nodes[0].add(new IfNotExists());
return this;
},
indexes: function() {
this.indexesClause = new Indexes({
table: this.table
});
return this.add(this.indexesClause);
}
});
module.exports = Query;