/
sql.js
194 lines (164 loc) · 4.77 KB
/
sql.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
var BaseTransformer = require('./base_transformer').BaseTransformer
, utils = require('utilities')
, operation = require('../../query/operation')
, comparison = require('../../query/comparison')
, datatypes = require('../../datatypes');
var sql = utils.mixin(new BaseTransformer(), new (function () {
this.transformValue = function (prop, datatype) {
var ret;
// FIXME: Is special-casing this the right way to go?
if (utils.isEmpty(prop)) {
ret = 'NULL';
}
else {
if (datatype == 'boolean') {
ret = prop ? this.TRUE_VALUE : this.FALSE_VALUE;
ret = String(ret);
}
else {
ret = datatypes[datatype].serialize(prop, {
escape: 'sql'
, useQuotes: true
});
}
}
return ret;
};
this.transformSortOrder = function (sort, tableName) {
var sql = ''
, fieldArr = []
, names
, modelName
, propertyName;
for (var p in sort) {
if (p.indexOf('.') > -1) {
names = p.split('.');
modelName = this._tableizeModelName(names[0]);
propertyName = this._columnizePropertyName(names[1]);
}
else {
modelName = this._tableizeModelName(tableName);
propertyName = this._columnizePropertyName(p);
}
fieldArr.push(modelName + '.' + propertyName + ' ' + sort[p].toUpperCase());
}
sql += 'ORDER BY ' + fieldArr.join(', ');
return sql;
}
this.transformConditions = function (conditions) {
var sql = this.transformOperation(conditions);
return sql;
};
/*
* Note: please return null on empty operations.
* returning empty strings leads to weirdness like
* `WHERE ()` appearing in queries, which breaks
* postgresql (and possibly others)
*/
this.transformOperation = function (op) {
var self = this
, ops = [];
if (op.isEmpty()) {
return null;
}
else {
if (op.type == 'not') {
return 'NOT (' + self.transformOperation(op.operand()) + ')';
}
else {
op.forEach(function (o) {
var temp;
if (o instanceof operation.OperationBase) {
temp = self.transformOperation(o);
if(temp)
ops.push(temp);
}
else {
temp = self.transformComparison(o);
if(temp)
ops.push(self.transformComparison(o));
}
});
if(ops.length)
return '(' + ops.join(' ' + op.type.toUpperCase() + ' ') + ')';
else
return null;
}
}
};
this.transformComparison = function (comp) {
return [this.transformComparisonFieldName(comp),
this.transformComparisonComparator(comp),
this.transformComparisonValue(comp)].join(' ');
};
this.transformComparisonFieldName = function (comp) {
var val = comp.value
, name;
name = this._tableizeModelName(comp.model);
name += '.';
name += this._columnizePropertyName(comp.field);
if (comp.opts.nocase && comp.datatype == 'string') {
name = 'LOWER(' + name + ')';
}
// `<fieldname> IN ()` reduces to `false`
if(comp instanceof comparison.InclusionComparison && !val.length) {
name = '';
}
return name;
};
this.transformComparisonComparator = function (comp) {
var val = comp.value
, comparator = comp.sqlComparatorString;
if (val === null) {
comparator = (comp instanceof comparison.EqualToComparison) ?
'IS' : 'IS NOT';
}
// `<fieldname> IN ()` reduces to `false`
if(comp instanceof comparison.InclusionComparison && !val.length) {
comparator = '';
}
return comparator;
};
// FIXME: This should integrate better with `transformValue`
this.transformComparisonValue = function (comp) {
var self = this
, val = comp.value
, ret = '';
if (utils.isEmpty(val)) {
ret = 'NULL';
}
else {
if (comp instanceof comparison.LikeComparison) {
// Prefix with percent if they didn't include one
if (val.indexOf('%') == -1) {
val += '%';
}
}
// Inclusion, multiple values
if (comp instanceof comparison.InclusionComparison) {
ret = [];
val.forEach(function (item) {
item = self.transformValue(item, comp.datatype);
if (comp.opts.nocase) {
item = 'LOWER(' + item + ')';
}
ret.push(item);
});
// `<fieldname> IN ()` reduces to `false`
if(!ret.length)
ret = this.FALSE_VALUE;
else
ret = '(' + ret.join(', ') + ')';
}
// Single val
else {
ret = this.transformValue(val, comp.datatype);
if (comp.opts.nocase) {
ret = 'LOWER(' + ret + ')';
}
}
}
return ret;
};
})());
module.exports = sql;