2016-03-02 17:07:05 +01:00
|
|
|
// MySQL Schema Compiler
|
|
|
|
// -------
|
2019-06-04 00:37:17 +02:00
|
|
|
const SchemaCompiler = require('../../../schema/compiler');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2021-01-01 17:46:10 +02:00
|
|
|
class SchemaCompiler_MSSQL extends SchemaCompiler {
|
|
|
|
constructor(client, builder) {
|
|
|
|
super(client, builder);
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
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}`
|
|
|
|
);
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
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(
|
2021-02-04 15:54:26 +02:00
|
|
|
`exec sp_rename ${this.client.parameter(
|
|
|
|
prefixedTableName(this.schema, tableName),
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
|
|
|
)}, ${this.client.parameter(to, this.builder, this.bindingsHolder)}`
|
2016-05-17 01:01:34 +10:00
|
|
|
);
|
2021-01-01 17:46:10 +02: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) {
|
2021-02-04 15:54:26 +02:00
|
|
|
const formattedTable = this.client.parameter(
|
|
|
|
this.formatter.wrap(prefixedTableName(this.schema, tableName)),
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2018-07-04 11:28:47 +02:00
|
|
|
|
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 });
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
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) {
|
2021-02-04 15:54:26 +02:00
|
|
|
const formattedColumn = this.client.parameter(
|
|
|
|
column,
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
|
|
|
);
|
|
|
|
const formattedTable = this.client.parameter(
|
|
|
|
this.formatter.wrap(prefixedTableName(this.schema, tableName)),
|
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
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 });
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SchemaCompiler_MSSQL.prototype.dropTablePrefix = 'DROP TABLE ';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function prefixedTableName(prefix, table) {
|
|
|
|
return prefix ? `${prefix}.${table}` : table;
|
|
|
|
}
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = SchemaCompiler_MSSQL;
|