2016-05-17 01:01:34 +10:00
|
|
|
/* eslint max-len:0 */
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
const utils = require('../utils');
|
|
|
|
const TableCompiler = require('../../../schema/tablecompiler');
|
2020-12-28 16:55:08 +02:00
|
|
|
const helpers = require('../../../util/helpers');
|
2021-01-06 23:21:10 +02:00
|
|
|
const Trigger = require('./internal/trigger');
|
2021-07-25 17:23:17 +10:00
|
|
|
const { isObject } = require('../../../util/is');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Table Compiler
|
|
|
|
// ------
|
|
|
|
|
2021-01-01 18:46:16 +02:00
|
|
|
class TableCompiler_Oracle extends TableCompiler {
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2017-06-10 20:01:57 +02:00
|
|
|
addColumns(columns, prefix) {
|
|
|
|
if (columns.sql.length > 0) {
|
|
|
|
prefix = prefix || this.addColumnsPrefix;
|
|
|
|
|
2020-04-18 20:41:23 +03:00
|
|
|
const columnSql = columns.sql;
|
2017-06-10 20:01:57 +02:00
|
|
|
const alter = this.lowerCase ? 'alter table ' : 'ALTER TABLE ';
|
|
|
|
|
|
|
|
let sql = `${alter}${this.tableName()} ${prefix}`;
|
|
|
|
if (columns.sql.length > 1) {
|
|
|
|
sql += `(${columnSql.join(', ')})`;
|
|
|
|
} else {
|
|
|
|
sql += columnSql.join(', ');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pushQuery({
|
|
|
|
sql,
|
|
|
|
bindings: columns.bindings,
|
|
|
|
});
|
|
|
|
}
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2017-06-10 20:01:57 +02:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// Compile a rename column command.
|
2016-05-17 01:01:34 +10:00
|
|
|
renameColumn(from, to) {
|
2016-10-14 17:00:39 +02:00
|
|
|
// Remove quotes around tableName
|
2018-07-09 08:10:34 -04:00
|
|
|
const tableName = this.tableName().slice(1, -1);
|
2023-10-16 17:31:38 -06:00
|
|
|
const trigger = new Trigger(this.client.version);
|
2018-07-09 08:10:34 -04:00
|
|
|
return this.pushQuery(
|
2023-10-16 17:31:38 -06:00
|
|
|
trigger.renameColumnTrigger(this.client.logger, tableName, from, to)
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
compileAdd(builder) {
|
|
|
|
const table = this.formatter.wrap(builder);
|
|
|
|
const columns = this.prefixArray('add column', this.getColumns(builder));
|
2016-03-02 17:07:05 +01:00
|
|
|
return this.pushQuery({
|
2018-07-09 08:10:34 -04:00
|
|
|
sql: `alter table ${table} ${columns.join(', ')}`,
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Adds the "create" query to the query sequence.
|
2021-10-15 15:57:46 +02:00
|
|
|
createQuery(columns, ifNot, like) {
|
|
|
|
const columnsSql =
|
|
|
|
like && this.tableNameLike()
|
|
|
|
? ' as (select * from ' + this.tableNameLike() + ' where 0=1)'
|
2022-01-06 14:44:16 +01:00
|
|
|
: ' (' + columns.sql.join(', ') + this._addChecks() + ')';
|
2021-10-15 15:57:46 +02:00
|
|
|
const sql = `create table ${this.tableName()}${columnsSql}`;
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
this.pushQuery({
|
|
|
|
// catch "name is already used by an existing object" for workaround for "if not exists"
|
|
|
|
sql: ifNot ? utils.wrapSqlWithCatch(sql, -955) : sql,
|
2018-07-09 08:10:34 -04:00
|
|
|
bindings: columns.bindings,
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
if (this.single.comment) this.comment(this.single.comment);
|
2021-11-10 21:24:34 +01:00
|
|
|
if (like) {
|
|
|
|
this.addColumns(columns, this.addColumnsPrefix);
|
|
|
|
}
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Compiles the comment on the table.
|
2016-05-17 01:01:34 +10:00
|
|
|
comment(comment) {
|
2017-10-31 23:22:07 +01:00
|
|
|
this.pushQuery(`comment on table ${this.tableName()} is '${comment}'`);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2017-02-16 20:34:59 +02:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropColumn() {
|
|
|
|
const columns = helpers.normalizeArr.apply(null, arguments);
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.tableName()} drop (${this.formatter.columnize(
|
|
|
|
columns
|
|
|
|
)})`
|
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_indexCommand(type, tableName, columns) {
|
2023-10-16 17:31:38 -06:00
|
|
|
const nameHelper = new utils.NameHelper(this.client.version);
|
2018-07-09 08:10:34 -04:00
|
|
|
return this.formatter.wrap(
|
2023-10-16 17:31:38 -06:00
|
|
|
nameHelper.generateCombinedName(
|
|
|
|
this.client.logger,
|
|
|
|
type,
|
|
|
|
tableName,
|
|
|
|
columns
|
|
|
|
)
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-19 21:30:17 +02:00
|
|
|
primary(columns, constraintName) {
|
2021-07-25 17:23:17 +10:00
|
|
|
let deferrable;
|
|
|
|
if (isObject(constraintName)) {
|
|
|
|
({ constraintName, deferrable } = constraintName);
|
|
|
|
}
|
|
|
|
deferrable = deferrable ? ` deferrable initially ${deferrable}` : '';
|
2018-07-09 08:10:34 -04:00
|
|
|
constraintName = constraintName
|
|
|
|
? this.formatter.wrap(constraintName)
|
|
|
|
: this.formatter.wrap(`${this.tableNameRaw}_pkey`);
|
2022-01-03 20:55:24 +01:00
|
|
|
const primaryCols = columns;
|
|
|
|
let incrementsCols = [];
|
|
|
|
if (this.grouped.columns) {
|
|
|
|
incrementsCols = this._getIncrementsColumnNames();
|
|
|
|
if (incrementsCols) {
|
|
|
|
incrementsCols.forEach((c) => {
|
|
|
|
if (!primaryCols.includes(c)) {
|
|
|
|
primaryCols.unshift(c);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(
|
2022-01-03 20:55:24 +01:00
|
|
|
primaryCols
|
2021-07-25 17:23:17 +10:00
|
|
|
)})${deferrable}`
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-19 21:30:17 +02:00
|
|
|
dropPrimary(constraintName) {
|
2018-07-09 08:10:34 -04:00
|
|
|
constraintName = constraintName
|
|
|
|
? this.formatter.wrap(constraintName)
|
|
|
|
: this.formatter.wrap(this.tableNameRaw + '_pkey');
|
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.tableName()} drop constraint ${constraintName}`
|
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
index(columns, indexName) {
|
2018-07-09 08:10:34 -04:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(
|
|
|
|
`create index ${indexName} on ${this.tableName()}` +
|
|
|
|
' (' +
|
|
|
|
this.formatter.columnize(columns) +
|
|
|
|
')'
|
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropIndex(columns, indexName) {
|
2018-07-09 08:10:34 -04:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('index', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`drop index ${indexName}`);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
unique(columns, indexName) {
|
2021-07-25 17:23:17 +10:00
|
|
|
let deferrable;
|
|
|
|
if (isObject(indexName)) {
|
|
|
|
({ indexName, deferrable } = indexName);
|
|
|
|
}
|
|
|
|
deferrable = deferrable ? ` deferrable initially ${deferrable}` : '';
|
2018-07-09 08:10:34 -04:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.tableName()} add constraint ${indexName}` +
|
|
|
|
' unique (' +
|
|
|
|
this.formatter.columnize(columns) +
|
2021-07-25 17:23:17 +10:00
|
|
|
')' +
|
|
|
|
deferrable
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropUnique(columns, indexName) {
|
2018-07-09 08:10:34 -04:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.tableName()} drop constraint ${indexName}`
|
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropForeign(columns, indexName) {
|
2018-07-09 08:10:34 -04:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('foreign', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.tableName()} drop constraint ${indexName}`
|
|
|
|
);
|
2021-01-01 18:46:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TableCompiler_Oracle.prototype.addColumnsPrefix = 'add ';
|
|
|
|
TableCompiler_Oracle.prototype.alterColumnsPrefix = 'modify ';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = TableCompiler_Oracle;
|