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

127 lines
3.2 KiB
JavaScript
Raw Normal View History

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