Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add way to distinguish base and partitioned tables in PgDatabaseMetaData.getTables #1708

Merged
merged 2 commits into from Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
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
Expand Up @@ -29,6 +29,7 @@
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -758,11 +759,27 @@ public void testPartialIndexInfo() throws SQLException {

@Test
public void testTableTypes() throws SQLException {
// At the moment just test that no exceptions are thrown KJ
final List<String> expectedTableTypes = new ArrayList<String>(Arrays.asList("FOREIGN TABLE", "INDEX",
"MATERIALIZED VIEW", "PARTITIONED TABLE", "SEQUENCE", "SYSTEM INDEX", "SYSTEM TABLE", "SYSTEM TOAST INDEX",
"SYSTEM TOAST TABLE", "SYSTEM VIEW", "TABLE", "TEMPORARY INDEX", "TEMPORARY SEQUENCE", "TEMPORARY TABLE",
"TEMPORARY VIEW", "TYPE", "VIEW"));
final List<String> foundTableTypes = new ArrayList<String>();

// Test that no exceptions are thrown
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);

// Test that the table types returned are the same as those expected
ResultSet rs = dbmd.getTableTypes();
while (rs.next()) {
String tableType = new String(rs.getBytes(1));
foundTableTypes.add(tableType);
}
rs.close();
Collections.sort(expectedTableTypes);
Collections.sort(foundTableTypes);
Assert.assertEquals("The table types received from DatabaseMetaData should match the 17 expected types",
true, foundTableTypes.equals(expectedTableTypes));
}

@Test
Expand Down Expand Up @@ -1270,7 +1287,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