Skip to content

Commit

Permalink
Support InputStream/Reader as statement parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Jan 31, 2024
1 parent af5acb6 commit 8261c4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -55,6 +57,7 @@
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @author Yanming Zhou
* @since 1.1
* @see PreparedStatementSetter
* @see PreparedStatementCreator
Expand Down Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +48,7 @@

/**
* @author Juergen Hoeller
* @author Yanming Zhou
* @since 31.08.2004
*/
class StatementCreatorUtilsTests {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8261c4f

Please sign in to comment.