columncompiler.js
3.15 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
123
124
125
126
import { assign, uniq, map } from 'lodash'
import inherits from 'inherits';
import Raw from '../../../raw';
import ColumnCompiler from '../../../schema/columncompiler';
import Trigger from './trigger';
// Column Compiler
// -------
function ColumnCompiler_Oracle() {
ColumnCompiler.apply(this, arguments);
this.modifiers = ['defaultTo', 'checkIn', 'nullable', 'comment'];
}
inherits(ColumnCompiler_Oracle, ColumnCompiler);
assign(ColumnCompiler_Oracle.prototype, {
// helper function for pushAdditional in increments() and bigincrements()
_createAutoIncrementTriggerAndSequence () {
// TODO Add warning that sequence etc is created
this.pushAdditional(function () {
const tableName = this.tableCompiler.tableNameRaw;
const createTriggerSQL = Trigger.createAutoIncrementTrigger(tableName);
this.pushQuery(createTriggerSQL);
});
},
increments () {
this._createAutoIncrementTriggerAndSequence();
return 'integer not null primary key';
},
bigincrements () {
this._createAutoIncrementTriggerAndSequence();
return 'number(20, 0) not null primary key';
},
floating(precision) {
const parsedPrecision = this._num(precision, 0);
return `float${parsedPrecision ? `(${parsedPrecision})` : ''}`;
},
double(precision, scale) {
// if (!precision) return 'number'; // TODO: Check If default is ok
return `number(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
},
integer(length) {
return length ? `number(${this._num(length, 11)})` : 'integer';
},
tinyint: 'smallint',
smallint: 'smallint',
mediumint: 'integer',
biginteger: 'number(20, 0)',
text: 'clob',
enu (allowed) {
allowed = uniq(allowed);
const maxLength = (allowed || []).reduce((maxLength, name) =>
Math.max(maxLength, String(name).length)
, 1);
// implicitly add the enum values as checked values
this.columnBuilder._modifiers.checkIn = [allowed];
return `varchar2(${maxLength})`;
},
time: 'timestamp with time zone',
datetime(without) {
return without ? 'timestamp' : 'timestamp with time zone';
},
timestamp(without) {
return without ? 'timestamp' : 'timestamp with time zone';
},
bit: 'clob',
json: 'clob',
bool () {
// implicitly add the check for 0 and 1
this.columnBuilder._modifiers.checkIn = [[0, 1]];
return 'number(1, 0)';
},
varchar(length) {
return `varchar2(${this._num(length, 255)})`;
},
// Modifiers
// ------
comment(comment) {
this.pushAdditional(function() {
this.pushQuery(`comment on column ${this.tableCompiler.tableName()}.` +
this.formatter.wrap(this.args[0]) + " is '" + (comment || '')+ "'");
}, comment);
},
checkIn (value) {
// TODO: Maybe accept arguments also as array
// TODO: value(s) should be escaped properly
if (value === undefined) {
return '';
} else if (value instanceof Raw) {
value = value.toQuery();
} else if (Array.isArray(value)) {
value = map(value, v => `'${v}'`).join(', ');
} else {
value = `'${value}'`;
}
return `check (${this.formatter.wrap(this.args[0])} in (${value}))`;
}
});
export default ColumnCompiler_Oracle;