2016-03-02 17:07:05 +01:00
|
|
|
// PostgreSQL Column Compiler
|
|
|
|
// -------
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
const ColumnCompiler = require('../../../schema/columncompiler');
|
2020-10-05 21:29:39 +03:00
|
|
|
const { isObject } = require('../../../util/is');
|
2021-01-06 23:21:10 +02:00
|
|
|
const { toNumber } = require('../../../util/helpers');
|
2020-10-27 10:05:21 +02:00
|
|
|
const commentEscapeRegex = /(?<!')'(?!')/g;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
class ColumnCompiler_PG extends ColumnCompiler {
|
2021-02-04 15:54:26 +02:00
|
|
|
constructor(client, tableCompiler, columnBuilder) {
|
|
|
|
super(client, tableCompiler, columnBuilder);
|
2021-01-01 20:35:54 +02:00
|
|
|
this.modifiers = ['nullable', 'defaultTo', 'comment'];
|
2022-01-06 14:44:16 +01:00
|
|
|
this._addCheckModifiers();
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Types
|
|
|
|
// ------
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
bit(column) {
|
|
|
|
return column.length !== false ? `bit(${column.length})` : 'bit';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// 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}'))`;
|
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
|
|
|
}
|
|
|
|
|
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);
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
jsonb() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return jsonColumn(this.client, true);
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
|
2022-01-06 14:44:16 +01:00
|
|
|
checkRegex(regex, constraintName) {
|
|
|
|
return this._check(
|
|
|
|
`${this.formatter.wrap(
|
|
|
|
this.getColumnName()
|
|
|
|
)} ~ ${this.client._escapeBinding(regex)}`,
|
|
|
|
constraintName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:22:27 +01:00
|
|
|
datetime(withoutTz = false, precision) {
|
|
|
|
let useTz;
|
|
|
|
if (isObject(withoutTz)) {
|
|
|
|
({ useTz, precision } = withoutTz);
|
|
|
|
} else {
|
|
|
|
useTz = !withoutTz;
|
|
|
|
}
|
2021-07-21 08:23:20 +10:00
|
|
|
useTz = typeof useTz === 'boolean' ? useTz : true;
|
2021-10-30 23:28:21 +02:00
|
|
|
precision =
|
|
|
|
precision !== undefined && precision !== null
|
|
|
|
? '(' + precision + ')'
|
|
|
|
: '';
|
2018-11-26 10:22:27 +01:00
|
|
|
|
2021-07-21 08:23:20 +10:00
|
|
|
return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:22:27 +01:00
|
|
|
timestamp(withoutTz = false, precision) {
|
2021-10-30 23:28:21 +02:00
|
|
|
return this.datetime(withoutTz, precision);
|
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-10-27 10:05:21 +02:00
|
|
|
const escapedComment = comment
|
|
|
|
? `'${comment.replace(commentEscapeRegex, "''")}'`
|
|
|
|
: 'NULL';
|
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) +
|
2020-10-27 10:05:21 +02:00
|
|
|
` is ${escapedComment}`
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}, comment);
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2022-01-03 20:55:24 +01:00
|
|
|
|
|
|
|
increments(options = { primaryKey: true }) {
|
|
|
|
return (
|
|
|
|
'serial' +
|
|
|
|
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bigincrements(options = { primaryKey: true }) {
|
|
|
|
return (
|
|
|
|
'bigserial' +
|
|
|
|
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
|
|
|
|
);
|
|
|
|
}
|
2022-06-08 10:41:43 +02:00
|
|
|
|
|
|
|
uuid(options = { primaryKey: false }) {
|
|
|
|
return (
|
|
|
|
'uuid' +
|
|
|
|
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
|
|
|
|
);
|
|
|
|
}
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnCompiler_PG.prototype.bigint = 'bigint';
|
|
|
|
ColumnCompiler_PG.prototype.binary = 'bytea';
|
|
|
|
ColumnCompiler_PG.prototype.bool = 'boolean';
|
|
|
|
ColumnCompiler_PG.prototype.double = 'double precision';
|
|
|
|
ColumnCompiler_PG.prototype.floating = 'real';
|
|
|
|
ColumnCompiler_PG.prototype.smallint = 'smallint';
|
|
|
|
ColumnCompiler_PG.prototype.tinyint = 'smallint';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function jsonColumn(client, jsonb) {
|
2021-10-03 21:33:24 +03:00
|
|
|
if (
|
|
|
|
!client.version ||
|
|
|
|
client.config.client === 'cockroachdb' ||
|
2022-09-01 05:16:17 +10:00
|
|
|
client.config.jsonbSupport === true ||
|
2021-10-03 21:33:24 +03:00
|
|
|
parseFloat(client.version) >= 9.2
|
|
|
|
) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return jsonb ? 'jsonb' : 'json';
|
2021-10-03 21:33:24 +03:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = ColumnCompiler_PG;
|