compiler.js
6.42 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
// MSSQL Query Compiler
// ------
import inherits from 'inherits';
import QueryCompiler from '../../../query/compiler';
import { assign, isEmpty, compact } from 'lodash'
function QueryCompiler_MSSQL(client, builder) {
QueryCompiler.call(this, client, builder)
}
inherits(QueryCompiler_MSSQL, QueryCompiler)
const components = [
'columns', 'join', 'lock', 'where', 'union', 'group',
'having', 'order', 'limit', 'offset'
];
assign(QueryCompiler_MSSQL.prototype, {
_emptyInsertValue: 'default values',
select() {
const sql = this.with();
const statements = components.map(component =>
this[component](this)
);
return sql + compact(statements).join(' ');
},
// Compiles an "insert" query, allowing for multiple
// inserts using a single query statement.
insert() {
const insertValues = this.single.insert || [];
let sql = this.with() + `insert into ${this.tableName} `;
const { returning } = this.single;
const returningSql = returning
? this._returning('insert', returning) + ' '
: '';
if (Array.isArray(insertValues)) {
if (insertValues.length === 0) {
return ''
}
} else if (typeof insertValues === 'object' && isEmpty(insertValues)) {
return {
sql: sql + returningSql + this._emptyInsertValue,
returning
};
}
const insertData = this._prepInsert(insertValues);
if (typeof insertData === 'string') {
sql += insertData;
} else {
if (insertData.columns.length) {
sql += `(${this.formatter.columnize(insertData.columns)}`
sql += `) ${returningSql}values (`
let i = -1
while (++i < insertData.values.length) {
if (i !== 0) sql += '), ('
sql += this.formatter.parameterize(
insertData.values[i], this.client.valueForUndefined
)
}
sql += ')';
} else if (insertValues.length === 1 && insertValues[0]) {
sql += returningSql + this._emptyInsertValue
} else {
sql = ''
}
}
return {
sql,
returning
};
},
// Compiles an `update` query, allowing for a return value.
update() {
const updates = this._prepUpdate(this.single.update);
const join = this.join();
const where = this.where();
const order = this.order();
const top = this.top();
const { returning } = this.single;
return {
sql: this.with() + `update ${top ? top + ' ' : ''}${this.tableName}` +
' set ' + updates.join(', ') +
(join ? ` from ${this.tableName} ${join}` : '') +
(returning ? ` ${this._returning('update', returning)}` : '') +
(where ? ` ${where}` : '') +
(order ? ` ${order}` : '') +
(!returning ? this._returning('rowcount', '@@rowcount') : ''),
returning: returning || '@@rowcount'
};
},
// Compiles a `delete` query.
del() {
// Make sure tableName is processed by the formatter first.
const { tableName } = this;
const wheres = this.where();
const { returning } = this.single;
return {
sql: this.with() + `delete from ${tableName}` +
(returning ? ` ${this._returning('del', returning)}` : '') +
(wheres ? ` ${wheres}` : '') +
(!returning ? this._returning('rowcount', '@@rowcount') : ''),
returning: returning || '@@rowcount'
};
},
// Compiles the columns in the query, specifying if an item was distinct.
columns() {
let distinct = false;
if (this.onlyUnions()) return ''
const columns = this.grouped.columns || []
let i = -1, sql = [];
if (columns) {
while (++i < columns.length) {
const stmt = columns[i];
if (stmt.distinct) distinct = true
if (stmt.type === 'aggregate') {
sql.push(this.aggregate(stmt))
}
else if (stmt.value && stmt.value.length > 0) {
sql.push(this.formatter.columnize(stmt.value))
}
}
}
if (sql.length === 0) sql = ['*'];
const top = this.top();
return `select ${distinct ? 'distinct ' : ''}` +
(top ? top + ' ' : '') +
sql.join(', ') + (this.tableName ? ` from ${this.tableName}` : '');
},
_returning(method, value) {
switch (method) {
case 'update':
case 'insert':
return value
? `output ${this.formatter.columnizeWithPrefix('inserted.', value)}`
: '';
case 'del':
return value
? `output ${this.formatter.columnizeWithPrefix('deleted.', value)}`
: '';
case 'rowcount':
return value
? ';select @@rowcount'
: '';
}
},
// Compiles a `truncate` query.
truncate() {
return `truncate table ${this.tableName}`;
},
forUpdate() {
return 'with (READCOMMITTEDLOCK)';
},
forShare() {
return 'with (NOLOCK)';
},
// Compiles a `columnInfo` query.
columnInfo() {
const column = this.single.columnInfo;
let sql =`select * from information_schema.columns where table_name = ? and table_catalog = ?`;
const bindings = [this.single.table, this.client.database()];
if (this.single.schema) {
sql += ' and table_schema = ?';
bindings.push(this.single.schema);
} else {
sql += ` and table_schema = 'dbo'`;
}
return {
sql,
bindings: bindings,
output(resp) {
const out = resp.reduce(function(columns, val) {
columns[val.COLUMN_NAME] = {
defaultValue: val.COLUMN_DEFAULT,
type: val.DATA_TYPE,
maxLength: val.CHARACTER_MAXIMUM_LENGTH,
nullable: (val.IS_NULLABLE === 'YES')
};
return columns
}, {})
return column && out[column] || out;
}
};
},
top() {
const noLimit = !this.single.limit && this.single.limit !== 0;
const noOffset = !this.single.offset;
if (noLimit || !noOffset) return '';
return `top (${this.formatter.parameter(this.single.limit)})`;
},
limit() {
return '';
},
offset() {
const noLimit = !this.single.limit && this.single.limit !== 0;
const noOffset = !this.single.offset;
if (noOffset) return '';
let offset = `offset ${noOffset ? '0' : this.formatter.parameter(this.single.offset)} rows`;
if (!noLimit) {
offset += ` fetch next ${this.formatter.parameter(this.single.limit)} rows only`;
}
return offset;
},
})
// Set the QueryBuilder & QueryCompiler on the client object,
// in case anyone wants to modify things to suit their own purposes.
export default QueryCompiler_MSSQL;