Coverage Report - liquibase.sqlgenerator.core.FindForeignKeyConstraintsGeneratorMSSQL
 
Classes in this File Line Coverage Branch Coverage Complexity
FindForeignKeyConstraintsGeneratorMSSQL
16%
3/18
N/A
1
 
 1  
 package liquibase.sqlgenerator.core;
 2  
 
 3  
 import liquibase.database.Database;
 4  
 import liquibase.database.core.MSSQLDatabase;
 5  
 import liquibase.exception.ValidationErrors;
 6  
 import liquibase.sql.Sql;
 7  
 import liquibase.sql.UnparsedSql;
 8  
 import liquibase.sqlgenerator.SqlGenerator;
 9  
 import liquibase.sqlgenerator.SqlGeneratorChain;
 10  
 import liquibase.statement.core.FindForeignKeyConstraintsStatement;
 11  
 
 12  11
 public class FindForeignKeyConstraintsGeneratorMSSQL extends AbstractSqlGenerator<FindForeignKeyConstraintsStatement> {
 13  
     @Override
 14  
     public int getPriority() {
 15  1
         return PRIORITY_DATABASE;
 16  
     }
 17  
 
 18  
     @Override
 19  
     public boolean supports(FindForeignKeyConstraintsStatement statement, Database database) {
 20  1
         return database instanceof MSSQLDatabase;
 21  
     }
 22  
 
 23  
     public ValidationErrors validate(FindForeignKeyConstraintsStatement findForeignKeyConstraintsStatement,
 24  
             Database database, SqlGeneratorChain sqlGeneratorChain) {
 25  0
         ValidationErrors validationErrors = new ValidationErrors();
 26  0
         validationErrors.checkRequiredField("baseTableName", findForeignKeyConstraintsStatement.getBaseTableName());
 27  0
         return validationErrors;
 28  
     }
 29  
 
 30  
     public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database,
 31  
             SqlGeneratorChain sqlGeneratorChain) {
 32  0
         StringBuilder sb = new StringBuilder();
 33  
 
 34  0
         sb.append("SELECT TOP 1");
 35  0
         sb.append("OBJECT_NAME(f.parent_object_id) AS ")
 36  
                 .append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
 37  0
         sb.append("COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ")
 38  
                 .append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", ");
 39  0
         sb.append("OBJECT_NAME (f.referenced_object_id) AS ")
 40  
                 .append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", ");
 41  0
         sb.append("COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ")
 42  
                 .append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(",");
 43  0
         sb.append("f.name AS ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
 44  0
         sb.append("FROM sys.foreign_keys AS f ");
 45  0
         sb.append("INNER JOIN sys.foreign_key_columns AS fc ");
 46  0
         sb.append("ON f.OBJECT_ID = fc.constraint_object_id ");
 47  0
         sb.append("WHERE OBJECT_NAME(f.parent_object_id) = '").append(statement.getBaseTableName()).append("'");
 48  
 
 49  0
         return new Sql[] { new UnparsedSql(sb.toString()) };
 50  
     }
 51  
 }