2016-03-02 17:07:05 +01:00
|
|
|
// Column Compiler
|
|
|
|
// Used for designating column definitions
|
|
|
|
// during the table "create" / "alter" statements.
|
|
|
|
// -------
|
2019-06-04 00:37:17 +02:00
|
|
|
const Raw = require('../raw');
|
2020-12-28 16:55:08 +02:00
|
|
|
const helpers = require('./internal/helpers');
|
2020-04-18 20:41:23 +03:00
|
|
|
const groupBy = require('lodash/groupBy');
|
|
|
|
const first = require('lodash/first');
|
|
|
|
const has = require('lodash/has');
|
|
|
|
const tail = require('lodash/tail');
|
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');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
class ColumnCompiler {
|
|
|
|
constructor(client, tableCompiler, columnBuilder) {
|
|
|
|
this.client = client;
|
|
|
|
this.tableCompiler = tableCompiler;
|
|
|
|
this.columnBuilder = columnBuilder;
|
|
|
|
this._commonBuilder = this.columnBuilder;
|
|
|
|
this.args = columnBuilder._args;
|
|
|
|
this.type = columnBuilder._type.toLowerCase();
|
|
|
|
this.grouped = groupBy(columnBuilder._statements, 'grouping');
|
|
|
|
this.modified = columnBuilder._modifiers;
|
|
|
|
this.isIncrements = this.type.indexOf('increments') !== -1;
|
|
|
|
this.formatter = client.formatter(columnBuilder);
|
|
|
|
this.sequence = [];
|
|
|
|
this.modifiers = [];
|
|
|
|
}
|
2018-05-29 10:09:13 -04:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
defaults(label) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(this._defaultMap, label)) {
|
|
|
|
return this._defaultMap[label].bind(this)();
|
|
|
|
} else {
|
2018-07-09 08:10:34 -04:00
|
|
|
throw new Error(
|
2021-01-01 20:35:54 +02:00
|
|
|
`There is no default for the specified identifier ${label}`
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2017-11-30 15:05:39 -06:00
|
|
|
}
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2017-09-28 01:54:20 -04:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
// To convert to sql, we first go through and build the
|
|
|
|
// column as it would be in the insert statement
|
|
|
|
toSQL() {
|
|
|
|
this.pushQuery(this.compileColumn());
|
|
|
|
if (this.sequence.additional) {
|
|
|
|
this.sequence = this.sequence.concat(this.sequence.additional);
|
|
|
|
}
|
|
|
|
return this.sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compiles a column.
|
|
|
|
compileColumn() {
|
|
|
|
return (
|
|
|
|
this.formatter.wrap(this.getColumnName()) +
|
|
|
|
' ' +
|
|
|
|
this.getColumnType() +
|
|
|
|
this.getModifiers()
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2017-09-28 01:54:20 -04:00
|
|
|
}
|
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
// Assumes the autoincrementing key is named `id` if not otherwise specified.
|
|
|
|
getColumnName() {
|
|
|
|
const value = first(this.args);
|
|
|
|
return value || this.defaults('columnName');
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
getColumnType() {
|
2021-01-16 06:16:00 -08:00
|
|
|
// Column type is cached so side effects (such as in pg native enums) are only run once
|
|
|
|
if (!this._columnType) {
|
|
|
|
const type = this[this.type];
|
|
|
|
this._columnType =
|
|
|
|
typeof type === 'function' ? type.apply(this, tail(this.args)) : type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._columnType;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
getModifiers() {
|
|
|
|
const modifiers = [];
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
for (let i = 0, l = this.modifiers.length; i < l; i++) {
|
|
|
|
const modifier = this.modifiers[i];
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
//Cannot allow 'nullable' modifiers on increments types
|
|
|
|
if (!this.isIncrements || (this.isIncrements && modifier === 'comment')) {
|
|
|
|
if (has(this.modified, modifier)) {
|
|
|
|
const val = this[modifier].apply(this, this.modified[modifier]);
|
|
|
|
if (val) modifiers.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 01:54:20 -04:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
return modifiers.length > 0 ? ` ${modifiers.join(' ')}` : '';
|
|
|
|
}
|
2017-09-28 01:54:20 -04:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
// Types
|
|
|
|
// ------
|
|
|
|
varchar(length) {
|
2021-01-06 23:21:10 +02:00
|
|
|
return `varchar(${toNumber(length, 255)})`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
floating(precision, scale) {
|
2021-01-06 23:21:10 +02:00
|
|
|
return `float(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decimal(precision, scale) {
|
|
|
|
if (precision === null) {
|
|
|
|
throw new Error(
|
|
|
|
'Specifying no precision on decimal columns is not supported for that SQL dialect.'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2021-01-06 23:21:10 +02:00
|
|
|
return `decimal(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2017-11-30 15:05:39 -06:00
|
|
|
|
2021-01-06 21:50:13 +02:00
|
|
|
// Used to support custom types
|
|
|
|
specifictype(type) {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
// Modifiers
|
|
|
|
// -------
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
nullable(nullable) {
|
|
|
|
return nullable === false ? 'not null' : 'null';
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
notNullable() {
|
|
|
|
return this.nullable(false);
|
2017-11-30 15:05:39 -06:00
|
|
|
}
|
2021-01-01 20:35:54 +02:00
|
|
|
|
|
|
|
defaultTo(value) {
|
|
|
|
if (value === void 0) {
|
|
|
|
return '';
|
|
|
|
} else if (value === null) {
|
|
|
|
value = 'null';
|
|
|
|
} else if (value instanceof Raw) {
|
|
|
|
value = value.toQuery();
|
|
|
|
} else if (this.type === 'bool') {
|
|
|
|
if (value === 'false') value = 0;
|
|
|
|
value = `'${value ? 1 : 0}'`;
|
|
|
|
} else if (
|
|
|
|
(this.type === 'json' || this.type === 'jsonb') &&
|
|
|
|
isObject(value)
|
|
|
|
) {
|
|
|
|
value = `'${JSON.stringify(value)}'`;
|
|
|
|
} else {
|
|
|
|
value = this.client._escapeBinding(value.toString());
|
|
|
|
}
|
|
|
|
return `default ${value}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
ColumnCompiler.prototype.binary = 'blob';
|
|
|
|
ColumnCompiler.prototype.bool = 'boolean';
|
|
|
|
ColumnCompiler.prototype.date = 'date';
|
|
|
|
ColumnCompiler.prototype.datetime = 'datetime';
|
|
|
|
ColumnCompiler.prototype.time = 'time';
|
|
|
|
ColumnCompiler.prototype.timestamp = 'timestamp';
|
|
|
|
ColumnCompiler.prototype.enu = 'varchar';
|
2018-07-09 08:10:34 -04:00
|
|
|
ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
|
2016-03-02 17:07:05 +01:00
|
|
|
ColumnCompiler.prototype.uuid = 'char(36)';
|
2021-02-03 13:47:32 +01:00
|
|
|
ColumnCompiler.prototype.increments = ({ primaryKey = true } = {}) =>
|
|
|
|
'integer not null' + (primaryKey ? ' primary key' : '') + ' autoincrement';
|
|
|
|
ColumnCompiler.prototype.bigincrements = ({ primaryKey = true } = {}) =>
|
|
|
|
'integer not null' + (primaryKey ? ' primary key' : '') + ' autoincrement';
|
2021-01-01 20:35:54 +02:00
|
|
|
ColumnCompiler.prototype.integer = ColumnCompiler.prototype.smallint = ColumnCompiler.prototype.mediumint =
|
|
|
|
'integer';
|
|
|
|
ColumnCompiler.prototype.biginteger = 'bigint';
|
|
|
|
ColumnCompiler.prototype.text = 'text';
|
|
|
|
ColumnCompiler.prototype.tinyint = 'tinyint';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
ColumnCompiler.prototype.pushQuery = helpers.pushQuery;
|
|
|
|
ColumnCompiler.prototype.pushAdditional = helpers.pushAdditional;
|
|
|
|
ColumnCompiler.prototype.unshiftQuery = helpers.unshiftQuery;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
ColumnCompiler.prototype._defaultMap = {
|
|
|
|
columnName: function () {
|
|
|
|
if (!this.isIncrements) {
|
|
|
|
throw new Error(
|
|
|
|
`You did not specify a column name for the ${this.type} column.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return 'id';
|
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = ColumnCompiler;
|