knex/lib/dialects/oracle/schema/oracle-tablecompiler.js

175 lines
5.0 KiB
JavaScript
Raw Normal View History

/* eslint max-len:0 */
2016-03-02 17:07:05 +01:00
const utils = require('../utils');
const TableCompiler = require('../../../schema/tablecompiler');
const helpers = require('../../../util/helpers');
const Trigger = require('./internal/trigger');
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
addColumns(columns, prefix) {
if (columns.sql.length > 0) {
prefix = prefix || this.addColumnsPrefix;
const columnSql = columns.sql;
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
}
2016-03-02 17:07:05 +01:00
// Compile a rename column command.
renameColumn(from, to) {
// Remove quotes around tableName
const tableName = this.tableName().slice(1, -1);
return this.pushQuery(
Trigger.renameColumnTrigger(this.client.logger, tableName, from, to)
);
2021-01-01 18:46:16 +02:00
}
2016-03-02 17:07:05 +01: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({
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.
createQuery(columns, ifNot) {
const sql = `create table ${this.tableName()} (${columns.sql.join(', ')})`;
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,
bindings: columns.bindings,
2016-03-02 17:07:05 +01:00
});
if (this.single.comment) this.comment(this.single.comment);
2021-01-01 18:46:16 +02:00
}
2016-03-02 17:07:05 +01:00
// Compiles the comment on the table.
comment(comment) {
this.pushQuery(`comment on table ${this.tableName()} is '${comment}'`);
2021-01-01 18:46:16 +02:00
}
dropColumn() {
const columns = helpers.normalizeArr.apply(null, arguments);
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
changeType() {
2016-03-02 17:07:05 +01:00
// alter table + table + ' modify ' + wrapped + '// type';
2021-01-01 18:46:16 +02:00
}
2016-03-02 17:07:05 +01:00
_indexCommand(type, tableName, columns) {
return this.formatter.wrap(
utils.generateCombinedName(this.client.logger, type, tableName, columns)
);
2021-01-01 18:46:16 +02:00
}
2016-03-02 17:07:05 +01:00
primary(columns, constraintName) {
let deferrable;
if (isObject(constraintName)) {
({ constraintName, deferrable } = constraintName);
}
deferrable = deferrable ? ` deferrable initially ${deferrable}` : '';
constraintName = constraintName
? this.formatter.wrap(constraintName)
: this.formatter.wrap(`${this.tableNameRaw}_pkey`);
this.pushQuery(
`alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(
columns
)})${deferrable}`
);
2021-01-01 18:46:16 +02:00
}
2016-03-02 17:07:05 +01:00
dropPrimary(constraintName) {
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
index(columns, indexName) {
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
dropIndex(columns, indexName) {
indexName = indexName
? this.formatter.wrap(indexName)
: this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`drop index ${indexName}`);
2021-01-01 18:46:16 +02:00
}
2016-03-02 17:07:05 +01:00
unique(columns, indexName) {
let deferrable;
if (isObject(indexName)) {
({ indexName, deferrable } = indexName);
}
deferrable = deferrable ? ` deferrable initially ${deferrable}` : '';
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) +
')' +
deferrable
);
2021-01-01 18:46:16 +02:00
}
2016-03-02 17:07:05 +01:00
dropUnique(columns, indexName) {
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
dropForeign(columns, indexName) {
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
module.exports = TableCompiler_Oracle;