Skip to content

Commit e72057c

Browse files
authored
Resolve column name in ResultSetIterator.set (#426)
* resolve column name in ResultSetIterator.set the read path resolves the dyna-property name to the real column name via getColumnName, but set passed the raw name to updateObject, so with the default lowerCase a mixed-case column update targeted the wrong column. * use AtomicReference to capture the updated column name in test
1 parent 6a1b16c commit e72057c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/main/java/org/apache/commons/beanutils2/sql/ResultSetIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public void set(final String name, final Object value) {
249249
throw new IllegalArgumentException(name);
250250
}
251251
try {
252-
dynaClass.getResultSet().updateObject(name, value);
252+
dynaClass.getResultSet().updateObject(dynaClass.getColumnName(name), value);
253253
} catch (final SQLException e) {
254254
throw new IllegalArgumentException("set(" + name + "): SQLException: " + e);
255255
}

src/test/java/org/apache/commons/beanutils2/sql/DynaResultSetTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import static org.junit.jupiter.api.Assertions.assertThrows;
2626

2727
import java.math.BigDecimal;
28+
import java.sql.ResultSet;
29+
import java.sql.SQLException;
2830
import java.util.Iterator;
31+
import java.util.concurrent.atomic.AtomicReference;
2932

3033
import org.apache.commons.beanutils2.DynaBean;
3134
import org.apache.commons.beanutils2.DynaProperty;
@@ -96,6 +99,26 @@ void testGetName() {
9699
assertEquals("org.apache.commons.beanutils2.sql.ResultSetDynaClass", dynaClass.getName(), "DynaClass name");
97100
}
98101

102+
/**
103+
* With the default {@code lowerCase} option the property name differs from the real column name, and the read path resolves it through
104+
* {@code getColumnName}. Verify that {@code set} resolves it the same way, so the update targets the real column name and not the lower-cased property
105+
* name.
106+
*/
107+
@Test
108+
void testSetUsesColumnName() throws Exception {
109+
final AtomicReference<String> updatedColumn = new AtomicReference<>();
110+
final ResultSet resultSet = TestResultSet.createProxy(new TestResultSet() {
111+
@Override
112+
public void updateObject(final String columnName, final Object value) throws SQLException {
113+
updatedColumn.set(columnName);
114+
}
115+
});
116+
final ResultSetDynaClass rsdc = new ResultSetDynaClass(resultSet);
117+
final DynaBean row = rsdc.iterator().next();
118+
row.set("stringproperty", "new value");
119+
assertEquals("stringProperty", updatedColumn.get(), "update targets the real column name");
120+
}
121+
99122
@Test
100123
void testIteratorCount() {
101124

0 commit comments

Comments
 (0)