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