2015-12-08 11:37:31 -06:00
// MySQL Schema Compiler
// -------
'use strict' ;
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 , {
2015-12-14 09:56:53 -06:00
dropTablePrefix : 'DROP TABLE ' ,
2015-12-08 11:37:31 -06:00
dropTableIfExists : function dropTableIfExists ( tableName ) {
var name = this . formatter . wrap ( prefixedTableName ( this . schema , tableName ) ) ;
2015-12-14 09:56:53 -06:00
this . pushQuery ( 'if object_id(\'' + name + '\', \'U\') is not null DROP TABLE ' + name ) ;
2015-12-08 11:37:31 -06:00
} ,
// Rename a table on the schema.
renameTable : function renameTable ( tableName , to ) {
2015-12-09 17:53:53 -06:00
this . pushQuery ( 'exec sp_rename ' + this . formatter . parameter ( tableName ) + ', ' + this . formatter . parameter ( to ) ) ;
2015-12-08 11:37:31 -06:00
} ,
// Check whether a table exists on the query.
hasTable : function hasTable ( tableName ) {
this . pushQuery ( {
2015-12-09 17:53:53 -06:00
sql : 'select object_id from sys.tables where object_id = object_id(' + this . formatter . parameter ( this . formatter . wrap ( tableName ) ) + ')' ,
2015-12-08 11:37:31 -06:00
output : function output ( resp ) {
return resp . length > 0 ;
}
} ) ;
} ,
// Check whether a column exists on the schema.
hasColumn : function hasColumn ( tableName , column ) {
this . pushQuery ( {
2015-12-09 17:53:53 -06:00
sql : 'select object_id from sys.columns where name = ' + this . formatter . parameter ( column ) + ' and object_id = object_id(' + this . formatter . parameter ( this . formatter . wrap ( tableName ) ) + ')' ,
2015-12-08 11:37:31 -06:00
output : function output ( resp ) {
return resp . length > 0 ;
}
} ) ;
}
} ) ;
function prefixedTableName ( prefix , table ) {
return prefix ? prefix + '.' + table : table ;
}
module . exports = SchemaCompiler _MSSQL ;