2016-03-02 17:07:05 +01:00
|
|
|
// PostgreSQL Column Compiler
|
|
|
|
// -------
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
const inherits = require('inherits');
|
|
|
|
const ColumnCompiler = require('../../../schema/columncompiler');
|
2020-04-18 20:41:23 +03:00
|
|
|
const isObject = require('lodash/isObject');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function ColumnCompiler_PG() {
|
|
|
|
ColumnCompiler.apply(this, arguments);
|
2018-07-09 08:10:34 -04:00
|
|
|
this.modifiers = ['nullable', 'defaultTo', 'comment'];
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
inherits(ColumnCompiler_PG, ColumnCompiler);
|
|
|
|
|
2018-11-26 10:22:27 +01:00
|
|
|
Object.assign(ColumnCompiler_PG.prototype, {
|
2016-03-02 17:07:05 +01:00
|
|
|
// Types
|
|
|
|
// ------
|
|
|
|
bigincrements: 'bigserial primary key',
|
|
|
|
bigint: 'bigint',
|
|
|
|
binary: 'bytea',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
bit(column) {
|
|
|
|
return column.length !== false ? `bit(${column.length})` : 'bit';
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
bool: 'boolean',
|
|
|
|
|
|
|
|
// Create the column definition for an enum type.
|
|
|
|
// Using method "2" here: http://stackoverflow.com/a/10984951/525714
|
2018-05-29 10:09:13 -04:00
|
|
|
enu(allowed, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2018-11-09 00:47:15 -08:00
|
|
|
const values =
|
|
|
|
options.useNative && options.existingType
|
|
|
|
? undefined
|
|
|
|
: allowed.join("', '");
|
2018-05-29 10:09:13 -04:00
|
|
|
|
|
|
|
if (options.useNative) {
|
2019-08-24 18:37:29 +03:00
|
|
|
let enumName = '';
|
2019-08-26 00:07:30 +03:00
|
|
|
const schemaName = options.schemaName || this.tableCompiler.schemaNameRaw;
|
2019-08-24 18:37:29 +03:00
|
|
|
|
|
|
|
if (schemaName) {
|
|
|
|
enumName += `"${schemaName}".`;
|
|
|
|
}
|
|
|
|
|
|
|
|
enumName += `"${options.enumName}"`;
|
|
|
|
|
2018-11-09 00:47:15 -08:00
|
|
|
if (!options.existingType) {
|
|
|
|
this.tableCompiler.unshiftQuery(
|
2019-08-24 18:37:29 +03:00
|
|
|
`create type ${enumName} as enum ('${values}')`
|
2018-11-09 00:47:15 -08:00
|
|
|
);
|
|
|
|
}
|
2018-05-29 10:09:13 -04:00
|
|
|
|
2019-08-24 18:37:29 +03:00
|
|
|
return enumName;
|
2018-05-29 10:09:13 -04:00
|
|
|
}
|
|
|
|
return `text check (${this.formatter.wrap(this.args[0])} in ('${values}'))`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
double: 'double precision',
|
2017-11-30 15:05:39 -06:00
|
|
|
decimal(precision, scale) {
|
|
|
|
if (precision === null) return 'decimal';
|
|
|
|
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
floating: 'real',
|
|
|
|
increments: 'serial primary key',
|
2016-05-17 01:01:34 +10:00
|
|
|
json(jsonb) {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (jsonb) this.client.logger.deprecate('json(true)', 'jsonb()');
|
2016-03-02 17:07:05 +01:00
|
|
|
return jsonColumn(this.client, jsonb);
|
|
|
|
},
|
2016-05-17 01:01:34 +10:00
|
|
|
jsonb() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return jsonColumn(this.client, true);
|
|
|
|
},
|
|
|
|
smallint: 'smallint',
|
2018-07-09 08:10:34 -04:00
|
|
|
tinyint: 'smallint',
|
2018-11-26 10:22:27 +01:00
|
|
|
datetime(withoutTz = false, precision) {
|
|
|
|
let useTz;
|
|
|
|
if (isObject(withoutTz)) {
|
|
|
|
({ useTz, precision } = withoutTz);
|
|
|
|
} else {
|
|
|
|
useTz = !withoutTz;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${useTz ? 'timestamptz' : 'timestamp'}${
|
|
|
|
precision ? '(' + precision + ')' : ''
|
|
|
|
}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
2018-11-26 10:22:27 +01:00
|
|
|
timestamp(withoutTz = false, precision) {
|
|
|
|
let useTz;
|
|
|
|
if (isObject(withoutTz)) {
|
|
|
|
({ useTz, precision } = withoutTz);
|
|
|
|
} else {
|
|
|
|
useTz = !withoutTz;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${useTz ? 'timestamptz' : 'timestamp'}${
|
|
|
|
precision ? '(' + precision + ')' : ''
|
|
|
|
}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
uuid: 'uuid',
|
|
|
|
|
|
|
|
// 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');
|
2017-11-30 15:05:39 -06:00
|
|
|
|
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 ? `'${comment}'` : 'NULL')
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}, comment);
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function jsonColumn(client, jsonb) {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (!client.version || parseFloat(client.version) >= 9.2)
|
|
|
|
return jsonb ? 'jsonb' : 'json';
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = ColumnCompiler_PG;
|