Skip to content

Commit

Permalink
add test for identity, fix isAutoincrement in postgresql 10 fixes #130 (
Browse files Browse the repository at this point in the history
#1004)

* add test for identity, fix isAutoincrement in postgresql 10 fixes #130
  • Loading branch information
davecramer committed Nov 7, 2017
1 parent 059628f commit 2f6633b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.postgresql.PGResultSetMetaData;
import org.postgresql.core.BaseConnection;
import org.postgresql.core.Field;
import org.postgresql.core.ServerVersion;
import org.postgresql.util.GT;
import org.postgresql.util.JdbcBlackHole;
import org.postgresql.util.LruCache;
Expand Down Expand Up @@ -233,9 +234,14 @@ private void fetchFieldMetaData() throws SQLException {

StringBuilder sql = new StringBuilder(
"SELECT c.oid, a.attnum, a.attname, c.relname, n.nspname, "
+ "a.attnotnull OR (t.typtype = 'd' AND t.typnotnull), "
+ "pg_catalog.pg_get_expr(d.adbin, d.adrelid) LIKE '%nextval(%' "
+ "FROM pg_catalog.pg_class c "
+ "a.attnotnull OR (t.typtype = 'd' AND t.typnotnull), ");

if ( connection.haveMinimumServerVersion(ServerVersion.v10)) {
sql.append("a.attidentity != '' OR pg_catalog.pg_get_expr(d.adbin, d.adrelid) LIKE '%nextval(%' ");
} else {
sql.append("pg_catalog.pg_get_expr(d.adbin, d.adrelid) LIKE '%nextval(%' ");
}
sql.append( "FROM pg_catalog.pg_class c "
+ "JOIN pg_catalog.pg_namespace n ON (c.relnamespace = n.oid) "
+ "JOIN pg_catalog.pg_attribute a ON (c.oid = a.attrelid) "
+ "JOIN pg_catalog.pg_type t ON (a.atttypid = t.oid) "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import static org.junit.Assert.assertTrue;

import org.postgresql.PGResultSetMetaData;
import org.postgresql.core.ServerVersion;
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.test.TestUtil;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;

Expand All @@ -38,6 +40,11 @@ public void setUp() throws Exception {

TestUtil.dropSequence(conn, "serialtest_a_seq");
TestUtil.dropSequence(conn, "serialtest_b_seq");

if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v10)) {
TestUtil.createTable(conn, "identitytest", "id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
}

TestUtil.createTable(conn, "serialtest", "a serial, b bigserial, c int");
TestUtil.createTable(conn, "alltypes",
"bool boolean, i2 int2, i4 int4, i8 int8, num numeric(10,2), re real, fl float, ch char(3), vc varchar(3), tx text, d date, t time without time zone, tz time with time zone, ts timestamp without time zone, tsz timestamp with time zone, bt bytea");
Expand All @@ -52,6 +59,9 @@ public void tearDown() throws SQLException {
TestUtil.dropTable(conn, "rsmd1");
TestUtil.dropTable(conn, "timetest");
TestUtil.dropTable(conn, "serialtest");
if (TestUtil.haveMinimumServerVersion(conn, ServerVersion.v10)) {
TestUtil.dropTable(conn, "identitytest");
}
TestUtil.dropTable(conn, "alltypes");
TestUtil.dropTable(conn, "sizetest");
TestUtil.dropSequence(conn, "serialtest_a_seq");
Expand Down Expand Up @@ -248,6 +258,16 @@ public void testClosedResultSet() throws Exception {
assertEquals("rsmd1", rsmd.getColumnTypeName(1));
}

@Test
public void testIdentityColumn() throws Exception {
assumeMinimumServerVersion(ServerVersion.v10);
assumePreparedStatementMetadataSupported();
PreparedStatement pstmt = conn.prepareStatement("SELECT id FROM identitytest");
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rsmd = pstmt.getMetaData();
Assert.assertTrue(rsmd.isAutoIncrement(1));
}

private void assumePreparedStatementMetadataSupported() {
Assume.assumeTrue("prepared statement metadata is not supported for simple protocol",
preferQueryMode.compareTo(PreferQueryMode.EXTENDED_FOR_PREPARED) >= 0);
Expand Down

0 comments on commit 2f6633b

Please sign in to comment.