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');
|
2022-01-06 14:44:16 +01:00
|
|
|
const { operator: operator_ } = require('../../../formatter/wrappingFormatter');
|
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'];
|
2022-01-06 14:44:16 +01:00
|
|
|
this._addCheckModifiers();
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
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 } = {}) {
|
2021-02-28 17:27:48 +00:00
|
|
|
const formattedValue = formatDefault(value, this.type, this.client);
|
2021-02-27 23:08:15 +00:00
|
|
|
constraintName =
|
|
|
|
typeof constraintName !== 'undefined'
|
|
|
|
? constraintName
|
|
|
|
: `${
|
|
|
|
this.tableCompiler.tableNameRaw
|
|
|
|
}_${this.getColumnName()}_default`.toLowerCase();
|
2021-02-28 17:27:48 +00:00
|
|
|
if (this.columnBuilder._method === 'alter') {
|
|
|
|
this.pushAdditional(function () {
|
|
|
|
this.pushQuery(
|
|
|
|
`ALTER TABLE ${this.tableCompiler.tableName()} ADD CONSTRAINT ${this.formatter.wrap(
|
|
|
|
constraintName
|
|
|
|
)} DEFAULT ${formattedValue} FOR ${this.formatter.wrap(
|
|
|
|
this.getColumnName()
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return '';
|
|
|
|
}
|
2021-02-27 23:08:15 +00:00
|
|
|
if (!constraintName) {
|
2021-02-28 17:27:48 +00:00
|
|
|
return `DEFAULT ${formattedValue}`;
|
2021-02-27 23:08:15 +00:00
|
|
|
}
|
|
|
|
return `CONSTRAINT ${this.formatter.wrap(
|
|
|
|
constraintName
|
2021-02-28 17:27:48 +00:00
|
|
|
)} DEFAULT ${formattedValue}`;
|
2021-02-27 23:08:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 14:29:55 -04:00
|
|
|
comment(/** @type {string} */ comment) {
|
|
|
|
if (!comment) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-25 05:24:44 -04:00
|
|
|
// XXX: This is a byte limit, not character, so we cannot definitively say they'll exceed the limit without database collation info.
|
|
|
|
// (Yes, even if the column has its own collation, the sqlvariant still uses the database collation.)
|
|
|
|
// I'm not sure we even need to raise a warning, as MSSQL will return an error when the limit is exceeded itself.
|
|
|
|
if (comment && comment.length > 7500 / 2) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this.client.logger.warn(
|
2021-03-25 05:24:44 -04:00
|
|
|
'Your comment might be longer than the max comment length for MSSQL of 7,500 bytes.'
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2021-08-21 14:29:55 -04:00
|
|
|
|
|
|
|
// See: https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addextendedproperty-transact-sql?view=sql-server-ver15#b-adding-an-extended-property-to-a-column-in-a-table
|
|
|
|
const value = this.formatter.escapingStringDelimiters(comment);
|
|
|
|
const level0name = this.tableCompiler.schemaNameRaw || 'dbo';
|
|
|
|
const level1name = this.formatter.escapingStringDelimiters(
|
|
|
|
this.tableCompiler.tableNameRaw
|
|
|
|
);
|
|
|
|
const level2name = this.formatter.escapingStringDelimiters(
|
|
|
|
this.args[0] || this.defaults('columnName')
|
|
|
|
);
|
|
|
|
|
|
|
|
const args = `N'MS_Description', N'${value}', N'Schema', N'${level0name}', N'Table', N'${level1name}', N'Column', N'${level2name}'`;
|
|
|
|
|
|
|
|
this.pushAdditional(function () {
|
|
|
|
const isAlreadyDefined = `EXISTS(SELECT * FROM sys.fn_listextendedproperty(N'MS_Description', N'Schema', N'${level0name}', N'Table', N'${level1name}', N'Column', N'${level2name}'))`;
|
|
|
|
this.pushQuery(
|
|
|
|
`IF ${isAlreadyDefined}\n EXEC sys.sp_updateextendedproperty ${args}\nELSE\n EXEC sys.sp_addextendedproperty ${args}`
|
|
|
|
);
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
return '';
|
2021-01-01 20:35:54 +02:00
|
|
|
}
|
2022-01-03 20:55:24 +01:00
|
|
|
|
2022-01-06 14:44:16 +01:00
|
|
|
checkLength(operator, length, constraintName) {
|
|
|
|
return this._check(
|
|
|
|
`LEN(${this.formatter.wrap(this.getColumnName())}) ${operator_(
|
|
|
|
operator,
|
|
|
|
this.columnBuilder,
|
|
|
|
this.bindingsHolder
|
|
|
|
)} ${toNumber(length)}`,
|
|
|
|
constraintName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkRegex(regex, constraintName) {
|
|
|
|
return this._check(
|
|
|
|
`${this.formatter.wrap(
|
|
|
|
this.getColumnName()
|
|
|
|
)} LIKE ${this.client._escapeBinding('%' + regex + '%')}`,
|
|
|
|
constraintName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-03 20:55:24 +01:00
|
|
|
increments(options = { primaryKey: true }) {
|
|
|
|
return (
|
|
|
|
'int identity(1,1) not null' +
|
|
|
|
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bigincrements(options = { primaryKey: true }) {
|
|
|
|
return (
|
|
|
|
'bigint identity(1,1) not null' +
|
|
|
|
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' 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-12-22 10:47:16 +01:00
|
|
|
ColumnCompiler_MSSQL.prototype.json = ColumnCompiler_MSSQL.prototype.jsonb =
|
|
|
|
'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)';
|
2021-12-07 16:38:03 +01:00
|
|
|
ColumnCompiler_MSSQL.prototype.uuid = ({ useBinaryUuid = false } = {}) =>
|
|
|
|
useBinaryUuid ? 'binary(16)' : 'uniqueidentifier';
|
|
|
|
|
2021-01-01 20:35:54 +02:00
|
|
|
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;
|