Skip to content

Commit

Permalink
feat: return all catalogs for getCatalogs metadata query (#2949) (#2953)
Browse files Browse the repository at this point in the history
Use the pg.catalog.pg_database table present in all catalogs to return
the list of available catalogs, instead of just the current one the
connection uses. The table is filtered to include only the
catalogs that can be connected to.

closes #2949

Co-authored-by: Papp István Péter <pip25@sonrisa.hu>
  • Loading branch information
pip25 and Papp István Péter committed Oct 21, 2023
1 parent 9bc47ec commit 9ea59bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
15 changes: 3 additions & 12 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1457,20 +1457,11 @@ public ResultSet getSchemas(@Nullable String catalog, @Nullable String schemaPat
return createMetaDataStatement().executeQuery(sql);
}

/**
* PostgreSQL does not support multiple catalogs from a single connection, so to reduce confusion
* we only return the current catalog. {@inheritDoc}
*/
@Override
public ResultSet getCatalogs() throws SQLException {
Field[] f = new Field[1];
List<Tuple> v = new ArrayList<Tuple>();
f[0] = new Field("TABLE_CAT", Oid.VARCHAR);
byte[] @Nullable [] tuple = new byte[1][];
tuple[0] = connection.encodeString(connection.getCatalog());
v.add(new Tuple(tuple));

return ((BaseStatement) createMetaDataStatement()).createDriverResultSet(f, v);
String sql = "SELECT datname AS TABLE_CAT FROM pg_catalog.pg_database"
+ " WHERE datallowconn = true";

This comment has been minimized.

Copy link
@vlsi

vlsi Nov 16, 2023

Member

The interface specification for getCatalogs reads "The results are ordered by catalog name"

We need add sorting here.

return createMetaDataStatement().executeQuery(sql);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,9 +1065,26 @@ public void testProcedures() throws SQLException {
public void testCatalogs() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getCatalogs();
assertTrue(rs.next());
assertEquals(con.getCatalog(), rs.getString(1));
assertTrue(!rs.next());

boolean foundDefault = false;
boolean foundTest = false;
int count;

for (count = 0; rs.next(); count++) {
String catalog = rs.getString("TABLE_CAT");
if ("test".equals(catalog)) {
foundTest = true;
}
if ("postgres".equals(catalog)) {
foundDefault = true;
}
}

rs.close();

assertTrue(count >= 2);
assertTrue(foundDefault);
assertTrue(foundTest);
}

@Test
Expand Down

0 comments on commit 9ea59bd

Please sign in to comment.