Skip to content

Commit a05b862

Browse files
Kirill Marchukmp911de
authored andcommitted
Add currentSchema alias for schema
Moved "schema" to connection options (instead of `SET SCHEMA TO` command execution upon connection) [resolves #271]
1 parent 7ee4fb2 commit a05b862

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Mono<Connection> connectionMono = Mono.from(connectionFactory.create());
8080
| `fetchSize` | The default number of rows to return when fetching results. Defaults to `0` for unlimited. _(Optional)_
8181
| `forceBinary` | Whether to force binary transfer. Defaults to `false`. _(Optional)_
8282
| `options` | A `Map<String, String>` of connection parameters. These are applied to each database connection created by the `ConnectionFactory`. Useful for setting generic [PostgreSQL connection parameters][psql-runtime-config]. _(Optional)_
83-
| `schema` | The schema to set. _(Optional)_
83+
| `schema` | The search path to set. _(Optional)_
8484
| `sslMode` | SSL mode to use, see `SSLMode` enum. Supported values: `DISABLE`, `ALLOW`, `PREFER`, `REQUIRE`, `VERIFY_CA`, `VERIFY_FULL`. _(Optional)_
8585
| `sslRootCert` | Path to SSL CA certificate in PEM format. _(Optional)_
8686
| `sslKey` | Path to SSL key for TLS authentication in PEM format. _(Optional)_

src/main/java/io/r2dbc/postgresql/PostgresqlConnectionConfiguration.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.ArrayList;
3636
import java.util.List;
3737
import java.util.Map;
38+
import java.util.HashMap;
3839
import java.util.ServiceLoader;
3940
import java.util.function.Function;
4041
import java.util.function.Supplier;
@@ -75,8 +76,6 @@ public final class PostgresqlConnectionConfiguration {
7576

7677
private final int port;
7778

78-
private final String schema;
79-
8079
private final String socket;
8180

8281
private final String username;
@@ -96,15 +95,29 @@ private PostgresqlConnectionConfiguration(String applicationName, boolean autode
9695
this.fetchSize = fetchSize;
9796
this.forceBinary = forceBinary;
9897
this.host = host;
99-
this.options = options;
98+
this.options = initOptions(options);
10099
this.password = password;
101100
this.port = port;
102-
this.schema = schema;
101+
addToOptions(schema);
103102
this.socket = socket;
104103
this.username = Assert.requireNonNull(username, "username must not be null");
105104
this.sslConfig = sslConfig;
106105
}
107106

107+
private Map<String, String> initOptions(@Nullable Map<String, String> options) {
108+
if (options == null) {
109+
return new HashMap<>();
110+
} else {
111+
return options;
112+
}
113+
}
114+
115+
private void addToOptions(String schema) {
116+
if (schema != null && !schema.isEmpty()) {
117+
options.put("search_path", schema);
118+
}
119+
}
120+
108121
/**
109122
* Returns a new {@link Builder}.
110123
*
@@ -139,7 +152,6 @@ public String toString() {
139152
", options='" + this.options + '\'' +
140153
", password='" + repeat(this.password != null ? this.password.length() : 0, "*") + '\'' +
141154
", port=" + this.port +
142-
", schema='" + this.schema + '\'' +
143155
", username='" + this.username + '\'' +
144156
'}';
145157
}
@@ -200,11 +212,6 @@ int getPort() {
200212
return this.port;
201213
}
202214

203-
@Nullable
204-
String getSchema() {
205-
return this.schema;
206-
}
207-
208215
@Nullable
209216
String getSocket() {
210217
return this.socket;

src/main/java/io/r2dbc/postgresql/PostgresqlConnectionFactory.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ private Mono<Client> tryConnectWithConfig(SSLConfig sslConfig, @Nullable Map<Str
201201
private Publisher<?> prepareConnection(PostgresqlConnection connection, ByteBufAllocator byteBufAllocator, DefaultCodecs codecs, boolean forReplication) {
202202

203203
List<Publisher<?>> publishers = new ArrayList<>();
204-
publishers.add(setSchema(connection));
205204

206205
if (!forReplication) {
207206
this.extensions.forEach(CodecRegistrar.class, it -> {
@@ -271,16 +270,6 @@ private Mono<IsolationLevel> getIsolationLevel(io.r2dbc.postgresql.api.Postgresq
271270
})).defaultIfEmpty(IsolationLevel.READ_COMMITTED).last();
272271
}
273272

274-
private Mono<Void> setSchema(PostgresqlConnection connection) {
275-
if (this.configuration.getSchema() == null) {
276-
return Mono.empty();
277-
}
278-
279-
return connection.createStatement(String.format("SET SCHEMA '%s'", this.configuration.getSchema()))
280-
.execute()
281-
.then();
282-
}
283-
284273
static class PostgresConnectionException extends R2dbcNonTransientResourceException {
285274

286275
public PostgresConnectionException(String msg, @Nullable Throwable cause) {

src/main/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProvider.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,15 @@ public final class PostgresqlConnectionFactoryProvider implements ConnectionFact
7272
public static final String LEGACY_POSTGRESQL_DRIVER = "postgres";
7373

7474
/**
75-
* Schema.
75+
* Schema search path (alias for "currentSchema").
7676
*/
7777
public static final Option<String> SCHEMA = Option.valueOf("schema");
7878

79+
/**
80+
* Schema search path.
81+
*/
82+
public static final Option<String> CURRENT_SCHEMA = Option.valueOf("currentSchema");
83+
7984
/**
8085
* Unix domain socket.
8186
*/
@@ -153,7 +158,13 @@ private static PostgresqlConnectionConfiguration createConfiguration(ConnectionF
153158
builder.host(connectionFactoryOptions.getRequiredValue(HOST));
154159
}
155160
builder.password(connectionFactoryOptions.getValue(PASSWORD));
156-
builder.schema(connectionFactoryOptions.getValue(SCHEMA));
161+
162+
if (connectionFactoryOptions.getValue(CURRENT_SCHEMA) != null) {
163+
builder.schema(connectionFactoryOptions.getValue(CURRENT_SCHEMA));
164+
} else {
165+
builder.schema(connectionFactoryOptions.getValue(SCHEMA));
166+
}
167+
157168
builder.username(connectionFactoryOptions.getRequiredValue(USER));
158169

159170
Integer port = connectionFactoryOptions.getValue(PORT);

src/test/java/io/r2dbc/postgresql/PostgresqlConnectionConfigurationTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ void configuration() {
8080
.hasFieldOrPropertyWithValue("connectTimeout", Duration.ofMillis(1000))
8181
.hasFieldOrPropertyWithValue("database", "test-database")
8282
.hasFieldOrPropertyWithValue("host", "test-host")
83-
.hasFieldOrPropertyWithValue("options", options)
83+
.hasFieldOrProperty("options")
8484
.hasFieldOrPropertyWithValue("password", null)
8585
.hasFieldOrPropertyWithValue("port", 100)
86-
.hasFieldOrPropertyWithValue("schema", "test-schema")
8786
.hasFieldOrPropertyWithValue("username", "test-username")
8887
.hasFieldOrProperty("sslConfig");
88+
89+
assertThat(configuration.getOptions())
90+
.containsEntry("lock_timeout", "10s")
91+
.containsEntry("statement_timeout", "60000")
92+
.containsEntry("search_path", "test-schema");
8993
}
9094

9195
@Test
@@ -104,9 +108,12 @@ void configurationDefaults() {
104108
.hasFieldOrPropertyWithValue("host", "test-host")
105109
.hasFieldOrPropertyWithValue("password", "test-password")
106110
.hasFieldOrPropertyWithValue("port", 5432)
107-
.hasFieldOrPropertyWithValue("schema", "test-schema")
111+
.hasFieldOrProperty("options")
108112
.hasFieldOrPropertyWithValue("username", "test-username")
109113
.hasFieldOrProperty("sslConfig");
114+
115+
assertThat(configuration.getOptions())
116+
.containsEntry("search_path", "test-schema");
110117
}
111118

112119
@Test

src/test/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void createAuthenticationMD5Password() {
6363
// @formatter:off
6464
Client client = TestClient.builder()
6565
.window()
66-
.expectRequest(new StartupMessage("test-application-name", "test-database", "test-username", null)).thenRespond(new AuthenticationMD5Password(TEST.buffer(4).writeInt(100)))
66+
.expectRequest(new StartupMessage("test-application-name", "test-database", "test-username", Collections.emptyMap())).thenRespond(new AuthenticationMD5Password(TEST.buffer(4).writeInt(100)))
6767
.expectRequest(new PasswordMessage("md55e9836cdb369d50e3bc7d127e88b4804")).thenRespond(AuthenticationOk.INSTANCE)
6868
.done()
6969
.build();

0 commit comments

Comments
 (0)