From 6a5132ff3019d2b3e2093229514114d8a33ac02e Mon Sep 17 00:00:00 2001 From: Alex Kasko Date: Wed, 25 Nov 2026 11:19:00 +0000 Subject: [PATCH] Disallow multiple statements in prepare (2.0) This is a backport of the PR #1008 to `v2.0-cyanoptera` branch. Historically the driver accepts multiple concatenated SQL statements in a query string. Where all statements are executed one by one and the result of the last statement is returned. This PR changes that, it continues allowing multiple concatenated statements in a plain Java `Statement`, but rejects them in a `PreparedStatement`. Thus the following code (that was working before) is now rejected: ```java try (Connection c = DriverManager.getConnection("jdbc:duckdb:")) { try (PreparedStatement p = c.prepareStatement("CREATE TABLE z(i int); SELECT 1 AS x")) { ... } } ``` Fixes: #1004 --- src/jni/duckdb_java.cpp | 12 +++++------- src/test/java/org/duckdb/TestDuckDBJDBC.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/jni/duckdb_java.cpp b/src/jni/duckdb_java.cpp index b7ca77a05..d7824886d 100644 --- a/src/jni/duckdb_java.cpp +++ b/src/jni/duckdb_java.cpp @@ -218,13 +218,11 @@ jobject _duckdb_jdbc_prepare(JNIEnv *env, jclass, jobject conn_ref_buf, jbyteArr throw InvalidInputException("No statements to execute."); } - // if there are multiple statements, we directly execute the statements besides the last one - // we only return the result of the last statement to the user, unless one of the previous statements fails - for (idx_t i = 0; i + 1 < statements.size(); i++) { - auto res = conn_ref->Query(std::move(statements[i])); - if (res->HasError()) { - res->ThrowError(); - } + // preparing must not have side effects, so multiple statements are rejected instead of + // executing all statements besides the last one + if (statements.size() > 1) { + throw InvalidInputException("Cannot prepare multiple statements at once, statements count: " + + std::to_string(statements.size())); } auto stmt_ref = make_uniq(); diff --git a/src/test/java/org/duckdb/TestDuckDBJDBC.java b/src/test/java/org/duckdb/TestDuckDBJDBC.java index d418d0a38..5b1e99e14 100644 --- a/src/test/java/org/duckdb/TestDuckDBJDBC.java +++ b/src/test/java/org/duckdb/TestDuckDBJDBC.java @@ -498,6 +498,22 @@ public static void test_multiple_statements_exception() throws Exception { } } + public static void test_multiple_statements_prepare_rejected() throws Exception { + try (Connection conn = DriverManager.getConnection(JDBC_URL)) { + assertThrows(() -> { conn.prepareStatement("CREATE TABLE z(i int); SELECT 1 AS x"); }, SQLException.class); + try (Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT count(*) FROM duckdb_tables() WHERE table_name = 'z'")) { + assertTrue(rs.next()); + assertEquals(rs.getInt(1), 0); + } + try (Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("CREATE TABLE z(i int); SELECT 1 AS x")) { + assertTrue(rs.next()); + assertEquals(rs.getInt(1), 1); + } + } + } + public static void test_crash_bug496() throws Exception { Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement();