1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class SmartResultSet {
43  
44      public SmartResultSet(ResultSet rs) throws SQLException {
45          _rs = rs;
46          _metaData = _rs.getMetaData();
47          _columnCount = _metaData.getColumnCount();
48          _columnIndexCache = new HashMap<String, Integer>();
49      }
50  
51      public int findColumn(String columnName) throws SQLException {
52          Integer columnIndex = _columnIndexCache.get(columnName);
53  
54          if (columnIndex != null) {
55              return columnIndex;
56          }
57  
58          // Check for the full column name
59  
60          for (int i = 1; i <= _columnCount; ++i) {
61              String availableName = _metaData.getColumnName(i);
62  
63              if (availableName.equalsIgnoreCase(columnName)) {
64                  _columnIndexCache.put(columnName, i);
65  
66                  return i;
67              }
68          }
69  
70          // Check for a shortened column name
71  
72          int pos = columnName.indexOf(CharPool.PERIOD);
73  
74          if (pos != -1) {
75              String shortName = columnName.substring(pos + 1);
76  
77              for (int i = 1; i <= _columnCount; ++i) {
78                  String availableName = _metaData.getColumnName(i);
79  
80                  if (availableName.equalsIgnoreCase(shortName)) {
81                      _columnIndexCache.put(columnName, i);
82  
83                      return i;
84                  }
85              }
86          }
87  
88          // Let the result set figure it out
89  
90          columnIndex = _rs.findColumn(columnName);
91  
92          _columnIndexCache.put(columnName, columnIndex);
93  
94          return columnIndex;
95      }
96  
97      public boolean first() throws SQLException {
98          return _rs.first();
99      }
100 
101     public Date getDate(int columnIndex) throws SQLException {
102         return _rs.getDate(columnIndex);
103     }
104 
105     public Date getDate(String columnName) throws SQLException {
106         int columnIndex = findColumn(columnName);
107 
108         return _rs.getDate(columnIndex);
109     }
110 
111     public double getDouble(int columnIndex) throws SQLException {
112         return _rs.getDouble(columnIndex);
113     }
114 
115     public double getDouble(String columnName) throws SQLException {
116         int columnIndex = findColumn(columnName);
117 
118         return _rs.getDouble(columnIndex);
119     }
120 
121     public float getFloat(int columnIndex) throws SQLException {
122         return _rs.getFloat(columnIndex);
123     }
124 
125     public float getFloat(String columnName) throws SQLException {
126         int columnIndex = findColumn(columnName);
127 
128         return _rs.getFloat(columnIndex);
129     }
130 
131     public int getInt(int columnIndex) throws SQLException {
132         return _rs.getInt(columnIndex);
133     }
134 
135     public int getInt(String columnName) throws SQLException {
136         int columnIndex = findColumn(columnName);
137 
138         return _rs.getInt(columnIndex);
139     }
140 
141     public long getLong(int columnIndex) throws SQLException {
142         return _rs.getLong(columnIndex);
143     }
144 
145     public long getLong(String columnName) throws SQLException {
146         int columnIndex = findColumn(columnName);
147 
148         return _rs.getLong(columnIndex);
149     }
150 
151     public short getShort(int columnIndex) throws SQLException {
152         return _rs.getShort(columnIndex);
153     }
154 
155     public short getShort(String columnName) throws SQLException {
156         int columnIndex = findColumn(columnName);
157 
158         return _rs.getShort(columnIndex);
159     }
160 
161     public String getString(int columnIndex) throws SQLException {
162         return _rs.getString(columnIndex);
163     }
164 
165     public String getString(String columnName) throws SQLException {
166         int columnIndex = findColumn(columnName);
167 
168         return _rs.getString(columnIndex);
169     }
170 
171     public Timestamp getTimestamp(int columnIndex) throws SQLException {
172         return _rs.getTimestamp(columnIndex);
173     }
174 
175     public Timestamp getTimestamp(String columnName) throws SQLException {
176         int columnIndex = findColumn(columnName);
177 
178         return _rs.getTimestamp(columnIndex);
179     }
180 
181     public boolean last() throws SQLException {
182         return _rs.last();
183     }
184 
185     public boolean next() throws SQLException {
186         return _rs.next();
187     }
188 
189     public boolean previous() throws SQLException {
190         return _rs.previous();
191     }
192 
193     private final ResultSet _rs;
194     private final ResultSetMetaData _metaData;
195     private final int _columnCount;
196     private final Map<String, Integer> _columnIndexCache;
197 
198 }