compiler.js
10.1 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
'use strict';
exports.__esModule = true;
var _compact2 = require('lodash/compact');
var _compact3 = _interopRequireDefault(_compact2);
var _reduce2 = require('lodash/reduce');
var _reduce3 = _interopRequireDefault(_reduce2);
var _map2 = require('lodash/map');
var _map3 = _interopRequireDefault(_map2);
var _isString2 = require('lodash/isString');
var _isString3 = _interopRequireDefault(_isString2);
var _isEmpty2 = require('lodash/isEmpty');
var _isEmpty3 = _interopRequireDefault(_isEmpty2);
var _isPlainObject2 = require('lodash/isPlainObject');
var _isPlainObject3 = _interopRequireDefault(_isPlainObject2);
var _assign2 = require('lodash/assign');
var _assign3 = _interopRequireDefault(_assign2);
var _inherits = require('inherits');
var _inherits2 = _interopRequireDefault(_inherits);
var _compiler = require('../../../query/compiler');
var _compiler2 = _interopRequireDefault(_compiler);
var _helpers = require('../../../helpers');
var helpers = _interopRequireWildcard(_helpers);
var _utils = require('../utils');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint max-len:0 */
// Oracle Query Builder & Compiler
// ------
var components = ['columns', 'join', 'where', 'union', 'group', 'having', 'order', 'lock'];
// Query Compiler
// -------
// Set the "Formatter" to use for the queries,
// ensuring that all parameterized values (even across sub-queries)
// are properly built into the same query.
function QueryCompiler_Oracle(client, builder) {
_compiler2.default.call(this, client, builder);
}
(0, _inherits2.default)(QueryCompiler_Oracle, _compiler2.default);
(0, _assign3.default)(QueryCompiler_Oracle.prototype, {
// Compiles an "insert" query, allowing for multiple
// inserts using a single query statement.
insert: function insert() {
var _this = this;
var insertValues = this.single.insert || [];
var returning = this.single.returning;
if (!Array.isArray(insertValues) && (0, _isPlainObject3.default)(this.single.insert)) {
insertValues = [this.single.insert];
}
// always wrap returning argument in array
if (returning && !Array.isArray(returning)) {
returning = [returning];
}
if (Array.isArray(insertValues) && insertValues.length === 1 && (0, _isEmpty3.default)(insertValues[0])) {
return this._addReturningToSqlAndConvert('insert into ' + this.tableName + ' (' + this.formatter.wrap(this.single.returning) + ') values (default)', returning, this.tableName);
}
if ((0, _isEmpty3.default)(this.single.insert) && typeof this.single.insert !== 'function') {
return '';
}
var insertData = this._prepInsert(insertValues);
var sql = {};
if ((0, _isString3.default)(insertData)) {
return this._addReturningToSqlAndConvert('insert into ' + this.tableName + ' ' + insertData, returning);
}
if (insertData.values.length === 1) {
return this._addReturningToSqlAndConvert('insert into ' + this.tableName + ' (' + this.formatter.columnize(insertData.columns) + ') values (' + this.formatter.parameterize(insertData.values[0]) + ')', returning, this.tableName);
}
var insertDefaultsOnly = insertData.columns.length === 0;
sql.sql = 'begin ' + (0, _map3.default)(insertData.values, function (value) {
var returningHelper = void 0;
var parameterizedValues = !insertDefaultsOnly ? _this.formatter.parameterize(value, _this.client.valueForUndefined) : '';
var returningValues = Array.isArray(returning) ? returning : [returning];
var subSql = 'insert into ' + _this.tableName + ' ';
if (returning) {
returningHelper = new _utils.ReturningHelper(returningValues.join(':'));
sql.outParams = (sql.outParams || []).concat(returningHelper);
}
if (insertDefaultsOnly) {
// no columns given so only the default value
subSql += '(' + _this.formatter.wrap(_this.single.returning) + ') values (default)';
} else {
subSql += '(' + _this.formatter.columnize(insertData.columns) + ') values (' + parameterizedValues + ')';
}
subSql += returning ? ' returning ROWID into ' + _this.formatter.parameter(returningHelper) : '';
// pre bind position because subSql is an execute immediate parameter
// later position binding will only convert the ? params
subSql = _this.formatter.client.positionBindings(subSql);
var parameterizedValuesWithoutDefault = parameterizedValues.replace('DEFAULT, ', '').replace(', DEFAULT', '');
return 'execute immediate \'' + subSql.replace(/'/g, "''") + (parameterizedValuesWithoutDefault || returning ? '\' using ' : '') + parameterizedValuesWithoutDefault + (parameterizedValuesWithoutDefault && returning ? ', ' : '') + (returning ? 'out ?' : '') + ';';
}).join(' ') + 'end;';
if (returning) {
sql.returning = returning;
// generate select statement with special order by to keep the order because 'in (..)' may change the order
sql.returningSql = 'select ' + this.formatter.columnize(returning) + ' from ' + this.tableName + ' where ROWID in (' + sql.outParams.map(function (v, i) {
return ':' + (i + 1);
}).join(', ') + ')' + ' order by case ROWID ' + sql.outParams.map(function (v, i) {
return 'when CHARTOROWID(:' + (i + 1) + ') then ' + i;
}).join(' ') + ' end';
}
return sql;
},
// Update method, including joins, wheres, order & limits.
update: function update() {
var updates = this._prepUpdate(this.single.update);
var where = this.where();
var returning = this.single.returning;
var sql = 'update ' + this.tableName + ' set ' + updates.join(', ') + (where ? ' ' + where : '');
if (!returning) {
return sql;
}
// always wrap returning argument in array
if (returning && !Array.isArray(returning)) {
returning = [returning];
}
return this._addReturningToSqlAndConvert(sql, returning, this.tableName);
},
// Compiles a `truncate` query.
truncate: function truncate() {
return 'truncate table ' + this.tableName;
},
forUpdate: function forUpdate() {
return 'for update';
},
forShare: function forShare() {
// lock for share is not directly supported by oracle
// use LOCK TABLE .. IN SHARE MODE; instead
helpers.warn('lock for share is not supported by oracle dialect');
return '';
},
// Compiles a `columnInfo` query.
columnInfo: function columnInfo() {
var column = this.single.columnInfo;
// Node oracle drivers doesn't support LONG type (which is data_default type)
var sql = 'select * from xmltable( \'/ROWSET/ROW\'\n passing dbms_xmlgen.getXMLType(\'\n select char_col_decl_length, column_name, data_type, data_default, nullable\n from user_tab_columns where table_name = \'\'' + this.single.table + '\'\' \')\n columns\n CHAR_COL_DECL_LENGTH number, COLUMN_NAME varchar2(200), DATA_TYPE varchar2(106),\n DATA_DEFAULT clob, NULLABLE varchar2(1))';
return {
sql: sql,
output: function output(resp) {
var out = (0, _reduce3.default)(resp, function (columns, val) {
columns[val.COLUMN_NAME] = {
type: val.DATA_TYPE,
defaultValue: val.DATA_DEFAULT,
maxLength: val.CHAR_COL_DECL_LENGTH,
nullable: val.NULLABLE === 'Y'
};
return columns;
}, {});
return column && out[column] || out;
}
};
},
select: function select() {
var _this2 = this;
var query = this.with();
var statements = (0, _map3.default)(components, function (component) {
return _this2[component]();
});
query += (0, _compact3.default)(statements).join(' ');
return this._surroundQueryWithLimitAndOffset(query);
},
aggregate: function aggregate(stmt) {
var val = stmt.value;
var splitOn = val.toLowerCase().indexOf(' as ');
var distinct = stmt.aggregateDistinct ? 'distinct ' : '';
// Allows us to speciy an alias for the aggregate types.
if (splitOn !== -1) {
var col = val.slice(0, splitOn);
var alias = val.slice(splitOn + 4);
return stmt.method + '(' + distinct + this.formatter.wrap(col) + ') ' + this.formatter.wrap(alias);
}
return stmt.method + '(' + distinct + this.formatter.wrap(val) + ')';
},
// for single commands only
_addReturningToSqlAndConvert: function _addReturningToSqlAndConvert(sql, returning, tableName) {
var res = {
sql: sql
};
if (!returning) {
return res;
}
var returningValues = Array.isArray(returning) ? returning : [returning];
var returningHelper = new _utils.ReturningHelper(returningValues.join(':'));
res.sql = sql + ' returning ROWID into ' + this.formatter.parameter(returningHelper);
res.returningSql = 'select ' + this.formatter.columnize(returning) + ' from ' + tableName + ' where ROWID = :1';
res.outParams = [returningHelper];
res.returning = returning;
return res;
},
_surroundQueryWithLimitAndOffset: function _surroundQueryWithLimitAndOffset(query) {
var limit = this.single.limit;
var offset = this.single.offset;
var hasLimit = limit || limit === 0 || limit === '0';
limit = +limit;
if (!hasLimit && !offset) return query;
query = query || "";
if (hasLimit && !offset) {
return 'select * from (' + query + ') where rownum <= ' + this.formatter.parameter(limit);
}
var endRow = +offset + (hasLimit ? limit : 10000000000000);
return "select * from " + "(select row_.*, ROWNUM rownum_ from (" + query + ") row_ " + "where rownum <= " + this.formatter.parameter(endRow) + ") " + "where rownum_ > " + this.formatter.parameter(offset);
}
});
// Compiles the `select` statement, or nested sub-selects
// by calling each of the component compilers, trimming out
// the empties, and returning a generated query string.
QueryCompiler_Oracle.prototype.first = QueryCompiler_Oracle.prototype.select;
exports.default = QueryCompiler_Oracle;
module.exports = exports['default'];