1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.OracleDatabase; |
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.SelectFromDatabaseChangeLogLockStatement; |
11 | |
import liquibase.util.StringUtils; |
12 | |
|
13 | 28 | public class SelectFromDatabaseChangeLogLockGenerator extends |
14 | |
AbstractSqlGenerator<SelectFromDatabaseChangeLogLockStatement> { |
15 | |
|
16 | |
public ValidationErrors validate(SelectFromDatabaseChangeLogLockStatement statement, Database database, |
17 | |
SqlGeneratorChain sqlGeneratorChain) { |
18 | 15 | ValidationErrors errors = new ValidationErrors(); |
19 | 15 | errors.checkRequiredField("columnToSelect", statement.getColumnsToSelect()); |
20 | |
|
21 | 15 | return errors; |
22 | |
} |
23 | |
|
24 | |
public Sql[] generateSql(SelectFromDatabaseChangeLogLockStatement statement, Database database, |
25 | |
SqlGeneratorChain sqlGeneratorChain) { |
26 | |
String liquibaseSchema; |
27 | 0 | liquibaseSchema = database.getLiquibaseSchemaName(); |
28 | |
|
29 | 0 | String[] columns = statement.getColumnsToSelect(); |
30 | 0 | int numberOfColumns = columns.length; |
31 | 0 | String[] escapedColumns = new String[numberOfColumns]; |
32 | 0 | for (int i = 0; i < numberOfColumns; i++) { |
33 | 0 | escapedColumns[i] = database.escapeColumnName(liquibaseSchema, |
34 | |
database.getDatabaseChangeLogLockTableName(), columns[i]); |
35 | |
} |
36 | |
|
37 | 0 | String sql = "SELECT " + StringUtils.join(escapedColumns, ",") + " FROM " |
38 | |
+ database.escapeTableName(liquibaseSchema, database.getDatabaseChangeLogLockTableName()) + " WHERE " |
39 | |
+ database.escapeColumnName(liquibaseSchema, database.getDatabaseChangeLogLockTableName(), "ID") + "=1"; |
40 | |
|
41 | 0 | if (database instanceof OracleDatabase) { |
42 | 0 | sql += " FOR UPDATE"; |
43 | |
} |
44 | 0 | return new Sql[] { new UnparsedSql(sql) }; |
45 | |
} |
46 | |
} |