2015-04-19 16:31:52 -04:00
|
|
|
|
|
|
|
// MySQL Column Compiler
|
|
|
|
// -------
|
2015-05-09 14:01:19 -04:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var inherits = require('inherits');
|
|
|
|
var ColumnCompiler = require('../../../schema/columncompiler');
|
|
|
|
var helpers = require('../../../helpers');
|
|
|
|
var assign = require('lodash/object/assign');
|
2015-04-19 16:31:52 -04:00
|
|
|
|
|
|
|
function ColumnCompiler_MySQL() {
|
|
|
|
ColumnCompiler.apply(this, arguments);
|
2015-05-09 13:58:18 -04:00
|
|
|
this.modifiers = ['unsigned', 'nullable', 'defaultTo', 'first', 'after', 'comment'];
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
|
|
|
inherits(ColumnCompiler_MySQL, ColumnCompiler);
|
|
|
|
|
|
|
|
// Types
|
|
|
|
// ------
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
assign(ColumnCompiler_MySQL.prototype, {
|
2015-04-19 16:31:52 -04:00
|
|
|
|
|
|
|
increments: 'int unsigned not null auto_increment primary key',
|
|
|
|
|
|
|
|
bigincrements: 'bigint unsigned not null auto_increment primary key',
|
|
|
|
|
|
|
|
bigint: 'bigint',
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
double: function double(precision, scale) {
|
|
|
|
if (!precision) return 'double';
|
|
|
|
return 'double(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
integer: function integer(length) {
|
|
|
|
length = length ? '(' + this._num(length, 11) + ')' : '';
|
|
|
|
return 'int' + length;
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
mediumint: 'mediumint',
|
|
|
|
|
|
|
|
smallint: 'smallint',
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
tinyint: function tinyint(length) {
|
|
|
|
length = length ? '(' + this._num(length, 1) + ')' : '';
|
|
|
|
return 'tinyint' + length;
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
text: function text(column) {
|
2015-04-19 16:31:52 -04:00
|
|
|
switch (column) {
|
|
|
|
case 'medium':
|
|
|
|
case 'mediumtext':
|
|
|
|
return 'mediumtext';
|
|
|
|
case 'long':
|
|
|
|
case 'longtext':
|
2015-05-09 13:58:18 -04:00
|
|
|
return 'longtext';
|
2015-04-19 16:31:52 -04:00
|
|
|
default:
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
mediumtext: function mediumtext() {
|
|
|
|
return this.text('medium');
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
longtext: function longtext() {
|
|
|
|
return this.text('long');
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
enu: function enu(allowed) {
|
|
|
|
return 'enum(\'' + allowed.join('\', \'') + '\')';
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
datetime: 'datetime',
|
|
|
|
|
|
|
|
timestamp: 'timestamp',
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
bit: function bit(length) {
|
|
|
|
return length ? 'bit(' + this._num(length) + ')' : 'bit';
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
2015-04-19 16:31:52 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
binary: function binary(length) {
|
|
|
|
return length ? 'varbinary(' + this._num(length) + ')' : 'blob';
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
2015-04-19 16:31:52 -04:00
|
|
|
|
|
|
|
// Modifiers
|
|
|
|
// ------
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
defaultTo: function defaultTo(value) {
|
2015-04-19 16:31:52 -04:00
|
|
|
/*jshint unused: false*/
|
2015-04-22 10:34:14 -04:00
|
|
|
var defaultVal = ColumnCompiler_MySQL.super_.prototype.defaultTo.apply(this, arguments);
|
2015-04-19 16:31:52 -04:00
|
|
|
if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return defaultVal;
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
return '';
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
unsigned: function unsigned() {
|
|
|
|
return 'unsigned';
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
first: function first() {
|
|
|
|
return 'first';
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
after: function after(column) {
|
|
|
|
return 'after ' + this.formatter.wrap(column);
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
comment: function comment(_comment) {
|
|
|
|
if (_comment && _comment.length > 255) {
|
|
|
|
helpers.warn('Your comment is longer than the max comment length for MySQL');
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
return _comment && 'comment \'' + _comment + '\'';
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-19 16:31:52 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
module.exports = ColumnCompiler_MySQL;
|