001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.dao.jdbc;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    
019    import java.sql.Date;
020    import java.sql.ResultSet;
021    import java.sql.ResultSetMetaData;
022    import java.sql.SQLException;
023    import java.sql.Timestamp;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    /**
029     * @author Minhchau Dang
030     * @author Brian Wing Shun Chan
031     */
032    public class SmartResultSet {
033    
034            public SmartResultSet(ResultSet rs) throws SQLException {
035                    _rs = rs;
036                    _metaData = _rs.getMetaData();
037                    _columnCount = _metaData.getColumnCount();
038                    _columnIndexCache = new HashMap<String, Integer>();
039            }
040    
041            public int findColumn(String columnName) throws SQLException {
042                    Integer columnIndex = _columnIndexCache.get(columnName);
043    
044                    if (columnIndex != null) {
045                            return columnIndex;
046                    }
047    
048                    // Check for the full column name
049    
050                    for (int i = 1; i <= _columnCount; ++i) {
051                            String availableName = _metaData.getColumnName(i);
052    
053                            if (availableName.equalsIgnoreCase(columnName)) {
054                                    _columnIndexCache.put(columnName, i);
055    
056                                    return i;
057                            }
058                    }
059    
060                    // Check for a shortened column name
061    
062                    int pos = columnName.indexOf(CharPool.PERIOD);
063    
064                    if (pos != -1) {
065                            String shortName = columnName.substring(pos + 1);
066    
067                            for (int i = 1; i <= _columnCount; ++i) {
068                                    String availableName = _metaData.getColumnName(i);
069    
070                                    if (availableName.equalsIgnoreCase(shortName)) {
071                                            _columnIndexCache.put(columnName, i);
072    
073                                            return i;
074                                    }
075                            }
076                    }
077    
078                    // Let the result set figure it out
079    
080                    columnIndex = _rs.findColumn(columnName);
081    
082                    _columnIndexCache.put(columnName, columnIndex);
083    
084                    return columnIndex;
085            }
086    
087            public boolean first() throws SQLException {
088                    return _rs.first();
089            }
090    
091            public Date getDate(int columnIndex) throws SQLException {
092                    return _rs.getDate(columnIndex);
093            }
094    
095            public Date getDate(String columnName) throws SQLException {
096                    int columnIndex = findColumn(columnName);
097    
098                    return _rs.getDate(columnIndex);
099            }
100    
101            public double getDouble(int columnIndex) throws SQLException {
102                    return _rs.getDouble(columnIndex);
103            }
104    
105            public double getDouble(String columnName) throws SQLException {
106                    int columnIndex = findColumn(columnName);
107    
108                    return _rs.getDouble(columnIndex);
109            }
110    
111            public float getFloat(int columnIndex) throws SQLException {
112                    return _rs.getFloat(columnIndex);
113            }
114    
115            public float getFloat(String columnName) throws SQLException {
116                    int columnIndex = findColumn(columnName);
117    
118                    return _rs.getFloat(columnIndex);
119            }
120    
121            public int getInt(int columnIndex) throws SQLException {
122                    return _rs.getInt(columnIndex);
123            }
124    
125            public int getInt(String columnName) throws SQLException {
126                    int columnIndex = findColumn(columnName);
127    
128                    return _rs.getInt(columnIndex);
129            }
130    
131            public long getLong(int columnIndex) throws SQLException {
132                    return _rs.getLong(columnIndex);
133            }
134    
135            public long getLong(String columnName) throws SQLException {
136                    int columnIndex = findColumn(columnName);
137    
138                    return _rs.getLong(columnIndex);
139            }
140    
141            public short getShort(int columnIndex) throws SQLException {
142                    return _rs.getShort(columnIndex);
143            }
144    
145            public short getShort(String columnName) throws SQLException {
146                    int columnIndex = findColumn(columnName);
147    
148                    return _rs.getShort(columnIndex);
149            }
150    
151            public String getString(int columnIndex) throws SQLException {
152                    return _rs.getString(columnIndex);
153            }
154    
155            public String getString(String columnName) throws SQLException {
156                    int columnIndex = findColumn(columnName);
157    
158                    return _rs.getString(columnIndex);
159            }
160    
161            public Timestamp getTimestamp(int columnIndex) throws SQLException {
162                    return _rs.getTimestamp(columnIndex);
163            }
164    
165            public Timestamp getTimestamp(String columnName) throws SQLException {
166                    int columnIndex = findColumn(columnName);
167    
168                    return _rs.getTimestamp(columnIndex);
169            }
170    
171            public boolean last() throws SQLException {
172                    return _rs.last();
173            }
174    
175            public boolean next() throws SQLException {
176                    return _rs.next();
177            }
178    
179            public boolean previous() throws SQLException {
180                    return _rs.previous();
181            }
182    
183            private final int _columnCount;
184            private final Map<String, Integer> _columnIndexCache;
185            private final ResultSetMetaData _metaData;
186            private final ResultSet _rs;
187    
188    }