From 903ff6f1847e6806cea86c30242e6f617206a210 Mon Sep 17 00:00:00 2001 From: diego Date: Mon, 15 Jul 2024 11:57:57 +0200 Subject: [PATCH] [CONJ-1192] add a technical option for 3.3 compatibility --- .../mariadb/jdbc/ClientPreparedStatement.java | 9 +++++++++ .../mariadb/jdbc/ServerPreparedStatement.java | 9 +++++++++ src/main/java/org/mariadb/jdbc/Statement.java | 7 +++++++ .../jdbc/client/result/CompleteResult.java | 2 +- .../mariadb/jdbc/integration/ProcedureTest.java | 2 ++ .../mariadb/jdbc/integration/StatementTest.java | 17 +++++++++++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java b/src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java index 9e060d009..d3708667b 100644 --- a/src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java +++ b/src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java @@ -7,6 +7,8 @@ import java.sql.*; import java.util.*; +import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.result.CompleteResult; import org.mariadb.jdbc.client.result.Result; import org.mariadb.jdbc.client.util.ClosableLock; import org.mariadb.jdbc.export.ExceptionFactory; @@ -257,6 +259,13 @@ public ResultSet executeQuery() throws SQLException { if (currResult instanceof Result) { return (Result) currResult; } + if (Boolean.parseBoolean( + con.getContext().getConf().nonMappedOptions().getProperty("permitNoResults", "false"))) { + // for compatibility with pre 3.4.0 version + return new CompleteResult( + new ColumnDecoder[0], new byte[0][], con.getContext(), resultSetType); + } + throw new SQLException( "PrepareStatement.executeQuery() command does NOT return a result-set as expected. Either" + " use PrepareStatement.execute(), PrepareStatement.executeUpdate(), or correct" diff --git a/src/main/java/org/mariadb/jdbc/ServerPreparedStatement.java b/src/main/java/org/mariadb/jdbc/ServerPreparedStatement.java index a1548e97c..cb547de91 100644 --- a/src/main/java/org/mariadb/jdbc/ServerPreparedStatement.java +++ b/src/main/java/org/mariadb/jdbc/ServerPreparedStatement.java @@ -8,7 +8,9 @@ import java.sql.*; import java.util.*; import java.util.regex.Pattern; +import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.Completion; +import org.mariadb.jdbc.client.result.CompleteResult; import org.mariadb.jdbc.client.result.Result; import org.mariadb.jdbc.client.util.ClosableLock; import org.mariadb.jdbc.client.util.Parameters; @@ -382,6 +384,13 @@ public ResultSet executeQuery() throws SQLException { if ((currResult instanceof Result)) { return (Result) currResult; } + if (Boolean.parseBoolean( + con.getContext().getConf().nonMappedOptions().getProperty("permitNoResults", "false"))) { + // for compatibility with pre 3.4.0 version + return new CompleteResult( + new ColumnDecoder[0], new byte[0][], con.getContext(), resultSetType); + } + throw new SQLException( "PrepareStatement.executeQuery() command does NOT return a result-set as expected. Either" + " use PrepareStatement.execute(), PrepareStatement.executeUpdate(), or correct" diff --git a/src/main/java/org/mariadb/jdbc/Statement.java b/src/main/java/org/mariadb/jdbc/Statement.java index 62dc53371..c2b1efe03 100644 --- a/src/main/java/org/mariadb/jdbc/Statement.java +++ b/src/main/java/org/mariadb/jdbc/Statement.java @@ -10,6 +10,7 @@ import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.Completion; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.result.CompleteResult; @@ -168,6 +169,12 @@ public ResultSet executeQuery(String sql) throws SQLException { if (currResult instanceof Result) { return (Result) currResult; } + if (Boolean.parseBoolean( + con.getContext().getConf().nonMappedOptions().getProperty("permitNoResults", "false"))) { + // for compatibility with pre 3.4.0 version + return new CompleteResult( + new ColumnDecoder[0], new byte[0][], con.getContext(), resultSetType); + } throw new SQLException( "Statement.executeQuery() command does NOT return a result-set as expected. Either use" + " Statement.execute(), Statement.executeUpdate(), or correct command"); diff --git a/src/main/java/org/mariadb/jdbc/client/result/CompleteResult.java b/src/main/java/org/mariadb/jdbc/client/result/CompleteResult.java index 29e49a98e..aa6c5ea60 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/CompleteResult.java +++ b/src/main/java/org/mariadb/jdbc/client/result/CompleteResult.java @@ -121,7 +121,7 @@ private CompleteResult(ColumnDecoder[] metadataList, CompleteResult prev) { * @param context connection context * @param resultSetType result set type */ - private CompleteResult( + public CompleteResult( ColumnDecoder[] metadataList, byte[][] data, Context context, int resultSetType) { super(metadataList, data, context, resultSetType); } diff --git a/src/test/java/org/mariadb/jdbc/integration/ProcedureTest.java b/src/test/java/org/mariadb/jdbc/integration/ProcedureTest.java index ce81c51c8..721cc4d24 100644 --- a/src/test/java/org/mariadb/jdbc/integration/ProcedureTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/ProcedureTest.java @@ -67,6 +67,8 @@ public void settingParameterBeforeOutRegistration() throws SQLException { cstmt.setLong(1, 44L); cstmt.execute(); assertEquals(88, cstmt.getLong(1)); + ResultSet rs = cstmt.executeQuery(); + assertFalse(rs.next()); } } } diff --git a/src/test/java/org/mariadb/jdbc/integration/StatementTest.java b/src/test/java/org/mariadb/jdbc/integration/StatementTest.java index 701b2217d..977c93411 100644 --- a/src/test/java/org/mariadb/jdbc/integration/StatementTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/StatementTest.java @@ -93,6 +93,15 @@ public void ensureJdbcErrorWhenNoResultset() throws SQLException { + " command"); ps.execute(); } + try (Connection con = createCon("&permitNoResults=true")) { + try (PreparedStatement ps = con.prepareStatement("DO ?", Statement.RETURN_GENERATED_KEYS)) { + ps.setInt(1, 1); + ps.execute(); + ResultSet rs = ps.executeQuery(); + assertFalse(rs.next()); + ps.execute(); + } + } try (PreparedStatement ps = sharedConnBinary.prepareStatement("DO ?", Statement.RETURN_GENERATED_KEYS)) { ps.setInt(1, 1); @@ -105,6 +114,14 @@ public void ensureJdbcErrorWhenNoResultset() throws SQLException { + " command"); ps.execute(); } + try (Connection con = createCon("permitNoResults=true&useServerPrepStmts=true")) { + try (PreparedStatement ps = con.prepareStatement("DO ?", Statement.RETURN_GENERATED_KEYS)) { + ps.setInt(1, 1); + ps.execute(); + ResultSet rs = ps.executeQuery(); + assertFalse(rs.next()); + } + } } @Test