-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathColumnNameResultSetReader.java
More file actions
267 lines (240 loc) · 8.16 KB
/
ColumnNameResultSetReader.java
File metadata and controls
267 lines (240 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.jdbc.mapper;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.exceptions.ConversionErrorException;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.data.exceptions.DataAccessException;
import io.micronaut.data.model.DataType;
import io.micronaut.data.runtime.convert.DataConversionService;
import io.micronaut.data.runtime.mapper.ResultReader;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Date;
/**
* A {@link ResultReader} for JDBC that uses the column name.
*
* @author graemerocher
* @since 1.0.0
*/
public final class ColumnNameResultSetReader implements ResultReader<ResultSet, String> {
private final ConversionService conversionService;
public ColumnNameResultSetReader() {
this(null);
}
/**
* Constructs a new instance.
*
* @param conversionService The data conversion service
* @since 3.1
*/
public ColumnNameResultSetReader(DataConversionService conversionService) {
// Backwards compatibility should be removed in the next version
this.conversionService = conversionService == null ? ConversionService.SHARED : conversionService;
}
@Override
public ConversionService getConversionService() {
return conversionService;
}
@Nullable
@Override
public Object readDynamic(@NonNull ResultSet resultSet, @NonNull String index, @NonNull DataType dataType) {
Object val = ResultReader.super.readDynamic(resultSet, index, dataType);
try {
return resultSet.wasNull() ? null : val;
} catch (SQLException e) {
throw exceptionForColumn(index, e);
}
}
@Override
public boolean next(ResultSet resultSet) {
try {
return resultSet.next();
} catch (SQLException e) {
throw new DataAccessException("Error calling next on SQL result set: " + e.getMessage(), e);
}
}
@Override
public <T> T convertRequired(@NonNull Object value, Class<T> type) {
//noinspection ConstantConditions
if (value == null) {
throw new DataAccessException("Cannot convert type null value to target type: " + type + ". Consider defining a TypeConverter bean to handle this case.");
}
Class wrapperType = ReflectionUtils.getWrapperType(type);
if (wrapperType.isInstance(value)) {
return (T) value;
}
return conversionService.convert(
value,
type
).orElseThrow(() ->
new DataAccessException("Cannot convert type [" + value.getClass() + "] with value [" + value + "] to target type: " + type + ". Consider defining a TypeConverter bean to handle this case.")
);
}
@Override
public Date readTimestamp(ResultSet resultSet, String index) {
try {
return resultSet.getTimestamp(index);
} catch (SQLException e) {
throw exceptionForColumn(index, e);
}
}
@Override
public Time readTime(ResultSet resultSet, String index) {
try {
return resultSet.getTime(index);
} catch (SQLException e) {
throw exceptionForColumn(index, e);
}
}
@Override
public long readLong(ResultSet resultSet, String name) {
try {
return resultSet.getLong(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public char readChar(ResultSet resultSet, String name) {
try {
String strValue = resultSet.getString(name);
if (StringUtils.isNotEmpty(strValue)) {
return strValue.charAt(0);
}
return 0;
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public Date readDate(ResultSet resultSet, String name) {
try {
return resultSet.getDate(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Nullable
@Override
public String readString(ResultSet resultSet, String name) {
try {
return resultSet.getString(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public int readInt(ResultSet resultSet, String name) {
try {
return resultSet.getInt(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public boolean readBoolean(ResultSet resultSet, String name) {
try {
return resultSet.getBoolean(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public float readFloat(ResultSet resultSet, String name) {
try {
return resultSet.getFloat(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public byte readByte(ResultSet resultSet, String name) {
try {
return resultSet.getByte(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public short readShort(ResultSet resultSet, String name) {
try {
return resultSet.getShort(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public double readDouble(ResultSet resultSet, String name) {
try {
return resultSet.getDouble(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public BigDecimal readBigDecimal(ResultSet resultSet, String name) {
try {
return resultSet.getBigDecimal(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public byte[] readBytes(ResultSet resultSet, String name) {
try {
return resultSet.getBytes(name);
} catch (SQLException e) {
throw exceptionForColumn(name, e);
}
}
@Override
public <T> T getRequiredValue(ResultSet resultSet, String name, Class<T> type) throws DataAccessException {
try {
Object o;
if (Blob.class.isAssignableFrom(type)) {
o = resultSet.getBlob(name);
} else if (Clob.class.isAssignableFrom(type)) {
o = resultSet.getClob(name);
} else if (type == float[].class || type == double[].class || type == byte[].class || type == int[].class) {
o = resultSet.getObject(name, type);
} else {
o = resultSet.getObject(name);
}
if (o == null) {
return null;
}
if (type.isInstance(o)) {
//noinspection unchecked
return (T) o;
} else {
return convertRequired(o, type);
}
} catch (SQLException | ConversionErrorException e) {
throw exceptionForColumn(name, e);
}
}
private DataAccessException exceptionForColumn(String name, Exception e) {
return new DataAccessException("Error reading object for name [" + name + "] from result set: " + e.getMessage(), e);
}
}