-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.js
199 lines (170 loc) · 6.34 KB
/
ast.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
'use strict';
var _ = require('lodash'),
Immutable = require('immutable'),
SqlParser = require('sql-parser');
function prototypeReplace(obj, f) {
var new_obj = Object.create(obj),
same = true;
_.each(obj, (value, key) => {
new_obj[key] = f(value, key);
same = (new_obj[key] === value) && same;
});
return same ? obj : new_obj;
}
/**
* Interpolate parameters without any wrappers
*/
function rawSQL() {
var parameters = _.toArray(arguments).slice(1);
/* parse AST with interpolation place holders */
var ast = parse.apply(this, arguments);
/* setup maps for efficient lookup */
var interpolate_map = new Map(),
parameters_used = [],
parameters_used_map = new Map();
function handleInterpolate(handler) {
return (s) => interpolate_map.has(s) ? handler(s, interpolate_map.get(s)) : s;
}
_.each(parameters, (value, index) => {
interpolate_map.set(`__interpolation_${index}`, value);
});
/* interpolate escaped values into the AST */
ast = interpolate(ast, {
interpolateInline: handleInterpolate((s, value) => {
if (!/^[A-Za-z0-9_ ]+$/.test(value)) {
throw Error(`Invalid table name ${value}`);
}
return value;
}),
interpolateParameter: handleInterpolate((s, value) => {
if (parameters_used_map.has(value)) {
return parameters_used_map.get(value);
}
if (value && value.ast && value.parameters) {
let ret = mergeVariables(new Immutable.List(parameters_used), {ast: value.ast, parameters: value.parameters});
parameters_used = ret.parameters.toArray();
parameters_used_map.set(value, ret.ast);
} else {
parameters_used.push(value);
parameters_used_map.set(value, new SqlParser.nodes.ParameterValue('$' + (parameters_used.length)));
}
return parameters_used_map.get(value);
})
});
return {ast: ast, parameters: new Immutable.List(parameters_used)};
}
exports.rawSQL = rawSQL;
function rawExpression(code) {
code = ['SELECT noop FROM noop WHERE ' + code[0]].concat(_.toArray(code).slice(1));
var ret = rawSQL.apply(null, [code].concat(_.toArray(arguments).slice(1)));
return {ast: ret.ast.where.conditions, parameters: ret.parameters};
}
exports.rawExpression = rawExpression;
/**
* Hack in interpolation handlers to `sql-parser`'s parser. Variables aren't
* generic enough because they don't work for table names.
*/
function parse(parts) {
var parameters = _.toArray(arguments).slice(1);
var code = _.reduce(
parts, (acc, x, index) => acc.concat([x, `__interpolation_${index}`]), []
).slice(0, -1).join('');
return SqlParser.parse(code);
}
exports.parse = parse;
/**
* Handle our hacked in interpolation types into `sql-parser`'s parser:
* "(SELECT * FROM __interpolation_${index})"
*
* @param {SqlParser.nodes.Node} ast
* @param {Object} options
* @param {Function} options.interpolateParameter
* @param {Function} options.interpolateInline
*/
function interpolate(ast, options) {
if (!_.isObject(ast)) {
return ast;
}
if (ast instanceof SqlParser.nodes.LiteralValue) {
if (ast.value instanceof SqlParser.nodes.LiteralValue) {
return new SqlParser.nodes.Table(
interpolateInline(ast, options),
interpolate(ast.value2, options)
);
return interpolateInline(ast, options);
}
return new SqlParser.nodes.LiteralValue(options.interpolateParameter(ast.value));
}
if (ast instanceof SqlParser.nodes.Field) {
return new SqlParser.nodes.Field(
interpolateInline(ast.field, options),
interpolateInline(ast.name, options)
);
}
if (ast instanceof SqlParser.nodes.Table) {
return interpolateInline(ast, options);
}
return prototypeReplace(ast, (value, key) => interpolate(value, options));
}
exports.interpolate = interpolate;
function interpolateInline(ast, options) {
if (ast instanceof SqlParser.nodes.Table) {
return new SqlParser.nodes.Table(
interpolateInline(ast.name, options),
interpolateInline(ast.alias, options),
interpolate(ast.win, options),
interpolate(ast.winFn, options),
interpolate(ast.winArg, options)
);
}
if (ast instanceof SqlParser.nodes.LiteralValue) {
if (ast.value instanceof SqlParser.nodes.LiteralValue) {
return new SqlParser.nodes.Table(
interpolateInline(ast, options),
interpolate(ast.value2, options)
);
return interpolateInline(ast, options);
}
return new SqlParser.nodes.LiteralValue(options.interpolateInline(ast.value));
}
return ast;
}
exports.interpolateInline = interpolateInline;
/**
* Since SQL parameters are ordered, when embedding on SQL statement into
* another, we need to merge their parameters.
*/
function mergeVariables(parameters, opts) {
var parameters = new Immutable.List(parameters),
replacements = new Immutable.Map();
if (opts.parameters.size === 0) {
return {ast: opts.ast, parameters: parameters};
}
function recurse(nested_ast) {
if (!_.isObject(nested_ast)) {
return nested_ast;
}
if (nested_ast instanceof SqlParser.nodes.ParameterValue) {
if (replacements.has(nested_ast.index)) {
return replacements.get(nested_ast.index);
}
var value = opts.parameters.get(nested_ast.index),
new_ast;
if (parameters.contains(value)) {
new_ast = new SqlParser.nodes.ParameterValue('$' + (
parameters.indexOf(value) + 1
));
} else {
parameters = parameters.push(value);
new_ast = new SqlParser.nodes.ParameterValue('$' + (
parameters.size
));
}
replacements.set(nested_ast.index, new_ast);
return new_ast;
}
return prototypeReplace(nested_ast, (value, key) => recurse(value));
}
return {ast: recurse(opts.ast), parameters: parameters};
}
exports.mergeVariables = mergeVariables;