-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
76 lines (65 loc) · 4.27 KB
/
index.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
// ██╗ ██╗ █████╗ ████████╗███████╗██████╗ ██╗ ██╗███╗ ██╗███████╗
// ██║ ██║██╔══██╗╚══██╔══╝██╔════╝██╔══██╗██║ ██║████╗ ██║██╔════╝
// ██║ █╗ ██║███████║ ██║ █████╗ ██████╔╝██║ ██║██╔██╗ ██║█████╗
// ██║███╗██║██╔══██║ ██║ ██╔══╝ ██╔══██╗██║ ██║██║╚██╗██║██╔══╝
// ╚███╔███╔╝██║ ██║ ██║ ███████╗██║ ██║███████╗██║██║ ╚████║███████╗
// ╚══╝╚══╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝╚═╝ ╚═══╝╚══════╝
//
// ███████╗ ██████╗ ██╗ ██████╗ ██╗ ██╗██╗██╗ ██████╗ ███████╗██████╗
// ██╔════╝██╔═══██╗██║ ██╔══██╗██║ ██║██║██║ ██╔══██╗██╔════╝██╔══██╗
// ███████╗██║ ██║██║ ██████╔╝██║ ██║██║██║ ██║ ██║█████╗ ██████╔╝
// ╚════██║██║▄▄ ██║██║ ██╔══██╗██║ ██║██║██║ ██║ ██║██╔══╝ ██╔══██╗
// ███████║╚██████╔╝███████╗ ██████╔╝╚██████╔╝██║███████╗██████╔╝███████╗██║ ██║
// ╚══════╝ ╚══▀▀═╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚═╝╚══════╝╚═════╝ ╚══════╝╚═╝ ╚═╝
//
// Use Waterline Statements to generate a SQL query that can be used in one of
// the many supported drivers or run independently.
var Knex = require('knex');
var Utils = require('waterline-utils');
var Sequelizer = require('./lib/sequelizer');
module.exports = function sqlBuilder(options) {
if (!options.dialect) {
throw new Error('Missing Dialect!');
}
// Modify deprecated dialect values
if (options.dialect==='mariadb') {
options.dialect='mysql';
}
if (options.dialect==='oracle') {
options.dialect='oracledb';
}
// Build up a Knex instance to use in the query builder
var knexInstance = Knex({
client: options.dialect,
useNullAsDefault: true
});
return {
// ╔═╗╔═╗╔╗╔╔═╗╦═╗╔═╗╔╦╗╔═╗╦═╗
// ║ ╦║╣ ║║║║╣ ╠╦╝╠═╣ ║ ║ ║╠╦╝
// ╚═╝╚═╝╝╚╝╚═╝╩╚═╩ ╩ ╩ ╚═╝╩╚═
// This the main function used by the adapters. Given a Statement, use the
// query parser to generate a token tree and then build up a SQL string.
generate: function generate(query) {
// Tokenize the values
var tokens = Utils.query.tokenizer(query);
// Analyze the tokens
var tree = Utils.query.analyzer(tokens);
// Generate the SQL
var sql = Sequelizer({
knex: knexInstance,
tree: tree
});
return sql;
},
// ╔═╗╔═╗╔═╗ ╦ ╦╔═╗╦ ╦╔═╗╔═╗╦═╗
// ╚═╗║╣ ║═╬╗║ ║║╣ ║ ║╔═╝║╣ ╠╦╝
// ╚═╝╚═╝╚═╝╚╚═╝╚═╝╩═╝╩╚═╝╚═╝╩╚═
// Given a token tree from the query parser, build up a SQL string.
sequelizer: function sequelizer(tree) {
return Sequelizer({
knex: knexInstance,
tree: tree
});
}
};
};