2014-04-08 16:25:57 -04:00
|
|
|
// Column Compiler
|
|
|
|
// Used for designating column definitions
|
|
|
|
// during the table "create" / "alter" statements.
|
|
|
|
// -------
|
2014-04-15 11:43:47 -04:00
|
|
|
var _ = require('lodash');
|
|
|
|
var Raw = require('../raw');
|
|
|
|
|
|
|
|
function ColumnCompiler(tableCompiler, columnBuilder) {
|
2014-04-09 10:11:41 -04:00
|
|
|
this.tableCompiler = tableCompiler;
|
2014-05-29 17:31:09 -04:00
|
|
|
this.columnBuilder = columnBuilder;
|
2014-04-15 11:43:47 -04:00
|
|
|
this.args = columnBuilder._args;
|
2014-06-03 02:23:20 -04:00
|
|
|
this.type = columnBuilder._type.toLowerCase();
|
2014-04-15 11:43:47 -04:00
|
|
|
this.grouped = _.groupBy(columnBuilder._statements, 'grouping');
|
|
|
|
this.modified = columnBuilder._modifiers;
|
2014-06-03 02:23:20 -04:00
|
|
|
this.isIncrements = (this.type.indexOf('increments') !== -1);
|
2014-04-15 11:43:47 -04:00
|
|
|
this.initCompiler();
|
2014-04-08 16:25:57 -04:00
|
|
|
}
|
|
|
|
|
2014-04-15 11:43:47 -04: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());
|
2014-05-29 17:31:09 -04:00
|
|
|
if (this.sequence.additional) {
|
|
|
|
this.sequence = this.sequence.concat(this.sequence.additional);
|
|
|
|
}
|
2014-04-15 11:43:47 -04:00
|
|
|
return this.sequence;
|
|
|
|
};
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
// Compiles a column.
|
2014-04-15 11:43:47 -04:00
|
|
|
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() {
|
|
|
|
var value = _.first(this.args);
|
|
|
|
if (value) return value;
|
|
|
|
if (this.isIncrements) {
|
|
|
|
return 'id';
|
|
|
|
} else {
|
|
|
|
throw new Error('You did not specify a column name for the ' + this.type + 'column.');
|
|
|
|
}
|
|
|
|
};
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
ColumnCompiler.prototype.getColumnType = function() {
|
2014-06-03 02:23:20 -04:00
|
|
|
var type = this[this.type];
|
2014-04-15 11:43:47 -04:00
|
|
|
return _.isFunction(type) ? type.apply(this, _.rest(this.args)) : type;
|
2014-04-08 16:25:57 -04:00
|
|
|
};
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
ColumnCompiler.prototype.getModifiers = function() {
|
|
|
|
var modifiers = [];
|
|
|
|
if (this.type.indexOf('increments') === -1) {
|
|
|
|
for (var i = 0, l = this.modifiers.length; i < l; i++) {
|
|
|
|
var modifier = this.modifiers[i];
|
|
|
|
if (_.has(this.modified, modifier)) {
|
|
|
|
var val = this[modifier].apply(this, this.modified[modifier]);
|
|
|
|
if (val) modifiers.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modifiers.length > 0 ? ' ' + modifiers.join(' ') : '';
|
2014-04-08 16:25:57 -04:00
|
|
|
};
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
// Types
|
2014-04-15 11:43:47 -04:00
|
|
|
// ------
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
ColumnCompiler.prototype.increments = 'integer not null primary key autoincrement';
|
|
|
|
ColumnCompiler.prototype.bigincrements = 'integer not null primary key autoincrement';
|
|
|
|
ColumnCompiler.prototype.integer =
|
|
|
|
ColumnCompiler.prototype.smallint =
|
|
|
|
ColumnCompiler.prototype.mediumint = 'integer';
|
|
|
|
ColumnCompiler.prototype.biginteger = 'bigint';
|
|
|
|
ColumnCompiler.prototype.varchar = function(length) {
|
|
|
|
return 'varchar(' + this._num(length, 255) + ')';
|
|
|
|
};
|
|
|
|
ColumnCompiler.prototype.text = 'text';
|
|
|
|
ColumnCompiler.prototype.tinyint = 'tinyint';
|
|
|
|
ColumnCompiler.prototype.floating = function(precision, scale) {
|
|
|
|
return 'float(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
|
|
|
|
};
|
|
|
|
ColumnCompiler.prototype.decimal = function(precision, scale) {
|
|
|
|
return 'decimal(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
|
|
|
|
};
|
2014-04-15 11:43:47 -04:00
|
|
|
ColumnCompiler.prototype.binary = 'blob';
|
|
|
|
ColumnCompiler.prototype.bool = 'boolean';
|
2014-04-16 01:23:50 -04:00
|
|
|
ColumnCompiler.prototype.date = 'date';
|
|
|
|
ColumnCompiler.prototype.datetime = 'datetime';
|
|
|
|
ColumnCompiler.prototype.time = 'time';
|
|
|
|
ColumnCompiler.prototype.timestamp = 'timestamp';
|
|
|
|
ColumnCompiler.prototype.enu = 'varchar';
|
2014-06-03 09:27:40 -04:00
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
ColumnCompiler.prototype.bit =
|
2014-04-16 01:23:50 -04:00
|
|
|
ColumnCompiler.prototype.json = 'text';
|
2014-06-03 09:27:40 -04:00
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
ColumnCompiler.prototype.uuid = 'char(36)';
|
2014-06-09 21:28:49 -04:00
|
|
|
ColumnCompiler.prototype.specifictype = function(type) {
|
2014-04-08 16:25:57 -04:00
|
|
|
return type;
|
|
|
|
};
|
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
// Modifiers
|
|
|
|
// -------
|
2014-04-08 16:25:57 -04:00
|
|
|
|
|
|
|
ColumnCompiler.prototype.nullable = function(nullable) {
|
|
|
|
return nullable === false ? 'not null' : 'null';
|
|
|
|
};
|
2014-04-15 11:43:47 -04:00
|
|
|
ColumnCompiler.prototype.notNullable = function() {
|
|
|
|
return this.nullable(false);
|
|
|
|
};
|
2014-04-08 16:25:57 -04:00
|
|
|
ColumnCompiler.prototype.defaultTo = function(value) {
|
2014-04-15 11:43:47 -04:00
|
|
|
if (value === void 0) {
|
|
|
|
return '';
|
2014-08-25 17:14:58 -04:00
|
|
|
} else if (value === null) {
|
|
|
|
value = "null";
|
2014-04-15 11:43:47 -04:00
|
|
|
} else if (value instanceof Raw) {
|
|
|
|
value = value.toQuery();
|
2014-06-03 02:23:20 -04:00
|
|
|
} else if (this.type === 'bool') {
|
2014-04-08 16:25:57 -04:00
|
|
|
if (value === 'false') value = 0;
|
2014-06-10 16:40:59 -04:00
|
|
|
value = "'" + (value ? 1 : 0) + "'";
|
2014-06-03 02:23:20 -04:00
|
|
|
} else if (this.type === 'json' && _.isObject(value)) {
|
|
|
|
return JSON.stringify(value);
|
2014-04-08 16:25:57 -04:00
|
|
|
} else {
|
|
|
|
value = "'" + value + "'";
|
|
|
|
}
|
|
|
|
return 'default ' + value;
|
|
|
|
};
|
2014-04-16 01:23:50 -04:00
|
|
|
ColumnCompiler.prototype._num = function(val, fallback) {
|
|
|
|
if (val == null) return fallback;
|
|
|
|
var number = parseInt(val, 10);
|
|
|
|
return isNaN(number) ? fallback : number;
|
|
|
|
};
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-08-25 17:14:58 -04:00
|
|
|
module.exports = ColumnCompiler;
|