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

103 lines
2.0 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) {
2016-03-02 17:07:05 +01:00
if (!precision) return 'double'
return `double(${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: 'mediumint',
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) {
return length ? `bit(${this._num(length)})` : 'bit'
2016-03-02 17:07:05 +01:00
},
binary(length) {
return length ? `varbinary(${this._num(length)})` : 'blob'
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() {
2016-03-02 17:07:05 +01:00
return 'first'
},
after(column) {
return `after ${this.formatter.wrap(column)}`
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;