-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support binary streams with JdbcClient
#32161
Comments
JdbcClient
to handle Postgres bytea
column type
JdbcClient
to handle Postgres bytea
column typeJdbcClient
Have you tried using Spring JDBC's support for BLOBs (Binary Large OBjects) via the |
I only tried using InputStream inputStream = new FileInputStream(file);
SqlLobValue lobValue = new SqlLobValue(inputStream, (int) file.length());
this.jdbcClient.sql(sql)
.param("data", lobValue)
.update(); It throws exception with text "SqlLobValue only supports SQL types BLOB and CLOB". |
It should be supported at package com.example.demo;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.jdbc.Sql;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
@JdbcTest
@ImportAutoConfiguration(classes = ServiceConnectionAutoConfiguration.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Sql(statements = "create table test(value bytea)")
@ImportTestcontainers
public class PostgreSQLByteaTests {
@Container
@ServiceConnection
static PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres");
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void test() {
InputStream binaryStream = new ByteArrayInputStream("test".getBytes());
assertThat(jdbcTemplate.update("insert into test values(?)", binaryStream)).isEqualTo(1);
}
} Exception is thrown as:
|
Thanks @quaff I just changed my code to For time being, I have changed my code to use |
Great @quaff I tried with your new patch and build locally. It works for my case with This is my first time to build this repository from source and test locally, seems like IDE local cache causes my initial false errors (hence I deleted my previous comment with error report). Restarted IDE with a clean start, your patch works for me. |
You can try the trick that paste changed files to your project src directory to override classes in jars. |
@dopsun as mentioned, for a BLOB type, we got The plain handling of an InputStream argument in We intend to address the file use case with support for special value types, e.g. For that reason, I'm going to close the PR. Thanks for your efforts there in any case, @quaff! |
@jhoeller Thanks. I tried again with your suggestion on overwriting static class InputStreamSqlLobValue extends SqlLobValue {
private final InputStream stream;
private final int length;
public InputStreamSqlLobValue(InputStream stream, int length) {
super(stream, length);
this.stream = stream;
this.length = length;
}
@Override
public void setTypeValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName) throws SQLException {
ps.setBinaryStream(paramIndex, this.stream, this.length);
}
} |
For the above, you could simply declare As for standard value types, it looks like we will introduce a pair of As a side note, a parameter of type |
In my case, the reason to choose Thanks for your suggestion on improvements. Here the latest version which is working: static class InputStreamValue implements SqlTypeValue {
private final InputStream stream;
private final int length;
public InputStreamValue(InputStream stream, int length) {
this.stream = stream;
this.length = length;
}
@Override
public void setTypeValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName) throws SQLException {
ps.setBinaryStream(paramIndex, this.stream, this.length);
}
} |
In Postgres database, there is a column type
bytea
. According to Storing Binary Data, here is the code to write files tobytea
column withPreparedStatement
:For the current version of
JdbcClient
(6.1.1),StatementSpec.param()
does not supportInputStream
. I have tried to open up underlying code and cannot find places where doing stream related code.Missing this feature, will prevent
JdbcClient
to be used whenbytea
column exists.The text was updated successfully, but these errors were encountered: