001/*
002 * Copyright 2002-2018 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;
023import org.springframework.lang.Nullable;
024
025/**
026 * The default implementation of Spring's {@link SqlRowSetMetaData} interface, wrapping a
027 * {@link java.sql.ResultSetMetaData} instance, catching any {@link SQLException SQLExceptions}
028 * and translating them to a corresponding Spring {@link InvalidResultSetAccessException}.
029 *
030 * <p>Used by {@link ResultSetWrappingSqlRowSet}.
031 *
032 * @author Thomas Risberg
033 * @author Juergen Hoeller
034 * @since 1.2
035 * @see ResultSetWrappingSqlRowSet#getMetaData()
036 */
037public class ResultSetWrappingSqlRowSetMetaData implements SqlRowSetMetaData {
038
039        private final ResultSetMetaData resultSetMetaData;
040
041        @Nullable
042        private String[] columnNames;
043
044
045        /**
046         * Create a new ResultSetWrappingSqlRowSetMetaData object
047         * for the given ResultSetMetaData instance.
048         * @param resultSetMetaData a disconnected ResultSetMetaData instance
049         * to wrap (usually a {@code javax.sql.RowSetMetaData} instance)
050         * @see java.sql.ResultSet#getMetaData
051         * @see javax.sql.RowSetMetaData
052         * @see ResultSetWrappingSqlRowSet#getMetaData
053         */
054        public ResultSetWrappingSqlRowSetMetaData(ResultSetMetaData resultSetMetaData) {
055                this.resultSetMetaData = resultSetMetaData;
056        }
057
058
059        @Override
060        public String getCatalogName(int column) throws InvalidResultSetAccessException {
061                try {
062                        return this.resultSetMetaData.getCatalogName(column);
063                }
064                catch (SQLException se) {
065                        throw new InvalidResultSetAccessException(se);
066                }
067        }
068
069        @Override
070        public String getColumnClassName(int column) throws InvalidResultSetAccessException {
071                try {
072                        return this.resultSetMetaData.getColumnClassName(column);
073                }
074                catch (SQLException se) {
075                        throw new InvalidResultSetAccessException(se);
076                }
077        }
078
079        @Override
080        public int getColumnCount() throws InvalidResultSetAccessException {
081                try {
082                        return this.resultSetMetaData.getColumnCount();
083                }
084                catch (SQLException se) {
085                        throw new InvalidResultSetAccessException(se);
086                }
087        }
088
089        @Override
090        public String[] getColumnNames() throws InvalidResultSetAccessException {
091                if (this.columnNames == null) {
092                        this.columnNames = new String[getColumnCount()];
093                        for (int i = 0; i < getColumnCount(); i++) {
094                                this.columnNames[i] = getColumnName(i + 1);
095                        }
096                }
097                return this.columnNames;
098        }
099
100        @Override
101        public int getColumnDisplaySize(int column) throws InvalidResultSetAccessException {
102                try {
103                        return this.resultSetMetaData.getColumnDisplaySize(column);
104                }
105                catch (SQLException se) {
106                        throw new InvalidResultSetAccessException(se);
107                }
108        }
109
110        @Override
111        public String getColumnLabel(int column) throws InvalidResultSetAccessException {
112                try {
113                        return this.resultSetMetaData.getColumnLabel(column);
114                }
115                catch (SQLException se) {
116                        throw new InvalidResultSetAccessException(se);
117                }
118        }
119
120        @Override
121        public String getColumnName(int column) throws InvalidResultSetAccessException {
122                try {
123                        return this.resultSetMetaData.getColumnName(column);
124                }
125                catch (SQLException se) {
126                        throw new InvalidResultSetAccessException(se);
127                }
128        }
129
130        @Override
131        public int getColumnType(int column) throws InvalidResultSetAccessException {
132                try {
133                        return this.resultSetMetaData.getColumnType(column);
134                }
135                catch (SQLException se) {
136                        throw new InvalidResultSetAccessException(se);
137                }
138        }
139
140        @Override
141        public String getColumnTypeName(int column) throws InvalidResultSetAccessException {
142                try {
143                        return this.resultSetMetaData.getColumnTypeName(column);
144                }
145                catch (SQLException se) {
146                        throw new InvalidResultSetAccessException(se);
147                }
148        }
149
150        @Override
151        public int getPrecision(int column) throws InvalidResultSetAccessException {
152                try {
153                        return this.resultSetMetaData.getPrecision(column);
154                }
155                catch (SQLException se) {
156                        throw new InvalidResultSetAccessException(se);
157                }
158        }
159
160        @Override
161        public int getScale(int column) throws InvalidResultSetAccessException {
162                try {
163                        return this.resultSetMetaData.getScale(column);
164                }
165                catch (SQLException se) {
166                        throw new InvalidResultSetAccessException(se);
167                }
168        }
169
170        @Override
171        public String getSchemaName(int column) throws InvalidResultSetAccessException {
172                try {
173                        return this.resultSetMetaData.getSchemaName(column);
174                }
175                catch (SQLException se) {
176                        throw new InvalidResultSetAccessException(se);
177                }
178        }
179
180        @Override
181        public String getTableName(int column) throws InvalidResultSetAccessException {
182                try {
183                        return this.resultSetMetaData.getTableName(column);
184                }
185                catch (SQLException se) {
186                        throw new InvalidResultSetAccessException(se);
187                }
188        }
189
190        @Override
191        public boolean isCaseSensitive(int column) throws InvalidResultSetAccessException {
192                try {
193                        return this.resultSetMetaData.isCaseSensitive(column);
194                }
195                catch (SQLException se) {
196                        throw new InvalidResultSetAccessException(se);
197                }
198        }
199
200        @Override
201        public boolean isCurrency(int column) throws InvalidResultSetAccessException {
202                try {
203                        return this.resultSetMetaData.isCurrency(column);
204                }
205                catch (SQLException se) {
206                        throw new InvalidResultSetAccessException(se);
207                }
208        }
209
210        @Override
211        public boolean isSigned(int column) throws InvalidResultSetAccessException {
212                try {
213                        return this.resultSetMetaData.isSigned(column);
214                }
215                catch (SQLException se) {
216                        throw new InvalidResultSetAccessException(se);
217                }
218        }
219
220}