tablecompiler.js
4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
exports.__esModule = true;
var _assign2 = require('lodash/assign');
var _assign3 = _interopRequireDefault(_assign2);
var _inherits = require('inherits');
var _inherits2 = _interopRequireDefault(_inherits);
var _utils = require('../utils');
var utils = _interopRequireWildcard(_utils);
var _tablecompiler = require('../../../schema/tablecompiler');
var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
var _helpers = require('../../../helpers');
var helpers = _interopRequireWildcard(_helpers);
var _trigger = require('./trigger');
var _trigger2 = _interopRequireDefault(_trigger);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Table Compiler
// ------
function TableCompiler_Oracle() {
_tablecompiler2.default.apply(this, arguments);
} /* eslint max-len:0 */
(0, _inherits2.default)(TableCompiler_Oracle, _tablecompiler2.default);
(0, _assign3.default)(TableCompiler_Oracle.prototype, {
// Compile a rename column command.
renameColumn: function renameColumn(from, to) {
// Remove quotes around tableName
var tableName = this.tableName().slice(1, -1);
return this.pushQuery(_trigger2.default.renameColumnTrigger(tableName, from, to));
},
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.
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.
comment: function comment(_comment) {
this.pushQuery('comment on table ' + this.tableName() + ' is \'' + (_comment || '') + '\'');
},
addColumnsPrefix: 'add ',
alterColumnsPrefix: 'modify ',
dropColumn: function dropColumn() {
var columns = helpers.normalizeArr.apply(null, arguments);
this.pushQuery('alter table ' + this.tableName() + ' drop (' + this.formatter.columnize(columns) + ')');
},
changeType: function changeType() {
// alter table + table + ' modify ' + wrapped + '// type';
},
_indexCommand: function _indexCommand(type, tableName, columns) {
return this.formatter.wrap(utils.generateCombinedName(type, tableName, columns));
},
primary: function 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) + ')');
},
dropPrimary: function dropPrimary(constraintName) {
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + constraintName);
},
index: function 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) + ')');
},
dropIndex: function dropIndex(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery('drop index ' + indexName);
},
unique: function unique(columns, indexName) {
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) + ')');
},
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);
},
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);
}
});
exports.default = TableCompiler_Oracle;
module.exports = exports['default'];