001
014
015 package com.liferay.portal.kernel.dao.jdbc;
016
017 import com.liferay.portal.kernel.util.CharPool;
018 import com.liferay.portal.kernel.util.StringUtil;
019
020 import java.sql.Date;
021 import java.sql.ResultSet;
022 import java.sql.ResultSetMetaData;
023 import java.sql.SQLException;
024 import java.sql.Timestamp;
025
026 import java.util.HashMap;
027 import java.util.Map;
028
029
033 public class SmartResultSet {
034
035 public SmartResultSet(ResultSet rs) throws SQLException {
036 _rs = rs;
037 _metaData = _rs.getMetaData();
038 _columnCount = _metaData.getColumnCount();
039 _columnIndexCache = new HashMap<String, Integer>();
040 }
041
042 public int findColumn(String columnName) throws SQLException {
043 Integer columnIndex = _columnIndexCache.get(columnName);
044
045 if (columnIndex != null) {
046 return columnIndex;
047 }
048
049
050
051 for (int i = 1; i <= _columnCount; ++i) {
052 String availableName = _metaData.getColumnName(i);
053
054 if (StringUtil.equalsIgnoreCase(availableName, columnName)) {
055 _columnIndexCache.put(columnName, i);
056
057 return i;
058 }
059 }
060
061
062
063 int pos = columnName.indexOf(CharPool.PERIOD);
064
065 if (pos != -1) {
066 String shortName = columnName.substring(pos + 1);
067
068 for (int i = 1; i <= _columnCount; ++i) {
069 String availableName = _metaData.getColumnName(i);
070
071 if (StringUtil.equalsIgnoreCase(availableName, shortName)) {
072 _columnIndexCache.put(columnName, i);
073
074 return i;
075 }
076 }
077 }
078
079
080
081 columnIndex = _rs.findColumn(columnName);
082
083 _columnIndexCache.put(columnName, columnIndex);
084
085 return columnIndex;
086 }
087
088 public boolean first() throws SQLException {
089 return _rs.first();
090 }
091
092 public Date getDate(int columnIndex) throws SQLException {
093 return _rs.getDate(columnIndex);
094 }
095
096 public Date getDate(String columnName) throws SQLException {
097 int columnIndex = findColumn(columnName);
098
099 return _rs.getDate(columnIndex);
100 }
101
102 public double getDouble(int columnIndex) throws SQLException {
103 return _rs.getDouble(columnIndex);
104 }
105
106 public double getDouble(String columnName) throws SQLException {
107 int columnIndex = findColumn(columnName);
108
109 return _rs.getDouble(columnIndex);
110 }
111
112 public float getFloat(int columnIndex) throws SQLException {
113 return _rs.getFloat(columnIndex);
114 }
115
116 public float getFloat(String columnName) throws SQLException {
117 int columnIndex = findColumn(columnName);
118
119 return _rs.getFloat(columnIndex);
120 }
121
122 public int getInt(int columnIndex) throws SQLException {
123 return _rs.getInt(columnIndex);
124 }
125
126 public int getInt(String columnName) throws SQLException {
127 int columnIndex = findColumn(columnName);
128
129 return _rs.getInt(columnIndex);
130 }
131
132 public long getLong(int columnIndex) throws SQLException {
133 return _rs.getLong(columnIndex);
134 }
135
136 public long getLong(String columnName) throws SQLException {
137 int columnIndex = findColumn(columnName);
138
139 return _rs.getLong(columnIndex);
140 }
141
142 public short getShort(int columnIndex) throws SQLException {
143 return _rs.getShort(columnIndex);
144 }
145
146 public short getShort(String columnName) throws SQLException {
147 int columnIndex = findColumn(columnName);
148
149 return _rs.getShort(columnIndex);
150 }
151
152 public String getString(int columnIndex) throws SQLException {
153 return _rs.getString(columnIndex);
154 }
155
156 public String getString(String columnName) throws SQLException {
157 int columnIndex = findColumn(columnName);
158
159 return _rs.getString(columnIndex);
160 }
161
162 public Timestamp getTimestamp(int columnIndex) throws SQLException {
163 return _rs.getTimestamp(columnIndex);
164 }
165
166 public Timestamp getTimestamp(String columnName) throws SQLException {
167 int columnIndex = findColumn(columnName);
168
169 return _rs.getTimestamp(columnIndex);
170 }
171
172 public boolean last() throws SQLException {
173 return _rs.last();
174 }
175
176 public boolean next() throws SQLException {
177 return _rs.next();
178 }
179
180 public boolean previous() throws SQLException {
181 return _rs.previous();
182 }
183
184 private final int _columnCount;
185 private final Map<String, Integer> _columnIndexCache;
186 private final ResultSetMetaData _metaData;
187 private final ResultSet _rs;
188
189 }