From 797f5dbba64f01fc97e56ffd50292c4fe7fd762a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 5 Apr 2016 11:12:36 +0200 Subject: [PATCH] StatementCreatorUtils handles Types.BOOLEAN through PreparedStatement.setBoolean Issue: SPR-14116 --- .../jdbc/core/StatementCreatorUtils.java | 10 +++++- .../jdbc/object/SqlUpdateTests.java | 31 +++++++++++-------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java index 4aed272b13..bab52f6af3 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -364,6 +364,14 @@ public abstract class StatementCreatorUtils { ps.setObject(paramIndex, inValue, sqlType); } } + else if (sqlType == Types.BOOLEAN) { + if (inValue instanceof Boolean) { + ps.setBoolean(paramIndex, (Boolean) inValue); + } + else { + ps.setObject(paramIndex, inValue, Types.BOOLEAN); + } + } else if (sqlType == Types.DATE) { if (inValue instanceof java.util.Date) { if (inValue instanceof java.sql.Date) { diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/object/SqlUpdateTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/object/SqlUpdateTests.java index 7791abe412..dad295b2fa 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/object/SqlUpdateTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/object/SqlUpdateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,16 +49,22 @@ public class SqlUpdateTests { private static final String UPDATE = "update seat_status set booking_id = null"; + private static final String UPDATE_INT = "update seat_status set booking_id = null where performance_id = ?"; + private static final String UPDATE_INT_INT = "update seat_status set booking_id = null where performance_id = ? and price_band_id = ?"; + private static final String UPDATE_NAMED_PARAMETERS = "update seat_status set booking_id = null where performance_id = :perfId and price_band_id = :priceId"; + private static final String UPDATE_STRING = "update seat_status set booking_id = null where name = ?"; + private static final String UPDATE_OBJECTS = "update seat_status set booking_id = null where performance_id = ? and price_band_id = ? and name = ? and confirmed = ?"; + private static final String INSERT_GENERATE_KEYS = "insert into show (name) values(?)"; @@ -66,11 +72,16 @@ public class SqlUpdateTests { public ExpectedException thrown = ExpectedException.none(); private DataSource dataSource; + private Connection connection; + private PreparedStatement preparedStatement; + private ResultSet resultSet; + private ResultSetMetaData resultSetMetaData; + @Before public void setUp() throws Exception { dataSource = mock(DataSource.class); @@ -87,6 +98,7 @@ public class SqlUpdateTests { verify(connection).close(); } + @Test public void testUpdate() throws SQLException { given(preparedStatement.executeUpdate()).willReturn(1); @@ -192,7 +204,7 @@ public class SqlUpdateTests { verify(preparedStatement).setObject(1, 1, Types.NUMERIC); verify(preparedStatement).setObject(2, 1, Types.NUMERIC, 2); verify(preparedStatement).setString(3, "rod"); - verify(preparedStatement).setObject(4, Boolean.TRUE, Types.BOOLEAN); + verify(preparedStatement).setBoolean(4, Boolean.TRUE); } @Test @@ -231,7 +243,7 @@ public class SqlUpdateTests { verify(preparedStatement).setObject(1, 1, Types.NUMERIC); verify(preparedStatement).setObject(2, 1, Types.NUMERIC); verify(preparedStatement).setString(3, "rod"); - verify(preparedStatement).setObject(4, Boolean.TRUE, Types.BOOLEAN); + verify(preparedStatement).setBoolean(4, Boolean.TRUE); } @Test @@ -360,10 +372,7 @@ public class SqlUpdateTests { } public int run(int performanceId, int type, String name, boolean confirmed) { - Object[] params = - new Object[] {performanceId, type, name, - new Boolean(confirmed)}; - return update(params); + return update(performanceId, type, name, confirmed); } } @@ -379,8 +388,7 @@ public class SqlUpdateTests { } public int run(String name, KeyHolder generatedKeyHolder) { - Object[] params = new Object[] {name}; - return update(params, generatedKeyHolder); + return update(new Object[] {name}, generatedKeyHolder); } } @@ -394,10 +402,7 @@ public class SqlUpdateTests { } public int run(int performanceId, int type, String name, boolean confirmed) { - Object[] params = - new Object[] { - performanceId, type, name, new Boolean(confirmed)}; - return update(params); + return update(performanceId, type, name, confirmed); } }