2016-05-17 01:01:34 +10:00
|
|
|
/* eslint max-len:0 */
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Oracle Query Builder & Compiler
|
|
|
|
// ------
|
2020-04-18 20:41:23 +03:00
|
|
|
const compact = require('lodash/compact');
|
|
|
|
const identity = require('lodash/identity');
|
|
|
|
const isEmpty = require('lodash/isEmpty');
|
|
|
|
const isPlainObject = require('lodash/isPlainObject');
|
|
|
|
const reduce = require('lodash/reduce');
|
2021-01-09 17:59:53 +02:00
|
|
|
const QueryCompiler = require('../../../query/querycompiler');
|
2019-06-04 00:37:17 +02:00
|
|
|
const { ReturningHelper } = require('../utils');
|
2020-10-05 21:29:39 +03:00
|
|
|
const { isString } = require('../../../util/is');
|
2016-05-17 01:01:34 +10:00
|
|
|
|
|
|
|
const components = [
|
2023-04-14 06:07:05 +02:00
|
|
|
'comments',
|
2018-07-09 08:10:34 -04:00
|
|
|
'columns',
|
|
|
|
'join',
|
|
|
|
'where',
|
|
|
|
'union',
|
|
|
|
'group',
|
|
|
|
'having',
|
|
|
|
'order',
|
|
|
|
'lock',
|
2016-05-17 01:01:34 +10:00
|
|
|
];
|
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.
|
2020-02-01 21:52:00 +05:30
|
|
|
class QueryCompiler_Oracle extends QueryCompiler {
|
2021-01-07 23:34:46 +02:00
|
|
|
constructor(client, builder, formatter) {
|
|
|
|
super(client, builder, formatter);
|
2019-06-04 00:37:17 +02:00
|
|
|
|
2020-10-31 15:22:52 +00:00
|
|
|
const { onConflict } = this.single;
|
|
|
|
if (onConflict) {
|
|
|
|
throw new Error('.onConflict() is not supported for oracledb.');
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:52:00 +05:30
|
|
|
// 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.
|
|
|
|
this.first = this.select;
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Compiles an "insert" query, allowing for multiple
|
|
|
|
// inserts using a single query statement.
|
2016-05-17 01:01:34 +10:00
|
|
|
insert() {
|
2018-07-09 08:10:34 -04:00
|
|
|
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)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
insertValues = [this.single.insert];
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// always wrap returning argument in array
|
|
|
|
if (returning && !Array.isArray(returning)) {
|
|
|
|
returning = [returning];
|
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (
|
|
|
|
Array.isArray(insertValues) &&
|
|
|
|
insertValues.length === 1 &&
|
|
|
|
isEmpty(insertValues[0])
|
|
|
|
) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (
|
|
|
|
isEmpty(this.single.insert) &&
|
|
|
|
typeof this.single.insert !== 'function'
|
|
|
|
) {
|
2016-03-02 17:07:05 +01:00
|
|
|
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)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this._addReturningToSqlAndConvert(
|
|
|
|
`insert into ${this.tableName} ${insertData}`,
|
|
|
|
returning
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (insertData.values.length === 1) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this._addReturningToSqlAndConvert(
|
|
|
|
`insert into ${this.tableName} (${this.formatter.columnize(
|
|
|
|
insertData.columns
|
2021-02-03 21:17:20 +02:00
|
|
|
)}) values (${this.client.parameterize(
|
|
|
|
insertData.values[0],
|
|
|
|
undefined,
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
|
|
|
)})`,
|
2018-07-09 08:10:34 -04:00
|
|
|
returning,
|
|
|
|
this.tableName
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
const insertDefaultsOnly = insertData.columns.length === 0;
|
|
|
|
|
|
|
|
sql.sql =
|
|
|
|
'begin ' +
|
2020-04-18 20:41:23 +03:00
|
|
|
insertData.values
|
|
|
|
.map((value) => {
|
|
|
|
let returningHelper;
|
|
|
|
const parameterizedValues = !insertDefaultsOnly
|
2021-02-03 21:17:20 +02:00
|
|
|
? this.client.parameterize(
|
|
|
|
value,
|
|
|
|
this.client.valueForUndefined,
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
|
|
|
)
|
2020-04-18 20:41:23 +03:00
|
|
|
: '';
|
|
|
|
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
|
2021-02-04 15:54:26 +02:00
|
|
|
? ` returning ROWID into ${this.client.parameter(
|
|
|
|
returningHelper,
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
2020-12-31 14:38:50 +02:00
|
|
|
)}`
|
2020-04-18 20:41:23 +03:00
|
|
|
: '';
|
|
|
|
|
|
|
|
// 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(' ') +
|
2018-07-09 08:10:34 -04:00
|
|
|
'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
|
2018-07-09 08:10:34 -04:00
|
|
|
sql.returningSql =
|
|
|
|
`select ${this.formatter.columnize(returning)}` +
|
|
|
|
' from ' +
|
|
|
|
this.tableName +
|
|
|
|
' 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;
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// 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;
|
2018-07-09 08:10:34 -04:00
|
|
|
const sql =
|
|
|
|
`update ${this.tableName}` +
|
|
|
|
' 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
|
2018-10-22 13:38:17 +02:00
|
|
|
if (!Array.isArray(returning)) {
|
2016-03-02 17:07:05 +01:00
|
|
|
returning = [returning];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._addReturningToSqlAndConvert(sql, returning, this.tableName);
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Compiles a `truncate` query.
|
2016-05-17 01:01:34 +10:00
|
|
|
truncate() {
|
|
|
|
return `truncate table ${this.tableName}`;
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
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';
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
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
|
2018-07-09 08:10:34 -04:00
|
|
|
this.client.logger.warn(
|
|
|
|
'lock for share is not supported by oracle dialect'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
return '';
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Compiles a `columnInfo` query.
|
2016-05-17 01:01:34 +10:00
|
|
|
columnInfo() {
|
|
|
|
const column = this.single.columnInfo;
|
2018-01-03 00:05:48 +02:00
|
|
|
|
|
|
|
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
|
|
// need to run the identifiers through that function, but not format them as
|
|
|
|
// identifiers otherwise.
|
|
|
|
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
|
|
|
2016-10-14 17:00:39 +02:00
|
|
|
// Node oracle drivers doesn't support LONG type (which is data_default type)
|
|
|
|
const sql = `select * from xmltable( '/ROWSET/ROW'
|
|
|
|
passing dbms_xmlgen.getXMLType('
|
|
|
|
select char_col_decl_length, column_name, data_type, data_default, nullable
|
2020-10-28 17:42:24 -03:00
|
|
|
from all_tab_columns where table_name = ''${table}'' ')
|
2016-10-14 17:00:39 +02:00
|
|
|
columns
|
|
|
|
CHAR_COL_DECL_LENGTH number, COLUMN_NAME varchar2(200), DATA_TYPE varchar2(106),
|
|
|
|
DATA_DEFAULT clob, NULLABLE varchar2(1))`;
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
2016-10-14 17:00:39 +02:00
|
|
|
sql: sql,
|
2016-05-17 01:01:34 +10:00
|
|
|
output(resp) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const out = reduce(
|
|
|
|
resp,
|
2020-04-19 00:40:23 +02:00
|
|
|
function (columns, val) {
|
2018-07-09 08:10:34 -04:00
|
|
|
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;
|
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
select() {
|
2016-09-13 12:14:04 +02:00
|
|
|
let query = this.with();
|
2020-04-18 20:41:23 +03:00
|
|
|
const statements = components.map((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);
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
aggregate(stmt) {
|
2018-03-12 22:42:43 +01:00
|
|
|
return this._aggregate(stmt, { aliasSeparator: ' ' });
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// for single commands only
|
2016-05-17 01:01:34 +10:00
|
|
|
_addReturningToSqlAndConvert(sql, returning, tableName) {
|
|
|
|
const res = {
|
2018-07-09 08:10:34 -04:00
|
|
|
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(':'));
|
2018-07-09 08:10:34 -04:00
|
|
|
res.sql =
|
|
|
|
sql +
|
|
|
|
' returning ROWID into ' +
|
2021-02-04 15:54:26 +02:00
|
|
|
this.client.parameter(returningHelper, this.builder, this.bindingsHolder);
|
2018-07-09 08:10:34 -04: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;
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_surroundQueryWithLimitAndOffset(query) {
|
2018-07-09 08:10:34 -04:00
|
|
|
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;
|
2018-07-09 08:10:34 -04:00
|
|
|
query = query || '';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (hasLimit && !offset) {
|
2021-11-08 10:49:11 +01:00
|
|
|
return `select * from (${query}) where rownum <= ${this._getValueOrParameterFromAttribute(
|
|
|
|
'limit',
|
|
|
|
limit
|
2018-07-09 08:10:34 -04:00
|
|
|
)}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
const endRow = +offset + (hasLimit ? limit : 10000000000000);
|
|
|
|
|
|
|
|
return (
|
|
|
|
'select * from ' +
|
|
|
|
'(select row_.*, ROWNUM rownum_ from (' +
|
|
|
|
query +
|
|
|
|
') row_ ' +
|
|
|
|
'where rownum <= ' +
|
2021-11-08 10:49:11 +01:00
|
|
|
(this.single.skipBinding['offset']
|
|
|
|
? endRow
|
|
|
|
: this.client.parameter(endRow, this.builder, this.bindingsHolder)) +
|
2018-07-09 08:10:34 -04:00
|
|
|
') ' +
|
|
|
|
'where rownum_ > ' +
|
2021-11-08 10:49:11 +01:00
|
|
|
this._getValueOrParameterFromAttribute('offset', offset)
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2020-02-01 21:52:00 +05:30
|
|
|
}
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = QueryCompiler_Oracle;
|