Skip to content

Commit 4781ad7

Browse files
committed
Ensure thatPreferredCursorExecution is the only configuration flag to control cursor preference.
[resolves #267] Signed-off-by: Mark Paluch <[email protected]>
1 parent ff4dc10 commit 4781ad7

8 files changed

+195
-180
lines changed

src/main/java/io/r2dbc/mssql/ConnectionOptions.java

-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package io.r2dbc.mssql;
1818

1919
import io.r2dbc.mssql.codec.Codecs;
20-
import io.r2dbc.mssql.codec.DefaultCodecs;
2120
import reactor.util.annotation.Nullable;
2221

2322
import java.time.Duration;
@@ -38,10 +37,6 @@ class ConnectionOptions {
3837

3938
private volatile Duration statementTimeout = Duration.ZERO;
4039

41-
ConnectionOptions() {
42-
this(sql -> false, new DefaultCodecs(), new IndefinitePreparedStatementCache(), true);
43-
}
44-
4540
ConnectionOptions(Predicate<String> preferCursoredExecution, Codecs codecs, PreparedStatementCache preparedStatementCache, boolean sendStringParametersAsUnicode) {
4641
this.preferCursoredExecution = preferCursoredExecution;
4742
this.codecs = codecs;

src/main/java/io/r2dbc/mssql/MssqlConnectionConfiguration.java

+39-15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.security.KeyStore;
4646
import java.time.Duration;
4747
import java.util.Arrays;
48+
import java.util.Locale;
4849
import java.util.Optional;
4950
import java.util.UUID;
5051
import java.util.function.Function;
@@ -182,14 +183,14 @@ MssqlConnectionConfiguration withRedirect(Redirect redirect) {
182183
}
183184

184185
return new MssqlConnectionConfiguration(this.applicationName, this.connectionId, this.connectTimeout, this.database, redirectServerName, hostNameInCertificate, this.lockWaitTimeout,
185-
this.password,
186-
this.preferCursoredExecution, redirect.getPort(), this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
187-
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword, this.username);
186+
this.password,
187+
this.preferCursoredExecution, redirect.getPort(), this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
188+
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword, this.username);
188189
}
189190

190191
ClientConfiguration toClientConfiguration() {
191192
return new DefaultClientConfiguration(this.connectTimeout, this.host, this.hostNameInCertificate, this.port, this.ssl, this.sslContextBuilderCustomizer,
192-
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword);
193+
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword);
193194
}
194195

195196
ConnectionOptions toConnectionOptions() {
@@ -354,7 +355,7 @@ public static final class Builder {
354355
@Nullable
355356
private Duration lockWaitTimeout;
356357

357-
private Predicate<String> preferCursoredExecution = sql -> false;
358+
private Predicate<String> preferCursoredExecution = DefaultCursorPreference.INSTANCE;
358359

359360
private CharSequence password;
360361

@@ -714,11 +715,11 @@ public MssqlConnectionConfiguration build() {
714715
}
715716

716717
return new MssqlConnectionConfiguration(this.applicationName, this.connectionId, this.connectTimeout, this.database, this.host, this.hostNameInCertificate, this.lockWaitTimeout,
717-
this.password,
718-
this.preferCursoredExecution, this.port, this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
719-
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive,
720-
this.tcpNoDelay, this.trustServerCertificate, this.trustStore,
721-
this.trustStoreType, this.trustStorePassword, this.username);
718+
this.password,
719+
this.preferCursoredExecution, this.port, this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
720+
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive,
721+
this.tcpNoDelay, this.trustServerCertificate, this.trustStore,
722+
this.trustStoreType, this.trustStorePassword, this.username);
722723
}
723724

