1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.exception.UnexpectedLiquibaseException; |
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.SelectFromDatabaseChangeLogStatement; |
11 | |
import liquibase.util.StringUtils; |
12 | |
|
13 | 28 | public class SelectFromDatabaseChangeLogGenerator extends AbstractSqlGenerator<SelectFromDatabaseChangeLogStatement> { |
14 | |
|
15 | |
public ValidationErrors validate(SelectFromDatabaseChangeLogStatement statement, Database database, |
16 | |
SqlGeneratorChain sqlGeneratorChain) { |
17 | 15 | ValidationErrors errors = new ValidationErrors(); |
18 | 15 | errors.checkRequiredField("columnToSelect", statement.getColumnsToSelect()); |
19 | |
|
20 | 15 | return errors; |
21 | |
} |
22 | |
|
23 | |
public Sql[] generateSql(SelectFromDatabaseChangeLogStatement statement, Database database, |
24 | |
SqlGeneratorChain sqlGeneratorChain) { |
25 | 0 | String sql = "SELECT " + StringUtils.join(statement.getColumnsToSelect(), ",").toUpperCase() + " FROM " |
26 | |
+ database.escapeTableName(database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()); |
27 | |
|
28 | 0 | SelectFromDatabaseChangeLogStatement.WhereClause whereClause = statement.getWhereClause(); |
29 | 0 | if (whereClause != null) { |
30 | 0 | if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByTag) { |
31 | 0 | sql += " WHERE TAG='" + ((SelectFromDatabaseChangeLogStatement.ByTag) whereClause).getTagName() + "'"; |
32 | 0 | } else if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByNotNullCheckSum) { |
33 | 0 | sql += " WHERE MD5SUM IS NOT NULL"; |
34 | |
} else { |
35 | 0 | throw new UnexpectedLiquibaseException("Unknown where clause type: " + whereClause.getClass().getName()); |
36 | |
} |
37 | |
} |
38 | |
|
39 | 0 | if (statement.getOrderByColumns() != null && statement.getOrderByColumns().length > 0) { |
40 | 0 | sql += " ORDER BY " + StringUtils.join(statement.getOrderByColumns(), ", ").toUpperCase(); |
41 | |
} |
42 | |
|
43 | 0 | return new Sql[] { new UnparsedSql(sql) }; |
44 | |
} |
45 | |
} |