Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get correct column length for simple domains #1605

Merged
merged 2 commits into from Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 31 additions & 8 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Expand Up @@ -1497,7 +1497,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 @@ -1580,12 +1580,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 @@ -1607,8 +1632,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