724725
}
@@ -887,12 +888,35 @@ public SslContext getSslContext() throws GeneralSecurityException {
887888
private static SslContextBuilder createSslContextBuilder() {
888889
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
889890
sslContextBuilder.sslProvider(
890-
OpenSsl.isAvailable() ?
891-
io.netty.handler.ssl.SslProvider.OPENSSL :
892-
io.netty.handler.ssl.SslProvider.JDK)
893-
.ciphers(null, IdentityCipherSuiteFilter.INSTANCE)
894-
.applicationProtocolConfig(null);
891+
OpenSsl.isAvailable() ?
892+
io.netty.handler.ssl.SslProvider.OPENSSL :
893+
io.netty.handler.ssl.SslProvider.JDK)
894+
.ciphers(null, IdentityCipherSuiteFilter.INSTANCE)
895+
.applicationProtocolConfig(null);
895896
return sslContextBuilder;
896897
}
897898

899+
900+
static class DefaultCursorPreference implements Predicate<String> {
901+
902+
static final DefaultCursorPreference INSTANCE = new DefaultCursorPreference();
903+
904+
@Override
905+
public boolean test(String sql) {
906+
907+
if (sql.isEmpty()) {
908+
return false;
909+
}
910+
911+
String lc = sql.trim().toLowerCase(Locale.ENGLISH);
912+
if (lc.contains("for xml") || lc.contains("for json")) {
913+
return false;
914+
}
915+
916+
char c = sql.charAt(0);
917+
918+
return (c == 's' || c == 'S') && lc.startsWith("select");
919+
}
920+
}
921+
898922
}

src/main/java/io/r2dbc/mssql/SimpleMssqlStatement.java

+1-24
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import reactor.util.Logger;
3030
import reactor.util.Loggers;
3131

32-
import java.util.Locale;
3332
import java.util.function.Predicate;
3433

3534
/**
@@ -61,7 +60,7 @@ final class SimpleMssqlStatement extends MssqlStatementSupport implements MssqlS
6160
*/
6261
SimpleMssqlStatement(Client client, ConnectionOptions connectionOptions, String sql) {
6362

64-
super(connectionOptions.prefersCursors(sql) || prefersCursors(sql));
63+
super(connectionOptions.prefersCursors(sql));
6564
this.connectionOptions = connectionOptions;
6665

6766
Assert.requireNonNull(client, "Client must not be null");
@@ -162,26 +161,4 @@ public SimpleMssqlStatement fetchSize(int fetchSize) {
162161
return this;
163162
}
164163

165-
/**
166-
* Returns {@code true} if the query is supported by this {@link MssqlStatement}. Cursored execution is supported for {@literal SELECT} queries.
167-
*
168-
* @param sql the query to inspect.
169-
* @return {@code true} if the {@code sql} query is supported.
170-
*/
171-
static boolean prefersCursors(String sql) {
172-
173-
if (sql.isEmpty()) {
174-
return false;
175-
}
176-
177-
String lc = sql.trim().toLowerCase(Locale.ENGLISH);
178-
if (lc.contains("for xml") || lc.contains("for json")) {
179-
return false;
180-
}
181-
182-
char c = sql.charAt(0);
183-
184-
return (c == 's' || c == 'S') && lc.startsWith("select");
185-
}
186-
187164
}

src/test/java/io/r2dbc/mssql/MssqlBatchUnitTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void shouldExecuteSingleBatch() {
3939
.thenRespond(DoneToken.create(1))
4040
.build();
4141

42-
new MssqlBatch(client, new ConnectionOptions())
42+
new MssqlBatch(client, new TestConnectionOptions())
4343
.add("foo")
4444
.execute()
4545
.as(StepVerifier::create)
@@ -55,7 +55,7 @@ void shouldExecuteMultiBatch() {
5555
.thenRespond(DoneToken.create(1), DoneToken.create(1))
5656
.build();
5757

58-
new MssqlBatch(client, new ConnectionOptions())
58+
new MssqlBatch(client, new TestConnectionOptions())
5959
.add("foo")
6060
.add("bar")
6161
.execute()
@@ -73,7 +73,7 @@ void shouldFailOnExecution() {
7373
"proc", 0))
7474
.build();
7575

76-
new MssqlBatch(client, new ConnectionOptions())
76+
new MssqlBatch(client, new TestConnectionOptions())
7777
.add("foo")
7878
.execute()
7979
.as(StepVerifier::create)

0 commit comments

Comments
 (0)