From 43a77a7c51412e97d61a16ebcd1552db234ace02 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 9 Nov 2021 15:44:32 +0100 Subject: [PATCH] Refine default cursor preference when using FOR JSON/FOR XML. We now no longer prefer cursor usage by default when the statement ends with a FOR XML/JSON clause to avoid server side errors reporting that cursor usage is not supported. [resolves #209] Signed-off-by: Mark Paluch --- src/main/java/io/r2dbc/mssql/SimpleMssqlStatement.java | 8 ++++++-- src/test/java/io/r2dbc/mssql/JsonIntegrationTests.java | 2 +- src/test/java/io/r2dbc/mssql/XmlIntegrationTests.java | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/r2dbc/mssql/SimpleMssqlStatement.java b/src/main/java/io/r2dbc/mssql/SimpleMssqlStatement.java index 9185b77c..e71ffd11 100644 --- a/src/main/java/io/r2dbc/mssql/SimpleMssqlStatement.java +++ b/src/main/java/io/r2dbc/mssql/SimpleMssqlStatement.java @@ -174,9 +174,13 @@ static boolean prefersCursors(String sql) { return false; } - char c = sql.charAt(0); + String lc = sql.trim().toLowerCase(Locale.ENGLISH); + if (lc.contains("for xml") || lc.contains("for json")) { + return false; + } - return (c == 's' || c == 'S') && sql.toLowerCase(Locale.ENGLISH).startsWith("select"); + char c = sql.charAt(0); + return (c == 's' || c == 'S') && lc.startsWith("select"); } } diff --git a/src/test/java/io/r2dbc/mssql/JsonIntegrationTests.java b/src/test/java/io/r2dbc/mssql/JsonIntegrationTests.java index 6e99aaac..887119ba 100644 --- a/src/test/java/io/r2dbc/mssql/JsonIntegrationTests.java +++ b/src/test/java/io/r2dbc/mssql/JsonIntegrationTests.java @@ -30,7 +30,7 @@ class JsonIntegrationTests extends IntegrationTestSupport { @Test void shouldExecuteForJsonSimple() { - connection.createStatement("select 1 as a for json path").fetchSize(0).execute() + connection.createStatement("select 1 as a for json path").execute() .flatMap(result -> result.map((row, rowMetadata) -> row.get(0))) .as(StepVerifier::create) .expectNext("[{\"a\":1}]") diff --git a/src/test/java/io/r2dbc/mssql/XmlIntegrationTests.java b/src/test/java/io/r2dbc/mssql/XmlIntegrationTests.java index 41ba38a2..e11c0e6e 100644 --- a/src/test/java/io/r2dbc/mssql/XmlIntegrationTests.java +++ b/src/test/java/io/r2dbc/mssql/XmlIntegrationTests.java @@ -30,7 +30,7 @@ class XmlIntegrationTests extends IntegrationTestSupport { @Test void shouldExecuteForXmlSimple() { - connection.createStatement("select 1 as a for xml path").fetchSize(0).execute() + connection.createStatement("select 1 as a for xml path").execute() .flatMap(result -> result.map((row, rowMetadata) -> row.get(0))) .as(StepVerifier::create) .expectNext("1")