File tree 1 file changed +19
-0
lines changed
spring-jdbc/src/main/java/org/springframework/jdbc/support
1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 36
36
37
37
import org .springframework .jdbc .CannotGetJdbcConnectionException ;
38
38
import org .springframework .jdbc .datasource .DataSourceUtils ;
39
+ import org .springframework .util .NumberUtils ;
39
40
40
41
/**
41
42
* Generic utility methods for working with JDBC. Mainly for internal use
@@ -184,6 +185,24 @@ else if (Blob.class == requiredType) {
184
185
else if (Clob .class == requiredType ) {
185
186
return rs .getClob (index );
186
187
}
188
+ else if (requiredType .isEnum ()) {
189
+ // Enums can either be represented through a String or an enum index value:
190
+ // leave enum type conversion up to the caller (e.g. a ConversionService)
191
+ // but make sure that we return nothing other than a String or an Integer.
192
+ Object obj = rs .getObject (index );
193
+ if (obj instanceof String ) {
194
+ return obj ;
195
+ }
196
+ else if (obj instanceof Number ) {
197
+ // Defensively convert any Number to an Integer (as needed by our
198
+ // ConversionService's IntegerToEnumConverterFactory) for use as index
199
+ return NumberUtils .convertNumberToTargetClass ((Number ) obj , Integer .class );
200
+ }
201
+ else {
202
+ // e.g. on Postgres: getObject returns a PGObject but we need a String
203
+ return rs .getString (index );
204
+ }
205
+ }
187
206
188
207
else {
189
208
// Some unknown type desired -> rely on getObject.
You can’t perform that action at this time.
0 commit comments