Skip to content

Commit

Permalink
fix: get correct column length for simple domains (#1605)
Browse files Browse the repository at this point in the history
* fix: get correct column length for simple domains
  • Loading branch information
davecramer committed Nov 18, 2019
1 parent 47f756f commit 8abf316
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
39 changes: 31 additions & 8 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Expand Up @@ -1502,7 +1502,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
}

sql += "SELECT n.nspname,c.relname,a.attname,a.atttypid,a.attnotnull "
+ "OR (t.typtype = 'd' AND t.typnotnull) AS attnotnull,a.atttypmod,a.attlen,";
+ "OR (t.typtype = 'd' AND t.typnotnull) AS attnotnull,a.atttypmod,a.attlen,t.typtypmod,";

if (connection.haveMinimumServerVersion(ServerVersion.v8_4)) {
sql += "row_number() OVER (PARTITION BY a.attrelid ORDER BY a.attnum) AS attnum, ";
Expand Down Expand Up @@ -1585,12 +1585,37 @@ 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);
if (columnSize == 0) {
columnSize = connection.getTypeInfo().getDisplaySize(typeOid, typeMod);
}
int baseTypeOid = (int) rs.getLong("typbasetype");

int decimalDigits;
int columnSize;

/* this is really a DOMAIN type not sure where DISTINCT came from */
if ( sqlType == Types.DISTINCT ) {
/*
From the docs if typtypmod is -1
*/
int domainLength = rs.getInt("typtypmod");
decimalDigits = connection.getTypeInfo().getScale(baseTypeOid, typeMod);
/*
From the postgres docs:
Domains use typtypmod to record the typmod to be applied to their
base type (-1 if base type does not use a typmod). -1 if this type is not a domain.
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 ) {
columnSize = connection.getTypeInfo().getPrecision(baseTypeOid, typeMod);
} else {
columnSize = domainLength;
}
} else {
decimalDigits = connection.getTypeInfo().getScale(typeOid, typeMod);
columnSize = connection.getTypeInfo().getPrecision(typeOid, typeMod);
if (columnSize == 0) {
columnSize = connection.getTypeInfo().getDisplaySize(typeOid, typeMod);
}
}
tuple[6] = connection.encodeString(Integer.toString(columnSize));
tuple[8] = connection.encodeString(Integer.toString(decimalDigits));

Expand All @@ -1612,8 +1637,6 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
// Is nullable
tuple[17] = connection.encodeString(rs.getBoolean("attnotnull") ? "NO" : "YES");

int baseTypeOid = (int) rs.getLong("typbasetype");

tuple[18] = null; // SCOPE_CATLOG
tuple[19] = null; // SCOPE_SCHEMA
tuple[20] = null; // SCOPE_TABLE
Expand Down
Expand Up @@ -83,7 +83,8 @@ public void setUp() throws Exception {
}

TestUtil.createDomain(con, "nndom", "int not null");
TestUtil.createTable(con, "domaintable", "id nndom");
TestUtil.createDomain(con, "varbit2", "varbit(3)");
TestUtil.createTable(con, "domaintable", "id nndom, v varbit2");
stmt.close();
}

Expand Down Expand Up @@ -670,9 +671,22 @@ public void testNotNullDomainColumn() throws SQLException {
assertTrue(rs.next());
assertEquals("id", rs.getString("COLUMN_NAME"));
assertEquals("NO", rs.getString("IS_NULLABLE"));
assertTrue(rs.next());
assertTrue(!rs.next());
}

@Test
public void testDomainColumnSize() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getColumns("", "", "domaintable", "");
assertTrue(rs.next());
assertEquals("id", rs.getString("COLUMN_NAME"));
assertEquals(10, rs.getInt("COLUMN_SIZE"));
assertTrue(rs.next());
assertEquals("v", rs.getString("COLUMN_NAME"));
assertEquals(3, rs.getInt("COLUMN_SIZE"));
}

@Test
public void testAscDescIndexInfo() throws SQLException {
if (!TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_3)) {
Expand Down

0 comments on commit 8abf316

Please sign in to comment.