Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 25 additions & 29 deletions src/main/java/org/apache/commons/dbutils/BeanProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,44 +217,19 @@ private boolean isCompatibleType(final Object value, final Class<?> type) {
* @return An int[] with column index to property index mappings. The 0th
* element is meaningless because JDBC column indexing starts at 1.
*/
protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd,
final PropertyDescriptor[] props) throws SQLException {

protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd, final PropertyDescriptor[] props) throws SQLException {
final int cols = rsmd.getColumnCount();
final int[] columnToProperty = new int[cols + 1];
Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);

for (int col = 1; col <= cols; col++) {
String columnName = rsmd.getColumnLabel(col);
if (null == columnName || 0 == columnName.length()) {
columnName = rsmd.getColumnName(col);
}
String propertyName = columnToPropertyOverrides.get(columnName);
if (propertyName == null) {
propertyName = columnName;
}
if (propertyName == null) {
propertyName = Integer.toString(col);
}
String columnName = getColumnLabel(rsmd, col);
String propertyName = getPropertyName(columnName);

for (int i = 0; i < props.length; i++) {
final PropertyDescriptor prop = props[i];
final Method reader = prop.getReadMethod();

// Check for @Column annotations as explicit marks
final Column column;
if (reader != null) {
column = reader.getAnnotation(Column.class);
} else {
column = null;
}
final String propertyColumnName = getPropertyColumnName(prop);

final String propertyColumnName;
if (column != null) {
propertyColumnName = column.name();
} else {
propertyColumnName = prop.getName();
}
if (propertyName.equalsIgnoreCase(propertyColumnName)) {
columnToProperty[col] = i;
break;
Expand All @@ -265,6 +240,27 @@ protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd,
return columnToProperty;
}

private String getColumnLabel(ResultSetMetaData rsmd, int col) throws SQLException {
String columnLabel = rsmd.getColumnLabel(col);
return (columnLabel == null || columnLabel.isEmpty()) ? rsmd.getColumnName(col) : columnLabel;
}

private String getPropertyName(String columnName) {
String propertyName = columnToPropertyOverrides.get(columnName);
return (propertyName != null) ? propertyName : columnName;
}

private String getPropertyColumnName(PropertyDescriptor prop) {
final Method reader = prop.getReadMethod();
if (reader != null) {
Column column = reader.getAnnotation(Column.class);
if (column != null) {
return column.name();
}
}
return prop.getName();
}

/**
* Check whether a value is of the same primitive type as {@code targetType}.
*
Expand Down