01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.controls.system.jdbc;
20:
21: import java.sql.ResultSet;
22: import java.sql.ResultSetMetaData;
23: import java.sql.SQLException;
24: import java.util.HashMap;
25:
26: /**
27: * The ResultSetHashMap class extends a standard HashMap and populates it with data derived from a JDBC ResultSet.
28: * <p/>
29: * Note: the keys are treated case-insensitively, and therefore requests made on the map are
30: * case-insensitive. Any direct access to the keys will yield uppercase keys.
31: * <p/>
32: * Note: only the row associated with the current cursor position is used.
33: */
34: public class ResultSetHashMap extends HashMap<String, Object> {
35:
36: /**
37: * Default constructor.
38: */
39: ResultSetHashMap() {
40: super ();
41: }
42:
43: /**
44: * Constructor that initializes the map to a specific size.
45: * @param size the size
46: */
47: ResultSetHashMap(int size) {
48: super (size);
49: }
50:
51: /**
52: * This constructor is optimized for being called in a loop. It also canonicalizes the
53: * column names into upper case so that values in a map can be looked up using either
54: * upper, lower, or mixed case strings.
55: *
56: * @param rs the ResultSet to map
57: * @param keys an array of key objects to store in the map
58: * @throws SQLException if an error occurs while reading from the ResultSet
59: */
60: ResultSetHashMap(ResultSet rs, String[] keys) throws SQLException {
61: super (keys.length);
62:
63: assert keys.length == rs.getMetaData().getColumnCount() + 1;
64:
65: for (int i = 1; i < keys.length; i++) {
66: assert keys[i].equals(keys[i].toUpperCase());
67: put(keys[i], rs.getObject(i));
68: }
69: }
70:
71: ResultSetHashMap(ResultSet rs) throws SQLException {
72: super ();
73: ResultSetMetaData md = rs.getMetaData();
74: for (int i = 1; i <= md.getColumnCount(); i++) {
75: put(md.getColumnName(i), rs.getObject(i));
76: }
77: }
78:
79: public boolean containsKey(Object key) {
80: return super .containsKey(canonicalizeKey(key));
81: }
82:
83: public Object get(Object key) {
84: return super .get(canonicalizeKey(key));
85: }
86:
87: public Object put(String key, Object value) {
88: return super .put(canonicalizeKey(key), value);
89: }
90:
91: public Object remove(Object key) {
92: return super .remove(canonicalizeKey(key));
93: }
94:
95: private String canonicalizeKey(Object object) {
96: return object == null ? null : object.toString().toUpperCase();
97: }
98: }
|