knex/lib/dialects/mysql/schema/mysql-columncompiler.js

194 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2015-05-09 13:58:18 -04:00
// MySQL Column Compiler
// -------
const ColumnCompiler = require('../../../schema/columncompiler');
const { isObject } = require('../../../util/is');
const { toNumber } = require('../../../util/helpers');
2015-05-09 13:58:18 -04:00
const commentEscapeRegex = /(?<!\\)'/g;
class ColumnCompiler_MySQL extends ColumnCompiler {
constructor(client, tableCompiler, columnBuilder) {
super(client, tableCompiler, columnBuilder);
this.modifiers = [
'unsigned',
'nullable',
'defaultTo',
'comment',
'collate',
'first',
'after',
];
this._addCheckModifiers();
}
// Types
// ------
2015-05-09 13:58:18 -04:00
double(precision, scale) {
if (!precision) return 'double';
return `double(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
}
2015-05-09 13:58:18 -04:00
integer(length) {
length = length ? `(${toNumber(length, 11)})` : '';
return `int${length}`;
}
2015-05-09 13:58:18 -04:00
tinyint(length) {
length = length ? `(${toNumber(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';
}
}
2015-05-09 13:58:18 -04:00
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) {
if (isObject(precision)) {
({ precision } = precision);
}
return typeof precision === 'number'
? `datetime(${precision})`
: 'datetime';
}
2015-05-09 13:58:18 -04:00
timestamp(precision) {
if (isObject(precision)) {
({ precision } = precision);
}
return typeof precision === 'number'
? `timestamp(${precision})`
: 'timestamp';
}
2015-05-09 13:58:18 -04:00
time(precision) {
if (isObject(precision)) {
({ precision } = precision);
}
return typeof precision === 'number' ? `time(${precision})` : 'time';
}
bit(length) {
return length ? `bit(${toNumber(length)})` : 'bit';
}
2015-05-09 13:58:18 -04:00
binary(length) {
return length ? `varbinary(${toNumber(length)})` : 'blob';
}
2015-05-09 13:58:18 -04:00
json() {
return 'json';
}
jsonb() {
return 'json';
}
2015-05-09 13:58:18 -04:00
// Modifiers
// ------
defaultTo(value) {
// MySQL defaults to null by default, but breaks down if you pass it explicitly
// Note that in MySQL versions up to 5.7, logic related to updating
// timestamps when no explicit value is passed is quite insane - https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
if (value === null || value === undefined) {
return;
}
if ((this.type === 'json' || this.type === 'jsonb') && isObject(value)) {
// Default value for json will work only it is an expression
return `default ('${JSON.stringify(value)}')`;
}
const defaultVal = super.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 '';
}
unsigned() {
return 'unsigned';
}
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.replace(commentEscapeRegex, "\\'")}'`;
}
first() {
return 'first';
}
after(column) {
return `after ${this.formatter.wrap(column)}`;
}
collate(collation) {
return collation && `collate '${collation}'`;
}
checkRegex(regex, constraintName) {
return this._check(
`${this.formatter.wrap(
this.getColumnName()
)} REGEXP ${this.client._escapeBinding(regex)}`,
constraintName
);
}
increments(options = { primaryKey: true }) {
return (
'int unsigned not null' +
// In MySQL autoincrement are always a primary key. If you already have a primary key, we
// initialize this column as classic int column then modify it later in table compiler
(this.tableCompiler._canBeAddPrimaryKey(options)
? ' auto_increment primary key'
: '')
);
}
bigincrements(options = { primaryKey: true }) {
return (
'bigint unsigned not null' +
// In MySQL autoincrement are always a primary key. If you already have a primary key, we
// initialize this column as classic int column then modify it later in table compiler
(this.tableCompiler._canBeAddPrimaryKey(options)
? ' auto_increment primary key'
: '')
);
}
}
ColumnCompiler_MySQL.prototype.bigint = 'bigint';
ColumnCompiler_MySQL.prototype.mediumint = 'mediumint';
ColumnCompiler_MySQL.prototype.smallint = 'smallint';
2015-05-09 13:58:18 -04:00
module.exports = ColumnCompiler_MySQL;