View Javadoc

    1   package sharin.sql.runner.processor;
    2
    3   import java.sql.ResultSet;
    4   import java.sql.ResultSetMetaData;
    5   import java.util.HashMap;
    6   import java.util.Map;
    7
    8   import sharin.sql.runner.ResultSetProcessor;
    9   import sharin.util.PropertyUtils;
    10  import sharin.util.SqlUtils;
    11
    12  public class MapResultSetProcessor implements ResultSetProcessor {
    13
    14      public Object prepare(ResultSet rs) {
    15          ResultSetMetaData metaData = SqlUtils.getMetaData(rs);
    16          int columnCount = SqlUtils.getColumnCount(metaData);
    17          String[] columnLabels = new String[columnCount];
    18
    19          for (int i = 0; i < columnCount; i++) {
    20              columnLabels[i] = SqlUtils.getColumnLabel(metaData, i + 1);
    21          }
    22
    23          return columnLabels;
    24      }
    25
    26      public Object process(ResultSet rs, Object context) {
    27          String[] columnLabels = (String[]) context;
    28          Map<String, Object> result = new HashMap<String, Object>();
    29
    30          for (int i = 0; i < columnLabels.length; i++) {
    31              PropertyUtils.setNestedPropertyValue(result, columnLabels[i],
    32                      SqlUtils.getObject(rs, i + 1));
    33          }
    34
    35          return result;
    36      }
    37  }