File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
spring-jdbc/src/main/java/org/springframework/jdbc/support Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 3636
3737import org .springframework .jdbc .CannotGetJdbcConnectionException ;
3838import org .springframework .jdbc .datasource .DataSourceUtils ;
39+ import org .springframework .util .NumberUtils ;
3940
4041/**
4142 * Generic utility methods for working with JDBC. Mainly for internal use
@@ -184,6 +185,24 @@ else if (Blob.class == requiredType) {
184185 else if (Clob .class == requiredType ) {
185186 return rs .getClob (index );
186187 }
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+ }
187206
188207 else {
189208 // Some unknown type desired -> rely on getObject.
You can’t perform that action at this time.
0 commit comments