2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// MySQL Column Compiler
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import ColumnCompiler from '../../../schema/columncompiler';
|
2016-03-02 16:52:32 +01:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign } from 'lodash'
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2018-04-18 21:14:29 +01:00
|
|
|
function supportsPreciseTimestamps(client) {
|
|
|
|
if (!client.version) {
|
|
|
|
const message = 'To get rid of this warning you should specify the mysql dialect version in ' +
|
|
|
|
'your knex configuration. Currently this defaults to 5.5, but in a future ' +
|
|
|
|
'release it will default to 5.6 which supports high precision timestamps. ' +
|
|
|
|
'See http://knexjs.org/#Schema-timestamps for more information.'
|
2018-05-29 17:42:03 +02:00
|
|
|
client.logger.warn(message)
|
2018-04-18 21:14:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.version && parseFloat(client.version) > 5.5
|
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
function ColumnCompiler_MySQL() {
|
|
|
|
ColumnCompiler.apply(this, arguments);
|
2018-01-23 19:06:25 -05:00
|
|
|
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',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
double(precision, scale) {
|
2015-05-09 13:58:18 -04:00
|
|
|
if (!precision) return 'double'
|
2016-05-17 01:01:34 +10:00
|
|
|
return `double(${this._num(precision, 8)}, ${this._num(scale, 2)})`
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
integer(length) {
|
|
|
|
length = length ? `(${this._num(length, 11)})` : ''
|
|
|
|
return `int${length}`
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
mediumint: 'mediumint',
|
|
|
|
|
|
|
|
smallint: 'smallint',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
tinyint(length) {
|
|
|
|
length = length ? `(${this._num(length, 1)})` : ''
|
|
|
|
return `tinyint${length}`
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10: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'
|
|
|
|
default:
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
mediumtext() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return this.text('medium')
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
longtext() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return this.text('long')
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
enu(allowed) {
|
|
|
|
return `enum('${allowed.join("', '")}')`
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
2018-04-18 21:14:29 +01:00
|
|
|
datetime() {
|
|
|
|
return supportsPreciseTimestamps(this.client) ? 'datetime(6)' : 'datetime'
|
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2018-04-18 21:14:29 +01:00
|
|
|
timestamp() {
|
|
|
|
return supportsPreciseTimestamps(this.client) ? 'timestamp(6)' : 'timestamp'
|
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
bit(length) {
|
|
|
|
return length ? `bit(${this._num(length)})` : 'bit'
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
binary(length) {
|
|
|
|
return length ? `varbinary(${this._num(length)})` : 'blob'
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
2018-05-30 17:26:03 +03:00
|
|
|
json() {
|
|
|
|
return 'json';
|
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
// Modifiers
|
|
|
|
// ------
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
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
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
},
|
2016-03-08 08:41:13 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
unsigned() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return 'unsigned'
|
|
|
|
},
|
2016-03-08 08:41:13 +01:00
|
|
|
|
2017-05-15 19:16:15 +09:00
|
|
|
comment(comment) {
|
|
|
|
if (comment && comment.length > 255) {
|
2018-05-29 17:42:03 +02:00
|
|
|
this.client.logger.warn('Your comment is longer than the max comment length for MySQL')
|
2017-05-15 19:16:15 +09:00
|
|
|
}
|
|
|
|
return comment && `comment '${comment}'`
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
first() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return 'first'
|
|
|
|
},
|
2016-03-08 08:41:13 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
after(column) {
|
|
|
|
return `after ${this.formatter.wrap(column)}`
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
2016-03-08 08:41:13 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
collate(collation) {
|
|
|
|
return collation && `collate '${collation}'`
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default ColumnCompiler_MySQL;
|