View Javadoc

1   /*
2    * Copyright 2004-2007 Brian McCallister
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.skife.jdbi.v2;
18  
19  import org.skife.jdbi.v2.exceptions.ResultSetException;
20  import org.skife.jdbi.v2.tweak.ResultSetMapper;
21  
22  import java.sql.ResultSet;
23  import java.sql.ResultSetMetaData;
24  import java.sql.SQLException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  class DefaultMapper implements ResultSetMapper<Map<String, Object>>
29  {
30      public Map<String, Object> map(int index, ResultSet r, StatementContext ctx)
31      {
32          Map<String, Object> row = new DefaultResultMap();
33          ResultSetMetaData m;
34          try
35          {
36              m = r.getMetaData();
37          }
38          catch (SQLException e)
39          {
40              throw new ResultSetException("Unable to obtain metadata from result set", e, ctx);
41          }
42  
43          try
44          {
45              for (int i = 1; i <= m.getColumnCount(); i ++)
46              {
47                  String key = m.getColumnName(i);
48                  String alias = m.getColumnLabel(i);
49                  Object value = r.getObject(i);
50                  row.put(alias != null ? alias : key, value);
51              }
52          }
53          catch (SQLException e)
54          {
55              throw new ResultSetException("Unable to access specific metadata from " +
56                                           "result set metadata", e, ctx);
57          }
58          return row;
59      }
60  
61      private static class DefaultResultMap extends HashMap<String, Object>
62      {
63          public static final long serialVersionUID = 1L;
64  
65          public Object get(Object o)
66          {
67              return super.get(((String)o).toLowerCase());
68          }
69  
70          public Object put(String key, Object value)
71          {
72              return super.put(key.toLowerCase(), value);
73          }
74  
75          public boolean containsKey(Object key)
76          {
77              return super.containsKey(((String)key).toLowerCase());
78          }
79      }
80  }