/
query-builder.js
149 lines (114 loc) · 4.34 KB
/
query-builder.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
'use strict';
const assert = require('assert');
module.exports = function(informationSchema) {
const {
castForDb,
escapeKey,
isDefinedProperty
} = informationSchema;
return {
buildWhere,
buildAttributes
};
function buildAttributes(model, attributes, queryParams) {
if (!attributes) {
return '*';
}
// allow both { attributes: 'id'} and { attributes: [ 'name', 'email' ] }
if (!Array.isArray(attributes)) {
attributes = [ attributes ];
}
[].splice.apply(queryParams, [ queryParams.length, 0 ].concat(
attributes
));
return attributes.map(() => '??');
}
function buildWhere(model, conds = {}, queryParams, addWhere) {
if (!conds) {
return '';
}
let where = Object.keys(conds)
.map(key => buildCondition(model, key, conds[key], queryParams))
.join(' AND ');
if (addWhere && where) {
where = `WHERE ${ where }`;
}
return where;
}
function buildCondition(model, key, value, queryParams) {
if (isExpressionKey(key)) {
return buildExpression(model, key, value, queryParams);
}
assert(isDefinedProperty(model, key), `Unknown condition key ${ key }`);
if (value === null) {
return `${ escapedKey(key) } IS NULL`;
}
if (value instanceof Array) {
value = { inq: value };
}
if (typeof value === 'object' && value) {
const keys = Object.keys(value);
const key = keys.shift();
assert.equal(keys.length, 0,
`Only one key allowed in ${ key } condition, but given extra ${ keys.join(', ') }`);
return trivial(key, value[key]) || nonTrivial(key, value[key]);
}
queryParams.push(castForDb(model, key, value));
return `${ escapedKey(key) } = ?`;
// zero-length inclusion and exclusion are effectively trivial 0 or 1
function trivial(operator, operands) {
if ([ 'inq', 'nin' ].includes(operator) && !operands.length) {
return operator === 'inq' ? '0' : '1';
}
}
function nonTrivial(conditionType, operand) {
const conditions2op = {
gt: '>',
gte: '>=',
ne: '!=',
lt: '<',
lte: '<=',
like: 'LIKE',
nlike: 'NOT LIKE'
};
const conditionsSet = {
inq: 'IN',
nin: 'NOT IN'
};
if (conditionType === 'between') {
assert.equal(operand.length, 2, 'Between expects two operands');
queryParams.push(castForDb(model, key, operand[0]));
queryParams.push(castForDb(model, key, operand[1]));
return `${ escapedKey(key) } BETWEEN ? AND ?`;
}
if (conditionType in conditionsSet) {
[].splice.apply(queryParams, [
queryParams.length, 0
].concat(operand.map(value => castForDb(model, key, value))));
return `${ escapedKey(key) } ${ conditionsSet[conditionType] } (
${ operand.map(() => '?').join(', ') }
)`;
}
if (conditionType in conditions2op) {
queryParams.push(castForDb(model, key, operand));
return `${ escapedKey(key) } ${ conditions2op[conditionType] } ?`;
}
throw new Error(`Condition type "${ conditionType }" is not supported`);
}
}
function escapedKey(key) {
return key.split('.').map(escapeKey).join('.');
}
function isExpressionKey(key) {
return [ 'or', 'xor', 'not', 'and' ].includes(key);
}
function buildExpression(model, key, cases, queryParams) {
assert(isExpressionKey(key), `Is expression key: ${ key }`);
if (key === 'not') {
// assert.equal(cases.length, 1, 'Not ony operates with single operand');
return `( NOT (${ buildWhere(model, cases, queryParams) }) )`;
}
const alternatives = cases.map(c => buildWhere(model, c, queryParams));
return `(${ alternatives.join(` ${ key.toUpperCase() } `) })`;
}
};