View Javadoc

1   /*
2    * $Id: ColumnAliasRowProcessor.java 22580 2011-08-01 13:38:12Z evangelinamrm $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jdbc;
12  
13  import org.mule.util.CaseInsensitiveHashMap;
14  
15  import java.sql.ResultSet;
16  import java.sql.ResultSetMetaData;
17  import java.sql.SQLException;
18  import java.util.Map;
19  
20  import org.apache.commons.dbutils.BasicRowProcessor;
21  
22  /**
23   * Processes a row from a {@link ResultSet} using the column labels
24   * instead of the column names.
25   * <p/>
26   * This is needed because some database drivers return different values for the
27   * column name and column label. {@link BasicRowProcessor} uses column names,
28   * so in the mentioned cases column aliases are lost and are only available for
29   * calculated values.
30   */
31  public class ColumnAliasRowProcessor extends BasicRowProcessor
32  {
33  
34      @Override
35      public Map toMap(ResultSet rs) throws SQLException
36      {
37          Map result = new CaseInsensitiveHashMap();
38          ResultSetMetaData rsmd = rs.getMetaData();
39          int cols = rsmd.getColumnCount();
40  
41          for (int i = 1; i <= cols; i++)
42          {
43              result.put(rsmd.getColumnLabel(i), rs.getObject(i));
44          }
45  
46          return result;
47      }
48  }