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

129 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-12-08 11:37:31 -06:00
// MSSQL Table Builder & Compiler
// -------
var inherits = require('inherits');
2015-12-08 11:37:31 -06:00
var TableCompiler = require('../../../schema/tablecompiler');
var helpers = require('../../../helpers');
var Promise = require('../../../promise');
var assign = require('lodash/object/assign');
2015-12-08 11:37:31 -06:00
// Table Compiler
// ------
function TableCompiler_MSSQL() {
TableCompiler.apply(this, arguments);
}
inherits(TableCompiler_MSSQL, TableCompiler);
assign(TableCompiler_MSSQL.prototype, {
createAlterTableMethods: ['foreign', 'primary', 'unique'],
createQuery: function (columns, ifNot) {
var createStatement = ifNot ? 'if object_id(\'' + this.tableName() + '\', \'U\') is not null CREATE TABLE ' : 'CREATE TABLE ';
2015-12-14 11:00:51 -06:00
var sql = createStatement + this.tableName() + (this._formatting ? ' (\n ' : ' (') + columns.sql.join(this._formatting ? ',\n ' : ', ') + ')';
2015-12-08 11:37:31 -06:00
if (this.single.comment) {
var comment = (this.single.comment || '');
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
}
this.pushQuery(sql);
},
2015-12-14 09:56:53 -06:00
lowerCase: false,
2015-12-14 09:56:53 -06:00
addColumnsPrefix: 'ADD ',
2015-12-14 09:56:53 -06:00
dropColumnPrefix: 'DROP COLUMN ',
2015-12-08 11:37:31 -06:00
// Compiles the comment on the table.
comment: function () {
2015-12-08 11:37:31 -06:00
},
changeType: function () {
2015-12-08 11:37:31 -06:00
},
// Renames a column on the table.
renameColumn: function (from, to) {
this.pushQuery('exec sp_rename ' + this.formatter.parameter(this.tableName() + '.' + from) + ', ' + this.formatter.parameter(to) + ', \'COLUMN\'');
2015-12-08 11:37:31 -06:00
},
dropFKRefs: function (runner, refs) {
var formatter = this.client.formatter();
return Promise.all(refs.map(function (ref) {
var constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
var tableName = formatter.wrap(ref.TABLE_NAME);
2015-12-08 11:37:31 -06:00
return runner.query({
2015-12-14 09:56:53 -06:00
sql: 'ALTER TABLE ' + tableName + ' DROP CONSTRAINT ' + constraintName
2015-12-08 11:37:31 -06:00
});
}));
},
createFKRefs: function (runner, refs) {
var formatter = this.client.formatter();
2015-12-08 11:37:31 -06:00
return Promise.all(refs.map(function (ref) {
var tableName = formatter.wrap(ref.TABLE_NAME);
var keyName = formatter.wrap(ref.CONSTRAINT_NAME);
var column = formatter.columnize(ref.COLUMN_NAME);
2015-12-08 11:37:31 -06:00
var references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
var inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
var onUpdate = ' ON UPDATE ' + ref.UPDATE_RULE;
var onDelete = ' ON DELETE ' + ref.DELETE_RULE;
2015-12-08 11:37:31 -06:00
return runner.query({
sql: 'ALTER TABLE ' + tableName + ' ADD CONSTRAINT ' + keyName +
' FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete
2015-12-08 11:37:31 -06:00
});
}));
},
index: function (columns, indexName) {
2015-12-08 11:37:31 -06:00
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
2015-12-14 09:56:53 -06:00
this.pushQuery('CREATE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
2015-12-08 11:37:31 -06:00
},
primary: function (columns, indexName) {
2015-12-08 11:37:31 -06:00
indexName = indexName || this._indexCommand('primary', this.tableNameRaw, columns);
if (!this.forCreate) {
this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
} else {
this.pushQuery('CONSTRAINT ' + indexName + ' PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
}
2015-12-08 11:37:31 -06:00
},
unique: function (columns, indexName) {
2015-12-08 11:37:31 -06:00
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
if (!this.forCreate) {
this.pushQuery('CREATE UNIQUE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
} else {
this.pushQuery('CONSTRAINT ' + indexName + ' UNIQUE (' + this.formatter.columnize(columns) + ')');
}
2015-12-08 11:37:31 -06:00
},
// Compile a drop index command.
dropIndex: function (columns, indexName) {
2015-12-08 11:37:31 -06:00
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
2015-12-14 09:56:53 -06:00
this.pushQuery('DROP INDEX ' + indexName + ' ON ' + this.tableName());
2015-12-08 11:37:31 -06:00
},
// Compile a drop foreign key command.
dropForeign: function (columns, indexName) {
2015-12-08 11:37:31 -06:00
indexName = indexName || this._indexCommand('foreign', this.tableNameRaw, columns);
2015-12-14 09:56:53 -06:00
this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + indexName);
2015-12-08 11:37:31 -06:00
},
// Compile a drop primary key command.
dropPrimary: function () {
2015-12-14 09:56:53 -06:00
this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP PRIMARY KEY');
2015-12-08 11:37:31 -06:00
},
// Compile a drop unique key command.
dropUnique: function (column, indexName) {
2015-12-08 11:37:31 -06:00
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, column);
this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + indexName);
2015-12-08 11:37:31 -06:00
}
})
module.exports = TableCompiler_MSSQL;