2015-05-09 13:58:18 -04:00
// MySQL Column Compiler
// -------
2019-06-04 00:37:17 +02:00
const inherits = require ( 'inherits' ) ;
const ColumnCompiler = require ( '../../../schema/columncompiler' ) ;
2016-03-02 16:52:32 +01:00
2019-06-04 00:37:17 +02:00
const { isObject } = require ( 'lodash' ) ;
2015-05-09 13:58:18 -04:00
function ColumnCompiler _MySQL ( ) {
ColumnCompiler . apply ( this , arguments ) ;
2018-07-09 08:10:34 -04:00
this . modifiers = [
'unsigned' ,
'nullable' ,
'defaultTo' ,
'comment' ,
'collate' ,
'first' ,
'after' ,
] ;
2015-05-09 13:58:18 -04:00
}
inherits ( ColumnCompiler _MySQL , ColumnCompiler ) ;
// Types
// ------
2018-11-26 10:22:27 +01:00
Object . assign ( ColumnCompiler _MySQL . prototype , {
2015-05-09 13:58:18 -04:00
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 ) {
2018-07-09 08:10:34 -04:00
if ( ! precision ) return 'double' ;
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 ) {
2018-07-09 08:10:34 -04:00
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 ) {
2018-07-09 08:10:34 -04:00
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' :
2018-07-09 08:10:34 -04:00
return 'longtext' ;
2015-05-09 13:58:18 -04:00
default :
return 'text' ;
}
} ,
2016-05-17 01:01:34 +10:00
mediumtext ( ) {
2018-07-09 08:10:34 -04:00
return this . text ( 'medium' ) ;
2015-05-09 13:58:18 -04:00
} ,
2016-05-17 01:01:34 +10:00
longtext ( ) {
2018-07-09 08:10:34 -04:00
return this . text ( 'long' ) ;
2015-05-09 13:58:18 -04:00
} ,
2016-05-17 01:01:34 +10:00
enu ( allowed ) {
2018-07-09 08:10:34 -04:00
return ` enum(' ${ allowed . join ( "', '" ) } ') ` ;
2015-05-09 13:58:18 -04:00
} ,
2018-07-19 10:46:23 -04:00
datetime ( precision ) {
2018-11-26 10:22:27 +01:00
if ( isObject ( precision ) ) {
( { precision } = precision ) ;
}
2018-07-19 10:46:23 -04:00
return typeof precision === 'number'
? ` datetime( ${ precision } ) `
: 'datetime' ;
2018-04-18 21:14:29 +01:00
} ,
2015-05-09 13:58:18 -04:00
2018-07-19 10:46:23 -04:00
timestamp ( precision ) {
2018-11-26 10:22:27 +01:00
if ( isObject ( precision ) ) {
( { precision } = precision ) ;
}
2018-07-19 10:46:23 -04:00
return typeof precision === 'number'
? ` timestamp( ${ precision } ) `
2018-07-09 08:10:34 -04:00
: 'timestamp' ;
2018-04-18 21:14:29 +01:00
} ,
2015-05-09 13:58:18 -04:00
2018-07-19 10:46:23 -04:00
time ( precision ) {
2018-11-26 10:22:27 +01:00
if ( isObject ( precision ) ) {
( { precision } = precision ) ;
}
2018-07-19 10:46:23 -04:00
return typeof precision === 'number' ? ` time( ${ precision } ) ` : 'time' ;
} ,
2016-05-17 01:01:34 +10:00
bit ( length ) {
2018-07-09 08:10:34 -04:00
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 ) {
2018-07-09 08:10:34 -04:00
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 ) {
2019-03-30 14:58:56 +01:00
// 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 ;
}
2019-06-30 23:38:15 +05:30
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 ) } ') ` ;
}
2018-07-09 08:10:34 -04:00
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 ) {
2018-07-09 08:10:34 -04:00
return defaultVal ;
2015-05-09 13:58:18 -04:00
}
2018-07-09 08:10:34 -04:00
return '' ;
2015-05-09 13:58:18 -04:00
} ,
2016-03-08 08:41:13 +01:00
2016-05-17 01:01:34 +10:00
unsigned ( ) {
2018-07-09 08:10:34 -04:00
return 'unsigned' ;
2015-05-09 13:58:18 -04:00
} ,
2016-03-08 08:41:13 +01:00
2017-05-15 19:16:15 +09:00
comment ( comment ) {
if ( comment && comment . length > 255 ) {
2018-07-09 08:10:34 -04:00
this . client . logger . warn (
'Your comment is longer than the max comment length for MySQL'
) ;
2017-05-15 19:16:15 +09:00
}
2018-07-09 08:10:34 -04:00
return comment && ` comment ' ${ comment } ' ` ;
2017-05-15 19:16:15 +09:00
} ,
2016-05-17 01:01:34 +10:00
first ( ) {
2018-07-09 08:10:34 -04:00
return 'first' ;
2015-05-09 13:58:18 -04:00
} ,
2016-03-08 08:41:13 +01:00
2016-05-17 01:01:34 +10:00
after ( column ) {
2018-07-09 08:10:34 -04:00
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 ) {
2018-07-09 08:10:34 -04:00
return collation && ` collate ' ${ collation } ' ` ;
} ,
} ) ;
2015-05-09 13:58:18 -04:00
2019-06-04 00:37:17 +02:00
module . exports = ColumnCompiler _MySQL ;