2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// MySQL Column Compiler
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import ColumnCompiler from '../../../schema/columncompiler';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function ColumnCompiler_MSSQL() {
|
|
|
|
ColumnCompiler.apply(this, arguments);
|
|
|
|
this.modifiers = ['nullable', 'defaultTo', 'first', 'after', 'comment']
|
|
|
|
}
|
|
|
|
inherits(ColumnCompiler_MSSQL, ColumnCompiler);
|
|
|
|
|
|
|
|
// Types
|
|
|
|
// ------
|
|
|
|
|
|
|
|
assign(ColumnCompiler_MSSQL.prototype, {
|
|
|
|
|
|
|
|
increments: 'int identity(1,1) not null primary key',
|
|
|
|
|
|
|
|
bigincrements: 'bigint identity(1,1) not null primary key',
|
|
|
|
|
|
|
|
bigint: 'bigint',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
double(precision, scale) {
|
2016-07-20 04:22:17 +03:00
|
|
|
if (!precision) return 'decimal'
|
|
|
|
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`
|
|
|
|
},
|
|
|
|
|
|
|
|
floating(precision, scale) {
|
|
|
|
if (!precision) return 'decimal'
|
|
|
|
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
integer(length) {
|
|
|
|
length = length ? `(${this._num(length, 11)})` : ''
|
|
|
|
return `int${length}`
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-07-20 04:22:17 +03:00
|
|
|
mediumint: 'int',
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
smallint: 'smallint',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
tinyint(length) {
|
|
|
|
length = length ? `(${this._num(length, 1)})` : ''
|
|
|
|
return `tinyint${length}`
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
varchar(length) {
|
|
|
|
return `nvarchar(${this._num(length, 255)})`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
text: 'nvarchar(max)',
|
|
|
|
|
|
|
|
mediumtext: 'nvarchar(max)',
|
|
|
|
|
|
|
|
longtext: 'nvarchar(max)',
|
|
|
|
|
2018-02-03 08:33:02 -05:00
|
|
|
// TODO: mssql supports check constraints as of SQL Server 2008
|
|
|
|
// so make enu here more like postgres
|
2016-03-02 17:07:05 +01:00
|
|
|
enu: 'nvarchar(100)',
|
|
|
|
|
|
|
|
uuid: 'uniqueidentifier',
|
|
|
|
|
|
|
|
datetime: 'datetime',
|
|
|
|
|
|
|
|
timestamp: 'datetime',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
bit(length) {
|
2016-07-20 04:22:17 +03:00
|
|
|
if (length > 1) {
|
2018-05-29 17:42:03 +02:00
|
|
|
this.client.logger.warn('Bit field is exactly 1 bit length for MSSQL');
|
2016-07-20 04:22:17 +03:00
|
|
|
}
|
|
|
|
return 'bit';
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
binary(length) {
|
2016-06-01 02:42:17 +03:00
|
|
|
return length ? `varbinary(${this._num(length)})` : 'varbinary(max)'
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
bool: 'bit',
|
|
|
|
|
|
|
|
// Modifiers
|
|
|
|
// ------
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
defaultTo(value) {
|
|
|
|
const defaultVal = ColumnCompiler_MSSQL.super_.prototype.defaultTo.apply(this, arguments);
|
2016-03-02 17:07:05 +01:00
|
|
|
if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
|
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
first() {
|
2018-05-29 17:42:03 +02:00
|
|
|
this.client.logger.warn('Column first modifier not available for MSSQL');
|
2016-07-20 04:22:17 +03:00
|
|
|
return '';
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
after(column) {
|
2018-05-29 17:42:03 +02:00
|
|
|
this.client.logger.warn('Column after modifier not available for MSSQL');
|
2016-07-20 04:22:17 +03:00
|
|
|
return '';
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
comment(comment) {
|
2016-03-02 17:07:05 +01:00
|
|
|
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 MSSQL')
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default ColumnCompiler_MSSQL;
|