Skip to content

Commit 9c3471f

Browse files
authored
WIP needs tests. Initial PR from Thomas Kellerer to add support for p… (pgjdbc#823)
* PR from Thomas Kellerer to add support for partitioned tables in pg v10 * include proper handling of the new identity columns. getColumns() now returns YES for IS_AUTOINCREMENT * add test case for identity columns
1 parent 2d3e897 commit 9c3471f

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
11981198
+ " END "
11991199
+ " ELSE CASE c.relkind "
12001200
+ " WHEN 'r' THEN 'TEMPORARY TABLE' "
1201+
+ " WHEN 'p' THEN 'TEMPORARY TABLE' "
12011202
+ " WHEN 'i' THEN 'TEMPORARY INDEX' "
12021203
+ " WHEN 'S' THEN 'TEMPORARY SEQUENCE' "
12031204
+ " WHEN 'v' THEN 'TEMPORARY VIEW' "
@@ -1206,6 +1207,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
12061207
+ " END "
12071208
+ " WHEN false THEN CASE c.relkind "
12081209
+ " WHEN 'r' THEN 'TABLE' "
1210+
+ " WHEN 'p' THEN 'TABLE' "
12091211
+ " WHEN 'i' THEN 'INDEX' "
12101212
+ " WHEN 'S' THEN 'SEQUENCE' "
12111213
+ " WHEN 'v' THEN 'VIEW' "
@@ -1255,8 +1257,8 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
12551257
Map<String, String> ht = new HashMap<String, String>();
12561258
tableTypeClauses.put("TABLE", ht);
12571259
ht.put("SCHEMAS",
1258-
"c.relkind = 'r' AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'");
1259-
ht.put("NOSCHEMAS", "c.relkind = 'r' AND c.relname !~ '^pg_'");
1260+
"c.relkind IN ('r','p') AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'");
1261+
ht.put("NOSCHEMAS", "c.relkind IN ('r','p') AND c.relname !~ '^pg_'");
12601262
ht = new HashMap<String, String>();
12611263
tableTypeClauses.put("VIEW", ht);
12621264
ht.put("SCHEMAS",
@@ -1303,8 +1305,8 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
13031305
"c.relkind = 'v' AND c.relname ~ '^pg_' AND c.relname !~ '^pg_toast_' AND c.relname !~ '^pg_temp_'");
13041306
ht = new HashMap<String, String>();
13051307
tableTypeClauses.put("TEMPORARY TABLE", ht);
1306-
ht.put("SCHEMAS", "c.relkind = 'r' AND n.nspname ~ '^pg_temp_' ");
1307-
ht.put("NOSCHEMAS", "c.relkind = 'r' AND c.relname ~ '^pg_temp_' ");
1308+
ht.put("SCHEMAS", "c.relkind IN ('r','p') AND n.nspname ~ '^pg_temp_' ");
1309+
ht.put("NOSCHEMAS", "c.relkind IN ('r','p') AND c.relname ~ '^pg_temp_' ");
13081310
ht = new HashMap<String, String>();
13091311
tableTypeClauses.put("TEMPORARY INDEX", ht);
13101312
ht.put("SCHEMAS", "c.relkind = 'i' AND n.nspname ~ '^pg_temp_' ");
@@ -1435,6 +1437,11 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
14351437
sql += "a.attnum,";
14361438
}
14371439

1440+
if (connection.haveMinimumServerVersion(ServerVersion.v10)) {
1441+
sql += "nullif(a.attidentity, '') as attidentity,";
1442+
} else {
1443+
sql += "null as attidentity,";
1444+
}
14381445
sql += "pg_catalog.pg_get_expr(def.adbin, def.adrelid) AS adsrc,dsc.description,t.typbasetype,t.typtype "
14391446
+ " FROM pg_catalog.pg_namespace n "
14401447
+ " JOIN pg_catalog.pg_class c ON (c.relnamespace = n.oid) "
@@ -1444,7 +1451,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
14441451
+ " LEFT JOIN pg_catalog.pg_description dsc ON (c.oid=dsc.objoid AND a.attnum = dsc.objsubid) "
14451452
+ " LEFT JOIN pg_catalog.pg_class dc ON (dc.oid=dsc.classoid AND dc.relname='pg_class') "
14461453
+ " LEFT JOIN pg_catalog.pg_namespace dn ON (dc.relnamespace=dn.oid AND dn.nspname='pg_catalog') "
1447-
+ " WHERE c.relkind in ('r','v','f','m') and a.attnum > 0 AND NOT a.attisdropped ";
1454+
+ " WHERE c.relkind in ('r','p','v','f','m') and a.attnum > 0 AND NOT a.attisdropped ";
14481455

14491456
if (schemaPattern != null && !schemaPattern.isEmpty()) {
14501457
sql += " AND n.nspname LIKE " + escapeQuotes(schemaPattern);
@@ -1503,6 +1510,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
15031510
}
15041511
}
15051512
}
1513+
String identity = rs.getString("attidentity");
15061514

15071515
int decimalDigits = connection.getTypeInfo().getScale(typeOid, typeMod);
15081516
int columnSize = connection.getTypeInfo().getPrecision(typeOid, typeMod);
@@ -1541,7 +1549,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
15411549
: connection.encodeString(Integer.toString(connection.getTypeInfo().getSQLType(baseTypeOid))); // SOURCE_DATA_TYPE
15421550

