tablecompiler.js
3.8 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
/* eslint max-len:0 */
import inherits from 'inherits';
import * as utils from '../utils';
import TableCompiler from '../../../schema/tablecompiler';
import * as helpers from '../../../helpers';
import Trigger from './trigger';
import { assign } from 'lodash'
// Table Compiler
// ------
function TableCompiler_Oracle() {
TableCompiler.apply(this, arguments);
}
inherits(TableCompiler_Oracle, TableCompiler);
assign(TableCompiler_Oracle.prototype, {
// Compile a rename column command.
renameColumn(from, to) {
// Remove quotes around tableName
const tableName = this.tableName().slice(1, -1)
return this.pushQuery(Trigger.renameColumnTrigger(tableName, from, to));
},
compileAdd(builder) {
const table = this.formatter.wrap(builder);
const 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(columns, ifNot) {
const 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(comment) {
this.pushQuery(`comment on table ${this.tableName()} is '${comment || ''}'`);
},
addColumnsPrefix: 'add ',
alterColumnsPrefix: 'modify ',
dropColumn() {
const columns = helpers.normalizeArr.apply(null, arguments);
this.pushQuery(`alter table ${this.tableName()} drop (${this.formatter.columnize(columns)})`);
},
changeType() {
// alter table + table + ' modify ' + wrapped + '// type';
},
_indexCommand(type, tableName, columns) {
return this.formatter.wrap(utils.generateCombinedName(type, tableName, columns));
},
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(constraintName) {
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
this.pushQuery(`alter table ${this.tableName()} drop constraint ${constraintName}`);
},
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(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`drop index ${indexName}`);
},
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(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
},
dropForeign(columns, indexName) {
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
}
})
export default TableCompiler_Oracle;