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

100 lines
3.8 KiB
JavaScript
Raw Normal View History

'use strict';
2015-05-09 13:58:18 -04:00
var inherits = require('inherits');
var utils = require('../utils');
var TableCompiler = require('../../../schema/tablecompiler');
2015-05-09 13:58:18 -04:00
var helpers = require('../../../helpers');
var assign = require('lodash/object/assign');
// Table Compiler
// ------
function TableCompiler_Oracle() {
TableCompiler.apply(this, arguments);
}
inherits(TableCompiler_Oracle, TableCompiler);
assign(TableCompiler_Oracle.prototype, {
// Compile a rename column command.
2015-05-09 13:58:18 -04:00
renameColumn: function renameColumn(from, to) {
return this.pushQuery({
2015-05-09 13:58:18 -04:00
sql: 'alter table ' + this.tableName() + ' rename column ' + this.formatter.wrap(from) + ' to ' + this.formatter.wrap(to)
});
},
2015-05-09 13:58:18 -04:00
compileAdd: function compileAdd(builder) {
var table = this.formatter.wrap(builder);
var columns = this.prefixArray('add column', this.getColumns(builder));
return this.pushQuery({
sql: 'alter table ' + table + ' ' + columns.join(', ')
});
},
// Adds the "create" query to the query sequence.
2015-05-09 13:58:18 -04:00
createQuery: function createQuery(columns, ifNot) {
var sql = 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')';
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.
2015-05-09 13:58:18 -04:00
comment: function comment(_comment) {
2015-08-25 10:23:34 +03:00
this.pushQuery('comment on table ' + this.tableName() + ' is ' + "'" + (_comment || '') + "'");
},
addColumnsPrefix: 'add ',
2015-05-09 13:58:18 -04:00
dropColumn: function dropColumn() {
var columns = helpers.normalizeArr.apply(null, arguments);
this.pushQuery('alter table ' + this.tableName() + ' drop (' + this.formatter.columnize(columns) + ')');
},
2015-08-25 10:23:34 +03:00
changeType: function changeType() {
// alter table + table + ' modify ' + wrapped + '// type';
},
2015-05-09 13:58:18 -04:00
_indexCommand: function _indexCommand(type, tableName, columns) {
return this.formatter.wrap(utils.generateCombinedName(type, tableName, columns));
},
2015-05-09 13:58:18 -04:00
primary: function primary(columns) {
2015-08-25 10:23:34 +03:00
this.pushQuery('alter table ' + this.tableName() + " add primary key (" + this.formatter.columnize(columns) + ")");
},
2015-05-09 13:58:18 -04:00
dropPrimary: function dropPrimary() {
this.pushQuery('alter table ' + this.tableName() + ' drop primary key');
},
2015-05-09 13:58:18 -04:00
index: function index(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
2015-05-09 13:58:18 -04:00
this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
},
2015-05-09 13:58:18 -04:00
dropIndex: function dropIndex(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery('drop index ' + indexName);
},
2015-05-09 13:58:18 -04:00
unique: function unique(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
2015-05-09 13:58:18 -04:00
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + indexName + ' unique (' + this.formatter.columnize(columns) + ')');
},
2015-05-09 13:58:18 -04:00
dropUnique: function dropUnique(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
},
2015-05-09 13:58:18 -04:00
dropForeign: function dropForeign(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
}
2015-05-09 13:58:18 -04:00
});
2015-08-25 10:23:34 +03:00
module.exports = TableCompiler_Oracle;