Skip to content

Commit

Permalink
Ensure thatPreferredCursorExecution is the only configuration flag to…
Browse files Browse the repository at this point in the history
… control cursor preference.

[resolves #267]

Signed-off-by: Mark Paluch <mpaluch@vmware.com>
  • Loading branch information
mp911de committed May 25, 2023
1 parent ff4dc10 commit 4781ad7
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 180 deletions.
5 changes: 0 additions & 5 deletions src/main/java/io/r2dbc/mssql/ConnectionOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.r2dbc.mssql;

import io.r2dbc.mssql.codec.Codecs;
import io.r2dbc.mssql.codec.DefaultCodecs;
import reactor.util.annotation.Nullable;

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

private volatile Duration statementTimeout = Duration.ZERO;

ConnectionOptions() {
this(sql -> false, new DefaultCodecs(), new IndefinitePreparedStatementCache(), true);
}

ConnectionOptions(Predicate<String> preferCursoredExecution, Codecs codecs, PreparedStatementCache preparedStatementCache, boolean sendStringParametersAsUnicode) {
this.preferCursoredExecution = preferCursoredExecution;
this.codecs = codecs;
Expand Down
54 changes: 39 additions & 15 deletions src/main/java/io/r2dbc/mssql/MssqlConnectionConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.security.KeyStore;
import java.time.Duration;
import java.util.Arrays;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
Expand Down Expand Up @@ -182,14 +183,14 @@ MssqlConnectionConfiguration withRedirect(Redirect redirect) {
}

return new MssqlConnectionConfiguration(this.applicationName, this.connectionId, this.connectTimeout, this.database, redirectServerName, hostNameInCertificate, this.lockWaitTimeout,
this.password,
this.preferCursoredExecution, redirect.getPort(), this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword, this.username);
this.password,
this.preferCursoredExecution, redirect.getPort(), this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword, this.username);
}

ClientConfiguration toClientConfiguration() {
return new DefaultClientConfiguration(this.connectTimeout, this.host, this.hostNameInCertificate, this.port, this.ssl, this.sslContextBuilderCustomizer,
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword);
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive, this.tcpNoDelay, this.trustServerCertificate, this.trustStore, this.trustStoreType, this.trustStorePassword);
}

ConnectionOptions toConnectionOptions() {
Expand Down Expand Up @@ -354,7 +355,7 @@ public static final class Builder {
@Nullable
private Duration lockWaitTimeout;

private Predicate<String> preferCursoredExecution = sql -> false;
private Predicate<String> preferCursoredExecution = DefaultCursorPreference.INSTANCE;

private CharSequence password;

Expand Down Expand Up @@ -714,11 +715,11 @@ public MssqlConnectionConfiguration build() {
}

return new MssqlConnectionConfiguration(this.applicationName, this.connectionId, this.connectTimeout, this.database, this.host, this.hostNameInCertificate, this.lockWaitTimeout,
this.password,
this.preferCursoredExecution, this.port, this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive,
this.tcpNoDelay, this.trustServerCertificate, this.trustStore,
this.trustStoreType, this.trustStorePassword, this.username);
this.password,
this.preferCursoredExecution, this.port, this.sendStringParametersAsUnicode, this.ssl, this.sslContextBuilderCustomizer,
this.sslTunnelSslContextBuilderCustomizer, this.tcpKeepAlive,
this.tcpNoDelay, this.trustServerCertificate, this.trustStore,
this.trustStoreType, this.trustStorePassword, this.username);
}

}
Expand Down Expand Up @@ -887,12 +888,35 @@ public SslContext getSslContext() throws GeneralSecurityException {
private static SslContextBuilder createSslContextBuilder() {
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
sslContextBuilder.sslProvider(
OpenSsl.isAvailable() ?
io.netty.handler.ssl.SslProvider.OPENSSL :
io.netty.handler.ssl.SslProvider.JDK)
.ciphers(null, IdentityCipherSuiteFilter.INSTANCE)
.applicationProtocolConfig(null);
OpenSsl.isAvailable() ?
io.netty.handler.ssl.SslProvider.OPENSSL :
io.netty.handler.ssl.SslProvider.JDK)
.ciphers(null, IdentityCipherSuiteFilter.INSTANCE)
.applicationProtocolConfig(null);
return sslContextBuilder;
}


static class DefaultCursorPreference implements Predicate<String> {

static final DefaultCursorPreference INSTANCE = new DefaultCursorPreference();

@Override
public boolean test(String sql) {

if (sql.isEmpty()) {
return false;
}

String lc = sql.trim().toLowerCase(Locale.ENGLISH);
if (lc.contains("for xml") || lc.contains("for json")) {
return false;
}

char c = sql.charAt(0);

return (c == 's' || c == 'S') && lc.startsWith("select");
}
}

}
25 changes: 1 addition & 24 deletions src/main/java/io/r2dbc/mssql/SimpleMssqlStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import reactor.util.Logger;
import reactor.util.Loggers;

import java.util.Locale;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -61,7 +60,7 @@ final class SimpleMssqlStatement extends MssqlStatementSupport implements MssqlS
*/
SimpleMssqlStatement(Client client, ConnectionOptions connectionOptions, String sql) {

super(connectionOptions.prefersCursors(sql) || prefersCursors(sql));
super(connectionOptions.prefersCursors(sql));
this.connectionOptions = connectionOptions;

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

/**
* Returns {@code true} if the query is supported by this {@link MssqlStatement}. Cursored execution is supported for {@literal SELECT} queries.
*
* @param sql the query to inspect.
* @return {@code true} if the {@code sql} query is supported.
*/
static boolean prefersCursors(String sql) {

if (sql.isEmpty()) {
return false;
}

String lc = sql.trim().toLowerCase(Locale.ENGLISH);
if (lc.contains("for xml") || lc.contains("for json")) {
return false;
}

char c = sql.charAt(0);

return (c == 's' || c == 'S') && lc.startsWith("select");
}

}
6 changes: 3 additions & 3 deletions src/test/java/io/r2dbc/mssql/MssqlBatchUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void shouldExecuteSingleBatch() {
.thenRespond(DoneToken.create(1))
.build();

new MssqlBatch(client, new ConnectionOptions())
new MssqlBatch(client, new TestConnectionOptions())
.add("foo")
.execute()
.as(StepVerifier::create)
Expand All @@ -55,7 +55,7 @@ void shouldExecuteMultiBatch() {
.thenRespond(DoneToken.create(1), DoneToken.create(1))
.build();

new MssqlBatch(client, new ConnectionOptions())
new MssqlBatch(client, new TestConnectionOptions())
.add("foo")
.add("bar")
.execute()
Expand All @@ -73,7 +73,7 @@ void shouldFailOnExecution() {
"proc", 0))
.build();

new MssqlBatch(client, new ConnectionOptions())
new MssqlBatch(client, new TestConnectionOptions())
.add("foo")
.execute()
.as(StepVerifier::create)
Expand Down

0 comments on commit 4781ad7

Please sign in to comment.