Uploaded image for project: 'Commons DbUtils'
  1. Commons DbUtils
  2. DBUTILS-97

Add an Abstract ResultSetHandler implementation in order to reduce redundant 'resultSet' variable invocation

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.6
    • 1.6
    • None

    Description

      According to the DRY principle (Don't Repeat Yourself), repeating resultSet variable inside the ResultSetHandler#handle(ResultSet) over and over for each iteration can get a little tedious.
      It would be helpful adding a support class, named AbstractResultSetHandler, which implicitly gives users access to ResultSet's methods. For example, we could extend AbstractResultSetHandler and rewrite the mapping below:

      new ResultSetHandler<Collection<Map<String, Object>>> {
      
          @Override
          public Collection<Map<String, Object>> handle(ResultSet rs) throws SQLException {
              Collection<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
      
              while (rs.next()) {
                  Map<String, Object> current = new HashMap<String, Object>();
                  for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                      current.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
                  }
      
                  result.add(current);
              }
      
              return result;
          }
      
      }
      

      as:

      new AbstractResultSetHandler<Collection<Map<String, Object>>> {
      
          @Override
          protected Collection<Map<String, Object>> handle() throws SQLException {
              Collection<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
      
              while (next()) {
                  Map<String, Object> current = new HashMap<String, Object>();
                  for (int i = 1; i <= getMetaData().getColumnCount(); i++) {
                      current.put(getMetaData().getColumnName(i), getObject(i));
                  }
      
                  result.add(current);
              }
      
              return result;
          }
      
      }
      

      Attachments

        1. DBUTILS-97.patch
          60 kB
          Simone Tripodi

        Activity

          People

            simone.tripodi Simone Tripodi
            simone.tripodi Simone Tripodi
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: