-
Notifications
You must be signed in to change notification settings - Fork 918
Description
Describe the issue
Under preferQueryMode = simple, a PreparedStatement calls setBytes first and then call executeQuery, resulting in the following exception. Through debug, setBytes will eventually call the toString method of the SimpleParameterList class, but this method doesn't handle the bytea type. Is this an expected behavior?
org.postgresql.util.PSQLException: ERROR: syntax error at or near "and"
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2725)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2412)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:371)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:502)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:419)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:194)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:137)
at JdbcTest.main(JdbcTest.java:22)
Driver Version?
42.7.3 release
Java Version?
openjdk 21.0.2
OS Version?
macOs 13.0
PostgreSQL Version?
PostgreSQL 14.11 (Homebrew) on aarch64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit
To Reproduce
Steps to reproduce the behaviour:
Expected behaviour
Under preferQueryMode = simple , PreparedStatement call setBytes, executeQuery, It returns the correct result instead of throwing an exception.
Logs
nologs
Using the following template code make sure the bug can be replicated in the driver alone.
import java.sql.*;
public class JdbcTest {
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:postgresql://localhost:5432/test?preferQueryMode=simple";
String user = "test";
String pass = "";
try {
conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
stmt.execute("drop table if exists t");
stmt.execute("create table t (c1 int, c2 bytea, c3 text)");
stmt.execute("insert into t select i, i::text::bytea, i::text from generate_series(0, 9) i");
PreparedStatement pstmt = conn.prepareStatement("select c1 from t where c2 = ? and c3 = ?");
pstmt.setBytes(1, "3".getBytes());
pstmt.setString(2, "3");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}