/
base.js
129 lines (115 loc) · 3.19 KB
/
base.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
var Adapter
, BaseAdapter = require('../base_adapter').BaseAdapter
, datatypes = require('../../datatypes')
, utils = require('utilities')
, operation = require('../../query/operation');
Adapter = function () {
};
Adapter.prototype = new BaseAdapter();
utils.mixin(Adapter.prototype, new (function () {
this._tableizeModelName = function (name) {
var tableName = utils.inflection.pluralize(name);
tableName = utils.string.snakeize(tableName);
return tableName;
};
this._columnizePropertyName = function (name, options) {
var opts = options || {}
, columnName = utils.string.snakeize(name)
, useQuotes = typeof opts.useQuotes != 'undefined' ?
opts.useQuotes : true;
if (useQuotes) {
columnName = '"' + columnName + '"';
}
return columnName;
};
this._serializeValue = function (prop, datatype) {
var ret;
// FIXME: Is special-casing this the right way to go?
if (prop === null || prop === undefined || prop == '') {
ret = 'NULL';
}
else {
ret = datatypes[datatype].serialize(prop, {
escape: true
, useQuotes: true
});
}
return ret;
};
this._serializeSortOrder = function (sort) {
var sql = ''
, fieldArr = [];
for (var p in sort) {
fieldArr.push(this._columnizePropertyName(p) + ' ' + sort[p].toUpperCase());
}
sql += 'ORDER BY ' + fieldArr.join(', ');
return sql;
}
this._serializeConditions = function (conditions) {
var sql = this._serializeOperation(conditions);
return sql;
};
this._serializeOperation = function (op) {
var self = this
, ops = [];
if (op.isEmpty()) {
return '';
}
else {
op.forEach(function (o) {
if (o instanceof operation.OperationBase) {
ops.push(self._serializeOperation(o));
}
else {
ops.push(self._serializeComparison(o));
}
});
if (op.type == 'not') {
return 'NOT (' + self._serializeOperation(op.operand()) + ')';
}
else {
return '(' + ops.join(' ' + op.type.toUpperCase() + ' ') + ')';
}
}
};
this._serializeComparison = function (comp) {
return [this._serializeComparisonFieldName(comp),
this._serializeComparisonComparator(comp),
this._serializeComparisonValue(comp)].join(' ');
};
this._serializeComparisonFieldName = function (comp) {
var name = this._columnizePropertyName(comp.field);
if (comp.opts.lowercase) {
name = 'LOWER(' + name + ')';
}
return name;
};
this._serializeComparisonComparator = function (comp) {
var val = comp.value
, comparator = comp.sqlComparatorString;
if (val === null) {
comparator = comp.type == 'EqualTo' ?
'IS' : 'IS NOT';
}
return comparator;
};
this._serializeComparisonValue = function (comp) {
var val = comp.value;
if (val === null) {
val = 'NULL';
}
else {
if (comp.type == 'Like') {
if (val.indexOf('%') == -1) {
val += '%';
}
}
val = datatypes[comp.datatype].serialize(val, {
useQuotes: true
, escape: true
});
}
return val;
};
})());
module.exports.Adapter = Adapter;