1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.dao.jdbc;
24  
25  import com.liferay.portal.kernel.util.CharPool;
26  
27  import java.sql.Date;
28  import java.sql.ResultSet;
29  import java.sql.ResultSetMetaData;
30  import java.sql.SQLException;
31  import java.sql.Timestamp;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * <a href="SmartResultSet.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Minhchau Dang
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class SmartResultSet {
44  
45      public SmartResultSet(ResultSet rs) throws SQLException {
46          _rs = rs;
47          _metaData = _rs.getMetaData();
48          _columnCount = _metaData.getColumnCount();
49          _columnIndexCache = new HashMap<String, Integer>();
50      }
51  
52      public int findColumn(String columnName) throws SQLException {
53          Integer columnIndex = _columnIndexCache.get(columnName);
54  
55          if (columnIndex != null) {
56              return columnIndex;
57          }
58  
59          // Check for the full column name
60  
61          for (int i = 1; i <= _columnCount; ++i) {
62              String availableName = _metaData.getColumnName(i);
63  
64              if (availableName.equalsIgnoreCase(columnName)) {
65                  _columnIndexCache.put(columnName, i);
66  
67                  return i;
68              }
69          }
70  
71          // Check for a shortened column name
72  
73          int pos = columnName.indexOf(CharPool.PERIOD);
74  
75          if (pos != -1) {
76              String shortName = columnName.substring(pos + 1);
77  
78              for (int i = 1; i <= _columnCount; ++i) {
79                  String availableName = _metaData.getColumnName(i);
80  
81                  if (availableName.equalsIgnoreCase(shortName)) {
82                      _columnIndexCache.put(columnName, i);
83  
84                      return i;
85                  }
86              }
87          }
88  
89          // Let the result set figure it out
90  
91          columnIndex = _rs.findColumn(columnName);
92  
93          _columnIndexCache.put(columnName, columnIndex);
94  
95          return columnIndex;
96      }
97  
98      public boolean first() throws SQLException {
99          return _rs.first();
100     }
101 
102     public Date getDate(int columnIndex) throws SQLException {
103         return _rs.getDate(columnIndex);
104     }
105 
106     public Date getDate(String columnName) throws SQLException {
107         int columnIndex = findColumn(columnName);
108 
109         return _rs.getDate(columnIndex);
110     }
111 
112     public double getDouble(int columnIndex) throws SQLException {
113         return _rs.getDouble(columnIndex);
114     }
115 
116     public double getDouble(String columnName) throws SQLException {
117         int columnIndex = findColumn(columnName);
118 
119         return _rs.getDouble(columnIndex);
120     }
121 
122     public float getFloat(int columnIndex) throws SQLException {
123         return _rs.getFloat(columnIndex);
124     }
125 
126     public float getFloat(String columnName) throws SQLException {
127         int columnIndex = findColumn(columnName);
128 
129         return _rs.getFloat(columnIndex);
130     }
131 
132     public int getInt(int columnIndex) throws SQLException {
133         return _rs.getInt(columnIndex);
134     }
135 
136     public int getInt(String columnName) throws SQLException {
137         int columnIndex = findColumn(columnName);
138 
139         return _rs.getInt(columnIndex);
140     }
141 
142     public long getLong(int columnIndex) throws SQLException {
143         return _rs.getLong(columnIndex);
144     }
145 
146     public long getLong(String columnName) throws SQLException {
147         int columnIndex = findColumn(columnName);
148 
149         return _rs.getLong(columnIndex);
150     }
151 
152     public short getShort(int columnIndex) throws SQLException {
153         return _rs.getShort(columnIndex);
154     }
155 
156     public short getShort(String columnName) throws SQLException {
157         int columnIndex = findColumn(columnName);
158 
159         return _rs.getShort(columnIndex);
160     }
161 
162     public String getString(int columnIndex) throws SQLException {
163         return _rs.getString(columnIndex);
164     }
165 
166     public String getString(String columnName) throws SQLException {
167         int columnIndex = findColumn(columnName);
168 
169         return _rs.getString(columnIndex);
170     }
171 
172     public Timestamp getTimestamp(int columnIndex) throws SQLException {
173         return _rs.getTimestamp(columnIndex);
174     }
175 
176     public Timestamp getTimestamp(String columnName) throws SQLException {
177         int columnIndex = findColumn(columnName);
178 
179         return _rs.getTimestamp(columnIndex);
180     }
181 
182     public boolean last() throws SQLException {
183         return _rs.last();
184     }
185 
186     public boolean next() throws SQLException {
187         return _rs.next();
188     }
189 
190     public boolean previous() throws SQLException {
191         return _rs.previous();
192     }
193 
194     private final ResultSet _rs;
195     private final ResultSetMetaData _metaData;
196     private final int _columnCount;
197     private final Map<String, Integer> _columnIndexCache;
198 
199 }