1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.dao.jdbc;
16  
17  import com.liferay.portal.kernel.util.CharPool;
18  
19  import java.sql.Date;
20  import java.sql.ResultSet;
21  import java.sql.ResultSetMetaData;
22  import java.sql.SQLException;
23  import java.sql.Timestamp;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  /**
29   * <a href="SmartResultSet.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Minhchau Dang
32   * @author Brian Wing Shun Chan
33   */
34  public class SmartResultSet {
35  
36      public SmartResultSet(ResultSet rs) throws SQLException {
37          _rs = rs;
38          _metaData = _rs.getMetaData();
39          _columnCount = _metaData.getColumnCount();
40          _columnIndexCache = new HashMap<String, Integer>();
41      }
42  
43      public int findColumn(String columnName) throws SQLException {
44          Integer columnIndex = _columnIndexCache.get(columnName);
45  
46          if (columnIndex != null) {
47              return columnIndex;
48          }
49  
50          // Check for the full column name
51  
52          for (int i = 1; i <= _columnCount; ++i) {
53              String availableName = _metaData.getColumnName(i);
54  
55              if (availableName.equalsIgnoreCase(columnName)) {
56                  _columnIndexCache.put(columnName, i);
57  
58                  return i;
59              }
60          }
61  
62          // Check for a shortened column name
63  
64          int pos = columnName.indexOf(CharPool.PERIOD);
65  
66          if (pos != -1) {
67              String shortName = columnName.substring(pos + 1);
68  
69              for (int i = 1; i <= _columnCount; ++i) {
70                  String availableName = _metaData.getColumnName(i);
71  
72                  if (availableName.equalsIgnoreCase(shortName)) {
73                      _columnIndexCache.put(columnName, i);
74  
75                      return i;
76                  }
77              }
78          }
79  
80          // Let the result set figure it out
81  
82          columnIndex = _rs.findColumn(columnName);
83  
84          _columnIndexCache.put(columnName, columnIndex);
85  
86          return columnIndex;
87      }
88  
89      public boolean first() throws SQLException {
90          return _rs.first();
91      }
92  
93      public Date getDate(int columnIndex) throws SQLException {
94          return _rs.getDate(columnIndex);
95      }
96  
97      public Date getDate(String columnName) throws SQLException {
98          int columnIndex = findColumn(columnName);
99  
100         return _rs.getDate(columnIndex);
101     }
102 
103     public double getDouble(int columnIndex) throws SQLException {
104         return _rs.getDouble(columnIndex);
105     }
106 
107     public double getDouble(String columnName) throws SQLException {
108         int columnIndex = findColumn(columnName);
109 
110         return _rs.getDouble(columnIndex);
111     }
112 
113     public float getFloat(int columnIndex) throws SQLException {
114         return _rs.getFloat(columnIndex);
115     }
116 
117     public float getFloat(String columnName) throws SQLException {
118         int columnIndex = findColumn(columnName);
119 
120         return _rs.getFloat(columnIndex);
121     }
122 
123     public int getInt(int columnIndex) throws SQLException {
124         return _rs.getInt(columnIndex);
125     }
126 
127     public int getInt(String columnName) throws SQLException {
128         int columnIndex = findColumn(columnName);
129 
130         return _rs.getInt(columnIndex);
131     }
132 
133     public long getLong(int columnIndex) throws SQLException {
134         return _rs.getLong(columnIndex);
135     }
136 
137     public long getLong(String columnName) throws SQLException {
138         int columnIndex = findColumn(columnName);
139 
140         return _rs.getLong(columnIndex);
141     }
142 
143     public short getShort(int columnIndex) throws SQLException {
144         return _rs.getShort(columnIndex);
145     }
146 
147     public short getShort(String columnName) throws SQLException {
148         int columnIndex = findColumn(columnName);
149 
150         return _rs.getShort(columnIndex);
151     }
152 
153     public String getString(int columnIndex) throws SQLException {
154         return _rs.getString(columnIndex);
155     }
156 
157     public String getString(String columnName) throws SQLException {
158         int columnIndex = findColumn(columnName);
159 
160         return _rs.getString(columnIndex);
161     }
162 
163     public Timestamp getTimestamp(int columnIndex) throws SQLException {
164         return _rs.getTimestamp(columnIndex);
165     }
166 
167     public Timestamp getTimestamp(String columnName) throws SQLException {
168         int columnIndex = findColumn(columnName);
169 
170         return _rs.getTimestamp(columnIndex);
171     }
172 
173     public boolean last() throws SQLException {
174         return _rs.last();
175     }
176 
177     public boolean next() throws SQLException {
178         return _rs.next();
179     }
180 
181     public boolean previous() throws SQLException {
182         return _rs.previous();
183     }
184 
185     private final ResultSet _rs;
186     private final ResultSetMetaData _metaData;
187     private final int _columnCount;
188     private final Map<String, Integer> _columnIndexCache;
189 
190 }