-
Notifications
You must be signed in to change notification settings - Fork 0
/
oql_compiler.js
225 lines (188 loc) · 4.98 KB
/
oql_compiler.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
var osdf_utils = require('osdf_utils');
var logger = osdf_utils.get_logger();
var parser = require('oql_jison_parser');
// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;
exports.compile = function(tree) {
logger.debug('In compile.');
var query = {
query: {
filtered: {}
}
};
query.query.filtered.filter = [];
var processed = _processNode(tree);
if (! isEmpty(processed)) {
query.query.filtered.filter = [ processed ];
} else {
delete query.query.filtered.filter;
}
return query;
};
exports.parse = function(query) {
logger.debug('In parse.');
var tree = parser.parse(query);
return tree;
};
var generators = {
'COMPARISON': function(node) {
var left = node[0];
var comparator = node[1];
var right = node[2];
if (typeof left === 'object') {
left = _processNode(left);
}
if (typeof right === 'object') {
right = _processNode(right);
}
var generated = generators[comparator](left, right);
return generated;
},
'ALL': function(node) {
var text = node[0];
var generated = {
'query': {
'query_string': {
'query': null
}
}
};
generated.query.query_string.query = text;
return generated;
},
'SEARCH': function(node) {
var text;
var field;
var generated;
if (node.length === 2) {
text = node[0];
field = node[1];
generated = {'term': {}};
generated.term[field] = text;
} else {
text = node[1];
field = node[2];
generated = {'not': {'term': {}}};
generated.not.term[field] = text;
}
return generated;
},
'&&': function(left, right) {
if (isEmpty(left) && isEmpty(right)) {
return null;
} else {
var _and = { bool: { must: [] } };
_and.bool.must.push(left);
_and.bool.must.push(right);
return _and;
}
},
'||': function(left, right) {
if (isEmpty(left) && isEmpty(right)) {
return null;
} else {
var _or = { bool: { should: [] } };
_or.bool.should.push(left);
_or.bool.should.push(right);
return _or;
}
},
'<': function(left, right) {
var _lt = {
range: {}
};
_lt.range[ left] = {
lt: right
};
return _lt;
},
'<=': function(left, right) {
var _lte = {
range: {}
};
_lte.range[ left ] = {
lte: right
};
return _lte;
},
'>': function(left, right) {
var _gt = {
range: {}
};
_gt.range[ left ] = {
gt: right
};
return _gt;
},
'>=': function(left, right) {
var _gte = {
range: {}
};
_gte.range[ left ] = {
gte: right
};
return _gte;
},
'==': function(left, right) {
var _eq = {
term: {}
};
if (typeof left === 'boolean') {
// As in [ true, '==', field ]
_eq.term[ right ] = left;
} else {
// As in [ field, '==', true ]
_eq.term[ left ] = right;
}
return _eq;
},
'!=': function(left, right) {
var _ne = {
bool: {
must_not: {
term: {}
}
}
};
if (typeof left === 'boolean') {
// As in [ true, '!=', field ]
_ne.bool.must_not.term[ right ] = left;
} else {
// As in [ field, '!=', true ]
_ne.bool.must_not.term[ left ] = right;
}
return _ne;
}
};
function _processNode(node) {
logger.debug('In _processNode.');
if (node.length === 1) {
return generators['ALL'](node);
} else if (node.length === 2) {
return generators['SEARCH'](node);
} else if (node.length === 3) {
if (node[0] === '!') {
// Negated search...
return generators['SEARCH'](node);
} else {
return generators['COMPARISON'](node);
}
} else {
throw new Error('Invalid AST node.');
}
}
function isEmpty(obj) {
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}