Permalink
Browse files

WIP needs tests. Initial PR from Thomas Kellerer to add support for p… (

#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
  • Loading branch information...
davecramer committed May 23, 2017
1 parent 2d3e897 commit 9c3471f21f6ac9cf1a5e5700115ac7d0d55e0ad9
@@ -1198,6 +1198,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
+ " END "
+ " ELSE CASE c.relkind "
+ " WHEN 'r' THEN 'TEMPORARY TABLE' "
+ " WHEN 'p' THEN 'TEMPORARY TABLE' "
+ " WHEN 'i' THEN 'TEMPORARY INDEX' "
+ " WHEN 'S' THEN 'TEMPORARY SEQUENCE' "
+ " WHEN 'v' THEN 'TEMPORARY VIEW' "
@@ -1206,6 +1207,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 'i' THEN 'INDEX' "
+ " WHEN 'S' THEN 'SEQUENCE' "
+ " WHEN 'v' THEN 'VIEW' "
@@ -1255,8 +1257,8 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
Map<String, String> ht = new HashMap<String, String>();
tableTypeClauses.put("TABLE", ht);
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_'");
"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 = new HashMap<String, String>();
tableTypeClauses.put("VIEW", ht);
ht.put("SCHEMAS",
@@ -1303,8 +1305,8 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
"c.relkind = 'v' AND c.relname ~ '^pg_' AND c.relname !~ '^pg_toast_' AND c.relname !~ '^pg_temp_'");
ht = new HashMap<String, String>();
tableTypeClauses.put("TEMPORARY TABLE", ht);
ht.put("SCHEMAS", "c.relkind = 'r' AND n.nspname ~ '^pg_temp_' ");
ht.put("NOSCHEMAS", "c.relkind = 'r' AND c.relname ~ '^pg_temp_' ");
ht.put("SCHEMAS", "c.relkind IN ('r','p') AND n.nspname ~ '^pg_temp_' ");
ht.put("NOSCHEMAS", "c.relkind IN ('r','p') AND c.relname ~ '^pg_temp_' ");
ht = new HashMap<String, String>();
tableTypeClauses.put("TEMPORARY INDEX", ht);
ht.put("SCHEMAS", "c.relkind = 'i' AND n.nspname ~ '^pg_temp_' ");
@@ -1435,6 +1437,11 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
sql += "a.attnum,";
}
if (connection.haveMinimumServerVersion(ServerVersion.v10)) {
sql += "nullif(a.attidentity, '') as attidentity,";
} else {
sql += "null as attidentity,";
}
sql += "pg_catalog.pg_get_expr(def.adbin, def.adrelid) AS adsrc,dsc.description,t.typbasetype,t.typtype "
+ " FROM pg_catalog.pg_namespace n "
+ " JOIN pg_catalog.pg_class c ON (c.relnamespace = n.oid) "
@@ -1444,7 +1451,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
+ " LEFT JOIN pg_catalog.pg_description dsc ON (c.oid=dsc.objoid AND a.attnum = dsc.objsubid) "
+ " LEFT JOIN pg_catalog.pg_class dc ON (dc.oid=dsc.classoid AND dc.relname='pg_class') "
+ " LEFT JOIN pg_catalog.pg_namespace dn ON (dc.relnamespace=dn.oid AND dn.nspname='pg_catalog') "
+ " WHERE c.relkind in ('r','v','f','m') and a.attnum > 0 AND NOT a.attisdropped ";
+ " WHERE c.relkind in ('r','p','v','f','m') and a.attnum > 0 AND NOT a.attisdropped ";
if (schemaPattern != null && !schemaPattern.isEmpty()) {
sql += " AND n.nspname LIKE " + escapeQuotes(schemaPattern);
@@ -1503,6 +1510,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
}
}
}
String identity = rs.getString("attidentity");
int decimalDigits = connection.getTypeInfo().getScale(typeOid, typeMod);
int columnSize = connection.getTypeInfo().getPrecision(typeOid, typeMod);
@@ -1541,7 +1549,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
: connection.encodeString(Integer.toString(connection.getTypeInfo().getSQLType(baseTypeOid))); // SOURCE_DATA_TYPE
String autoinc = "NO";
if (defval != null && defval.contains("nextval(")) {
if (defval != null && defval.contains("nextval(") || identity != null) {
autoinc = "YES";
}
tuple[22] = connection.encodeString(autoinc);
@@ -1658,7 +1666,7 @@ public ResultSet getTablePrivileges(String catalog, String schemaPattern,
+ " FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c, pg_catalog.pg_roles r "
+ " WHERE c.relnamespace = n.oid "
+ " AND c.relowner = r.oid "
+ " AND c.relkind = 'r' ";
+ " AND c.relkind IN ('r','p') ";
if (schemaPattern != null && !schemaPattern.isEmpty()) {
sql += " AND n.nspname LIKE " + escapeQuotes(schemaPattern);
@@ -1786,6 +1794,7 @@ private static void addACLPrivileges(String acl, Map<String, Map<String, List<St
sqlpriv = "INSERT";
break;
case 'r':
case 'p':
sqlpriv = "SELECT";
break;
case 'w':
@@ -2044,13 +2053,15 @@ protected ResultSet getImportedExportedKeys(String primaryCatalog, String primar
+ " WHEN 'n' THEN " + DatabaseMetaData.importedKeySetNull
+ " WHEN 'd' THEN " + DatabaseMetaData.importedKeySetDefault
+ " WHEN 'r' THEN " + DatabaseMetaData.importedKeyRestrict
+ " WHEN 'p' THEN " + DatabaseMetaData.importedKeyRestrict
+ " WHEN 'a' THEN " + DatabaseMetaData.importedKeyNoAction
+ " ELSE NULL END AS UPDATE_RULE, "
+ "CASE con.confdeltype "
+ " WHEN 'c' THEN " + DatabaseMetaData.importedKeyCascade
+ " WHEN 'n' THEN " + DatabaseMetaData.importedKeySetNull
+ " WHEN 'd' THEN " + DatabaseMetaData.importedKeySetDefault
+ " WHEN 'r' THEN " + DatabaseMetaData.importedKeyRestrict
+ " WHEN 'p' THEN " + DatabaseMetaData.importedKeyRestrict
+ " WHEN 'a' THEN " + DatabaseMetaData.importedKeyNoAction
+ " ELSE NULL END AS DELETE_RULE, "
+ "con.conname AS FK_NAME, pkic.relname AS PK_NAME, "
@@ -51,6 +51,7 @@ public void setUp() throws Exception {
TestUtil.createCompositeType(con, "custom", "i int");
TestUtil.createCompositeType(con, "_custom", "f float");
// 8.2 does not support arrays of composite types
TestUtil.createTable(con, "customtable", "c1 custom, c2 _custom"
+ (TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_3) ? ", c3 custom[], c4 _custom[]" : ""));
@@ -1209,4 +1210,51 @@ public void testInformationAboutArrayTypes() throws SQLException {
assertTrue(!rs.next());
}
@Test
public void testPartitionedTables() throws SQLException {
if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v10)) {
Statement stmt = null;
try {
stmt = con.createStatement();
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"});
assertTrue(rs.next());
assertEquals("measurement", rs.getString("table_name"));
} finally {
if (stmt != null) {
stmt.execute("drop table measurement");
stmt.close();
}
}
}
}
@Test
public void testIdentityColumns() throws SQLException {
if ( TestUtil.haveMinimumServerVersion(con, ServerVersion.v10) ) {
Statement stmt = null;
try {
stmt = con.createStatement();
stmt.execute("CREATE TABLE test_new ("
+ "id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,"
+ "payload text)");
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getColumns("", "", "test_new", "id");
assertTrue(rs.next());
assertEquals(rs.getString("COLUMN_NAME"), "id");
assertTrue(rs.getBoolean("IS_AUTOINCREMENT"));
} finally {
if ( stmt != null ) {
stmt.execute( "drop table test_new");
stmt.close();
}
}
}
}
}

0 comments on commit 9c3471f

Please sign in to comment.