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

150 lines
5.5 KiB
JavaScript
Raw Normal View History

/* eslint max-len:0 */
2016-03-02 17:07:05 +01:00
// MSSQL Table Builder & Compiler
// -------
import inherits from 'inherits';
import TableCompiler from '../../../schema/tablecompiler';
import * as helpers from '../../../helpers';
import Promise from '../../../promise';
2016-03-02 17:07:05 +01:00
import { assign } from 'lodash'
2016-03-02 17:07:05 +01:00
// Table Compiler
// ------
function TableCompiler_MSSQL() {
TableCompiler.apply(this, arguments);
}
inherits(TableCompiler_MSSQL, TableCompiler);
assign(TableCompiler_MSSQL.prototype, {
createAlterTableMethods: ['foreign', 'primary', 'unique'],
createQuery (columns, ifNot) {
const createStatement = ifNot ? `if object_id('${this.tableName()}', 'U') is null CREATE TABLE ` : 'CREATE TABLE ';
const sql = createStatement + this.tableName() + (this._formatting ? ' (\n ' : ' (') + columns.sql.join(this._formatting ? ',\n ' : ', ') + ')';
2016-03-02 17:07:05 +01:00
if (this.single.comment) {
const comment = (this.single.comment || '');
2016-03-02 17:07:05 +01:00
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
}
this.pushQuery(sql);
},
lowerCase: false,
addColumnsPrefix: 'ADD ',
dropColumnPrefix: 'DROP COLUMN ',
// Compiles column add. Multiple columns need only one ADD clause (not one ADD per column) so core addColumns doesn't work. #1348
addColumns (columns) {
if (columns.sql.length > 0) {
this.pushQuery({
sql: (this.lowerCase ? 'alter table ' : 'ALTER TABLE ') + this.tableName() + ' ' + this.addColumnsPrefix + columns.sql.join(', '),
bindings: columns.bindings
});
}
},
// Compiles column drop. Multiple columns need only one DROP clause (not one DROP per column) so core dropColumn doesn't work. #1348
dropColumn () {
const _this2 = this;
const columns = helpers.normalizeArr.apply(null, arguments);
const drops = (Array.isArray(columns) ? columns : [columns]).map(column => _this2.formatter.wrap(column));
this.pushQuery((this.lowerCase ? 'alter table ' : 'ALTER TABLE ') + this.tableName() + ' ' + this.dropColumnPrefix + drops.join(', '));
},
2016-03-02 17:07:05 +01:00
// Compiles the comment on the table.
comment () {
2016-03-02 17:07:05 +01:00
},
changeType () {
2016-03-02 17:07:05 +01:00
},
// Renames a column on the table.
renameColumn (from, to) {
this.pushQuery(`exec sp_rename ${this.formatter.parameter(this.tableName() + '.' + from)}, ${this.formatter.parameter(to)}, 'COLUMN'`);
2016-03-02 17:07:05 +01:00
},
dropFKRefs (runner, refs) {
const formatter = this.client.formatter();
2016-03-02 17:07:05 +01:00
return Promise.all(refs.map(function (ref) {
const constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
const tableName = formatter.wrap(ref.TABLE_NAME);
2016-03-02 17:07:05 +01:00
return runner.query({
sql: `ALTER TABLE ${tableName} DROP CONSTRAINT ${constraintName}`
2016-03-02 17:07:05 +01:00
});
}));
},
createFKRefs (runner, refs) {
const formatter = this.client.formatter();
2016-03-02 17:07:05 +01:00
return Promise.all(refs.map(function (ref) {
const tableName = formatter.wrap(ref.TABLE_NAME);
const keyName = formatter.wrap(ref.CONSTRAINT_NAME);
const column = formatter.columnize(ref.COLUMN_NAME);
const references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
const inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
const onUpdate = ` ON UPDATE ${ref.UPDATE_RULE}`;
const onDelete = ` ON DELETE ${ref.DELETE_RULE}`;
2016-03-02 17:07:05 +01:00
return runner.query({
sql: `ALTER TABLE ${tableName} ADD CONSTRAINT ${keyName}` +
2016-03-02 17:07:05 +01:00
' FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete
});
}));
},
index (columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`CREATE INDEX ${indexName} ON ${this.tableName()} (${this.formatter.columnize(columns)})`);
2016-03-02 17:07:05 +01:00
},
primary (columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('primary', this.tableNameRaw, columns);
2016-03-02 17:07:05 +01:00
if (!this.forCreate) {
this.pushQuery(`ALTER TABLE ${this.tableName()} ADD PRIMARY KEY (${this.formatter.columnize(columns)})`);
2016-03-02 17:07:05 +01:00
} else {
this.pushQuery(`CONSTRAINT ${indexName} PRIMARY KEY (${this.formatter.columnize(columns)})`);
2016-03-02 17:07:05 +01:00
}
},
unique (columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
2016-03-02 17:07:05 +01:00
if (!this.forCreate) {
this.pushQuery(`CREATE UNIQUE INDEX ${indexName} ON ${this.tableName()} (${this.formatter.columnize(columns)})`);
2016-03-02 17:07:05 +01:00
} else {
this.pushQuery(`CONSTRAINT ${indexName} UNIQUE (${this.formatter.columnize(columns)})`);
2016-03-02 17:07:05 +01:00
}
},
// Compile a drop index command.
dropIndex (columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(`DROP INDEX ${indexName} ON ${this.tableName()}`);
2016-03-02 17:07:05 +01:00
},
// Compile a drop foreign key command.
dropForeign (columns, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
this.pushQuery(`ALTER TABLE ${this.tableName()} DROP CONSTRAINT ${indexName}`);
2016-03-02 17:07:05 +01:00
},
// Compile a drop primary key command.
dropPrimary () {
this.pushQuery(`ALTER TABLE ${this.tableName()} DROP PRIMARY KEY`);
2016-03-02 17:07:05 +01:00
},
// Compile a drop unique key command.
dropUnique (column, indexName) {
2015-07-15 23:16:30 -03:00
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, column);
this.pushQuery(`ALTER TABLE ${this.tableName()} DROP CONSTRAINT ${indexName}`);
2016-03-02 17:07:05 +01:00
}
})
export default TableCompiler_MSSQL;