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: PgDatabaseMetaData.getTables() should return UPPERCASE column names as per spec #2092

Merged
merged 2 commits into from
Mar 22, 2021
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
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));

}
}