forked from balderdashy/sails-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.js
More file actions
325 lines (253 loc) · 9.57 KB
/
sql.js
File metadata and controls
325 lines (253 loc) · 9.57 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var mysql = require('mysql');
var _ = require('underscore');
_.str = require('underscore.string');
var sql = {
// Convert mysql format to standard javascript object
normalizeSchema: function (schema) {
return _.reduce(schema, function(memo, field) {
// Marshal mysql DESCRIBE to waterline collection semantics
var attrName = field.Field;
var type = field.Type;
// Remove (n) column-size indicators
type = type.replace(/\([0-9]+\)$/,'');
memo[attrName] = {
type: type,
defaultsTo: field.Default,
autoIncrement: field.Extra === 'auto_increment'
};
return memo;
}, {});
},
// @returns ALTER query for adding a column
addColumn: function (collectionName, attrName, attrDef) {
// Escape table name and attribute name
var tableName = mysql.escapeId(collectionName);
sails.log.verbose("ADDING ",attrName, "with",attrDef);
// Build column definition
var columnDefinition = sql._schema(collectionName, attrDef, attrName);
return 'ALTER TABLE ' + tableName + ' ADD ' + columnDefinition;
},
// @returns ALTER query for dropping a column
removeColumn: function (collectionName, attrName) {
// Escape table name and attribute name
var tableName = mysql.escapeId(collectionName);
attrName = mysql.escapeId(attrName);
return 'ALTER TABLE ' + tableName + ' DROP COLUMN ' + attrName;
},
selectQuery: function (collectionName, options) {
// Escape table name
var tableName = mysql.escapeId(collectionName);
// Build query
return 'SELECT * FROM ' + tableName + ' ' + sql.serializeOptions(collectionName, options);
},
insertQuery: function (collectionName, data) {
// Escape table name
var tableName = mysql.escapeId(collectionName);
// Build query
return 'INSERT INTO ' + tableName + ' ' + '(' + sql.attributes(collectionName, data) + ')' + ' VALUES (' + sql.values(collectionName, data) + ')';
},
// Create a schema csv for a DDL query
schema: function(collectionName, attributes) {
return sql.build(collectionName, attributes, sql._schema);
},
_schema: function(collectionName, attribute, attrName) {
attrName = mysql.escapeId(attrName);
var type = sqlTypeCast(attribute.type);
// return attrName + ' ' + type + ' ' + (attribute.autoIncrement ? 'NOT NULL AUTO_INCREMENT, ' + 'PRIMARY KEY(' + attrName + ')' : '');
return attrName + ' ' + type + ' ' + (attribute.autoIncrement ? 'NOT NULL AUTO_INCREMENT PRIMARY KEY' : '');
},
// Create an attribute csv for a DQL query
attributes: function(collectionName, attributes) {
return sql.build(collectionName, attributes, sql.prepareAttribute);
},
// Create a value csv for a DQL query
// key => optional, overrides the keys in the dictionary
values: function(collectionName, values, key) {
return sql.build(collectionName, values, sql.prepareValue, ', ', key);
},
// Create a WHERE criteria snippet for a DQL query
criteria: function(collectionName, values) {
return sql.build(collectionName, values, sql.prepareCriterion);
},
prepareCriterion: function(collectionName, value, key, parentKey) {
// Special sub-attr case
if (validSubAttrCriteria(value)) {
return sql.where(collectionName, value, null, key);
}
// Build escaped attr and value strings using either the key,
// or if one exists, the parent key
var attrStr, valueStr;
// Special comparator case
if (parentKey) {
attrStr = sql.prepareAttribute(collectionName, value, parentKey);
valueStr = sql.prepareValue(collectionName, value, parentKey);
// Why don't we strip you out of those bothersome apostrophes?
var nakedButClean = _.str.trim(valueStr,'\'');
if (key === '<' || key === 'lessThan') return attrStr + '<' + valueStr;
else if (key === '<=' || key === 'lessThanOrEqual') return attrStr + '<=' + valueStr;
else if (key === '>' || key === 'greaterThan') return attrStr + '>' + valueStr;
else if (key === '>=' || key === 'greaterThanOrEqual') return attrStr + '>=' + valueStr;
else if (key === '!' || key === 'not') {
if (value === null) return attrStr + 'IS NOT NULL';
else return attrStr + '<>' + valueStr;
}
else if (key === 'like') return attrStr + ' LIKE \'' + nakedButClean + '\'';
else if (key === 'contains') return attrStr + ' LIKE \'%' + nakedButClean + '%\'';
else if (key === 'startsWith') return attrStr + ' LIKE \'' + nakedButClean + '%\'';
else if (key === 'endsWith') return attrStr + ' LIKE \'%' + nakedButClean + '\'';
else throw new Error('Unknown comparator: ' + key);
} else {
attrStr = sql.prepareAttribute(collectionName, value, key);
valueStr = sql.prepareValue(collectionName, value, key);
// Special IS NULL case
if (_.isNull(value)) {
return attrStr + " IS NULL";
} else return attrStr + "=" + valueStr;
}
},
prepareValue: function(collectionName, value, attrName) {
// Cast dates to SQL
if (_.isDate(value)) {
value = toSqlDate(value);
}
// Cast functions to strings
if (_.isFunction(value)) {
value = value.toString();
}
// Escape (also wraps in quotes)
return mysql.escape(value);
},
prepareAttribute: function(collectionName, value, attrName) {
return mysql.escapeId(attrName);
},
// Starting point for predicate evaluation
// parentKey => if set, look for comparators and apply them to the parent key
where: function(collectionName, where, key, parentKey) {
return sql.build(collectionName, where, sql.predicate, ' AND ', undefined, parentKey);
},
// Recursively parse a predicate calculus and build a SQL query
predicate: function(collectionName, criterion, key, parentKey) {
var queryPart = '';
if (parentKey) {
return sql.prepareCriterion(collectionName, criterion, key, parentKey);
}
// OR
if (key.toLowerCase() === 'or') {
queryPart = sql.build(collectionName, criterion, sql.where, ' OR ');
return ' ( ' + queryPart + ' ) ';
}
// AND
else if (key.toLowerCase() === 'and') {
queryPart = sql.build(collectionName, criterion, sql.where, ' AND ');
return ' ( ' + queryPart + ' ) ';
}
// IN
else if (_.isArray(criterion)) {
queryPart = sql.prepareAttribute(collectionName, null, key) + " IN (" + sql.values(collectionName, criterion, key) + ")";
return queryPart;
}
// LIKE
else if (key.toLowerCase() === 'like') {
return sql.build(collectionName, criterion, function(collectionName, value, attrName) {
var attrStr = sql.prepareAttribute(collectionName, value, attrName);
// TODO: Handle regexp criterias
if (_.isRegExp(value)) {
throw new Error('RegExp LIKE criterias not supported by the MySQLAdapter yet. Please contribute @ http://github.com/balderdashy/sails-mysql');
}
var valueStr = sql.prepareValue(collectionName, value, attrName);
// Handle escaped percent (%) signs [encoded as %%%]
valueStr = valueStr.replace(/%%%/g, '\\%');
return attrStr + " LIKE " + valueStr;
}, ' AND ');
}
// NOT
else if (key.toLowerCase() === 'not') {
throw new Error('NOT not supported yet!');
}
// Basic criteria item
else {
return sql.prepareCriterion(collectionName, criterion, key);
}
},
serializeOptions: function(collectionName, options) {
var queryPart = '';
if (options.where) {
queryPart += 'WHERE ' + sql.where(collectionName, options.where) + ' ';
}
if (options.sort) {
queryPart += 'ORDER BY ';
// Sort through each sort attribute criteria
_.each(options.sort, function(direction, attrName) {
queryPart += sql.prepareAttribute(collectionName, null, attrName) + ' ';
// Basic MongoDB-style numeric sort direction
if (direction === 1) {
queryPart += 'ASC ';
} else {
queryPart += 'DESC ';
}
});
}
if (options.limit) {
queryPart += 'LIMIT ' + options.limit + ' ';
} else {
// Some MySQL hackery here. For details, see:
// http://stackoverflow.com/questions/255517/mysql-offset-infinite-rows
queryPart += 'LIMIT 18446744073709551610 ';
}
if (options.skip) {
queryPart += 'OFFSET ' + options.skip + ' ';
}
return queryPart;
},
// Put together the CSV aggregation
// separator => optional, defaults to ', '
// keyOverride => optional, overrides the keys in the dictionary
// (used for generating value lists in IN queries)
// parentKey => key of the parent to this object
build: function(collectionName, collection, fn, separator, keyOverride, parentKey) {
separator = separator || ', ';
var $sql = '';
_.each(collection, function(value, key) {
$sql += fn(collectionName, value, keyOverride || key, parentKey);
// (always append separator)
$sql += separator;
});
// (then remove final one)
return _.str.rtrim($sql, separator);
}
};
// Cast waterline types into SQL data types
function sqlTypeCast(type) {
type = type && type.toLowerCase();
switch (type) {
case 'string':
case 'text':
return 'TEXT';
case 'boolean':
case 'int':
case 'integer':
return 'INT';
case 'float':
case 'double':
return 'FLOAT';
case 'date':
return 'DATE';
case 'datetime':
return 'DATETIME';
default:
console.error("Unregistered type given: " + type);
return "TEXT";
}
}
function wrapInQuotes(val) {
return '"' + val + '"';
}
function toSqlDate(date) {
return [[date.getFullYear(), ((date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1)), ((date.getDate() < 10 ? '0' : '') + date.getDate())].join("-"), date.toLocaleTimeString()].join(" ");
}
// Return whether this criteria is valid as an object inside of an attribute
function validSubAttrCriteria(c) {
return _.isObject(c) && (
c.not || c.greaterThan || c.lessThan || c.greaterThanOrEqual || c.lessThanOrEqual || c['<'] || c['<='] || c['!'] || c['>'] || c['>='] || c.startsWith || c.endsWith || c.contains || c.like);
}
module.exports = sql;