dgraham 2003/10/15 22:00:20
Modified: dbutils/src/java/org/apache/commons/dbutils
BasicResultSetConverter.java
Log:
Refactored column name to bean property indexing from toBeanList()
into new mapColumnsToProperties() method.
Revision Changes Path
1.3 +40 -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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BasicResultSetConverter.java 16 Oct 2003 04:51:48 -0000 1.2
+++ BasicResultSetConverter.java 16 Oct 2003 05:00:20 -0000 1.3
@@ -183,23 +183,10 @@
PropertyDescriptor[] pd = propertyDescriptors(type);
ResultSetMetaData rsmd = rs.getMetaData();
- int cols = rsmd.getColumnCount();
-
- int columnNameToIndex[] = new int[cols + 1];
- for (int col = 1; col <= cols; col++) {
- String columnName = rsmd.getColumnName(col);
- for (int j = 0; j < pd.length; j++) {
+ int[] columnNameToIndex = this.mapColumnsToProperties(rsmd, pd);
- if (columnName.equalsIgnoreCase(pd[j].getName())) {
- columnNameToIndex[col] = j;
- break;
-
- } else{
- columnNameToIndex[col] = PROPERTY_NOT_FOUND;
- }
- }
- }
+ int cols = rsmd.getColumnCount();
do {
Object obj = newInstance(type);
@@ -220,6 +207,41 @@
} while (rs.next());
return results;
+ }
+
+ /**
+ * The positions in the returned array represent column numbers. The values
+ * stored at each position represent the index in the PropertyDescriptor[]
+ * for the bean property that matches the column name.
+ *
+ * @return An int[] with column index to property index mappings. The 0th
+ * element is meaningless as column indexing starts at 1.
+ *
+ * @throws SQLException
+ */
+ private int[] mapColumnsToProperties(
+ ResultSetMetaData rsmd,
+ PropertyDescriptor[] pd)
+ throws SQLException {
+
+ int cols = rsmd.getColumnCount();
+ int columnNameToIndex[] = new int[cols + 1];
+
+ for (int col = 1; col <= cols; col++) {
+ String columnName = rsmd.getColumnName(col);
+ for (int j = 0; j < pd.length; j++) {
+
+ if (columnName.equalsIgnoreCase(pd[j].getName())) {
+ columnNameToIndex[col] = j;
+ break;
+
+ } else {
+ columnNameToIndex[col] = PROPERTY_NOT_FOUND;
+ }
+ }
+ }
+
+ return columnNameToIndex;
}
// See interface for javadoc.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]