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