Commons DbUtils
  1. Commons DbUtils
  2. DBUTILS-91

Enhance BasicRowProcessor to have row mapping easier to configure

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.4
    • Fix Version/s: 1.5
    • Labels:
      None

      Description

      BasicRowProcessor by default makes use of BeanProcessor for mapping result set row columns to bean property names. BeanProcessor uses bean property names, and performs case-insensitive matching of those names to column names.

      Currently BasicRowProcessor can be configured with custom BeanProcessor extension to customize the row mapping, but it gets rather ugly.

      It would be great if BeanProcessor was configurable with column name to bean property name strategy. Besides current strategy, DbUtils should also bundle strategy implementation configurable with a property name to column name (or other way around) map.

        Activity

        Stevo Slavic created issue -
        Hide
        Stevo Slavic added a comment - - edited

        Here is sample code that I currently (commons-dbutils:commons-dbutils:1.4:jar) have to use to override column to property name mapping:

        final Map<String, String> columnToPropertyOverrides = new HashMap<>();
        columnToPropertyOverrides.put("foo", "bar");
        ResultSetHandler<List<Acme>> rsh = new BeanListHandler<Acme>(Acme.class, new BasicRowProcessor(new BeanProcessor() {
        	@Override
        	protected int[] mapColumnsToProperties(ResultSetMetaData rsmd,
        			PropertyDescriptor[] props) throws SQLException {
        		int cols = rsmd.getColumnCount();
        		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;
        			}
        			for (int i = 0; i < props.length; i++) {
        				if (propertyName.equalsIgnoreCase(props[i].getName())) {
        					columnToProperty[col] = i;
        					break;
        				}
        			}
        		}
        
        		return columnToProperty;
        	}
        }));
        
        Show
        Stevo Slavic added a comment - - edited Here is sample code that I currently (commons-dbutils:commons-dbutils:1.4:jar) have to use to override column to property name mapping: final Map< String , String > columnToPropertyOverrides = new HashMap<>(); columnToPropertyOverrides.put( "foo" , "bar" ); ResultSetHandler<List<Acme>> rsh = new BeanListHandler<Acme>(Acme.class, new BasicRowProcessor( new BeanProcessor() { @Override protected int [] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props) throws SQLException { int cols = rsmd.getColumnCount(); 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; } for ( int i = 0; i < props.length; i++) { if (propertyName.equalsIgnoreCase(props[i].getName())) { columnToProperty[col] = i; break ; } } } return columnToProperty; } }));
        Hide
        Stevo Slavic added a comment -

        Here's a rather simple patch ( DBUTILS-91.patch ) which makes overriding mapping defaults a bit cleaner.

        final Map<String, String> columnToPropertyOverrides = new HashMap<>();
        columnToPropertyOverrides.put("foo", "bar");
        ResultSetHandler<List<Acme>> rsh = new BeanListHandler<Acme>(Acme.class, new BasicRowProcessor(new BeanProcessor(columnToPropertyOverrides)));
        
        Show
        Stevo Slavic added a comment - Here's a rather simple patch ( DBUTILS-91.patch ) which makes overriding mapping defaults a bit cleaner. final Map< String , String > columnToPropertyOverrides = new HashMap<>(); columnToPropertyOverrides.put( "foo" , "bar" ); ResultSetHandler<List<Acme>> rsh = new BeanListHandler<Acme>(Acme.class, new BasicRowProcessor( new BeanProcessor(columnToPropertyOverrides)));
        Stevo Slavic made changes -
        Field Original Value New Value
        Attachment DBUTILS-91.patch [ 12535340 ]
        Hide
        Simone Tripodi added a comment - - edited

        Patch applied, see r1358253, thanks for contributing.

        Please, for future patches, remind the following:

        • respect the original file format - no tabs;
        • javadoc was not complete (the new constructor didn't have the argument documented);
        • when adding new methods/constructors, add the @since tag;
        • the constructor didn't shield the class from potential null maps, which could have caused NPEs at runtime.

        thanks

        Show
        Simone Tripodi added a comment - - edited Patch applied, see r1358253, thanks for contributing. Please, for future patches, remind the following: respect the original file format - no tabs; javadoc was not complete (the new constructor didn't have the argument documented); when adding new methods/constructors, add the @since tag; the constructor didn't shield the class from potential null maps, which could have caused NPEs at runtime. thanks
        Simone Tripodi made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Simone Tripodi [ simone.tripodi ]
        Fix Version/s 1.5 [ 12317964 ]
        Resolution Fixed [ 1 ]
        Hide
        Simone Tripodi added a comment -

        1.5-RC released

        Show
        Simone Tripodi added a comment - 1.5-RC released
        Simone Tripodi made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Simone Tripodi
            Reporter:
            Stevo Slavic
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development