knex/src/dialects/mysql/schema/columncompiler.js

145 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-05-09 13:58:18 -04:00
// MySQL Column Compiler
// -------
import inherits from 'inherits';
import ColumnCompiler from '../../../schema/columncompiler';
2016-03-02 16:52:32 +01:00
import { assign } from 'lodash';
2015-05-09 13:58:18 -04:00
function ColumnCompiler_MySQL() {
ColumnCompiler.apply(this, arguments);
this.modifiers = [
'unsigned',
'nullable',
'defaultTo',
'comment',
'collate',
'first',
'after',
];
2015-05-09 13:58:18 -04:00
}
inherits(ColumnCompiler_MySQL, ColumnCompiler);
// Types
// ------
assign(ColumnCompiler_MySQL.prototype, {
increments: 'int unsigned not null auto_increment primary key',
bigincrements: 'bigint unsigned not null auto_increment primary key',
bigint: 'bigint',
double(precision, scale) {
if (!precision) return 'double';
return `double(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
2015-05-09 13:58:18 -04:00
},
integer(length) {
length = length ? `(${this._num(length, 11)})` : '';
return `int${length}`;
2015-05-09 13:58:18 -04:00
},
mediumint: 'mediumint',
smallint: 'smallint',
tinyint(length) {
length = length ? `(${this._num(length, 1)})` : '';
return `tinyint${length}`;
2015-05-09 13:58:18 -04:00
},
text(column) {
2015-05-09 13:58:18 -04:00
switch (column) {
case 'medium':
case 'mediumtext':
return 'mediumtext';
case 'long':
case 'longtext':
return 'longtext';
2015-05-09 13:58:18 -04:00
default:
return 'text';
}
},
mediumtext() {
return this.text('medium');
2015-05-09 13:58:18 -04:00
},
longtext() {
return this.text('long');
2015-05-09 13:58:18 -04:00
},
enu(allowed) {
return `enum('${allowed.join("', '")}')`;
2015-05-09 13:58:18 -04:00
},
datetime(precision) {
return typeof precision === 'number'
? `datetime(${precision})`
: 'datetime';
},
2015-05-09 13:58:18 -04:00
timestamp(precision) {
return typeof precision === 'number'
? `timestamp(${precision})`
: 'timestamp';
},
2015-05-09 13:58:18 -04:00
time(precision) {
return typeof precision === 'number' ? `time(${precision})` : 'time';
},
bit(length) {
return length ? `bit(${this._num(length)})` : 'bit';
2015-05-09 13:58:18 -04:00
},
binary(length) {
return length ? `varbinary(${this._num(length)})` : 'blob';
2015-05-09 13:58:18 -04:00
},
json() {
return 'json';
},
2015-05-09 13:58:18 -04:00
// Modifiers
// ------
defaultTo(value) {
const defaultVal = ColumnCompiler_MySQL.super_.prototype.defaultTo.apply(
this,
arguments
);
2015-05-09 13:58:18 -04:00
if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
return defaultVal;
2015-05-09 13:58:18 -04:00
}
return '';
2015-05-09 13:58:18 -04:00
},
unsigned() {
return 'unsigned';
2015-05-09 13:58:18 -04:00
},
comment(comment) {
if (comment && comment.length > 255) {
this.client.logger.warn(
'Your comment is longer than the max comment length for MySQL'
);
}
return comment && `comment '${comment}'`;
},
first() {
return 'first';
2015-05-09 13:58:18 -04:00
},
after(column) {
return `after ${this.formatter.wrap(column)}`;
2015-05-09 13:58:18 -04:00
},
collate(collation) {
return collation && `collate '${collation}'`;
},
});
2015-05-09 13:58:18 -04:00
export default ColumnCompiler_MySQL;