-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
167 lines (144 loc) · 4.15 KB
/
index.js
File metadata and controls
167 lines (144 loc) · 4.15 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
// SQL parser library
import SQLParser from 'node-sql-parser'
// SQLParser does most of the work, but it returns floating
// point numbers as strings
function tidy(p) {
if (p.type === 'number' && typeof p.value === 'string') {
return parseFloat(p.value)
} else {
return p.value
}
}
// recursive function that dives into the tree of parsed WHERE clauses and creates
// the equivalent selector. The selector function calls itself as it meets operators
// like AND and OR, making the JSON tree deeper at this point.
// s - the selector object to be added to
// condition - the parsed SQL condition to be converted.
const selector = function (s, condition) {
let op = null
switch (condition.operator) {
case '=':
s[condition.left.column] = { $eq: tidy(condition.right) }
break
case '!=':
s[condition.left.column] = { $ne: tidy(condition.right) }
break
case '<':
s[condition.left.column] = { $lt: tidy(condition.right) }
break
case '<=':
s[condition.left.column] = { $lte: tidy(condition.right) }
break
case '>':
s[condition.left.column] = { $gt: tidy(condition.right) }
break
case '>=':
s[condition.left.column] = { $gte: tidy(condition.right) }
break
case 'IN':
s[condition.left.column] = { $in: condition.right.value.map(function (v) { return tidy(v) }) }
break
case 'NOT IN':
s[condition.left.column] = { $nin: condition.right.value.map(function (v) { return tidy(v) }) }
break
case 'AND':
op = '$and'
s[op] = []
s[op].push(selector({}, condition.left))
s[op].push(selector({}, condition.right))
break
case 'OR':
op = '$or'
s[op] = []
s[op].push(selector({}, condition.left))
s[op].push(selector({}, condition.right))
break
default:
throw (new Error('unsupported SQL operation ' + condition.operation))
}
return s
}
// parase an SQL query and return the equivalent Cloudant QUERY
export function parse(query) {
// throw on non string
if (typeof query !== 'string') {
throw (new Error('query must be a string'))
}
// empty cloudant query object
const obj = {}
// parse the SQL into a tree
const parser = new SQLParser.Parser()
let tree
try {
tree = parser.astify(query)
} catch (e) {
throw('Failed to parse SQL')
}
// extract table name
obj.table = tree.from[0].table
// look for exceptions
if (tree.type !== 'select') {
throw new Error('must be select')
}
if (tree.distinct !== null) {
throw new Error('distinct not allowed')
}
if (tree.groupby !== null) {
throw new Error('groupby not allowed')
}
if (tree.having !== null) {
throw new Error('having not allowed')
}
if (tree.collate !== null) {
throw new Error('collate not allowed')
}
// extract the field list
if (tree.columns) {
obj.fields = []
for (const i in tree.columns) {
const field = tree.columns[i]
if (field.as) {
throw new Error('as not allowed')
}
if (field.expr.table !== null) {
throw new Error('table.column not allowed')
}
if (field.expr.type === 'column_ref') {
if (field.expr.column !== '*') {
obj.fields.push(field.expr.column)
}
}
}
if (obj.fields.length === 0) {
delete obj.fields
}
}
// extract where clauses by recursively walking the tree
if (tree.where) {
obj.selector = selector({}, tree.where)
}
// extract order by and convert to mango syntax
if (tree.orderby) {
obj.sort = []
let lastorder = null
for (const i in tree.orderby) {
const t = tree.orderby[i]
const s = {}
s[t.expr.column] = (t.type === 'DESC') ? 'desc' : 'asc'
if (lastorder != null && s[t.expr.column] !== lastorder) {
throw new Error('ORDER BY must be either all ASC or all DESC, not mixed')
}
lastorder = s[t.expr.column]
obj.sort.push(s)
}
}
// skip and limit
if (tree.limit) {
obj.limit = tree.limit.value[0].value
if (tree.limit.value.length > 1) {
obj.skip = tree.limit.value[0].value
obj.limit = tree.limit.value[1].value
}
}
return obj
}