15431551
String autoinc = "NO";
1544-
if (defval != null && defval.contains("nextval(")) {
1552+
if (defval != null && defval.contains("nextval(") || identity != null) {
15451553
autoinc = "YES";
15461554
}
15471555
tuple[22] = connection.encodeString(autoinc);
@@ -1658,7 +1666,7 @@ public ResultSet getTablePrivileges(String catalog, String schemaPattern,
16581666
+ " FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c, pg_catalog.pg_roles r "
16591667
+ " WHERE c.relnamespace = n.oid "
16601668
+ " AND c.relowner = r.oid "
1661-
+ " AND c.relkind = 'r' ";
1669+
+ " AND c.relkind IN ('r','p') ";
16621670

16631671
if (schemaPattern != null && !schemaPattern.isEmpty()) {
16641672
sql += " AND n.nspname LIKE " + escapeQuotes(schemaPattern);
@@ -1786,6 +1794,7 @@ private static void addACLPrivileges(String acl, Map<String, Map<String, List<St
17861794
sqlpriv = "INSERT";
17871795
break;
17881796
case 'r':
1797+
case 'p':
17891798
sqlpriv = "SELECT";
17901799
break;
17911800
case 'w':
@@ -2044,13 +2053,15 @@ protected ResultSet getImportedExportedKeys(String primaryCatalog, String primar
20442053
+ " WHEN 'n' THEN " + DatabaseMetaData.importedKeySetNull
20452054
+ " WHEN 'd' THEN " + DatabaseMetaData.importedKeySetDefault
20462055
+ " WHEN 'r' THEN " + DatabaseMetaData.importedKeyRestrict
2056+
+ " WHEN 'p' THEN " + DatabaseMetaData.importedKeyRestrict
20472057
+ " WHEN 'a' THEN " + DatabaseMetaData.importedKeyNoAction
20482058
+ " ELSE NULL END AS UPDATE_RULE, "
20492059
+ "CASE con.confdeltype "
20502060
+ " WHEN 'c' THEN " + DatabaseMetaData.importedKeyCascade
20512061
+ " WHEN 'n' THEN " + DatabaseMetaData.importedKeySetNull
20522062
+ " WHEN 'd' THEN " + DatabaseMetaData.importedKeySetDefault
20532063
+ " WHEN 'r' THEN " + DatabaseMetaData.importedKeyRestrict
2064+
+ " WHEN 'p' THEN " + DatabaseMetaData.importedKeyRestrict
20542065
+ " WHEN 'a' THEN " + DatabaseMetaData.importedKeyNoAction
20552066
+ " ELSE NULL END AS DELETE_RULE, "
20562067
+ "con.conname AS FK_NAME, pkic.relname AS PK_NAME, "

pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void setUp() throws Exception {
5151
TestUtil.createCompositeType(con, "custom", "i int");
5252
TestUtil.createCompositeType(con, "_custom", "f float");
5353

54+
5455
// 8.2 does not support arrays of composite types
5556
TestUtil.createTable(con, "customtable", "c1 custom, c2 _custom"
5657
+ (TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_3) ? ", c3 custom[], c4 _custom[]" : ""));
@@ -1209,4 +1210,51 @@ public void testInformationAboutArrayTypes() throws SQLException {
12091210
assertTrue(!rs.next());
12101211
}
12111212

1213+
@Test
1214+
public void testPartitionedTables() throws SQLException {
1215+
if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v10)) {
1216+
Statement stmt = null;
1217+
try {
1218+
stmt = con.createStatement();
1219+
stmt.execute(
1220+
"CREATE TABLE measurement (logdate date not null,peaktemp int,unitsales int ) PARTITION BY RANGE (logdate);");
1221+
DatabaseMetaData dbmd = con.getMetaData();
1222+
ResultSet rs = dbmd.getTables("", "", "measurement", new String[]{"TABLE"});
1223+
assertTrue(rs.next());
1224+
assertEquals("measurement", rs.getString("table_name"));
1225+
1226+
} finally {
1227+
if (stmt != null) {
1228+
stmt.execute("drop table measurement");
1229+
stmt.close();
1230+
}
1231+
}
1232+
}
1233+
}
1234+
1235+
@Test
1236+
public void testIdentityColumns() throws SQLException {
1237+
if ( TestUtil.haveMinimumServerVersion(con, ServerVersion.v10) ) {
1238+
Statement stmt = null;
1239+
try {
1240+
stmt = con.createStatement();
1241+
stmt.execute("CREATE TABLE test_new ("
1242+
+ "id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,"
1243+
+ "payload text)");
1244+
DatabaseMetaData dbmd = con.getMetaData();
1245+
ResultSet rs = dbmd.getColumns("", "", "test_new", "id");
1246+
assertTrue(rs.next());
1247+
assertEquals(rs.getString("COLUMN_NAME"), "id");
1248+
assertTrue(rs.getBoolean("IS_AUTOINCREMENT"));
1249+
1250+
} finally {
1251+
if ( stmt != null ) {
1252+
stmt.execute( "drop table test_new");
1253+
stmt.close();
1254+
}
1255+
}
1256+
}
1257+
1258+
}
1259+
12121260
}

0 commit comments

Comments
 (0)