Browse Source

Call com.moparisthebest.jdbc.Finishable.finish(ResultSet) in more cases as appropriate

Travis Burtrum 8 months ago
parent
commit
1fa08bc74b

+ 7
- 0
jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeRowToObjectMapper.java View File

@@ -319,6 +319,7 @@ public class CompileTimeRowToObjectMapper {
319 319
 
320 320
 		if (resultSetConstructor) {
321 321
 			java.append("final ").append(tType).append(" ret = new ").append(tType).append("(rs);\n");
322
+			finishIfNeeded(java);
322 323
 			return;
323 324
 		}
324 325
 
@@ -330,6 +331,7 @@ public class CompileTimeRowToObjectMapper {
330 331
 					java.append(",\n");
331 332
 			}
332 333
 			java.append(");\n");
334
+			finishIfNeeded(java);
333 335
 			return;
334 336
 		}
335 337
 
@@ -382,6 +384,7 @@ public class CompileTimeRowToObjectMapper {
382 384
 					java.append("final ").append(tType).append(" ret = ");
383 385
 					extractColumnValueString(java, 1, typeId, _returnTypeClass.toString());
384 386
 					java.append(";\n");
387
+					finishIfNeeded(java);
385 388
 					return;
386 389
 				} else {
387 390
 					// we still might want a single value (i.e. java.util.Date)
@@ -433,6 +436,10 @@ public class CompileTimeRowToObjectMapper {
433 436
 				java.append(");\n");
434 437
 			}
435 438
 		}
439
+		finishIfNeeded(java);
440
+	}
441
+
442
+	public void finishIfNeeded(final Appendable java) throws IOException {
436 443
 		// if this resultObject is Finishable, call finish()
437 444
 		if (rsm.types.isAssignable(_returnTypeClass, rsm.finishableType))
438 445
 			java.append("ret.finish(rs);\n");

+ 12
- 4
querymapper/src/main/java/com/moparisthebest/jdbc/RowToObjectMapper.java View File

@@ -238,7 +238,7 @@ public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
238 238
 
239 239
 		if (resultSetConstructor)
240 240
 			try {
241
-				return constructor.newInstance(_resultSet);
241
+				return finishIfNeeded(constructor.newInstance(_resultSet), _resultSet);
242 242
 			} catch (Throwable e) {
243 243
 				throw new MapperException(e.getClass().getName() + " when trying to create instance of : "
244 244
 						+ _returnTypeClass.getName() + " sending in a ResultSet object as a parameter", e);
@@ -249,7 +249,7 @@ public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
249 249
 				for(int x = 1; x <= _columnCount; ++x)
250 250
 					_args[x-1] = extractColumnValue(_fieldOrder[x], _fieldTypes[x], _fieldClasses[x]);
251 251
 				//System.out.println("creating " + constructor + " sending in a objects: " + Arrays.toString(_args));
252
-				return constructor.newInstance(_args);
252
+				return finishIfNeeded(constructor.newInstance(_args), _resultSet);
253 253
 			} catch (Throwable e) {
254 254
 				throw new MapperException(e.getClass().getName() + " when trying to create instance of : "
255 255
 						+ _returnTypeClass.getName() + " sending in a objects: " + Arrays.toString(_args), e);
@@ -298,12 +298,12 @@ public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
298 298
 
299 299
 			try {
300 300
 				if (typeId != TypeMappingsFactory.TYPE_UNKNOWN) {
301
-					return (T)extractColumnValue(1, typeId, _returnTypeClass);
301
+					return finishIfNeeded((T)extractColumnValue(1, typeId, _returnTypeClass), _resultSet);
302 302
 				} else {
303 303
 					// we still might want a single value (i.e. java.util.Date)
304 304
 					Object val = extractColumnValue(1, typeId, _returnTypeClass);
305 305
 					if (_returnTypeClass.isAssignableFrom(val.getClass())) {
306
-						return _returnTypeClass.cast(val);
306
+						return finishIfNeeded(_returnTypeClass.cast(val), _resultSet);
307 307
 					}
308 308
 				}
309 309
 			} catch (Exception e) {
@@ -379,6 +379,14 @@ public class RowToObjectMapper<K, T> extends AbstractRowMapper<K, T> {
379 379
 			} catch (SQLException e) {
380 380
 				throw new MapperException(e.getMessage(), e);
381 381
 			}
382
+		finishIfNeeded(resultObject, _resultSet);
383
+		return resultObject;
384
+	}
385
+
386
+	public static <T> T finishIfNeeded(final T resultObject, final ResultSet _resultSet) throws SQLException {
387
+		// if this resultObject is Finishable, call finish()
388
+		if (resultObject instanceof Finishable) // todo: use isAssignable and cache this...
389
+			((Finishable) resultObject).finish(_resultSet);
382 390
 		return resultObject;
383 391
 	}
384 392