mirror of
https://github.com/knex/knex.git
synced 2025-07-11 19:11:26 +00:00
53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
![]() |
|
||
|
// MySQL Schema Compiler
|
||
|
// -------
|
||
|
var inherits = require('inherits');
|
||
|
var SchemaCompiler = require('../../../schema/compiler');
|
||
|
var assign = require('lodash/object/assign');
|
||
|
|
||
|
function SchemaCompiler_MSSQL(client, builder) {
|
||
|
SchemaCompiler.call(this, client, builder)
|
||
|
}
|
||
|
inherits(SchemaCompiler_MSSQL, SchemaCompiler)
|
||
|
|
||
|
assign(SchemaCompiler_MSSQL.prototype, {
|
||
|
|
||
|
dropTableIfExists: function(tableName) {
|
||
|
var name = this.formatter.wrap(prefixedTableName(this.schema, tableName));
|
||
|
this.pushQuery('if object_id(\'' + name +'\', \'U\') is not null drop table ' + name);
|
||
|
},
|
||
|
|
||
|
// Rename a table on the schema.
|
||
|
renameTable: function(tableName, to) {
|
||
|
this.pushQuery('rename table ' + this.formatter.wrap(tableName) + ' to ' + this.formatter.wrap(to));
|
||
|
},
|
||
|
|
||
|
// Check whether a table exists on the query.
|
||
|
hasTable: function(tableName) {
|
||
|
this.pushQuery({
|
||
|
sql: 'SELECT object_id FROM sys.tables WHERE object_id = Object_ID(N\'' + this.formatter.parameter(tableName) + '\')',
|
||
|
output: function(resp) {
|
||
|
return resp.length > 0;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Check whether a column exists on the schema.
|
||
|
hasColumn: function(tableName, column) {
|
||
|
this.pushQuery({
|
||
|
sql: 'SELECT object_id FROM sys.columns WHERE Name Like ' + this.formatter.parameter(column) +
|
||
|
' AND object_id = Object_ID(N\'' + this.formatter.wrap(tableName) + '\')',
|
||
|
output: function(resp) {
|
||
|
return resp.length > 0;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
})
|
||
|
|
||
|
function prefixedTableName(prefix, table) {
|
||
|
return prefix ? `${prefix}.${table}` : table;
|
||
|
}
|
||
|
|
||
|
module.exports = SchemaCompiler_MSSQL;
|