1
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
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
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
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
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 }