package liquibase.snapshot.jvm;

import liquibase.database.Database;
import liquibase.database.core.MSSQLDatabase;
import liquibase.database.structure.ForeignKeyConstraintType;
import liquibase.exception.DatabaseException;

public class MSSQLDatabaseSnapshotGenerator extends JdbcDatabaseSnapshotGenerator {
    public boolean supports(Database database) {
        return database instanceof MSSQLDatabase;
    }

    public int getPriority(Database database) {
        return PRIORITY_DATABASE;
    }

    @Override
    protected String convertTableNameToDatabaseTableName(String tableName) {
        return tableName;
    }

    @Override
    protected String convertColumnNameToDatabaseTableName(String columnName) {
        return columnName;
    }

    /**
     * The sp_fkeys stored procedure spec says that returned integer values of 0, 1 and 2 
     * translate to cascade, noAction and SetNull, which are not the values in the JDBC
     * standard. This override is a sticking plaster to stop invalid SQL from being generated.
     * 
     * @param JDBC foreign constraint type from JTDS (via sys.sp_fkeys)
     */
    @Override
    protected ForeignKeyConstraintType convertToForeignKeyConstraintType(int jdbcType) throws DatabaseException {
    	
        if (jdbcType == 0) {
            return ForeignKeyConstraintType.importedKeyCascade;
        } else if (jdbcType == 1) {
            return ForeignKeyConstraintType.importedKeyNoAction;
        } else if (jdbcType == 2) {
            return ForeignKeyConstraintType.importedKeySetNull;
        } else {
            throw new DatabaseException("Unknown constraint type: " + jdbcType);
        }
    }

}
@@ -221,16 +221,36 @@
 eption;%0D
+%0Aimport java.sql.*;%0D
 %0A%0D%0Apubli
@@ -1308,9 +1308,894 @@
     
-%09
+    // for new sql server driver 6.3 or above%0D%0A        if (jdbcType == DatabaseMetaData.importedKeyCascade) %7B%0D%0A            return ForeignKeyConstraintType.importedKeyCascade;%0D%0A        %7D else if (jdbcType == DatabaseMetaData.importedKeyNoAction) %7B%0D%0A            return ForeignKeyConstraintType.importedKeyNoAction;%0D%0A        %7D else if (jdbcType == DatabaseMetaData.importedKeyRestrict) %7B%0D%0A            return ForeignKeyConstraintType.importedKeyNoAction;%0D%0A        %7D else if (jdbcType == DatabaseMetaData.importedKeySetDefault) %7B%0D%0A            return ForeignKeyConstraintType.importedKeySetDefault;%0D%0A        %7D else if (jdbcType == DatabaseMetaData.importedKeySetNull) %7B%0D%0A            return ForeignKeyConstraintType.importedKeySetNull;%0D%0A        %7D else %7B%0D%0A            throw new DatabaseException(%22Unknown constraint type: %22 + jdbcType);%0D%0A        %7D%0D%0A%0D%0A/**%0D%0A        // for older sql server driver
 %0D%0A  
@@ -2603,18 +2603,20 @@
      %7D%0D%0A
+*/%0D%0A
     %7D%0D%0A
-%0D%0A
 %7D

Support sql server driver 6.3 or above. Note that the older drivers will not be supported anymore