Skip to content

Commit

Permalink
fix: PgDatabaseMetaData should return UPPERCASE column names as per s… (
Browse files Browse the repository at this point in the history
#2092)

* fix: PgDatabaseMetaData should return UPPERCASE column names as per spec, fixes #830

* fix javadoc errors
  • Loading branch information
davecramer committed Mar 22, 2021
1 parent d6ef27a commit e912c6c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 5 additions & 3 deletions pgjdbc/src/main/java/org/postgresql/core/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.qual.Pure;

/*
*/
public class Field {
// The V3 protocol defines two constants for the format of data
public static final int TEXT_FORMAT = 0;
Expand All @@ -20,7 +18,7 @@ public class Field {
private final int length; // Internal Length of this field
private final int oid; // OID of the type
private final int mod; // type modifier of this field
private final String columnLabel; // Column label
private String columnLabel; // Column label

private int format = TEXT_FORMAT; // In the V3 protocol each field has a format
// 0 = text, 1 = binary
Expand Down Expand Up @@ -172,4 +170,8 @@ public String getPGType() {
public boolean isTypeInitialized() {
return pgType != NOT_YET_LOADED;
}

public void upperCaseLabel() {
columnLabel = columnLabel.toUpperCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ public ResultSet getTables(@Nullable String catalog, @Nullable String schemaPatt
}
String sql = select + orderby;

return createMetaDataStatement().executeQuery(sql);
return ((PgResultSet)createMetaDataStatement().executeQuery(sql)).upperCaseFieldLabels();
}

private static final Map<String, Map<String, String>> tableTypeClauses;
Expand Down
16 changes: 16 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3993,4 +3993,20 @@ private Calendar getDefaultCalendar() {
}
return sharedCalendar;
}

/**
* This is here to be used by metadata functions
* to make all column labels upper case.
* Because postgres folds columns to lower case in queries it will be easier
* to change the fields after the fact rather than try to coerce all the columns
* to upper case in the queries as this would require surrounding all columns with " and
* escaping them making them even harder to read than they are now.
* @return PgResultSet
*/
protected PgResultSet upperCaseFieldLabels() {
for (Field field: fields ) {
field.upperCaseLabel();
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1583,4 +1583,16 @@ public void testSmallSerialSequenceLikeColumns() throws SQLException {
stmt.close();
}

@Test
public void testUpperCaseMetaDataLabels() throws SQLException {
ResultSet rs = con.getMetaData().getTables(null, null, null, null);
ResultSetMetaData rsmd = rs.getMetaData();

assertEquals("TABLE_CAT", rsmd.getColumnName(1));
assertEquals("TABLE_SCHEM", rsmd.getColumnName(2));
assertEquals("TABLE_NAME", rsmd.getColumnName(3));
assertEquals("TABLE_TYPE", rsmd.getColumnName(4));
assertEquals("REMARKS", rsmd.getColumnName(5));

}
}

0 comments on commit e912c6c

Please sign in to comment.