2016-05-17 01:01:34 +10:00
|
|
|
/* eslint max-len:0 */
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Oracle Query Builder & Compiler
|
|
|
|
// ------
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign, isPlainObject, isEmpty, isString, map, reduce, compact } from 'lodash'
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import QueryCompiler from '../../../query/compiler';
|
|
|
|
import * as helpers from '../../../helpers';
|
|
|
|
import { ReturningHelper } from '../utils';
|
|
|
|
|
|
|
|
const components = [
|
|
|
|
'columns', 'join', 'where', 'union', 'group', 'having', 'order', 'lock'
|
|
|
|
];
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
QueryCompiler.call(this, client, builder)
|
|
|
|
}
|
|
|
|
inherits(QueryCompiler_Oracle, QueryCompiler)
|
|
|
|
|
|
|
|
assign(QueryCompiler_Oracle.prototype, {
|
|
|
|
|
|
|
|
// Compiles an "insert" query, allowing for multiple
|
|
|
|
// inserts using a single query statement.
|
2016-05-17 01:01:34 +10:00
|
|
|
insert() {
|
|
|
|
let insertValues = this.single.insert || []
|
2016-05-18 19:59:24 +10:00
|
|
|
let { returning } = this.single;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (!Array.isArray(insertValues) && isPlainObject(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 && isEmpty(insertValues[0])) {
|
2016-05-17 01:01:34 +10:00
|
|
|
return this._addReturningToSqlAndConvert(`insert into ${this.tableName} (${this.formatter.wrap(this.single.returning)}) values (default)`, returning, this.tableName);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmpty(this.single.insert) && typeof this.single.insert !== 'function') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const insertData = this._prepInsert(insertValues);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const sql = {};
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (isString(insertData)) {
|
2016-05-17 01:01:34 +10:00
|
|
|
return this._addReturningToSqlAndConvert(`insert into ${this.tableName} ${insertData}`, returning);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (insertData.values.length === 1) {
|
2016-05-17 01:01:34 +10:00
|
|
|
return this._addReturningToSqlAndConvert(`insert into ${this.tableName} (${this.formatter.columnize(insertData.columns)}) values (${this.formatter.parameterize(insertData.values[0])})`, returning, this.tableName);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const insertDefaultsOnly = (insertData.columns.length === 0);
|
|
|
|
|
|
|
|
sql.sql = 'begin ' + map(insertData.values, (value) => {
|
|
|
|
let returningHelper;
|
|
|
|
const parameterizedValues = !insertDefaultsOnly ? this.formatter.parameterize(value, this.client.valueForUndefined) : '';
|
|
|
|
const returningValues = Array.isArray(returning) ? returning : [returning];
|
|
|
|
let subSql = `insert into ${this.tableName} `;
|
|
|
|
|
|
|
|
if (returning) {
|
|
|
|
returningHelper = new 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);
|
|
|
|
|
|
|
|
const parameterizedValuesWithoutDefault = parameterizedValues.replace('DEFAULT, ', '').replace(', DEFAULT', '');
|
|
|
|
return `execute immediate '${subSql.replace(/'/g, "''")}` +
|
|
|
|
((parameterizedValuesWithoutDefault || returning) ? '\' using ' : '') +
|
|
|
|
parameterizedValuesWithoutDefault +
|
|
|
|
((parameterizedValuesWithoutDefault && returning) ? ', ' : '') +
|
|
|
|
(returning ? 'out ?' : '') + ';';
|
|
|
|
}).join(' ') + 'end;';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (returning) {
|
|
|
|
sql.returning = returning;
|
|
|
|
// generate select statement with special order by to keep the order because 'in (..)' may change the order
|
2016-05-17 01:01:34 +10:00
|
|
|
sql.returningSql = `select ${this.formatter.columnize(returning)}` +
|
2016-03-02 17:07:05 +01:00
|
|
|
' from ' + this.tableName +
|
2016-05-17 01:01:34 +10:00
|
|
|
' where ROWID in (' + sql.outParams.map((v, i) => `:${i + 1}`).join(', ') + ')' +
|
|
|
|
' order by case ROWID ' + sql.outParams.map((v, i) => `when CHARTOROWID(:${i + 1}) then ${i}`).join(' ') + ' end';
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Update method, including joins, wheres, order & limits.
|
2016-05-17 01:01:34 +10:00
|
|
|
update() {
|
|
|
|
const updates = this._prepUpdate(this.single.update);
|
2016-05-18 19:59:24 +10:00
|
|
|
const where = this.where();
|
2016-05-17 01:01:34 +10:00
|
|
|
let { returning } = this.single;
|
|
|
|
const sql = `update ${this.tableName}` +
|
2016-03-02 17:07:05 +01:00
|
|
|
' set ' + updates.join(', ') +
|
2016-05-17 01:01:34 +10:00
|
|
|
(where ? ` ${where}` : '');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
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.
|
2016-05-17 01:01:34 +10:00
|
|
|
truncate() {
|
|
|
|
return `truncate table ${this.tableName}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
forUpdate() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'for update';
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
forShare() {
|
2016-03-02 17:07:05 +01:00
|
|
|
// 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.
|
2016-05-17 01:01:34 +10:00
|
|
|
columnInfo() {
|
|
|
|
const column = this.single.columnInfo;
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
|
|
|
sql: 'select COLUMN_NAME, DATA_TYPE, CHAR_COL_DECL_LENGTH, NULLABLE from USER_TAB_COLS where TABLE_NAME = :1',
|
|
|
|
bindings: [this.single.table],
|
2016-05-17 01:01:34 +10:00
|
|
|
output(resp) {
|
|
|
|
const out = reduce(resp, function(columns, val) {
|
2016-03-02 17:07:05 +01:00
|
|
|
columns[val.COLUMN_NAME] = {
|
|
|
|
type: val.DATA_TYPE,
|
|
|
|
maxLength: val.CHAR_COL_DECL_LENGTH,
|
|
|
|
nullable: (val.NULLABLE === 'Y')
|
|
|
|
};
|
|
|
|
return columns;
|
|
|
|
}, {});
|
|
|
|
return column && out[column] || out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
select() {
|
2016-09-13 12:14:04 +02:00
|
|
|
let query = this.with();
|
2016-05-17 01:01:34 +10:00
|
|
|
const statements = map(components, (component) => {
|
2016-03-02 17:07:05 +01:00
|
|
|
return this[component]();
|
|
|
|
});
|
2016-09-13 12:14:04 +02:00
|
|
|
query += compact(statements).join(' ');
|
2016-03-02 17:07:05 +01:00
|
|
|
return this._surroundQueryWithLimitAndOffset(query);
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
aggregate(stmt) {
|
|
|
|
const val = stmt.value;
|
|
|
|
const splitOn = val.toLowerCase().indexOf(' as ');
|
|
|
|
const distinct = stmt.aggregateDistinct ? 'distinct ' : '';
|
2016-03-02 17:07:05 +01:00
|
|
|
// Allows us to speciy an alias for the aggregate types.
|
|
|
|
if (splitOn !== -1) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const col = val.slice(0, splitOn);
|
|
|
|
const alias = val.slice(splitOn + 4);
|
2016-03-02 17:07:05 +01:00
|
|
|
return stmt.method + '(' + distinct + this.formatter.wrap(col) + ') ' + this.formatter.wrap(alias);
|
|
|
|
}
|
|
|
|
return stmt.method + '(' + distinct + this.formatter.wrap(val) + ')';
|
|
|
|
},
|
|
|
|
|
|
|
|
// for single commands only
|
2016-05-17 01:01:34 +10:00
|
|
|
_addReturningToSqlAndConvert(sql, returning, tableName) {
|
|
|
|
const res = {
|
|
|
|
sql
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!returning) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const returningValues = Array.isArray(returning) ? returning : [returning];
|
|
|
|
const returningHelper = new ReturningHelper(returningValues.join(':'));
|
2016-03-02 17:07:05 +01:00
|
|
|
res.sql = sql + ' returning ROWID into ' + this.formatter.parameter(returningHelper);
|
2016-05-17 01:01:34 +10:00
|
|
|
res.returningSql = `select ${this.formatter.columnize(returning)} from ${tableName} where ROWID = :1`;
|
2016-03-02 17:07:05 +01:00
|
|
|
res.outParams = [returningHelper];
|
|
|
|
res.returning = returning;
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_surroundQueryWithLimitAndOffset(query) {
|
|
|
|
let { limit } = this.single
|
|
|
|
const { offset } = this.single
|
|
|
|
const hasLimit = (limit || limit === 0 || limit === '0');
|
2016-03-02 17:07:05 +01:00
|
|
|
limit = +limit;
|
|
|
|
|
|
|
|
if (!hasLimit && !offset) return query;
|
|
|
|
query = query || "";
|
|
|
|
|
|
|
|
if (hasLimit && !offset) {
|
2016-05-17 01:01:34 +10:00
|
|
|
return `select * from (${query}) where rownum <= ${this.formatter.parameter(limit)}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const endRow = +(offset) + (hasLimit ? limit : 10000000000000);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default QueryCompiler_Oracle;
|