You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/10/16 06:51:48 UTC
cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils BasicResultSetConverter.java
dgraham 2003/10/15 21:51:48
Modified: dbutils/src/java/org/apache/commons/dbutils
BasicResultSetConverter.java
Log:
toBean() and toBeanList() now match column names to bean property
names case insensitively for databases that always return column
names in upper or lower case. Also, if a column does not have a
corresponding bean property, it is ignored instead of throwing an
exception.
Revision Changes Path
1.2 +25 -18 jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/BasicResultSetConverter.java
Index: BasicResultSetConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/BasicResultSetConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BasicResultSetConverter.java 16 Oct 2003 04:21:11 -0000 1.1
+++ BasicResultSetConverter.java 16 Oct 2003 04:51:48 -0000 1.2
@@ -86,6 +86,12 @@
* @author David Graham
*/
public class BasicResultSetConverter implements ResultSetConverter {
+
+ /**
+ * Special array index that indicates there is no bean property that
+ * matches a column from a ResultSet.
+ */
+ private static final int PROPERTY_NOT_FOUND = -1;
/**
* The Singleton instance of this class.
@@ -147,12 +153,12 @@
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
- LOOP : for (int i = 1; i <= cols; i++) {
+ for (int i = 1; i <= cols; i++) {
String columnName = rsmd.getColumnName(i);
for (int j = 0; j < pd.length; j++) {
- if (columnName.equals(pd[j].getName())) {
+ if (columnName.equalsIgnoreCase(pd[j].getName())) {
Object value = rs.getObject(i);
if (rs.wasNull() && pd[j].getPropertyType().isPrimitive()) {
@@ -160,12 +166,8 @@
}
callSetter(pd[j], obj, value);
- continue LOOP;
}
}
-
- throw new SQLException(
- columnName + " not found in " + obj.getClass().getName());
}
return obj;
@@ -182,19 +184,21 @@
PropertyDescriptor[] pd = propertyDescriptors(type);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
- int nameToIndex[] = new int[cols + 1];
+
+ int columnNameToIndex[] = new int[cols + 1];
- LOOP : for (int i = 1; i <= cols; i++) {
- String columnName = rsmd.getColumnName(i);
+ for (int col = 1; col <= cols; col++) {
+ String columnName = rsmd.getColumnName(col);
for (int j = 0; j < pd.length; j++) {
- if (columnName.equals(pd[j].getName())) {
- nameToIndex[i] = j;
- continue LOOP;
+ if (columnName.equalsIgnoreCase(pd[j].getName())) {
+ columnNameToIndex[col] = j;
+ break;
+
+ } else{
+ columnNameToIndex[col] = PROPERTY_NOT_FOUND;
}
}
-
- throw new SQLException(" index not found for " + columnName);
}
do {
@@ -206,7 +210,9 @@
continue;
}
- callSetter(pd[nameToIndex[i]], obj, value);
+ if (columnNameToIndex[i] != PROPERTY_NOT_FOUND) {
+ callSetter(pd[columnNameToIndex[i]], obj, value);
+ }
}
results.add(obj);
@@ -283,6 +289,7 @@
* @throws DbException if introspection failed.
*/
private PropertyDescriptor[] propertyDescriptors(Class c) {
+ // TODO Cache BeanInfo classes for better performance?
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(c);
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org