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

113 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
// MySQL Column Compiler
// -------
import inherits from 'inherits';
import ColumnCompiler from '../../../schema/columncompiler';
import * as helpers from '../../../helpers';
2016-03-02 17:07:05 +01: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',
double(precision, scale) {
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
},
integer(length) {
length = length ? `(${this._num(length, 11)})` : ''
return `int${length}`
2016-03-02 17:07:05 +01:00
},
mediumint: 'int',
2016-03-02 17:07:05 +01:00
smallint: 'smallint',
tinyint(length) {
length = length ? `(${this._num(length, 1)})` : ''
return `tinyint${length}`
2016-03-02 17:07:05 +01: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)',
enu: 'nvarchar(100)',
uuid: 'uniqueidentifier',
datetime: 'datetime',
timestamp: 'datetime',
bit(length) {
if (length > 1) {
helpers.warn('Bit field is exactly 1 bit length for MSSQL');
}
return 'bit';
2016-03-02 17:07:05 +01: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
// ------
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 ''
},
first() {
helpers.warn('Column first modifier not available for MSSQL');
return '';
2016-03-02 17:07:05 +01:00
},
after(column) {
helpers.warn('Column after modifier not available for MSSQL');
return '';
2016-03-02 17:07:05 +01:00
},
comment(comment) {
2016-03-02 17:07:05 +01:00
if (comment && comment.length > 255) {
helpers.warn('Your comment is longer than the max comment length for MSSQL')
}
return ''
}
})
export default ColumnCompiler_MSSQL;