Skip to content

Commit

Permalink
JDBC - Ensure empty query column names are renamed to column_n
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelborn committed Jan 21, 2025
1 parent f4a9d05 commit 4b65eea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,16 @@ private IStruct buildQueryRow( ResultSet resultSet, ResultSetMetaData resultSetM
* @throws SQLException
*/
private void buildQueryColumns( Query result, ResultSetMetaData resultSetMetaData ) throws SQLException {
int columnCount = resultSetMetaData.getColumnCount();
int columnCount = resultSetMetaData.getColumnCount();
int emptyCounter = 0;
// The column count starts from 1
for ( int i = 1; i <= columnCount; i++ ) {
String label = resultSetMetaData.getColumnLabel( i );
if ( label.isBlank() ) {
label = "column_" + ( emptyCounter++ );
}
result.addColumn(
Key.of( resultSetMetaData.getColumnLabel( i ) ),
Key.of( label ),
QueryColumnType.fromSQLType( resultSetMetaData.getColumnType( i ) )
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/ortus/boxlang/runtime/types/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ public static Query fromResultSet( ResultSet resultSet ) {
// This will map which column in the JDBC result corresponds with the ordinal position of each query column
List<Integer> columnMapList = new ArrayList<>();

int emptyCounter = 0;
// The column count starts from 1
for ( int i = 1; i <= columnCount; i++ ) {
Key colName = Key.of( resultSetMetaData.getColumnLabel( i ) );
String label = resultSetMetaData.getColumnLabel( i );
if ( label.isBlank() ) {
label = "column_" + ( emptyCounter++ );
}
Key colName = Key.of( label );
// If we haven't hit this column name before....
if ( !query.hasColumn( colName ) ) {
// Add it
Expand Down

0 comments on commit 4b65eea

Please sign in to comment.