View Javadoc

1   package org.modeshape.connector.meta.jdbc;
2   
3   import java.sql.Connection;
4   import java.sql.DatabaseMetaData;
5   import java.sql.ResultSet;
6   import java.sql.SQLException;
7   import java.sql.Statement;
8   import java.util.LinkedList;
9   import java.util.List;
10  
11  /**
12   * The Microsoft SQL Server JDBC drivers return a list of users from the {@link DatabaseMetaData#getSchemas()} method instead of
13   * the actual schemas. Unfortunately, the {@link DatabaseMetaData#getTables(String, String, String, String[])} method actually
14   * returns schema names, so the default {@link JdbcMetadataCollector} implementation doesn't match up correctly. This class should
15   * be used when the Microsoft JDBC driver is used for database connectivity. The jTDS driver has already corrected this bug and
16   * can use the default {@link JdbcMetadataCollector}.
17   */
18  public class SqlServerMetadataCollector extends JdbcMetadataCollector {
19  
20      @Override
21      public List<String> getSchemaNames( Connection conn,
22                                          String catalogName ) throws JdbcMetadataException {
23          Statement stmt = null;
24          ResultSet rs = null;
25          List<String> schemaNames = new LinkedList<String>();
26  
27          try {
28              stmt = conn.createStatement();
29  
30              // There's no correlation between schemas and catalogs in MS SQL Server, so return all schemas
31              rs = stmt.executeQuery("SELECT name AS TABLE_SCHEM FROM sys.schemas ORDER BY TABLE_SCHEM");
32              while (rs.next()) {
33                  schemaNames.add(rs.getString("TABLE_SCHEM"));
34              }
35  
36              return schemaNames;
37          } catch (SQLException se) {
38              throw new JdbcMetadataException(se);
39          } finally {
40              try {
41                  if (rs != null) rs.close();
42              } catch (SQLException ignore) {
43              }
44              try {
45                  if (stmt != null) stmt.close();
46              } catch (SQLException ignore) {
47              }
48          }
49      }
50  
51  }