2020-04-18 20:41:23 +03:00
|
|
|
const uniq = require('lodash/uniq');
|
2019-06-04 00:37:17 +02:00
|
|
|
const Raw = require('../../../raw');
|
|
|
|
const ColumnCompiler = require('../../../schema/columncompiler');
|
2021-01-06 23:21:10 +02:00
|
|
|
const {
|
|
|
|
createAutoIncrementTriggerAndSequence,
|
|
|
|
} = require('./internal/incrementUtils');
|
|
|
|
const { toNumber } = require('../../../util/helpers');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Column Compiler
|
|
|
|
// -------
|
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
class ColumnCompiler_Oracle extends ColumnCompiler {
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.modifiers = ['defaultTo', 'checkIn', 'nullable', 'comment'];
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2022-01-03 20:55:24 +01:00
|
|
|
increments(options = { primaryKey: true }) {
|
2021-01-06 23:21:10 +02:00
|
|
|
createAutoIncrementTriggerAndSequence(this);
|
2022-01-03 20:55:24 +01:00
|
|
|
return (
|
|
|
|
'integer not null' +
|
|
|
|
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
|
|
|
|
);
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2022-01-03 20:55:24 +01:00
|
|
|
bigincrements(options = { primaryKey: true }) {
|
2021-01-06 23:21:10 +02:00
|
|
|
createAutoIncrementTriggerAndSequence(this);
|
2022-01-03 20:55:24 +01:00
|
|
|
return (
|
|
|
|
'number(20, 0) not null' +
|
|
|
|
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
|
|
|
|
);
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
floating(precision) {
|
2021-01-06 23:21:10 +02:00
|
|
|
const parsedPrecision = toNumber(precision, 0);
|
2016-05-17 01:01:34 +10:00
|
|
|
return `float${parsedPrecision ? `(${parsedPrecision})` : ''}`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
double(precision, scale) {
|
2016-03-02 17:07:05 +01:00
|
|
|
// if (!precision) return 'number'; // TODO: Check If default is ok
|
2021-01-06 23:21:10 +02:00
|
|
|
return `number(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2017-11-30 15:05:39 -06:00
|
|
|
decimal(precision, scale) {
|
|
|
|
if (precision === null) return 'decimal';
|
2021-01-06 23:21:10 +02:00
|
|
|
return `decimal(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2017-11-30 15:05:39 -06:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
integer(length) {
|
2021-01-06 23:21:10 +02:00
|
|
|
return length ? `number(${toNumber(length, 11)})` : 'integer';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
enu(allowed) {
|
2016-03-02 17:07:05 +01:00
|
|
|
allowed = uniq(allowed);
|
2018-07-09 08:10:34 -04:00
|
|
|
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];
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
return `varchar2(${maxLength})`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
datetime(without) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return without ? 'timestamp' : 'timestamp with time zone';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
timestamp(without) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return without ? 'timestamp' : 'timestamp with time zone';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
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)';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
varchar(length) {
|
2021-01-06 23:21:10 +02:00
|
|
|
return `varchar2(${toNumber(length, 255)})`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Modifiers
|
|
|
|
// ------
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
comment(comment) {
|
2017-09-28 01:54:20 -04:00
|
|
|
const columnName = this.args[0] || this.defaults('columnName');
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
this.pushAdditional(function () {
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery(
|
|
|
|
`comment on column ${this.tableCompiler.tableName()}.` +
|
|
|
|
this.formatter.wrap(columnName) +
|
|
|
|
" is '" +
|
|
|
|
(comment || '') +
|
|
|
|
"'"
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}, comment);
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
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)) {
|
2020-04-18 20:41:23 +03:00
|
|
|
value = value.map((v) => `'${v}'`).join(', ');
|
2016-03-02 17:07:05 +01:00
|
|
|
} else {
|
2016-05-17 01:01:34 +10:00
|
|
|
value = `'${value}'`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
return `check (${this.formatter.wrap(this.args[0])} in (${value}))`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnCompiler_Oracle.prototype.tinyint = 'smallint';
|
|
|
|
ColumnCompiler_Oracle.prototype.smallint = 'smallint';
|
|
|
|
ColumnCompiler_Oracle.prototype.mediumint = 'integer';
|
|
|
|
ColumnCompiler_Oracle.prototype.biginteger = 'number(20, 0)';
|
|
|
|
ColumnCompiler_Oracle.prototype.text = 'clob';
|
|
|
|
ColumnCompiler_Oracle.prototype.time = 'timestamp with time zone';
|
|
|
|
ColumnCompiler_Oracle.prototype.bit = 'clob';
|
|
|
|
ColumnCompiler_Oracle.prototype.json = 'clob';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = ColumnCompiler_Oracle;
|