@Test public void testListDatabases() throws SQLException { ResultSet rs = mock(ResultSet.class); when(rs.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(rs.getString("TABLE_SCHEM")).thenReturn("schema1").thenReturn("schema2"); when(rs.getString("TABLE_CAT")).thenReturn("catalog1").thenReturn("testdb"); when(connection.getCatalog()).thenReturn("testdb"); when(connection.getMetaData()).thenReturn(dbmd); when(dbmd.getTables("testdb", null, null, null)).thenReturn(rs); List<String> dbs = jdbcMetadata.listDatabases(); Assert.assertEquals(1, dbs.size()); Assert.assertEquals("schema2", dbs.get(0)); }
@Override public List<String> listDatabases() throws SQLException { Set<String> ret = new HashSet<>(); try (Connection con = SqlUtil.getConnection(dbconf)) { String database = con.getCatalog(); Preconditions.checkArgument(StringUtils.isNotEmpty(database), "SQL Server needs a specific database in " + "connection string."); try (ResultSet rs = con.getMetaData().getTables(database, null, null, null)) { String schema; String catalog; while (rs.next()) { schema = rs.getString("TABLE_SCHEM"); catalog = rs.getString("TABLE_CAT"); if (database.equals(catalog) || schema.equals("dbo")) { ret.add(schema); } } } } return new ArrayList<>(ret); }
SQLServerJdbcMetadata extends DefaultJdbcMetadata { @Override public List<String> listDatabases() throws SQLException { Set<String> ret = new HashSet<>(); try (Connection con = SqlUtil.getConnection(dbconf)) { String database = con.getCatalog(); Preconditions.checkArgument(StringUtils.isNotEmpty(database), "SQL Server needs a specific database in " + "connection string."); try (ResultSet rs = con.getMetaData().getTables(database, null, null, null)) { String schema; String catalog; while (rs.next()) { schema = rs.getString("TABLE_SCHEM"); catalog = rs.getString("TABLE_CAT"); if (database.equals(catalog) || schema.equals("dbo")) { ret.add(schema); } } } } return new ArrayList<>(ret); } }
SQLServerJdbcMetadata extends DefaultJdbcMetadata { @Override public List<String> listDatabases() throws SQLException { Set<String> ret = new HashSet<>(); try (Connection con = SqlUtil.getConnection(dbconf)) { String database = con.getCatalog(); Preconditions.checkArgument(StringUtils.isNotEmpty(database), "SQL Server needs a specific database in " + "connection string."); try (ResultSet rs = con.getMetaData().getTables(database, null, null, null)) { String schema; String catalog; while (rs.next()) { schema = rs.getString("TABLE_SCHEM"); catalog = rs.getString("TABLE_CAT"); if (database.equals(catalog) || schema.equals("dbo")) { ret.add(schema); } } } } return new ArrayList<>(ret); } SQLServerJdbcMetadata(DBConnConf dbConnConf); }
SQLServerJdbcMetadata extends DefaultJdbcMetadata { @Override public List<String> listDatabases() throws SQLException { Set<String> ret = new HashSet<>(); try (Connection con = SqlUtil.getConnection(dbconf)) { String database = con.getCatalog(); Preconditions.checkArgument(StringUtils.isNotEmpty(database), "SQL Server needs a specific database in " + "connection string."); try (ResultSet rs = con.getMetaData().getTables(database, null, null, null)) { String schema; String catalog; while (rs.next()) { schema = rs.getString("TABLE_SCHEM"); catalog = rs.getString("TABLE_CAT"); if (database.equals(catalog) || schema.equals("dbo")) { ret.add(schema); } } } } return new ArrayList<>(ret); } SQLServerJdbcMetadata(DBConnConf dbConnConf); @Override List<String> listDatabases(); @Override SourceDialect getDialect(); }
SQLServerJdbcMetadata extends DefaultJdbcMetadata { @Override public List<String> listDatabases() throws SQLException { Set<String> ret = new HashSet<>(); try (Connection con = SqlUtil.getConnection(dbconf)) { String database = con.getCatalog(); Preconditions.checkArgument(StringUtils.isNotEmpty(database), "SQL Server needs a specific database in " + "connection string."); try (ResultSet rs = con.getMetaData().getTables(database, null, null, null)) { String schema; String catalog; while (rs.next()) { schema = rs.getString("TABLE_SCHEM"); catalog = rs.getString("TABLE_CAT"); if (database.equals(catalog) || schema.equals("dbo")) { ret.add(schema); } } } } return new ArrayList<>(ret); } SQLServerJdbcMetadata(DBConnConf dbConnConf); @Override List<String> listDatabases(); @Override SourceDialect getDialect();  }