ts-sql-query
Version:
Type-safe SQL query builder like QueryDSL or JOOQ in Java or Linq in .Net for TypeScript with MariaDB, MySql, Oracle, PostgreSql, Sqlite and SqlServer support.
950 lines • 65.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OracleSqlBuilder = void 0;
const SqlBuilder_1 = require("./SqlBuilder");
const TypeAdapter_1 = require("../TypeAdapter");
const values_1 = require("../expressions/values");
const AbstractSqlBuilder_1 = require("./AbstractSqlBuilder");
const Column_1 = require("../utils/Column");
const values_2 = require("../expressions/values");
class OracleSqlBuilder extends AbstractSqlBuilder_1.AbstractSqlBuilder {
constructor() {
super();
this.oracle = true;
this._insertSupportWith = false;
this._supportTableAliasWithAs = false;
this._trueValue = '1';
this._falseValue = '0';
this._trueValueForCondition = '(1=1)';
this._falseValueForCondition = '(0=1)';
this._nullValueForCondition = '(0=null)';
this._operationsThatNeedParenthesis._getTime = true;
this._operationsThatNeedParenthesis._getMonth = true;
this._operationsThatNeedParenthesis._getDay = true;
this._operationsThatNeedParenthesis._negate = true;
}
_getUuidStrategy() {
return this._connectionConfiguration.uuidStrategy || 'custom-functions';
}
_isReservedKeyword(word) {
return word.toUpperCase() in reservedWords;
}
_nextSequenceValue(_params, sequenceName) {
return this._escape(sequenceName, false) + '.nextval';
}
_currentSequenceValue(_params, sequenceName) {
return this._escape(sequenceName, false) + '.currval';
}
_fromNoTable() {
return ' from dual';
}
_appendSql(value, params) {
if ((0, values_1.isValueSource)(value) && !(0, Column_1.isColumn)(value)) {
const valueSourcePrivate = (0, values_2.__getValueSourcePrivate)(value);
if (valueSourcePrivate.__isBooleanForCondition) {
if (valueSourcePrivate.__optionalType === 'required') {
return 'case when ' + super._appendConditionSql(value, params) + ' then 1 else 0 end';
}
else {
return 'case when ' + super._appendConditionSql(value, params) + ' then 1 when not ' + super._appendConditionSqlParenthesis(value, params) + ' then 0 else null end';
}
}
}
return super._appendSql(value, params);
}
_appendConditionSql(value, params) {
if ((0, values_1.isValueSource)(value) && !(0, Column_1.isColumn)(value) && (0, SqlBuilder_1.hasToSql)(value)) {
const valueSourcePrivate = (0, values_2.__getValueSourcePrivate)(value);
if ((0, values_1.__isBooleanValueSource)(valueSourcePrivate) && !valueSourcePrivate.__isBooleanForCondition) {
const sql = value.__toSqlForCondition(this, params);
if (!sql || sql === this._trueValueForCondition || sql === this._falseValueForCondition) {
return sql;
}
else {
return '(' + sql + ' = 1)';
}
}
}
return super._appendConditionSql(value, params);
}
_appendConditionParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast) {
if ((0, values_1.__isBooleanValueType)(columnType)) {
if ((0, Column_1.isColumn)(value)) {
const columnPrivate = (0, Column_1.__getColumnPrivate)(value);
const typeAdapter = columnPrivate.__typeAdapter;
if (typeAdapter instanceof TypeAdapter_1.CustomBooleanTypeAdapter) {
return '(' + this._appendRawColumnName(value, params) + ' = ' + this._appendLiteralValue(typeAdapter.trueValue, params) + ')';
}
}
return '(' + this._appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast) + ' = 1)';
}
return this._appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast);
}
_appendColumnName(column, params) {
const columnPrivate = (0, Column_1.__getColumnPrivate)(column);
const typeAdapter = columnPrivate.__typeAdapter;
if ((0, values_1.__isBooleanValueSource)(columnPrivate)) {
if (typeAdapter instanceof TypeAdapter_1.CustomBooleanTypeAdapter) {
if (columnPrivate.__optionalType === 'required') {
return 'case when ' + this._appendRawColumnName(column, params) + ' = ' + this._appendLiteralValue(typeAdapter.trueValue, params) + ' then 1 else 0 end';
}
else {
return 'case ' + this._appendRawColumnName(column, params) + ' when ' + this._appendLiteralValue(typeAdapter.trueValue, params) + ' then 1 when ' + this._appendLiteralValue(typeAdapter.falseValue, params) + ' then 0 else null end';
}
}
}
return this._appendRawColumnName(column, params);
}
_appendColumnNameForCondition(column, params) {
const columnPrivate = (0, Column_1.__getColumnPrivate)(column);
const typeAdapter = columnPrivate.__typeAdapter;
if ((0, values_1.__isBooleanValueSource)(columnPrivate)) {
if (typeAdapter instanceof TypeAdapter_1.CustomBooleanTypeAdapter) {
return '(' + this._appendRawColumnName(column, params) + ' = ' + this._appendLiteralValue(typeAdapter.trueValue, params) + ')';
}
else {
return '(' + this._appendRawColumnName(column, params) + ' = 1)';
}
}
return this._appendRawColumnName(column, params);
}
_inlineSelectAsValueForCondition(query, params) {
if (query.__oneColumn) {
const columns = query.__columns;
for (const prop in columns) {
const column = columns[prop];
if ((0, values_1.isValueSource)(column) && (0, values_1.__isBooleanValueSource)((0, values_2.__getValueSourcePrivate)(column))) {
return '((' + this._buildInlineSelect(query, params) + ') = 1)';
}
else {
return this._buildInlineSelect(query, params);
}
}
}
return this._buildInlineSelect(query, params);
}
_appendCustomBooleanRemapForColumnIfRequired(column, value, params) {
const columnPrivate = (0, Column_1.__getColumnPrivate)(column);
const columnTypeAdapter = columnPrivate.__typeAdapter;
const columnTypeName = columnPrivate.__valueTypeName;
const columnType = columnPrivate.__valueType;
if (!(0, values_1.__isBooleanValueSource)(columnPrivate)) {
return null; // non-boolean
}
if (columnTypeAdapter instanceof TypeAdapter_1.CustomBooleanTypeAdapter) {
if ((0, Column_1.isColumn)(value)) {
const valuePrivate = (0, Column_1.__getColumnPrivate)(value);
const valueTypeAdapter = valuePrivate.__typeAdapter;
if (valueTypeAdapter instanceof TypeAdapter_1.CustomBooleanTypeAdapter) {
if (columnTypeAdapter.trueValue === valueTypeAdapter.trueValue && columnTypeAdapter.falseValue === valueTypeAdapter.falseValue) {
return this._appendRawColumnName(value, params); // same boolean as column
}
if (columnPrivate.__optionalType === 'required') {
// remapped
return 'case when ' + this._appendRawColumnName(value, params) + ' = ' + this._appendLiteralValue(valueTypeAdapter.trueValue, params) + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' else ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' end';
}
else {
// remapped
return 'case ' + this._appendRawColumnName(value, params) + ' when ' + this._appendLiteralValue(valueTypeAdapter.trueValue, params) + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' when ' + this._appendLiteralValue(valueTypeAdapter.falseValue, params) + ' then ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' else null end';
}
}
else {
if (columnPrivate.__optionalType === 'required') {
// remapped
return 'case when ' + this._appendConditionValue(value, params, columnType, columnTypeName, columnTypeAdapter) + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' else ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' end';
}
else {
// remapped
return 'case ' + this._appendValue(value, params, columnType, columnTypeName, columnTypeAdapter) + ' when ' + this._trueValue + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' when ' + this._falseValue + ' then ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' else null end';
}
}
}
else if ((0, values_1.isValueSource)(value)) {
// There are some boolean expressions involved
if (columnPrivate.__optionalType === 'required') {
// remapped
return 'case when ' + this._appendConditionValue(value, params, columnType, columnTypeName, columnTypeAdapter) + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' else ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' end';
}
else {
// remapped
return 'case when ' + this._appendConditionValue(value, params, columnType, columnTypeName, columnTypeAdapter) + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' when not ' + this._appendConditionValueParenthesis(value, params, columnType, columnTypeName, columnTypeAdapter) + ' then ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' else null end';
}
}
else {
if (columnPrivate.__optionalType === 'required') {
// remapped
return 'case when ' + this._appendConditionValue(value, params, columnType, columnTypeName, columnTypeAdapter) + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' else ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' end';
}
else if (value === true || value === false) {
// remapped
return 'case ' + this._appendValue(value, params, columnType, columnTypeName, columnTypeAdapter) + ' when ' + this._trueValue + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' when ' + this._falseValue + ' then ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' else null end';
}
else {
// remapped
// Oracle detects wrongly the value type in this case (when value is null), for this reason a cast in injected
return 'case cast(' + this._appendValue(value, params, columnType, columnTypeName, columnTypeAdapter) + ' as number) when ' + this._trueValue + ' then ' + this._appendLiteralValue(columnTypeAdapter.trueValue, params) + ' when ' + this._falseValue + ' then ' + this._appendLiteralValue(columnTypeAdapter.falseValue, params) + ' else null end';
}
}
}
// if value is column and its type adapter is CustomBooleanTypeAdapter append value will be required to normalize value
// if not it is same boolean, nothing to transform here
return null;
}
_appendWithColumns(withData, params) {
if (withData.__selectData.__type === 'plain') {
return '';
}
const columns = {};
(0, SqlBuilder_1.flattenQueryColumns)(withData.__selectData.__columns, columns, '');
let result = '';
for (const property in columns) {
if (result) {
result += ', ';
}
result += this._appendColumnAlias(property, params);
}
return '(' + result + ')';
}
_appendWithKeyword(_recursive) {
// Oracle doesn't uses the recursive keyword
return 'with';
}
_appendColumnAlias(name, params) {
if (!this._isWithGeneratedFinished(params)) {
// Avoid quote identifiers when the with clause is generated
return this._escape(name, true);
}
if (this._isAggregateArrayWrapped(params)) {
// Avoid quote identifiers when the aggregate array query wrapper is generated
return this._escape(name, true);
}
// Avoid automatically uppercase identifiers by oracle
return this._forceAsIdentifier(name);
}
_appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast) {
if ((0, values_1.__isUuidValueType)(columnType) && this._getUuidStrategy() === 'custom-functions') {
return 'uuid_to_raw(' + super._appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast) + ')';
}
return super._appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast);
}
_appendColumnValue(value, params, isOutermostQuery) {
if (isOutermostQuery && this._getUuidStrategy() === 'custom-functions') {
if ((0, values_1.__isUuidValueSource)((0, values_2.__getValueSourcePrivate)(value))) {
return 'raw_to_uuid(' + this._appendSql(value, params) + ')';
}
}
return this._appendSql(value, params);
}
_asString(params, valueSource) {
// Transform an uuid to string
if (this._getUuidStrategy() === 'string') {
// No conversion required
return this._appendSql(valueSource, params);
}
return 'raw_to_uuid(' + this._appendSql(valueSource, params) + ')';
}
_appendCompoundOperator(compoundOperator, _params) {
switch (compoundOperator) {
case 'union':
return ' union ';
case 'unionAll':
return ' union all ';
case 'intersect':
return ' intersect ';
case 'intersectAll':
return ' intersect all ';
case 'except':
return ' minus ';
case 'exceptAll':
return ' minus all ';
case 'minus':
return ' except ';
case 'minusAll':
return ' except all ';
default:
throw new Error('Invalid compound operator: ' + compoundOperator);
}
}
_buildSelectWithColumnsInfoForCompound(query, params, columnsForInsert, isOutermostQuery) {
const result = this._buildSelectWithColumnsInfo(query, params, columnsForInsert, isOutermostQuery);
if (query.__limit !== undefined || query.__offset !== undefined || query.__orderBy || query.__customization?.beforeOrderByItems || query.__customization?.afterOrderByItems) {
return 'select * from (' + result + ')';
}
return result;
}
_buildSelectOrderBy(query, params) {
if (query.__type === 'plain') {
return super._buildSelectOrderBy(query, params);
}
const orderBy = query.__orderBy;
if (!orderBy) {
let orderByColumns = '';
const customization = query.__customization;
if (customization && customization.beforeOrderByItems) {
orderByColumns += this._appendRawFragment(customization.beforeOrderByItems, params);
}
if (customization && customization.afterOrderByItems) {
if (orderByColumns) {
orderByColumns += ', ';
}
orderByColumns += this._appendRawFragment(customization.afterOrderByItems, params);
}
if (!orderByColumns) {
return '';
}
if (query.__orderingSiblingsOnly) {
// Oracle recursive
return ' order siblings by ' + orderByColumns;
}
else {
return ' order by ' + orderByColumns;
}
}
const columns = {};
(0, SqlBuilder_1.flattenQueryColumns)(query.__columns, columns, '');
const columnNames = Object.getOwnPropertyNames(columns);
let orderByColumns = '';
const customization = query.__customization;
if (customization && customization.beforeOrderByItems) {
orderByColumns += this._appendRawFragment(customization.beforeOrderByItems, params);
}
for (const entry of orderBy) {
if (orderByColumns) {
orderByColumns += ', ';
}
const order = entry.order;
if (!order) {
orderByColumns += this._appendCompoundOrderByColumnAlias(entry, columnNames, query, params);
}
else
switch (order) {
case 'asc':
case 'desc':
case 'asc nulls first':
case 'asc nulls last':
case 'desc nulls first':
case 'desc nulls last':
orderByColumns += this._appendCompoundOrderByColumnAlias(entry, columnNames, query, params) + ' ' + order;
break;
case 'insensitive':
case 'asc insensitive':
case 'desc insensitive':
case 'asc nulls first insensitive':
case 'asc nulls last insensitive':
case 'desc nulls first insensitive':
case 'desc nulls last insensitive': {
let sqlOrder = order.substring(0, order.length - 12);
if (sqlOrder) {
sqlOrder = ' ' + sqlOrder;
}
orderByColumns += this._appendCompoundOrderByColumnAliasInsensitive(entry, columnNames, query, params) + sqlOrder;
break;
}
default:
throw new Error('Invalid order by: ' + order);
}
}
if (customization && customization.afterOrderByItems) {
if (orderByColumns) {
orderByColumns += ', ';
}
orderByColumns += this._appendRawFragment(customization.afterOrderByItems, params);
}
if (!orderByColumns) {
return '';
}
if (query.__orderingSiblingsOnly) {
// Oracle recursive
return ' order siblings by ' + orderByColumns;
}
else {
return ' order by ' + orderByColumns;
}
}
_appendCompoundOrderByColumnAlias(entry, columnNames, query, params) {
const expression = entry.expression;
const columns = query.__columns;
if (typeof expression === 'string') {
const column = (0, SqlBuilder_1.getQueryColumn)(columns, expression);
if (!column) {
throw new Error('Column ' + expression + ' included in the order by not found in the select clause');
}
return (columnNames.indexOf(expression) + 1) + '';
}
else if ((0, values_1.isValueSource)(expression)) {
const oldSafeTableOrView = this._getSafeTableOrView(params);
this._setSafeTableOrView(params, undefined);
const result = this._appendSql(expression, params);
this._setSafeTableOrView(params, oldSafeTableOrView);
return result;
}
else {
const oldSafeTableOrView = this._getSafeTableOrView(params);
this._setSafeTableOrView(params, undefined);
const result = this._appendRawFragment(expression, params);
this._setSafeTableOrView(params, oldSafeTableOrView);
return result;
}
}
_appendCompoundOrderByColumnAliasInsensitive(entry, columnNames, query, params) {
const collation = this._connectionConfiguration.insesitiveCollation;
const stringColumn = this._isStringOrderByColumn(entry, query);
if (!stringColumn) {
// Ignore the insensitive term, it do nothing
return this._appendCompoundOrderByColumnAlias(entry, columnNames, query, params);
}
else if (collation) {
return this._appendCompoundOrderByColumnAlias(entry, columnNames, query, params) + ' collate ' + collation;
}
else if (collation === '') {
return this._appendCompoundOrderByColumnAlias(entry, columnNames, query, params);
}
else {
return 'lower(' + this._appendCompoundOrderByColumnAlias(entry, columnNames, query, params) + ')';
}
}
_buildSelectLimitOffset(query, params) {
let result = super._buildSelectLimitOffset(query, params);
if (!result && this._isAggregateArrayWrapped(params) && (query.__orderBy || query.__customization?.beforeOrderByItems || query.__customization?.afterOrderByItems)) {
result += ' offset 0 rows'; // Workaround to force oracle to order the result (if not the order by is ignored)
}
return result;
}
_buildInsertMultiple(query, params) {
const multiple = query.__multiple;
if (!multiple) {
throw new Error('Exepected a multiple insert');
}
if (multiple.length <= 0) {
return '';
}
this._ensureRootQuery(query, params);
const table = query.__table;
const oldSafeTableOrView = this._getSafeTableOrView(params);
const oldFakeNameOf = this._getFakeNamesOf(params);
this._setFakeNamesOf(params, undefined);
const customization = query.__customization;
this._setSafeTableOrView(params, table);
const returning = !!query.__idColumn || !!query.__columns;
let insertQuery = '';
if (this._insertSupportWith) {
insertQuery += this._buildWith(query, params);
}
if (returning) {
insertQuery += 'begin ';
}
else {
insertQuery = 'insert ';
if (customization && customization.afterInsertKeyword) {
insertQuery += this._appendRawFragment(customization.afterInsertKeyword, params) + ' ';
}
insertQuery += 'all';
}
const shape = query.__shape;
let columnsInShape;
if (shape) {
columnsInShape = {};
for (let property in shape) {
const colunName = shape[property];
if (typeof colunName === 'string') {
columnsInShape[colunName] = true;
}
}
}
for (let i = 0, length = multiple.length; i < length; i++) {
if (customization && customization.beforeQuery) {
insertQuery += this._appendRawFragment(customization.beforeQuery, params) + ' ';
}
if (returning) {
insertQuery += 'insert ';
if (customization && customization.afterInsertKeyword) {
insertQuery += this._appendRawFragment(customization.afterInsertKeyword, params) + ' ';
}
}
insertQuery += this._buildInsertOnConflictBeforeInto(query, params);
insertQuery += 'into ';
insertQuery += this._appendTableOrViewName(table, params);
const sets = multiple[i];
let columns = '';
const sequences = [];
for (var columnName in table) {
if (columnsInShape) {
if (columnName in columnsInShape) {
continue;
}
}
else {
if (columnName in sets) {
continue;
}
}
const column = (0, Column_1.__getColumnOfObject)(table, columnName);
if (!column) {
continue;
}
const columnPrivate = (0, Column_1.__getColumnPrivate)(column);
if (!columnPrivate.__sequenceName) {
continue;
}
if (columns) {
columns += ', ';
}
columns += this._appendSql(column, params);
sequences.push(columnPrivate.__sequenceName);
}
if (shape) { // Follow shape order
const properties = Object.getOwnPropertyNames(shape);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
const columnName = shape[property];
const column = (0, Column_1.__getColumnOfObject)(table, columnName);
if (!column) {
// Additional property provided in the value object
// Skipped because it is not part of the table
// This allows to have more complex objects used in the query
continue;
}
if (!(property in sets)) {
// No value set for that property in the shape
continue;
}
if (columns) {
columns += ', ';
}
columns += this._appendRawColumnName(column, params);
}
}
else { // No shape, follow set order
const properties = Object.getOwnPropertyNames(sets);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
const column = (0, Column_1.__getColumnOfObject)(table, property);
if (!column) {
// Additional property provided in the value object
// Skipped because it is not part of the table
// This allows to have more complex objects used in the query
continue;
}
if (columns) {
columns += ', ';
}
columns += this._appendRawColumnName(column, params);
}
}
insertQuery += ' (' + columns + ')';
insertQuery += this._buildInsertOutput(query, params);
let values = '';
for (let i = 0, length = sequences.length; i < length; i++) {
const sequenceName = sequences[i];
if (values) {
values += ', ';
}
values += this._nextSequenceValue(params, sequenceName);
}
if (shape) { // Follow shape order
const properties = Object.getOwnPropertyNames(shape);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
const columnName = shape[property];
const column = (0, Column_1.__getColumnOfObject)(table, columnName);
if (!column) {
// Additional property provided in the value object
// Skipped because it is not part of the table
// This allows to have more complex objects used in the query
continue;
}
if (!(property in sets)) {
// No value set for that property in the shape
continue;
}
if (values) {
values += ', ';
}
const value = sets[property];
values += this._appendValueForColumn(column, value, params);
}
}
else { // No shape, follow set order
const properties = Object.getOwnPropertyNames(sets);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
const column = (0, Column_1.__getColumnOfObject)(table, property);
if (!column) {
// Additional property provided in the value object
// Skipped because it is not part of the table
// This allows to have more complex objects used in the query
continue;
}
if (values) {
values += ', ';
}
const value = sets[property];
values += this._appendValueForColumn(column, value, params);
}
}
insertQuery += ' values (' + values + ')';
insertQuery += this._buildInsertOnConflictBeforeReturning(query, params);
insertQuery += this._buildInsertReturning(query, params);
if (customization && customization.afterQuery) {
insertQuery += ' ' + this._appendRawFragment(customization.afterQuery, params);
}
if (returning) {
insertQuery += '; ';
}
}
this._setSafeTableOrView(params, oldSafeTableOrView);
this._setFakeNamesOf(params, oldFakeNameOf);
this._resetRootQuery(query, params);
if (returning) {
insertQuery += 'end;';
}
else {
insertQuery += ' select ' + multiple.length + ' from dual';
}
return insertQuery;
}
_buildInsertDefaultValues(query, params) {
this._ensureRootQuery(query, params);
const oldSafeTableOrView = this._getSafeTableOrView(params);
const oldFakeNameOf = this._getFakeNamesOf(params);
this._setFakeNamesOf(params, undefined);
const table = query.__table;
const customization = query.__customization;
this._setSafeTableOrView(params, table);
let insertQuery = '';
if (customization && customization.beforeQuery) {
insertQuery += this._appendRawFragment(customization.beforeQuery, params) + ' ';
}
if (this._insertSupportWith) {
insertQuery += this._buildWith(query, params);
}
insertQuery += 'insert ';
if (customization && customization.afterInsertKeyword) {
insertQuery += this._appendRawFragment(customization.afterInsertKeyword, params) + ' ';
}
insertQuery += this._buildInsertOnConflictBeforeInto(query, params);
insertQuery += 'into ';
insertQuery += this._appendTableOrViewName(table, params);
let columns = '';
for (var columnName in table) {
const column = (0, Column_1.__getColumnOfObject)(table, columnName);
if (!column) {
continue;
}
if (columns) {
columns += ', ';
}
columns += this._appendRawColumnName(column, params);
}
insertQuery += ' (' + columns + ')';
insertQuery += this._buildInsertOutput(query, params);
let values = '';
for (var columnName in table) {
const column = (0, Column_1.__getColumnOfObject)(table, columnName);
if (!column) {
continue;
}
if (values) {
values += ', ';
}
const columnPrivate = (0, Column_1.__getColumnPrivate)(column);
if (columnPrivate.__sequenceName) {
values += this._nextSequenceValue(params, columnPrivate.__sequenceName);
}
else {
values += 'default';
}
}
insertQuery += ' values (' + values + ')';
insertQuery += this._buildInsertOnConflictBeforeReturning(query, params);
insertQuery += this._buildInsertReturning(query, params);
if (customization && customization.afterQuery) {
insertQuery += ' ' + this._appendRawFragment(customization.afterQuery, params);
}
this._setSafeTableOrView(params, oldSafeTableOrView);
this._setFakeNamesOf(params, oldFakeNameOf);
this._resetRootQuery(query, params);
return insertQuery;
}
_buildInsertOutput(_query, _params) {
return '';
}
_buildInsertReturning(query, params) {
const idColumn = query.__idColumn;
if (idColumn) {
this._setContainsInsertReturningClause(params, true);
return ' returning ' + this._appendSql(idColumn, params) + ' into ' + this._queryRunner.addOutParam(params, ''); // Empty name for the out params, no special name is requiered
}
const result = this._buildQueryReturning(query.__columns, params);
this._setContainsInsertReturningClause(params, !!result);
return result;
}
_buildQueryReturning(queryColumns, params) {
if (!queryColumns) {
return '';
}
const columns = {};
(0, SqlBuilder_1.flattenQueryColumns)(queryColumns, columns, '');
let requireComma = false;
let result = '';
for (const property in columns) {
if (requireComma) {
result += ', ';
}
result += this._appendSql(columns[property], params);
requireComma = true;
}
if (result) {
result += ' into ';
}
requireComma = false;
for (const property in columns) {
if (requireComma) {
result += ', ';
}
result += this._queryRunner.addOutParam(params, property);
requireComma = true;
}
if (!result) {
return '';
}
return ' returning ' + result;
}
_isNull(params, valueSource) {
if ((0, Column_1.isColumn)(valueSource)) {
this._appendRawColumnName(valueSource, params) + ' is null';
}
if ((0, values_1.isValueSource)(valueSource)) {
const valueSourcePrivate = (0, values_2.__getValueSourcePrivate)(valueSource);
if (valueSourcePrivate.__isBooleanForCondition) {
if (valueSourcePrivate.__optionalType === 'required') {
return this._falseValueForCondition;
}
else {
// This is a boolean value (0 or 1 from a column) that need to be use in a boolean expression
return '(case when ' + this._appendConditionSql(valueSource, params) + ' then 0 when not ' + this._appendConditionSqlParenthesis(valueSource, params) + ' then 0 else 1 end = 1)';
}
}
}
return this._appendSqlParenthesis(valueSource, params) + ' is null';
}
_isNotNull(params, valueSource) {
if ((0, Column_1.isColumn)(valueSource)) {
this._appendRawColumnName(valueSource, params) + ' is not null';
}
if ((0, values_1.isValueSource)(valueSource)) {
const valueSourcePrivate = (0, values_2.__getValueSourcePrivate)(valueSource);
if (valueSourcePrivate.__isBooleanForCondition) {
if (valueSourcePrivate.__optionalType === 'required') {
return this._trueValueForCondition;
}
else {
// This is a boolean value (0 or 1 from a column) that need to be use in a boolean expression
return '(case when ' + this._appendConditionSql(valueSource, params) + ' then 1 when not ' + this._appendConditionSqlParenthesis(valueSource, params) + ' then 1 else 0 end = 1)';
}
}
}
return this._appendSqlParenthesis(valueSource, params) + ' is not null';
}
_is(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
if ((0, Column_1.isColumn)(valueSource) && (0, Column_1.isColumn)(value) && this._hasSameBooleanTypeAdapter(valueSource, value)) {
return 'decode(' + this._appendRawColumnName(valueSource, params) + ', ' + this._appendRawColumnName(value, params) + ', 1, 0 ) = 1';
}
return 'decode(' + this._appendSqlParenthesis(valueSource, params) + ', ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + ', 1, 0 ) = 1';
}
_isNot(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
if ((0, Column_1.isColumn)(valueSource) && (0, Column_1.isColumn)(value) && this._hasSameBooleanTypeAdapter(valueSource, value)) {
return 'decode(' + this._appendRawColumnName(valueSource, params) + ', ' + this._appendRawColumnName(value, params) + ', 1, 0 ) = 0';
}
return 'decode(' + this._appendSqlParenthesis(valueSource, params) + ', ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + ', 1, 0 ) = 0';
}
_random(_params) {
return '(select dbms_random.value from dual)';
}
_currentDate(_params) {
return 'trunc(current_timestamp)';
}
_valueWhenNull(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return 'nvl(' + this._appendSql(valueSource, params) + ', ' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + ')';
}
_divide(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return this._appendSqlParenthesis(valueSource, params) + ' / ' + this._appendValueParenthesis(value, params, this._getMathArgumentType(columnType, columnTypeName, value), this._getMathArgumentTypeName(columnType, columnTypeName, value), typeAdapter);
}
_asDouble(params, valueSource) {
return 'cast(' + this._appendSql(valueSource, params) + ' as float)';
}
_log10(params, valueSource) {
return 'log(' + this._appendSql(valueSource, params) + ', 10)';
}
_cbrt(params, valueSource) {
return 'power(' + this._appendSql(valueSource, params) + ', 3)';
}
_getDate(params, valueSource) {
return 'extract(day from ' + this._appendSql(valueSource, params) + ')';
}
_getTime(params, valueSource) {
return "extract(day from(sys_extract_utc(" + this._appendSql(valueSource, params) + ") - to_timestamp('1970-01-01', 'YYYY-MM-DD'))) * 86400000 + to_number(to_char(sys_extract_utc(" + this._appendSql(valueSource, params) + "), 'SSSSSFF3'))";
}
_getFullYear(params, valueSource) {
return 'extract(year from ' + this._appendSql(valueSource, params) + ')';
}
_getMonth(params, valueSource) {
return 'extract(month from ' + this._appendSql(valueSource, params) + ') - 1';
}
_getDay(params, valueSource) {
// https://riptutorial.com/oracle/example/31649/getting-the-day-of-the-week
return "mod(trunc(" + this._appendSql(valueSource, params) + ") - trunc(" + this._appendSql(valueSource, params) + ", 'IW') + 1, 7)";
}
_getHours(params, valueSource) {
return 'extract(hour from ' + this._appendSql(valueSource, params) + ')';
}
_getMinutes(params, valueSource) {
return 'extract(minute from ' + this._appendSql(valueSource, params) + ')';
}
_getSeconds(params, valueSource) {
return 'trunc(extract(second from ' + this._appendSql(valueSource, params) + '))';
}
_getMilliseconds(params, valueSource) {
return "to_number(to_char(" + this._appendSql(valueSource, params) + ", 'FF3'))";
}
_buildCallProcedure(params, procedureName, procedureParams) {
let result = 'begin ' + this._escape(procedureName, false) + '(';
if (procedureParams.length > 0) {
result += this._appendSql(procedureParams[0], params);
for (let i = 1, length = procedureParams.length; i < length; i++) {
result += ', ' + this._appendSql(procedureParams[i], params);
}
}
return result + '); end;';
}
_buildCallFunction(params, functionName, functionParams) {
let result = 'select ' + this._escape(functionName, false) + '(';
if (functionParams.length > 0) {
result += this._appendSql(functionParams[0], params);
for (let i = 1, length = functionParams.length; i < length; i++) {
result += ', ' + this._appendSql(functionParams[i], params);
}
}
return result + ') from dual';
}
_stringConcat(params, separator, value) {
if (separator === undefined || separator === null) {
return 'listagg(' + this._appendSql(value, params) + ", ',') within group (order by" + this._appendSql(value, params) + ')';
}
else if (separator === '') {
return 'listagg(' + this._appendSql(value, params) + ') within group (order by' + this._appendSql(value, params) + ')';
}
else {
return 'listagg(' + this._appendSql(value, params) + ', ' + this._appendValue(separator, params, 'string', 'string', undefined) + ') within group (order by' + this._appendSql(value, params) + ')';
}
}
_stringConcatDistinct(params, separator, value) {
if (separator === undefined || separator === null) {
return 'listagg(distinct ' + this._appendSql(value, params) + ", ',') within group (order by" + this._appendSql(value, params) + ')';
}
else if (separator === '') {
return 'listagg(distinct ' + this._appendSql(value, params) + ') within group (order by' + this._appendSql(value, params) + ')';
}
else {
return 'listagg(distinct ' + this._appendSql(value, params) + ', ' + this._appendValue(separator, params, 'string', 'string', undefined) + ') within group (order by' + this._appendSql(value, params) + ')';
}
}
_escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) {
if (typeof value === 'string') {
value = value.replace(/\[/g, '[[]');
value = value.replace(/%/g, '[%]');
value = value.replace(/_/g, '[]');
return this._appendValue(value, params, columnType, columnTypeName, typeAdapter);
}
else {
return "replace(replace(replace(" + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + ", '[', '[[]'), '%', '[%]'), '_', '[]')";
}
}
_like(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return this._appendSqlParenthesis(valueSource, params) + ' like ' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'";
}
_notLike(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return this._appendSqlParenthesis(valueSource, params) + ' not like ' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'";
}
_likeInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
const collation = this._connectionConfiguration.insesitiveCollation;
if (collation) {
return this._appendSqlParenthesis(valueSource, params) + ' like (' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + ' collate ' + collation + ") escape '\\'";
}
else if (collation === '') {
return this._appendSqlParenthesis(valueSource, params) + ' like ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'";
}
else {
return 'lower(' + this._appendSql(valueSource, params) + ') like lower(' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + ") escape '\\'";
}
}
_notLikeInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
const collation = this._connectionConfiguration.insesitiveCollation;
if (collation) {
return this._appendSqlParenthesis(valueSource, params) + ' not like (' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + ' collate ' + collation + ") escape '\\'";
}
else if (collation === '') {
return this._appendSqlParenthesis(valueSource, params) + ' not like ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'";
}
else {
return 'lower(' + this._appendSql(valueSource, params) + ') not like lower(' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + ") escape '\\'";
}
}
_startsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return this._appendSqlParenthesis(valueSource, params) + ' like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'";
}
_notStartsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return this._appendSqlParenthesis(valueSource, params) + ' not like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'";
}
_endsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return this._appendSqlParenthesis(valueSource, params) + " like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'";
}
_notEndsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
return this._appendSqlParenthesis(valueSource, params) + " not like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'";
}
_startsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
const collation = this._connectionConfiguration.insesitiveCollation;
if (collation) {
return this._appendSqlParenthesis(valueSource, params) + ' like ((' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') collate " + collation + ") escape '\\'";
}
else if (collation === '') {
return this._appendSqlParenthesis(valueSource, params) + ' like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'";
}
else {
return 'lower(' + this._appendSql(valueSource, params) + ') like lower(' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'";
}
}
_notStartsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
const collation = this._connectionConfiguration.insesitiveCollation;
if (collation) {
return this._appendSqlParenthesis(valueSource, params) + ' not like ((' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') collate " + collation + ") escape '\\'";
}
else if (collation === '') {
return this._appendSqlParenthesis(valueSource, params) + ' not like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'";
}
else {
return 'lower(' + this._appendSql(valueSource, params) + ') not like lower(' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'";
}
}
_endsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
const collation = this._connectionConfiguration.insesitiveCollation;
if (collation) {
return this._appendSqlParenthesis(valueSource, params) + " like (('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") collate " + collation + ") escape '\\'";
}
else if (collation === '') {
return this._appendSqlParenthesis(valueSource, params) + " like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'";
}
else {
return 'lower(' + this._appendSql(valueSource, params) + ") like lower('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'";
}
}
_notEndsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) {
const collation = this._connectionConfiguration.insesitiveCollation;
if (collation) {
return this._appendSqlParenthesis(valueSource, params) + " not like (('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") collate " + collation + ") escape '\\'";
}
else if (collation === '') {
return this._appendSqlParenthesis(valueSource, params) + " not like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'";
}
else {
return 'lower(' + this._appendSql(valueSource, params) + ") not like lower('%' || " + this._escap