2016-03-02 17:07:05 +01:00
|
|
|
// MySQL Schema Compiler
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import SchemaCompiler from '../../../schema/compiler';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
import { assign } from 'lodash';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function SchemaCompiler_MSSQL(client, builder) {
|
2018-07-09 08:10:34 -04:00
|
|
|
SchemaCompiler.call(this, client, builder);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
inherits(SchemaCompiler_MSSQL, SchemaCompiler);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
assign(SchemaCompiler_MSSQL.prototype, {
|
|
|
|
dropTablePrefix: 'DROP TABLE ',
|
2016-05-17 01:01:34 +10:00
|
|
|
dropTableIfExists(tableName) {
|
|
|
|
const name = this.formatter.wrap(prefixedTableName(this.schema, tableName));
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery(
|
|
|
|
`if object_id('${name}', 'U') is not null DROP TABLE ${name}`
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Rename a table on the schema.
|
2016-05-17 01:01:34 +10:00
|
|
|
renameTable(tableName, to) {
|
|
|
|
this.pushQuery(
|
2018-07-09 08:10:34 -04:00
|
|
|
`exec sp_rename ${this.formatter.parameter(
|
|
|
|
tableName
|
|
|
|
)}, ${this.formatter.parameter(to)}`
|
2016-05-17 01:01:34 +10:00
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Check whether a table exists on the query.
|
2016-05-17 01:01:34 +10:00
|
|
|
hasTable(tableName) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const formattedTable = this.formatter.parameter(
|
|
|
|
this.formatter.wrap(tableName)
|
|
|
|
);
|
2016-05-17 01:01:34 +10:00
|
|
|
const sql =
|
|
|
|
`select object_id from sys.tables ` +
|
|
|
|
`where object_id = object_id(${formattedTable})`;
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Check whether a column exists on the schema.
|
2016-05-17 01:01:34 +10:00
|
|
|
hasColumn(tableName, column) {
|
|
|
|
const formattedColumn = this.formatter.parameter(column);
|
2018-07-09 08:10:34 -04:00
|
|
|
const formattedTable = this.formatter.parameter(
|
|
|
|
this.formatter.wrap(tableName)
|
|
|
|
);
|
2016-05-17 01:01:34 +10:00
|
|
|
const sql =
|
|
|
|
`select object_id from sys.columns ` +
|
|
|
|
`where name = ${formattedColumn} ` +
|
|
|
|
`and object_id = object_id(${formattedTable})`;
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
|
|
|
|
},
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function prefixedTableName(prefix, table) {
|
|
|
|
return prefix ? `${prefix}.${table}` : table;
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default SchemaCompiler_MSSQL;
|