01: package liquibase.database.sql;
02:
03: import liquibase.database.*;
04: import liquibase.exception.StatementNotSupportedOnDatabaseException;
05:
06: public class CreateViewStatement implements SqlStatement {
07:
08: private String schemaName;
09: private String viewName;
10: private String selectQuery;
11: private boolean replaceIfExists;
12:
13: public CreateViewStatement(String schemaName, String viewName,
14: String selectQuery, boolean replaceIfExists) {
15: this .schemaName = schemaName;
16: this .viewName = viewName;
17: this .selectQuery = selectQuery;
18: this .replaceIfExists = replaceIfExists;
19: }
20:
21: public String getSchemaName() {
22: return schemaName;
23: }
24:
25: public String getViewName() {
26: return viewName;
27: }
28:
29: public String getSelectQuery() {
30: return selectQuery;
31: }
32:
33: public boolean isReplaceIfExists() {
34: return replaceIfExists;
35: }
36:
37: public String getSqlStatement(Database database)
38: throws StatementNotSupportedOnDatabaseException {
39: String createClause;
40: if (database instanceof HsqlDatabase
41: || database instanceof DB2Database
42: || database instanceof CacheDatabase
43: || database instanceof MSSQLDatabase
44: || database instanceof DerbyDatabase) {
45: if (replaceIfExists) {
46: throw new StatementNotSupportedOnDatabaseException(
47: "replaceIfExists not supported", this , database);
48: }
49: }
50:
51: if (database instanceof FirebirdDatabase) {
52: if (replaceIfExists) {
53: createClause = "RECREATE VIEW";
54: } else {
55: createClause = "RECREATE VIEW";
56: }
57: } else {
58: createClause = "CREATE "
59: + (replaceIfExists ? "OR REPLACE " : "") + "VIEW";
60: }
61:
62: return createClause
63: + " "
64: + database.escapeViewName(getSchemaName(),
65: getViewName()) + " AS " + getSelectQuery();
66: }
67:
68: public String getEndDelimiter(Database database) {
69: return ";";
70: }
71:
72: public boolean supportsDatabase(Database database) {
73: return true;
74: }
75: }
|