-
Notifications
You must be signed in to change notification settings - Fork 356
Reestablish warning on duplicate column in query. #1825
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.dao.DataRetrievalFailureException; | ||
import org.springframework.data.jdbc.core.convert.RowDocumentExtractorSupport.AggregateContext; | ||
import org.springframework.data.jdbc.core.convert.RowDocumentExtractorSupport.RowDocumentSink; | ||
|
@@ -43,6 +45,9 @@ | |
*/ | ||
class RowDocumentResultSetExtractor { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(RowDocumentResultSetExtractor.class); | ||
public static final String DUPLICATE_COLUMN_WARNING = "ResultSet contains column \"{}\" multiple times. Later column index is {}"; | ||
|
||
private final RelationalMappingContext context; | ||
private final PathToColumnMapping propertyToColumn; | ||
|
||
|
@@ -66,9 +71,13 @@ static RowDocument toRowDocument(ResultSet resultSet) throws SQLException { | |
RowDocument document = new RowDocument(columnCount); | ||
|
||
for (int i = 0; i < columnCount; i++) { | ||
|
||
Object rsv = JdbcUtils.getResultSetValue(resultSet, i + 1); | ||
String columnName = md.getColumnLabel(i + 1); | ||
document.put(columnName, rsv instanceof Array a ? a.getArray() : rsv); | ||
Object old = document.put(columnName, rsv instanceof Array a ? a.getArray() : rsv); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering why we're not using For reference, none of Spring JDBC's mappers care about duplicate columns, they just accept the fact that Furthermore, when an external query declares the same column names, why is it us to validate this concern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, the JDBC spec says when you have duplicate column names, the first one should be returned. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because we are nice people and we notice something is weird. |
||
if (old != null) { | ||
log.warn(DUPLICATE_COLUMN_WARNING, columnName, i); | ||
} | ||
} | ||
|
||
return document; | ||
|
@@ -107,7 +116,12 @@ public Map<String, Integer> getColumnMap(ResultSet result) { | |
Map<String, Integer> columns = new LinkedCaseInsensitiveMap<>(metaData.getColumnCount()); | ||
|
||
for (int i = 0; i < metaData.getColumnCount(); i++) { | ||
columns.put(metaData.getColumnLabel(i + 1), i + 1); | ||
|
||
String columnLabel = metaData.getColumnLabel(i + 1); | ||
Object old = columns.put(columnLabel, i + 1); | ||
if (old != null) { | ||
log.warn(DUPLICATE_COLUMN_WARNING, columnLabel, i); | ||
} | ||
} | ||
return columns; | ||
} catch (SQLException e) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: We should use
JdbcUtils.lookupColumnName(…)
here.