Skip to content

Commit

Permalink
feat: add way to distinguish base and partitioned tables in PgDatabas…
Browse files Browse the repository at this point in the history
…eMetaData.getTables

There is currently no way to distinguish between base tables and
partitioned tables in the response from PgDatabadeMetaData.getTables.
Fix this by adding PARTITIONED TABLE to the tableTypeClauses map and
thus the TABLE_TYPE field in the query in getTables. Also update a test
(testPartitionedTables) that was using TABLE_TYPE of TABLE to grab
partitioned tables.

This should close pgjdbc#1590. However, perhaps this could be considered a
breaking change for anyone using getTables and expecting partitioned
tables to show up for .getTables(s, s1, s2, new String[]{"TABLE"})?
  • Loading branch information
MSGoodman committed Feb 18, 2020
1 parent 94641ef commit ae7ed97
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
+ " END "
+ " WHEN false THEN CASE c.relkind "
+ " WHEN 'r' THEN 'TABLE' "
+ " WHEN 'p' THEN 'TABLE' "
+ " WHEN 'p' THEN 'PARTITIONED TABLE' "
+ " WHEN 'i' THEN 'INDEX' "
+ " WHEN 'S' THEN 'SEQUENCE' "
+ " WHEN 'v' THEN 'VIEW' "
Expand Down Expand Up @@ -1339,9 +1339,12 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
tableTypeClauses = new HashMap<String, Map<String, String>>();
Map<String, String> ht = new HashMap<String, String>();
tableTypeClauses.put("TABLE", ht);
ht.put("SCHEMAS",
"c.relkind IN ('r','p') AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'");
ht.put("NOSCHEMAS", "c.relkind IN ('r','p') AND c.relname !~ '^pg_'");
ht.put("SCHEMAS", "c.relkind = 'r' AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'");
ht.put("NOSCHEMAS", "c.relkind = 'r' AND c.relname !~ '^pg_'");
ht = new HashMap<String, String>();
tableTypeClauses.put("PARTITIONED TABLE", ht);
ht.put("SCHEMAS", "c.relkind = 'p' AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'");
ht.put("NOSCHEMAS", "c.relkind = 'p' AND c.relname !~ '^pg_'");
ht = new HashMap<String, String>();
tableTypeClauses.put("VIEW", ht);
ht.put("SCHEMAS",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ public void testPartitionedTables() throws SQLException {
stmt.execute(
"CREATE TABLE measurement (logdate date not null,peaktemp int,unitsales int ) PARTITION BY RANGE (logdate);");
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getTables("", "", "measurement", new String[]{"TABLE"});
ResultSet rs = dbmd.getTables("", "", "measurement", new String[]{"PARTITIONED TABLE"});
assertTrue(rs.next());
assertEquals("measurement", rs.getString("table_name"));

Expand Down

0 comments on commit ae7ed97

Please sign in to comment.