knex/src/dialects/oracle/schema/tablecompiler.js

130 lines
4.4 KiB
JavaScript
Raw Normal View History

/* eslint max-len:0 */
2016-03-02 17:07:05 +01:00
import inherits from 'inherits';
import * as utils from '../utils';
import TableCompiler from '../../../schema/tablecompiler';
import * as helpers from '../../../helpers';
import Trigger from './trigger';
2016-03-02 17:07:05 +01: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, {
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.
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));
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
});
},
// 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
});
if (this.single.comment) this.comment(this.single.comment);
},
// Compiles the comment on the table.
comment(comment) {
this.pushQuery(`comment on table ${this.tableName()} is '${comment}'`);
2016-03-02 17:07:05 +01:00
},
addColumnsPrefix: 'add ',
alterColumnsPrefix: 'modify ',
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
},
changeType() {
2016-03-02 17:07:05 +01:00
// alter table + table + ' modify ' + wrapped + '// type';
},
_indexCommand(type, tableName, columns) {
return this.formatter.wrap(utils.generateCombinedName(this.client.logger, type, tableName, columns));
2016-03-02 17:07:05 +01:00
},
primary(columns, constraintName) {
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)})`);
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}`);
2016-03-02 17:07:05 +01:00
},
index(columns, indexName) {
2016-03-02 17:07:05 +01:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`create index ${indexName} on ${this.tableName()}` +
2016-03-02 17:07:05 +01:00
' (' + this.formatter.columnize(columns) + ')');
},
dropIndex(columns, indexName) {
2016-03-02 17:07:05 +01:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`drop index ${indexName}`);
2016-03-02 17:07:05 +01:00
},
unique(columns, indexName) {
2016-03-02 17:07:05 +01:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} add constraint ${indexName}` +
2016-03-02 17:07:05 +01:00
' unique (' + this.formatter.columnize(columns) + ')');
},
dropUnique(columns, indexName) {
2016-03-02 17:07:05 +01:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
2016-03-02 17:07:05 +01:00
},
dropForeign(columns, indexName) {
2016-03-02 17:07:05 +01:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
2016-03-02 17:07:05 +01:00
}
})
export default TableCompiler_Oracle;