001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.support.rowset;
018
019import java.sql.ResultSetMetaData;
020import java.sql.SQLException;
021
022import org.springframework.jdbc.InvalidResultSetAccessException;
023
024/**
025 * The default implementation of Spring's {@link SqlRowSetMetaData} interface, wrapping
026 * a {@link java.sql.ResultSetMetaData} instance, catching any {@link SQLException}s
027 * and translating them to a corresponding Spring {@link InvalidResultSetAccessException}.
028 *
029 * <p>Used by {@link ResultSetWrappingSqlRowSet}.
030 *
031 * @author Thomas Risberg
032 * @author Juergen Hoeller
033 * @since 1.2
034 * @see ResultSetWrappingSqlRowSet#getMetaData()
035 */
036public class ResultSetWrappingSqlRowSetMetaData implements SqlRowSetMetaData {
037
038        private final ResultSetMetaData resultSetMetaData;
039
040        private String[] columnNames;
041
042
043        /**
044         * Create a new ResultSetWrappingSqlRowSetMetaData object
045         * for the given ResultSetMetaData instance.
046         * @param resultSetMetaData a disconnected ResultSetMetaData instance
047         * to wrap (usually a {@code javax.sql.RowSetMetaData} instance)
048         * @see java.sql.ResultSet#getMetaData
049         * @see javax.sql.RowSetMetaData
050         * @see ResultSetWrappingSqlRowSet#getMetaData
051         */
052        public ResultSetWrappingSqlRowSetMetaData(ResultSetMetaData resultSetMetaData) {
053                this.resultSetMetaData = resultSetMetaData;
054        }
055
056
057        @Override
058        public String getCatalogName(int column) throws InvalidResultSetAccessException {
059                try {
060                        return this.resultSetMetaData.getCatalogName(column);
061                }
062                catch (SQLException se) {
063                        throw new InvalidResultSetAccessException(se);
064                }
065        }
066
067        @Override
068        public String getColumnClassName(int column) throws InvalidResultSetAccessException {
069                try {
070                        return this.resultSetMetaData.getColumnClassName(column);
071                }
072                catch (SQLException se) {
073                        throw new InvalidResultSetAccessException(se);
074                }
075        }
076
077        @Override
078        public int getColumnCount() throws InvalidResultSetAccessException {
079                try {
080                        return this.resultSetMetaData.getColumnCount();
081                }
082                catch (SQLException se) {
083                        throw new InvalidResultSetAccessException(se);
084                }
085        }
086
087        @Override
088        public String[] getColumnNames() throws InvalidResultSetAccessException {
089                if (this.columnNames == null) {
090                        this.columnNames = new String[getColumnCount()];
091                        for (int i = 0; i < getColumnCount(); i++) {
092                                this.columnNames[i] = getColumnName(i + 1);
093                        }
094                }
095                return this.columnNames;
096        }
097
098        @Override
099        public int getColumnDisplaySize(int column) throws InvalidResultSetAccessException {
100                try {
101                        return this.resultSetMetaData.getColumnDisplaySize(column);
102                }
103                catch (SQLException se) {
104                        throw new InvalidResultSetAccessException(se);
105                }
106        }
107
108        @Override
109        public String getColumnLabel(int column) throws InvalidResultSetAccessException {
110                try {
111                        return this.resultSetMetaData.getColumnLabel(column);
112                }
113                catch (SQLException se) {
114                        throw new InvalidResultSetAccessException(se);
115                }
116        }
117
118        @Override
119        public String getColumnName(int column) throws InvalidResultSetAccessException {
120                try {
121                        return this.resultSetMetaData.getColumnName(column);
122                }
123                catch (SQLException se) {
124                        throw new InvalidResultSetAccessException(se);
125                }
126        }
127
128        @Override
129        public int getColumnType(int column) throws InvalidResultSetAccessException {
130                try {
131                        return this.resultSetMetaData.getColumnType(column);
132                }
133                catch (SQLException se) {
134                        throw new InvalidResultSetAccessException(se);
135                }
136        }
137
138        @Override
139        public String getColumnTypeName(int column) throws InvalidResultSetAccessException {
140                try {
141                        return this.resultSetMetaData.getColumnTypeName(column);
142                }
143                catch (SQLException se) {
144                        throw new InvalidResultSetAccessException(se);
145                }
146        }
147
148        @Override
149        public int getPrecision(int column) throws InvalidResultSetAccessException {
150                try {
151                        return this.resultSetMetaData.getPrecision(column);
152                }
153                catch (SQLException se) {
154                        throw new InvalidResultSetAccessException(se);
155                }
156        }
157
158        @Override
159        public int getScale(int column) throws InvalidResultSetAccessException {
160                try {
161                        return this.resultSetMetaData.getScale(column);
162                }
163                catch (SQLException se) {
164                        throw new InvalidResultSetAccessException(se);
165                }
166        }
167
168        @Override
169        public String getSchemaName(int column) throws InvalidResultSetAccessException {
170                try {
171                        return this.resultSetMetaData.getSchemaName(column);
172                }
173                catch (SQLException se) {
174                        throw new InvalidResultSetAccessException(se);
175                }
176        }
177
178        @Override
179        public String getTableName(int column) throws InvalidResultSetAccessException {
180                try {
181                        return this.resultSetMetaData.getTableName(column);
182                }
183                catch (SQLException se) {
184                        throw new InvalidResultSetAccessException(se);
185                }
186        }
187
188        @Override
189        public boolean isCaseSensitive(int column) throws InvalidResultSetAccessException {
190                try {
191                        return this.resultSetMetaData.isCaseSensitive(column);
192                }
193                catch (SQLException se) {
194                        throw new InvalidResultSetAccessException(se);
195                }
196        }
197
198        @Override
199        public boolean isCurrency(int column) throws InvalidResultSetAccessException {
200                try {
201                        return this.resultSetMetaData.isCurrency(column);
202                }
203                catch (SQLException se) {
204                        throw new InvalidResultSetAccessException(se);
205                }
206        }
207
208        @Override
209        public boolean isSigned(int column) throws InvalidResultSetAccessException {
210                try {
211                        return this.resultSetMetaData.isSigned(column);
212                }
213                catch (SQLException se) {
214                        throw new InvalidResultSetAccessException(se);
215                }
216        }
217
218}