From 8261c4fb1e86b8bd122b09a4ff9f8ee223a0d87f Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 31 Jan 2024 10:31:31 +0800 Subject: [PATCH] Support InputStream/Reader as statement parameter Fix GH-32161 --- .../jdbc/core/StatementCreatorUtils.java | 13 +++++++++++-- .../jdbc/core/StatementCreatorUtilsTests.java | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 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 d3b0a98dc999..c6852fd00517 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-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -16,6 +16,8 @@ package org.springframework.jdbc.core; +import java.io.InputStream; +import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.math.BigDecimal; @@ -55,6 +57,7 @@ * * @author Thomas Risberg * @author Juergen Hoeller + * @author Yanming Zhou * @since 1.1 * @see PreparedStatementSetter * @see PreparedStatementCreator @@ -436,7 +439,13 @@ else if (inValue instanceof Calendar cal) { } else if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER && "Oracle".equals(ps.getConnection().getMetaData().getDatabaseProductName()))) { - if (isStringValue(inValue.getClass())) { + if (inValue instanceof InputStream binaryStream) { + ps.setBinaryStream(paramIndex, binaryStream); + } + else if (inValue instanceof Reader characterStream) { + ps.setCharacterStream(paramIndex, characterStream); + } + else if (isStringValue(inValue.getClass())) { ps.setString(paramIndex, inValue.toString()); } else if (isDateValue(inValue.getClass())) { diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java index 8f906e85646d..e965052e1141 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java @@ -16,6 +16,8 @@ package org.springframework.jdbc.core; +import java.io.InputStream; +import java.io.Reader; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ParameterMetaData; @@ -46,6 +48,7 @@ /** * @author Juergen Hoeller + * @author Yanming Zhou * @since 31.08.2004 */ class StatementCreatorUtilsTests { @@ -286,4 +289,17 @@ public void testSetParameterValueWithNullAndVendorSpecificType() throws SQLExcep StatementCreatorUtils.shouldIgnoreGetParameterType = false; } + @Test + void testSetParameterValueWithInputStream() throws SQLException { + InputStream binaryStream = mock(); + StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, binaryStream); + verify(preparedStatement).setBinaryStream(1, binaryStream); + } + + @Test + void testSetParameterValueWithReader() throws SQLException { + Reader characterStream = mock(); + StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, characterStream); + verify(preparedStatement).setCharacterStream(1, characterStream); + } }