2016-05-17 01:01:34 +10:00
|
|
|
/* eslint max-len:0 */
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import * as utils from '../utils';
|
|
|
|
import TableCompiler from '../../../schema/tablecompiler';
|
|
|
|
import * as helpers from '../../../helpers';
|
2016-10-14 17:00:39 +02:00
|
|
|
import Trigger from './trigger';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2017-06-10 20:01:57 +02:00
|
|
|
import { assign, map } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Table Compiler
|
|
|
|
// ------
|
|
|
|
|
|
|
|
function TableCompiler_Oracle() {
|
|
|
|
TableCompiler.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(TableCompiler_Oracle, TableCompiler);
|
|
|
|
|
|
|
|
assign(TableCompiler_Oracle.prototype, {
|
|
|
|
|
2017-06-10 20:01:57 +02:00
|
|
|
addColumns(columns, prefix) {
|
|
|
|
if (columns.sql.length > 0) {
|
|
|
|
prefix = prefix || this.addColumnsPrefix;
|
|
|
|
|
|
|
|
const columnSql = map(columns.sql, (column) => column);
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
const tableName = this.tableName().slice(1, -1)
|
2018-05-29 17:42:03 +02:00
|
|
|
return this.pushQuery(Trigger.renameColumnTrigger(this.client.logger, tableName, from, to));
|
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({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `alter table ${table} ${columns.join(', ')}`
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Adds the "create" query to the query sequence.
|
2016-05-17 01:01:34 +10:00
|
|
|
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
|
|
|
|
});
|
|
|
|
if (this.single.comment) this.comment(this.single.comment);
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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}'`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
addColumnsPrefix: 'add ',
|
|
|
|
|
2017-02-16 20:34:59 +02:00
|
|
|
alterColumnsPrefix: 'modify ',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropColumn() {
|
|
|
|
const columns = helpers.normalizeArr.apply(null, arguments);
|
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop (${this.formatter.columnize(columns)})`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
changeType() {
|
2016-03-02 17:07:05 +01:00
|
|
|
// alter table + table + ' modify ' + wrapped + '// type';
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_indexCommand(type, tableName, columns) {
|
2018-05-29 17:42:03 +02:00
|
|
|
return this.formatter.wrap(utils.generateCombinedName(this.client.logger, type, tableName, columns));
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-19 21:30:17 +02:00
|
|
|
primary(columns, constraintName) {
|
2016-05-19 21:45:01 +02:00
|
|
|
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(`${this.tableNameRaw}_pkey`);
|
2016-05-19 21:30:17 +02:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(columns)})`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-19 21:30:17 +02:00
|
|
|
dropPrimary(constraintName) {
|
|
|
|
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
|
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop constraint ${constraintName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
index(columns, indexName) {
|
2016-03-02 17:07:05 +01:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create index ${indexName} on ${this.tableName()}` +
|
2016-03-02 17:07:05 +01:00
|
|
|
' (' + this.formatter.columnize(columns) + ')');
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropIndex(columns, indexName) {
|
2016-03-02 17:07:05 +01: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}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
unique(columns, indexName) {
|
2016-03-02 17:07:05 +01:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} add constraint ${indexName}` +
|
2016-03-02 17:07:05 +01:00
|
|
|
' unique (' + this.formatter.columnize(columns) + ')');
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropUnique(columns, indexName) {
|
2016-03-02 17:07:05 +01:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropForeign(columns, indexName) {
|
2016-03-02 17:07:05 +01:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default TableCompiler_Oracle;
|