From 7f1752a1f2853c88333b3ac75c2dc0212272b254 Mon Sep 17 00:00:00 2001 From: Dave Cramer Date: Fri, 22 Nov 2019 15:15:49 -0500 Subject: [PATCH] fix:handle numeric domain types (#1611) * fix:handle numeric domain types --- .../org/postgresql/jdbc/PgDatabaseMetaData.java | 9 ++++++--- .../postgresql/test/jdbc2/DatabaseMetaDataTest.java | 13 ++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java b/pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java index 26440aec14..b2f69a6f32 100644 --- a/pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java +++ b/pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java @@ -1595,7 +1595,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa /* From the docs if typtypmod is -1 */ - int domainLength = rs.getInt("typtypmod"); + int typtypmod = rs.getInt("typtypmod"); decimalDigits = connection.getTypeInfo().getScale(baseTypeOid, typeMod); /* From the postgres docs: @@ -1604,10 +1604,13 @@ base type (-1 if base type does not use a typmod). -1 if this type is not a doma if it is -1 then get the precision from the basetype. This doesn't help if the basetype is a domain, but for actual types this will return the correct value. */ - if ( domainLength == -1 ) { + if ( typtypmod == -1 ) { columnSize = connection.getTypeInfo().getPrecision(baseTypeOid, typeMod); + } else if (baseTypeOid == Oid.NUMERIC ) { + decimalDigits = connection.getTypeInfo().getScale(baseTypeOid, typtypmod); + columnSize = connection.getTypeInfo().getPrecision(baseTypeOid, typtypmod); } else { - columnSize = domainLength; + columnSize = typtypmod; } } else { decimalDigits = connection.getTypeInfo().getScale(typeOid, typeMod); diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java index e8e031404f..9d244726e8 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java @@ -84,7 +84,9 @@ public void setUp() throws Exception { TestUtil.createDomain(con, "nndom", "int not null"); TestUtil.createDomain(con, "varbit2", "varbit(3)"); - TestUtil.createTable(con, "domaintable", "id nndom, v varbit2"); + TestUtil.createDomain(con, "float83", "numeric(8,3)"); + + TestUtil.createTable(con, "domaintable", "id nndom, v varbit2, f float83"); stmt.close(); } @@ -112,6 +114,9 @@ public void tearDown() throws Exception { stmt.execute("DROP FUNCTION f3(int, varchar)"); TestUtil.dropTable(con, "domaintable"); TestUtil.dropDomain(con, "nndom"); + TestUtil.dropDomain(con, "varbit2"); + TestUtil.dropDomain(con, "float83"); + TestUtil.closeDB(con); } @@ -672,6 +677,7 @@ public void testNotNullDomainColumn() throws SQLException { assertEquals("id", rs.getString("COLUMN_NAME")); assertEquals("NO", rs.getString("IS_NULLABLE")); assertTrue(rs.next()); + assertTrue(rs.next()); assertTrue(!rs.next()); } @@ -685,6 +691,11 @@ public void testDomainColumnSize() throws SQLException { assertTrue(rs.next()); assertEquals("v", rs.getString("COLUMN_NAME")); assertEquals(3, rs.getInt("COLUMN_SIZE")); + assertTrue(rs.next()); + assertEquals("f", rs.getString("COLUMN_NAME")); + assertEquals(8, rs.getInt("COLUMN_SIZE")); + assertEquals( 3, rs.getInt("DECIMAL_DIGITS")); + } @Test