View Javadoc
1   /*
2    * Copyright 2004-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.sqlmap.engine.type;
17  
18  import com.ibatis.sqlmap.client.extensions.ResultGetter;
19  
20  import java.math.BigDecimal;
21  import java.net.URL;
22  import java.sql.*;
23  import java.util.Calendar;
24  import java.util.Map;
25  
26  /**
27   * A ResultGetter implementation.
28   */
29  public class ResultGetterImpl implements ResultGetter {
30  
31    /** The rs. */
32    private ResultSet rs;
33  
34    /** The name. */
35    private String name;
36  
37    /** The index. */
38    private int index;
39  
40    /**
41     * Creates an instance for a PreparedStatement and column index.
42     *
43     * @param resultSet
44     *          - the result set
45     * @param columnIndex
46     *          - the column index
47     */
48    public ResultGetterImpl(ResultSet resultSet, int columnIndex) {
49      this.rs = resultSet;
50      this.index = columnIndex;
51    }
52  
53    /**
54     * Creates an instance for a PreparedStatement and column name.
55     *
56     * @param resultSet
57     *          - the result set
58     * @param columnName
59     *          - the column index
60     */
61    public ResultGetterImpl(ResultSet resultSet, String columnName) {
62      this.rs = resultSet;
63      this.name = columnName;
64    }
65  
66    public Array getArray() throws SQLException {
67      if (name != null) {
68        return rs.getArray(name);
69      } else {
70        return rs.getArray(index);
71      }
72    }
73  
74    public BigDecimal getBigDecimal() throws SQLException {
75      if (name != null) {
76        return rs.getBigDecimal(name);
77      } else {
78        return rs.getBigDecimal(index);
79      }
80    }
81  
82    public Blob getBlob() throws SQLException {
83      if (name != null) {
84        return rs.getBlob(name);
85      } else {
86        return rs.getBlob(index);
87      }
88    }
89  
90    public boolean getBoolean() throws SQLException {
91      if (name != null) {
92        return rs.getBoolean(name);
93      } else {
94        return rs.getBoolean(index);
95      }
96    }
97  
98    public byte getByte() throws SQLException {
99      if (name != null) {
100       return rs.getByte(name);
101     } else {
102       return rs.getByte(index);
103     }
104   }
105 
106   public byte[] getBytes() throws SQLException {
107     if (name != null) {
108       return rs.getBytes(name);
109     } else {
110       return rs.getBytes(index);
111     }
112   }
113 
114   public Clob getClob() throws SQLException {
115     if (name != null) {
116       return rs.getClob(name);
117     } else {
118       return rs.getClob(index);
119     }
120   }
121 
122   public Date getDate() throws SQLException {
123     if (name != null) {
124       return rs.getDate(name);
125     } else {
126       return rs.getDate(index);
127     }
128   }
129 
130   public Date getDate(Calendar cal) throws SQLException {
131     if (name != null) {
132       return rs.getDate(name, cal);
133     } else {
134       return rs.getDate(index, cal);
135     }
136   }
137 
138   public double getDouble() throws SQLException {
139     if (name != null) {
140       return rs.getDouble(name);
141     } else {
142       return rs.getDouble(index);
143     }
144   }
145 
146   public float getFloat() throws SQLException {
147     if (name != null) {
148       return rs.getFloat(name);
149     } else {
150       return rs.getFloat(index);
151     }
152   }
153 
154   public int getInt() throws SQLException {
155     if (name != null) {
156       return rs.getInt(name);
157     } else {
158       return rs.getInt(index);
159     }
160   }
161 
162   public long getLong() throws SQLException {
163     if (name != null) {
164       return rs.getLong(name);
165     } else {
166       return rs.getLong(index);
167     }
168   }
169 
170   public Object getObject() throws SQLException {
171     if (name != null) {
172       return rs.getObject(name);
173     } else {
174       return rs.getObject(index);
175     }
176   }
177 
178   public Object getObject(Map map) throws SQLException {
179     if (name != null) {
180       return rs.getObject(name, map);
181     } else {
182       return rs.getObject(index, map);
183     }
184   }
185 
186   public Ref getRef() throws SQLException {
187     if (name != null) {
188       return rs.getRef(name);
189     } else {
190       return rs.getRef(index);
191     }
192   }
193 
194   public short getShort() throws SQLException {
195     if (name != null) {
196       return rs.getShort(name);
197     } else {
198       return rs.getShort(index);
199     }
200   }
201 
202   public String getString() throws SQLException {
203     if (name != null) {
204       return rs.getString(name);
205     } else {
206       return rs.getString(index);
207     }
208   }
209 
210   public Time getTime() throws SQLException {
211     if (name != null) {
212       return rs.getTime(name);
213     } else {
214       return rs.getTime(index);
215     }
216   }
217 
218   public Time getTime(Calendar cal) throws SQLException {
219     if (name != null) {
220       return rs.getTime(name);
221     } else {
222       return rs.getTime(index);
223     }
224   }
225 
226   public Timestamp getTimestamp() throws SQLException {
227     if (name != null) {
228       return rs.getTimestamp(name);
229     } else {
230       return rs.getTimestamp(index);
231     }
232   }
233 
234   public Timestamp getTimestamp(Calendar cal) throws SQLException {
235     if (name != null) {
236       return rs.getTimestamp(name, cal);
237     } else {
238       return rs.getTimestamp(index, cal);
239     }
240   }
241 
242   public URL getURL() throws SQLException {
243     if (name != null) {
244       return rs.getURL(name);
245     } else {
246       return rs.getURL(index);
247     }
248   }
249 
250   public boolean wasNull() throws SQLException {
251     return rs.wasNull();
252   }
253 
254   public ResultSet getResultSet() {
255     return rs;
256   }
257 
258   public int getColumnIndex() {
259     return index;
260   }
261 
262   public String getColumnName() {
263     return name;
264   }
265 }