forked from AlaSQL/alasql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40select.js
275 lines (227 loc) · 7.36 KB
/
40select.js
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
/*
//
// Select run-time part for Alasql.js
// Date: 03.11.2014
// (c) 2014, Andrey Gershun
//
*/
//
// Main part of SELECT procedure
//
yy.Select = function (params) { return yy.extend(this, params); }
yy.Select.prototype.toString = function() {
var s = '';
if(this.explain) s+= K('EXPLAIN')+' ';
s += K('SELECT')+' ';
if(this.modifier) s += K(this.modifier)+' ';
if(this.top) s += K('TOP')+' '+N(this.top.value)+' ';
s += this.columns.map(function(col){
var s = col.toString();
// console.log(col);
if(typeof col.as != "undefined") s += ' '+K('AS')+' '+L(col.as);
return s;
}).join(', ');
if(this.from) {
s += NL()+ID()+K('FROM')+' '+this.from.map(function(f){
// console.log(f);
var ss = f.toString();
if(f.as) ss += ' '+K('AS')+' '+f.as;
return ss;
}).join(',');
};
if(this.joins) {
s += this.joins.map(function(jn){
var ss = NL()+ID();
if(jn.joinmode) ss += K(jn.joinmode)+' ';
ss += K('JOIN')+' ';
ss += jn.table.toString();
if(jn.using) ss += ' '+K('USING')+' '+jn.using.toString();
if(jn.on) ss += ' '+K('ON')+' '+jn.on.toString();
return ss;
});
}
if(this.where) s += NL()+ID()+K('WHERE')+' '+this.where.toString();
if(this.group) s += NL()+ID()+K('GROUP BY')+' '+this.group.toString();
if(this.having) s += NL()+ID()+K('HAVING')+' '+this.having.toString();
if(this.order) s += NL()+ID()+K('ORDER BY')+' '+this.order.toString();
if(this.limit) s += NL()+ID()+K('LIMIT')+' '+this.limit.value;
if(this.offset) s += NL()+ID()+K('OFFSET')+' '+this.offset.value;
if(this.union) s += NL()+K('UNION')+NL()+this.union.toString();
if(this.unionall) s += NL()+K('UNION ALL')+NL()+this.unionall.toString();
if(this.except) s += NL()+K('EXCEPT')+NL()+this.except.toString();
if(this.intersect) s += NL()+K('INTERSECT')+NL()+this.intersect.toString();
return s;
};
// Compile SELECT statement
yy.Select.prototype.compile = function(databaseid) {
var db = alasql.databases[databaseid];
// Create variable for query
var query = new Query();
query.removeKeys = [];
query.explain = this.explain; // Explain
query.explaination = [];
query.explid = 1;
query.modifier = this.modifier;
query.database = db;
// 0. Precompile whereexists
this.compileWhereExists(query);
// 0. Precompile queries for IN, NOT IN, ANY and ALL operators
this.compileQueries(query);
query.defcols = this.compileDefCols(query, databaseid);
// 1. Compile FROM clause
query.fromfn = this.compileFrom(query);
// 2. Compile JOIN clauses
if(this.joins) this.compileJoins(query);
// 3. Compile SELECT clause
query.selectfns = this.compileSelect1(query);
// 5. Optimize WHERE and JOINS
if(this.where) this.compileWhereJoins(query);
// 4. Compile WHERE clause
query.wherefn = this.compileWhere(query);
// 6. Compile GROUP BY
if(this.group) query.groupfn = this.compileGroup(query);
// 6. Compile HAVING
if(this.having) query.havingfn = this.compileHaving(query);
query.selectfn = this.compileSelect2(query);
// 7. Compile DISTINCT, LIMIT and OFFSET
query.distinct = this.distinct;
// 8. Compile ORDER BY clause
if(this.order) query.orderfn = this.compileOrder(query);
// TOP
if(this.top) {
query.limit = this.top.value;
} else if(this.limit) {
query.limit = this.limit.value;
if(this.offset) {
query.offset = this.offset.value;
}
}
// 9. Compile ordering function for UNION and UNIONALL
if(this.union) {
query.unionfn = this.union.compile(databaseid);
if(this.union.order) {
query.orderfn = this.union.compileOrder(query);
} else {
query.orderfn = null;
}
} else if(this.unionall) {
query.unionallfn = this.unionall.compile(databaseid);
if(this.unionall.order) {
query.orderfn = this.unionall.compileOrder(query);
} else {
query.orderfn = null;
}
} else if(this.except) {
query.exceptfn = this.except.compile(databaseid);
if(this.except.order) {
query.orderfn = this.except.compileOrder(query);
} else {
query.orderfn = null;
}
} else if(this.intersect) {
query.intersectfn = this.intersect.compile(databaseid);
if(this.intersect.order) {
query.intersectfn = this.intersect.compileOrder(query);
} else {
query.orderfn = null;
}
};
// SELECT INTO
// console.log(this.into);
if(this.into) {
if(this.into instanceof yy.Table) {
if(alasql.autocommit && alasql.databases[this.into.databaseid||databaseid].engineid) {
query.intoallfns = 'return alasql.engines["'+alasql.databases[this.into.databaseid||databaseid].engineid+'"]'+
'.intoTable("'+(this.into.databaseid||databaseid)+'","'+this.into.tableid+'",this.data, columns, cb);';
} else {
query.intofns =
'alasql.databases[\''+(this.into.databaseid||databaseid)+'\'].tables'+
'[\''+this.into.tableid+'\'].data.push(r);';
}
} else if (this.into instanceof yy.FuncValue) {
/*
query.intofns = 'alasql.into[\''+this.into.funcid+'\'](';
var ss = ['r','i'];
if(this.into.args && this.into.args.length>0 )
this.into.args.forEach(function(arg){
ss.push(arg.toJavaScript());
});
query.intofns += ss.join(',')+')';
*/
var qs = 'alasql.into[\''+this.into.funcid.toUpperCase()+'\'](';
if(this.into.args && this.into.args.length>0 ) {
qs += this.into.args[0].toJavaScript()+',';
if(this.into.args.length > 1) {
qs += this.into.args[1].toJavaScript()+',';
} else {
qs += 'null,';
}
} else {
qs += 'null, null,'
}
query.intoallfns = qs+'this.data,columns,cb)';
//console.log('999');
} else if (this.into instanceof yy.ParamValue) {
// console.log(184);
// query.intofns = 'params[\''+this.into.param+"\'](r)";
query.intofns = "params['"+this.into.param+"'].push(r)";
};
// console.log(query.intofns);
if(query.intofns) {
query.intofn = new Function("r,i,params",query.intofns);
};
if(query.intoallfns) {
// console.log(query.intoallfns);
query.intoallfn = new Function("columns,cb",query.intoallfns);
}
}
//console.log(query);
// Now, compile all togeather into one function with query object in scope
var statement = function(params, cb, oldscope) {
query.params = params;
var res1 = queryfn(query,oldscope,function(res){
//console.log(res[0].schoolid);
//console.log(184,res);
var res2 = modify(query, res);
if(cb) cb(res2);
//console.log(8888,res2);
return res2;
});
//console.log(9999,res1);
// if(typeof res1 != 'undefined') res1 = modify(query,res1);
return res1;
};
// statement.dbversion = ;
// console.log(statement.query);
//console.log(202,statement);
statement.query = query;
return statement;
};
function modify(query, res) {
if(query.modifier == 'VALUE') {
// console.log(222,res);
var key = Object.keys(res[0])[0];
res = res[0][key];
} if(query.modifier == 'ROW') {
var a = [];
for(var key in res[0]) {
a.push(res[0][key]);
};
res = a;
} if(query.modifier == 'COLUMN') {
var ar = [];
if(res.length > 0) {
var key = Object.keys(res[0])[0];
for(var i=0, ilen=res.length; i<ilen; i++){
ar.push(res[i][key]);
}
};
res = ar;
} if(query.modifier == 'MATRIX') {
res = arrayOfArrays(res);
}
return res;
};
yy.Select.prototype.exec = function(databaseid) {
throw new Error('Select statement should be precompiled');
};