2020-12-27 16:30:40 +02:00
|
|
|
// MSSQL Column Compiler
|
2016-03-02 17:07:05 +01:00
|
|
|
// -------
|
2019-06-04 00:37:17 +02:00
|
|
|
const ColumnCompiler = require('../../../schema/columncompiler');
|
2021-01-06 23:21:10 +02:00
|
|
|
const { toNumber } = require('../../../util/helpers');
|
2021-02-27 23:08:15 +00:00
|
|
|
const { formatDefault } = require('../../../formatter/formatterUtils');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
class ColumnCompiler_MSSQL extends ColumnCompiler {
|
2021-02-04 15:54:26 +02:00
|
|
|
constructor(client, tableCompiler, columnBuilder) {
|
|
|
|
super(client, tableCompiler, columnBuilder);
|
2021-01-01 20:35:54 +02:00
|
|
|
this.modifiers = ['nullable', 'defaultTo', 'first', 'after', 'comment'];
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
// Types
|
|
|
|
// ------
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
double(precision, scale) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return 'float';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-07-20 04:22:17 +03:00
|
|
|
|
|
|
|
floating(precision, scale) {
|
2018-06-29 10:47:06 +03:00
|
|
|
// ignore precicion / scale which is mysql specific stuff
|
|
|
|
return `float`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-03-05 08:02:06 +08:00
|
|
|
integer() {
|
|
|
|
// mssql does not support length
|
|
|
|
return 'int';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-03-05 08:02:06 +08:00
|
|
|
tinyint() {
|
|
|
|
// mssql does not support length
|
|
|
|
return 'tinyint';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
varchar(length) {
|
2021-01-06 23:21:10 +02:00
|
|
|
return `nvarchar(${toNumber(length, 255)})`;
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-08-23 07:10:19 -03:00
|
|
|
timestamp({ useTz = false } = {}) {
|
|
|
|
return useTz ? 'datetimeoffset' : 'datetime2';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
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';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
binary(length) {
|
2021-01-06 23:21:10 +02:00
|
|
|
return length ? `varbinary(${toNumber(length)})` : 'varbinary(max)';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Modifiers
|
|
|
|
// ------
|
|
|
|
|
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 '';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
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 '';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-02-27 23:08:15 +00:00
|
|
|
defaultTo(value, { constraintName } = {}) {
|
|
|
|
const formatedValue = formatDefault(value, this.type, this.client);
|
|
|
|
constraintName =
|
|
|
|
typeof constraintName !== 'undefined'
|
|
|
|
? constraintName
|
|
|
|
: `${
|
|
|
|
this.tableCompiler.tableNameRaw
|
|
|
|
}_${this.getColumnName()}_default`.toLowerCase();
|
|
|
|
if (!constraintName) {
|
|
|
|
return `DEFAULT ${formatedValue}`;
|
|
|
|
}
|
|
|
|
return `CONSTRAINT ${this.formatter.wrap(
|
|
|
|
constraintName
|
|
|
|
)} DEFAULT ${formatedValue}`;
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
comment(comment) {
|
2016-03-02 17:07:05 +01:00
|
|
|
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 MSSQL'
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return '';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:47:32 +01:00
|
|
|
ColumnCompiler_MSSQL.prototype.increments = ({ primaryKey = true } = {}) =>
|
|
|
|
'int identity(1,1) not null' + (primaryKey ? ' primary key' : '');
|
|
|
|
ColumnCompiler_MSSQL.prototype.bigincrements = ({ primaryKey = true } = {}) =>
|
|
|
|
'bigint identity(1,1) not null' + (primaryKey ? ' primary key' : '');
|
2021-01-01 20:35:54 +02:00
|
|
|
ColumnCompiler_MSSQL.prototype.bigint = 'bigint';
|
|
|
|
ColumnCompiler_MSSQL.prototype.mediumint = 'int';
|
|
|
|
ColumnCompiler_MSSQL.prototype.smallint = 'smallint';
|
|
|
|
ColumnCompiler_MSSQL.prototype.text = 'nvarchar(max)';
|
|
|
|
ColumnCompiler_MSSQL.prototype.mediumtext = 'nvarchar(max)';
|
|
|
|
ColumnCompiler_MSSQL.prototype.longtext = 'nvarchar(max)';
|
2021-02-08 18:54:51 +10:00
|
|
|
ColumnCompiler_MSSQL.prototype.json = 'nvarchar(max)';
|
2021-01-01 20:35:54 +02:00
|
|
|
|
|
|
|
// TODO: mssql supports check constraints as of SQL Server 2008
|
|
|
|
// so make enu here more like postgres
|
|
|
|
ColumnCompiler_MSSQL.prototype.enu = 'nvarchar(100)';
|
|
|
|
ColumnCompiler_MSSQL.prototype.uuid = 'uniqueidentifier';
|
|
|
|
ColumnCompiler_MSSQL.prototype.datetime = 'datetime2';
|
|
|
|
ColumnCompiler_MSSQL.prototype.bool = 'bit';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = ColumnCompiler_MSSQL;
|