1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.CacheDatabase; |
5 | |
import liquibase.database.core.DB2Database; |
6 | |
import liquibase.database.core.DerbyDatabase; |
7 | |
import liquibase.database.core.FirebirdDatabase; |
8 | |
import liquibase.database.core.H2Database; |
9 | |
import liquibase.database.core.HsqlDatabase; |
10 | |
import liquibase.database.core.InformixDatabase; |
11 | |
import liquibase.database.core.MSSQLDatabase; |
12 | |
import liquibase.database.core.SybaseASADatabase; |
13 | |
import liquibase.exception.ValidationErrors; |
14 | |
import liquibase.sql.Sql; |
15 | |
import liquibase.sql.UnparsedSql; |
16 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
17 | |
import liquibase.statement.core.CreateViewStatement; |
18 | |
|
19 | 25 | public class CreateViewGenerator extends AbstractSqlGenerator<CreateViewStatement> { |
20 | |
|
21 | |
@Override |
22 | |
public ValidationErrors validate(CreateViewStatement createViewStatement, Database database, |
23 | |
SqlGeneratorChain sqlGeneratorChain) { |
24 | 15 | ValidationErrors validationErrors = new ValidationErrors(); |
25 | |
|
26 | 15 | validationErrors.checkRequiredField("viewName", createViewStatement.getViewName()); |
27 | 15 | validationErrors.checkRequiredField("selectQuery", createViewStatement.getSelectQuery()); |
28 | |
|
29 | 15 | if (createViewStatement.isReplaceIfExists()) { |
30 | 0 | validationErrors.checkDisallowedField("replaceIfExists", createViewStatement.isReplaceIfExists(), database, |
31 | |
HsqlDatabase.class, H2Database.class, DB2Database.class, CacheDatabase.class, MSSQLDatabase.class, |
32 | |
DerbyDatabase.class, SybaseASADatabase.class, InformixDatabase.class); |
33 | |
} |
34 | |
|
35 | 15 | return validationErrors; |
36 | |
} |
37 | |
|
38 | |
@Override |
39 | |
public Sql[] generateSql(CreateViewStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
40 | |
String createClause; |
41 | |
|
42 | 0 | if (database instanceof FirebirdDatabase) { |
43 | 0 | if (statement.isReplaceIfExists()) { |
44 | 0 | createClause = "RECREATE VIEW"; |
45 | |
} else { |
46 | 0 | createClause = "RECREATE VIEW"; |
47 | |
} |
48 | 0 | } else if (database instanceof SybaseASADatabase |
49 | |
&& statement.getSelectQuery().toLowerCase().startsWith("create view")) { |
50 | |
|
51 | 0 | return new Sql[] { new UnparsedSql(statement.getSelectQuery()) }; |
52 | |
} else { |
53 | 0 | createClause = "CREATE " + (statement.isReplaceIfExists() ? "OR REPLACE " : "") + "VIEW"; |
54 | |
} |
55 | |
|
56 | 0 | return new Sql[] { new UnparsedSql(createClause + " " |
57 | |
+ database.escapeViewName(statement.getSchemaName(), statement.getViewName()) + " AS " |
58 | |
+ statement.getSelectQuery()) }; |
59 | |
} |
60 | |
} |