Coverage Report - liquibase.sqlgenerator.core.DeleteGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
DeleteGenerator
8%
2/24
5%
1/18
3
 
 1  
 package liquibase.sqlgenerator.core;
 2  
 
 3  
 import liquibase.database.Database;
 4  
 import liquibase.database.typeconversion.TypeConverterFactory;
 5  
 import liquibase.database.core.SQLiteDatabase;
 6  
 import liquibase.exception.ValidationErrors;
 7  
 import liquibase.sql.Sql;
 8  
 import liquibase.sql.UnparsedSql;
 9  
 import liquibase.sqlgenerator.SqlGenerator;
 10  
 import liquibase.sqlgenerator.SqlGeneratorChain;
 11  
 import liquibase.statement.core.DeleteStatement;
 12  
 
 13  
 import java.util.Date;
 14  
 
 15  11
 public class DeleteGenerator extends AbstractSqlGenerator<DeleteStatement> {
 16  
 
 17  
     @Override
 18  
     public boolean supports(DeleteStatement statement, Database database) {
 19  1
         return !(database instanceof SQLiteDatabase);
 20  
     }
 21  
 
 22  
     public ValidationErrors validate(DeleteStatement deleteStatement, Database database,
 23  
             SqlGeneratorChain sqlGeneratorChain) {
 24  0
         ValidationErrors validationErrors = new ValidationErrors();
 25  0
         validationErrors.checkRequiredField("tableName", deleteStatement.getTableName());
 26  0
         return validationErrors;
 27  
     }
 28  
 
 29  
     public Sql[] generateSql(DeleteStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
 30  0
         StringBuffer sql = new StringBuffer("DELETE FROM "
 31  
                 + database.escapeTableName(statement.getSchemaName(), statement.getTableName()));
 32  
 
 33  0
         if (statement.getWhereClause() != null) {
 34  0
             String fixedWhereClause = " WHERE " + statement.getWhereClause();
 35  0
             for (Object param : statement.getWhereParameters()) {
 36  0
                 fixedWhereClause = fixedWhereClause.replaceFirst("\\?", convertToString(param, database));
 37  
             }
 38  0
             sql.append(" ").append(fixedWhereClause);
 39  
         }
 40  
 
 41  0
         return new Sql[] { new UnparsedSql(sql.toString()) };
 42  
     }
 43  
 
 44  
     private String convertToString(Object newValue, Database database) {
 45  
         String sqlString;
 46  0
         if (newValue == null) {
 47  0
             sqlString = "NULL";
 48  0
         } else if (newValue instanceof String && database.shouldQuoteValue(((String) newValue))) {
 49  0
             sqlString = "'" + newValue + "'";
 50  0
         } else if (newValue instanceof Date) {
 51  0
             sqlString = database.getDateLiteral(((Date) newValue));
 52  0
         } else if (newValue instanceof Boolean) {
 53  0
             if (((Boolean) newValue)) {
 54  0
                 sqlString = TypeConverterFactory.getInstance().findTypeConverter(database).getBooleanType()
 55  
                         .getTrueBooleanValue();
 56  
             } else {
 57  0
                 sqlString = TypeConverterFactory.getInstance().findTypeConverter(database).getBooleanType()
 58  
                         .getFalseBooleanValue();
 59  
             }
 60  
         } else {
 61  0
             sqlString = newValue.toString();
 62  
         }
 63  0
         return sqlString;
 64  
     }
 65  
 }