View Javadoc

1   package liquibase.database.core;
2   
3   import liquibase.database.AbstractDatabase;
4   import liquibase.database.DatabaseConnection;
5   import liquibase.exception.DatabaseException;
6   import liquibase.executor.Executor;
7   import liquibase.executor.ExecutorService;
8   import liquibase.logging.LogFactory;
9   import liquibase.statement.core.GetViewDefinitionStatement;
10  
11  import java.util.HashSet;
12  import java.util.List;
13  import java.util.Set;
14  
15  /**
16   * Encapsulates Sybase ASE database support.
17   */
18  public class SybaseDatabase extends AbstractDatabase {
19      public static final String PRODUCT_NAME = "Adaptive Server Enterprise";
20      protected Set<String> systemTablesAndViews = new HashSet<String>();
21  
22      public String getProductName() {
23          return "Sybase SQL Server";
24      }
25  
26      public String getTypeName() {
27          return "sybase";
28      }
29  
30      public SybaseDatabase() {
31          systemTablesAndViews.add("syscolumns");
32          systemTablesAndViews.add("syscomments");
33          systemTablesAndViews.add("sysdepends");
34          systemTablesAndViews.add("sysfilegroups");
35          systemTablesAndViews.add("sysfiles");
36          systemTablesAndViews.add("sysfiles1");
37          systemTablesAndViews.add("sysforeignkeys");
38          systemTablesAndViews.add("sysfulltextcatalogs");
39          systemTablesAndViews.add("sysfulltextnotify");
40          systemTablesAndViews.add("sysindexes");
41          systemTablesAndViews.add("sysindexkeys");
42          systemTablesAndViews.add("sysmembers");
43          systemTablesAndViews.add("sysobjects");
44          systemTablesAndViews.add("syspermissions");
45          systemTablesAndViews.add("sysproperties");
46          systemTablesAndViews.add("sysprotects");
47          systemTablesAndViews.add("sysreferences");
48          systemTablesAndViews.add("systypes");
49          systemTablesAndViews.add("sysusers");
50          systemTablesAndViews.add("sysquerymetrics");
51          systemTablesAndViews.add("syssegments");
52          systemTablesAndViews.add("sysconstraints");
53      }
54  
55      /*
56       * public void setConnection(Connection connection) { super.setConnection(new SybaseConnectionDelegate(connection));
57       * }
58       */
59  
60      public int getPriority() {
61          return PRIORITY_DEFAULT;
62      }
63  
64      /**
65       * Sybase does not support DDL and meta data in transactions properly, as such we turn off the commit and turn on
66       * auto commit.
67       */
68      @Override
69      public boolean supportsDDLInTransaction() {
70          return false;
71      }
72  
73      @Override
74      public Set<String> getSystemTablesAndViews() {
75          return systemTablesAndViews;
76      }
77  
78      public boolean supportsInitiallyDeferrableColumns() {
79          return false;
80      }
81  
82      @Override
83      public boolean supportsSequences() {
84          return false;
85      }
86  
87      public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException {
88          String dbProductName = conn.getDatabaseProductName();
89          return isSybaseProductName(dbProductName);
90      }
91  
92      // package private to facilitate testing
93      boolean isSybaseProductName(String dbProductName) {
94          return "Adaptive Server Enterprise".equals(dbProductName) || "Sybase SQL Server".equals(dbProductName)
95                  || "sql server".equals(dbProductName) || "ASE".equals(dbProductName);
96      }
97  
98      public String getDefaultDriver(String url) {
99          if (url.startsWith("jdbc:sybase")) {
100             return "com.sybase.jdbc3.jdbc.SybDriver";
101         } else if (url.startsWith("jdbc:jtds:sybase")) {
102             return "net.sourceforge.jtds.jdbc.Driver";
103         }
104         return null;
105     }
106 
107     public String getCurrentDateTimeFunction() {
108         if (currentDateTimeFunction != null) {
109             return currentDateTimeFunction;
110         }
111 
112         return "GETDATE()";
113     }
114 
115     @Override
116     public String getAutoIncrementClause() {
117         return "IDENTITY";
118     }
119 
120     @Override
121     protected String getDefaultDatabaseSchemaName() throws DatabaseException {
122         return null;
123     }
124 
125     @Override
126     public String getDefaultCatalogName() throws DatabaseException {
127         return getConnection().getCatalog();
128     }
129 
130     @Override
131     public String getConcatSql(String... values) {
132         StringBuffer returnString = new StringBuffer();
133         for (String value : values) {
134             returnString.append(value).append(" + ");
135         }
136 
137         return returnString.toString().replaceFirst(" \\+ $", "");
138     }
139 
140     // protected void dropForeignKeys(Connection conn) throws JDBCException {
141     // Statement dropStatement = null;
142     // PreparedStatement fkStatement = null;
143     // ResultSet rs = null;
144     // try {
145     // dropStatement = conn.createStatement();
146     //
147     // fkStatement =
148     // conn.prepareStatement("select TABLE_NAME, CONSTRAINT_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='FOREIGN KEY' AND TABLE_CATALOG=?");
149     // fkStatement.setString(1, getDefaultCatalogName());
150     // rs = fkStatement.executeQuery();
151     // while (rs.next()) {
152     // DropForeignKeyConstraintChange dropFK = new DropForeignKeyConstraintChange();
153     // dropFK.setBaseTableName(rs.getString("TABLE_NAME"));
154     // dropFK.setConstraintName(rs.getString("CONSTRAINT_NAME"));
155     //
156     // try {
157     // dropStatement.execute(dropFK.generateStatements(this)[0]);
158     // } catch (UnsupportedChangeException e) {
159     // throw new JDBCException(e.getMessage());
160     // }
161     // }
162     // } catch (SQLException e) {
163     // throw new JDBCException(e);
164     // } finally {
165     // try {
166     // if (dropStatement != null) {
167     // dropStatement.close();
168     // }
169     // if (fkStatement != null) {
170     // fkStatement.close();
171     // }
172     // if (rs != null) {
173     // rs.close();
174     // }
175     // } catch (SQLException e) {
176     // throw new JDBCException(e);
177     // }
178     // }
179     //
180     // }
181 
182     public boolean supportsTablespaces() {
183         return true;
184     }
185 
186     @Override
187     public boolean isSystemTable(String catalogName, String schemaName, String tableName) {
188         return super.isSystemTable(catalogName, schemaName, tableName) || schemaName.equals("sys")
189                 || tableName.toLowerCase().startsWith("sybfi");
190     }
191 
192     @Override
193     public boolean isSystemView(String catalogName, String schemaName, String viewName) {
194         return super.isSystemView(catalogName, schemaName, viewName) || schemaName.equals("sys")
195                 || viewName.toLowerCase().equals("sybfi");
196     }
197 
198     public String generateDefaultConstraintName(String tableName, String columnName) {
199         return "DF_" + tableName + "_" + columnName;
200     }
201 
202     @Override
203     public String convertRequestedSchemaToCatalog(String requestedSchema) throws DatabaseException {
204         return getDefaultCatalogName();
205     }
206 
207     @Override
208     public String convertRequestedSchemaToSchema(String requestedSchema) throws DatabaseException {
209         if (requestedSchema == null) {
210             requestedSchema = getDefaultDatabaseSchemaName();
211         }
212 
213         if (requestedSchema == null) {
214             return "dbo";
215         }
216         return requestedSchema;
217     }
218 
219     @Override
220     public boolean supportsRestrictForeignKeys() {
221         return false;
222     }
223 
224     @Override
225     public String escapeDatabaseObject(String objectName) {
226         return "[" + objectName + "]";
227     }
228 
229     @Override
230     public String getViewDefinition(String schemaName, String viewName) throws DatabaseException {
231         GetViewDefinitionStatement statement = new GetViewDefinitionStatement(
232                 convertRequestedSchemaToSchema(schemaName), viewName);
233         Executor executor = ExecutorService.getInstance().getExecutor(this);
234         @SuppressWarnings("unchecked")
235         List<String> definitionRows = (List<String>) executor.queryForList(statement, String.class);
236         StringBuilder definition = new StringBuilder();
237         for (String d : definitionRows) {
238             definition.append(d);
239         }
240         return definition.toString();
241     }
242 
243     /**
244      * @return the major version if supported, otherwise -1
245      * @see liquibase.database.AbstractDatabase#getDatabaseMajorVersion()
246      */
247     @Override
248     public int getDatabaseMajorVersion() throws DatabaseException {
249         try {
250             return getConnection().getDatabaseMajorVersion();
251         } catch (UnsupportedOperationException e) {
252             LogFactory.getLogger().warning(
253                     "Your JDBC driver does not support getDatabaseMajorVersion(). Consider upgrading it.");
254             return -1;
255         }
256     }
257 
258     /**
259      * @return the minor version if supported, otherwise -1
260      * @see liquibase.database.AbstractDatabase#getDatabaseMinorVersion()
261      */
262     @Override
263     public int getDatabaseMinorVersion() throws DatabaseException {
264         try {
265             return getConnection().getDatabaseMinorVersion();
266         } catch (UnsupportedOperationException e) {
267             LogFactory.getLogger().warning(
268                     "Your JDBC driver does not support getDatabaseMajorVersion(). Consider upgrading it.");
269             return -1;
270         }
271     }
272 
273     @Override
274     public String escapeIndexName(String schemaName, String indexName) {
275         return super.escapeIndexName(null, indexName);
276     }
277 
278 }