2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Column Compiler
|
|
|
|
// Used for designating column definitions
|
|
|
|
// during the table "create" / "alter" statements.
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import Raw from '../raw';
|
|
|
|
import * as helpers from './helpers';
|
2016-05-18 20:22:50 +10:00
|
|
|
import { groupBy, first, tail, has, isObject } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function ColumnCompiler(client, tableCompiler, columnBuilder) {
|
2016-05-18 19:59:24 +10:00
|
|
|
this.client = client
|
2016-03-02 17:07:05 +01:00
|
|
|
this.tableCompiler = tableCompiler
|
|
|
|
this.columnBuilder = columnBuilder
|
2016-05-18 19:59:24 +10:00
|
|
|
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();
|
|
|
|
this.sequence = [];
|
2016-12-11 17:23:44 +01:00
|
|
|
this.modifiers = [];
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnCompiler.prototype.pushQuery = helpers.pushQuery
|
|
|
|
|
|
|
|
ColumnCompiler.prototype.pushAdditional = helpers.pushAdditional
|
|
|
|
|
2017-09-28 01:54:20 -04:00
|
|
|
ColumnCompiler.prototype._defaultMap = {
|
|
|
|
'columnName': function() {
|
|
|
|
if (!this.isIncrements) {
|
|
|
|
throw new Error(`You did not specify a column name for the ${this.type} column.`);
|
2017-11-30 15:05:39 -06:00
|
|
|
}
|
2017-09-28 01:54:20 -04:00
|
|
|
return 'id';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnCompiler.prototype.defaults = function(label) {
|
|
|
|
if (this._defaultMap.hasOwnProperty(label)){
|
|
|
|
return this._defaultMap[label].bind(this)();
|
|
|
|
} else {
|
|
|
|
throw new Error(`There is no default for the specified identifier ${label}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// To convert to sql, we first go through and build the
|
|
|
|
// column as it would be in the insert statement
|
|
|
|
ColumnCompiler.prototype.toSQL = function() {
|
|
|
|
this.pushQuery(this.compileColumn());
|
|
|
|
if (this.sequence.additional) {
|
|
|
|
this.sequence = this.sequence.concat(this.sequence.additional);
|
|
|
|
}
|
|
|
|
return this.sequence;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compiles a column.
|
|
|
|
ColumnCompiler.prototype.compileColumn = function() {
|
|
|
|
return this.formatter.wrap(this.getColumnName()) + ' ' +
|
|
|
|
this.getColumnType() + this.getModifiers();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assumes the autoincrementing key is named `id` if not otherwise specified.
|
|
|
|
ColumnCompiler.prototype.getColumnName = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const value = first(this.args);
|
2017-09-28 01:54:20 -04:00
|
|
|
return value || this.defaults('columnName');
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ColumnCompiler.prototype.getColumnType = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const type = this[this.type];
|
2016-03-02 17:07:05 +01:00
|
|
|
return typeof type === 'function' ? type.apply(this, tail(this.args)) : type;
|
|
|
|
};
|
|
|
|
|
|
|
|
ColumnCompiler.prototype.getModifiers = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const modifiers = [];
|
2017-09-28 01:54:20 -04:00
|
|
|
|
2017-11-30 15:05:39 -06:00
|
|
|
|
2017-09-28 01:54:20 -04:00
|
|
|
for (let i = 0, l = this.modifiers.length; i < l; i++) {
|
|
|
|
const modifier = this.modifiers[i];
|
|
|
|
|
|
|
|
//Cannot allow 'nullable' modifiers on increments types
|
|
|
|
if (!this.isIncrements || (this.isIncrements && modifier === 'comment')){
|
2016-03-02 17:07:05 +01:00
|
|
|
if (has(this.modified, modifier)) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const val = this[modifier].apply(this, this.modified[modifier]);
|
2016-03-02 17:07:05 +01:00
|
|
|
if (val) modifiers.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 15:05:39 -06:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
return modifiers.length > 0 ? ` ${modifiers.join(' ')}` : '';
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Types
|
|
|
|
// ------
|
|
|
|
|
2016-05-18 19:59:24 +10:00
|
|
|
ColumnCompiler.prototype.increments = 'integer not null primary key autoincrement';
|
2016-03-02 17:07:05 +01:00
|
|
|
ColumnCompiler.prototype.bigincrements = 'integer not null primary key autoincrement';
|
|
|
|
ColumnCompiler.prototype.integer =
|
|
|
|
ColumnCompiler.prototype.smallint =
|
2016-05-18 19:59:24 +10:00
|
|
|
ColumnCompiler.prototype.mediumint = 'integer';
|
|
|
|
ColumnCompiler.prototype.biginteger = 'bigint';
|
|
|
|
ColumnCompiler.prototype.varchar = function(length) {
|
2016-05-17 01:01:34 +10:00
|
|
|
return `varchar(${this._num(length, 255)})`;
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
ColumnCompiler.prototype.text = 'text';
|
|
|
|
ColumnCompiler.prototype.tinyint = 'tinyint';
|
|
|
|
ColumnCompiler.prototype.floating = function(precision, scale) {
|
2016-05-17 01:01:34 +10:00
|
|
|
return `float(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
ColumnCompiler.prototype.decimal = function(precision, scale) {
|
2017-11-30 15:05:39 -06:00
|
|
|
if (precision === null) {
|
|
|
|
throw new Error(
|
|
|
|
'Specifying no precision on decimal columns is not supported for that SQL dialect.'
|
|
|
|
);
|
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
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';
|
|
|
|
|
|
|
|
ColumnCompiler.prototype.bit =
|
|
|
|
ColumnCompiler.prototype.json = 'text';
|
|
|
|
|
|
|
|
ColumnCompiler.prototype.uuid = 'char(36)';
|
2016-05-17 01:01:34 +10:00
|
|
|
ColumnCompiler.prototype.specifictype = type => type;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Modifiers
|
|
|
|
// -------
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
ColumnCompiler.prototype.nullable = nullable => nullable === false ? 'not null' : 'null';
|
2016-03-02 17:07:05 +01:00
|
|
|
ColumnCompiler.prototype.notNullable = function() {
|
|
|
|
return this.nullable(false);
|
|
|
|
};
|
|
|
|
ColumnCompiler.prototype.defaultTo = function(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;
|
2016-05-17 01:01:34 +10:00
|
|
|
value = `'${value ? 1 : 0}'`;
|
2016-03-02 17:07:05 +01:00
|
|
|
} else if (this.type === 'json' && isObject(value)) {
|
|
|
|
return JSON.stringify(value);
|
|
|
|
} 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 `default ${value}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
ColumnCompiler.prototype._num = function(val, fallback) {
|
|
|
|
if (val === undefined || val === null) return fallback;
|
2016-05-17 01:01:34 +10:00
|
|
|
const number = parseInt(val, 10);
|
2016-03-02 17:07:05 +01:00
|
|
|
return isNaN(number) ? fallback : number;
|
|
|
|
};
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default ColumnCompiler;
|