Skip to content

Commit 9c958e3

Browse files
committed
Polishing
Adopt to ConnectionContext. Tweak log message wording. Defer Objects.toString(…) until actual logging happens. [#278][#309]
1 parent 2cced7b commit 9c958e3

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,48 @@
11
package io.r2dbc.postgresql;
22

3+
import io.r2dbc.postgresql.client.ConnectionContext;
34
import reactor.util.Logger;
45
import reactor.util.Loggers;
56

7+
import java.util.Objects;
8+
69
/**
710
* Binding logger to log parameter bindings
11+
*
12+
* @since 0.8.6
813
*/
914
final class BindingLogger {
1015

1116
private static final Logger BINDING_LOGGER = Loggers.getLogger("io.r2dbc.postgresql.PARAM");
1217

13-
static void logBinding(int index, String bindValue) {
14-
BINDING_LOGGER.debug("Parameter {} value bound: {}", index, bindValue);
18+
private static final boolean LOGGING_ENABLED = BINDING_LOGGER.isDebugEnabled();
19+
20+
static void logBind(ConnectionContext connectionContext, int index, Object bindValue) {
21+
22+
if (LOGGING_ENABLED) {
23+
BINDING_LOGGER.debug(connectionContext.getMessage("Bind parameter [{}] to: {}"), index, Objects.toString(bindValue));
24+
}
1525
}
1626

17-
static void logBinding(String name, String bindValue) {
18-
BINDING_LOGGER.debug("Parameter {} value bound: {}", name, bindValue);
27+
static void logBind(ConnectionContext connectionContext, String name, Object bindValue) {
28+
29+
if (LOGGING_ENABLED) {
30+
BINDING_LOGGER.debug(connectionContext.getMessage("Bind parameter [{}] to: {}"), name, Objects.toString(bindValue));
31+
}
1932
}
33+
34+
static void logBindNull(ConnectionContext connectionContext, int index, Class<?> type) {
35+
36+
if (LOGGING_ENABLED) {
37+
BINDING_LOGGER.debug(connectionContext.getMessage("Bind parameter [{}] to null, type: {}"), index, type.getName());
38+
}
39+
}
40+
41+
static void logBindNull(ConnectionContext connectionContext, String name, Class<?> type) {
42+
43+
if (LOGGING_ENABLED) {
44+
BINDING_LOGGER.debug(connectionContext.getMessage("Bind parameter [{}] to null, type: {}"), name, type.getName());
45+
}
46+
}
47+
2048
}

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

+17-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.r2dbc.postgresql.api.PostgresqlStatement;
2020
import io.r2dbc.postgresql.client.Binding;
21+
import io.r2dbc.postgresql.client.ConnectionContext;
2122
import io.r2dbc.postgresql.client.ExtendedQueryMessageFlow;
2223
import io.r2dbc.postgresql.message.backend.BackendMessage;
2324
import io.r2dbc.postgresql.message.backend.BindComplete;
@@ -31,7 +32,6 @@
3132
import java.util.Arrays;
3233
import java.util.HashSet;
3334
import java.util.List;
34-
import java.util.Objects;
3535
import java.util.Set;
3636
import java.util.function.Predicate;
3737
import java.util.regex.Matcher;
@@ -49,19 +49,22 @@ final class ExtendedQueryPostgresqlStatement implements PostgresqlStatement {
4949

5050
private final Bindings bindings;
5151

52-
private final ConnectionResources context;
52+
private final ConnectionResources resources;
53+
54+
private final ConnectionContext connectionContext;
5355

5456
private final String sql;
5557

5658
private int fetchSize;
5759

5860
private String[] generatedColumns;
5961

60-
ExtendedQueryPostgresqlStatement(ConnectionResources context, String sql) {
61-
this.context = Assert.requireNonNull(context, "context must not be null");
62+
ExtendedQueryPostgresqlStatement(ConnectionResources resources, String sql) {
63+
this.resources = Assert.requireNonNull(resources, "context must not be null");
64+
this.connectionContext = resources.getClient().getContext();
6265
this.sql = Assert.requireNonNull(sql, "sql must not be null");
6366
this.bindings = new Bindings(expectedSize(sql));
64-
fetchSize(this.context.getConfiguration().getFetchSize(sql));
67+
fetchSize(this.resources.getConfiguration().getFetchSize(sql));
6568
}
6669

6770
@Override
@@ -75,16 +78,16 @@ public ExtendedQueryPostgresqlStatement bind(String identifier, Object value) {
7578
Assert.requireNonNull(identifier, "identifier must not be null");
7679
Assert.requireType(identifier, String.class, "identifier must be a String");
7780

78-
BindingLogger.logBinding(identifier, Objects.toString(value));
81+
BindingLogger.logBind(this.connectionContext, identifier, value);
7982
return bind(getIndex(identifier), value);
8083
}
8184

8285
@Override
8386
public ExtendedQueryPostgresqlStatement bind(int index, Object value) {
8487
Assert.requireNonNull(value, "value must not be null");
8588

86-
BindingLogger.logBinding(index, Objects.toString(value));
87-
this.bindings.getCurrent().add(index, this.context.getCodecs().encode(value));
89+
BindingLogger.logBind(this.connectionContext, index, value);
90+
this.bindings.getCurrent().add(index, this.resources.getCodecs().encode(value));
8891

8992
return this;
9093
}
@@ -95,7 +98,7 @@ public ExtendedQueryPostgresqlStatement bindNull(String identifier, Class<?> typ
9598
Assert.requireType(identifier, String.class, "identifier must be a String");
9699
Assert.requireNonNull(type, "type must not be null");
97100

98-
BindingLogger.logBinding(identifier, "null of type " + type.getName());
101+
BindingLogger.logBindNull(this.connectionContext, identifier, type);
99102
bindNull(getIndex(identifier), type);
100103
return this;
101104
}
@@ -104,8 +107,8 @@ public ExtendedQueryPostgresqlStatement bindNull(String identifier, Class<?> typ
104107
public ExtendedQueryPostgresqlStatement bindNull(int index, Class<?> type) {
105108
Assert.requireNonNull(type, "type must not be null");
106109

107-
BindingLogger.logBinding(index, "null of type " + type.getName());
108-
this.bindings.getCurrent().add(index, this.context.getCodecs().encodeNull(type));
110+
BindingLogger.logBindNull(this.connectionContext, index, type);
111+
this.bindings.getCurrent().add(index, this.resources.getCodecs().encodeNull(type));
109112
return this;
110113
}
111114

@@ -145,7 +148,7 @@ public ExtendedQueryPostgresqlStatement fetchSize(int rows) {
145148
public String toString() {
146149
return "ExtendedQueryPostgresqlStatement{" +
147150
"bindings=" + this.bindings +
148-
", context=" + this.context +
151+
", context=" + this.resources +
149152
", sql='" + this.sql + '\'' +
150153
", generatedColumns=" + Arrays.toString(this.generatedColumns) +
151154
'}';
@@ -179,9 +182,9 @@ private Flux<io.r2dbc.postgresql.api.PostgresqlResult> execute(String sql) {
179182
this.bindings.finish();
180183

181184
ExceptionFactory factory = ExceptionFactory.withSql(sql);
182-
return this.context.getStatementCache().getName(this.bindings.first(), sql)
185+
return this.resources.getStatementCache().getName(this.bindings.first(), sql)
183186
.flatMapMany(name -> Flux.fromIterable(this.bindings.bindings).map(binding -> {
184-
return createPostgresqlResult(sql, factory, name, binding, this.context, this.fetchSize);
187+
return createPostgresqlResult(sql, factory, name, binding, this.resources, this.fetchSize);
185188
}))
186189
.cast(io.r2dbc.postgresql.api.PostgresqlResult.class);
187190
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.r2dbc.postgresql.api.PostgresqlConnection;
2020
import io.r2dbc.postgresql.client.Client;
2121
import io.r2dbc.postgresql.client.PortalNameSupplier;
22+
import io.r2dbc.postgresql.client.TestClient;
2223
import io.r2dbc.postgresql.codec.Codecs;
2324
import io.r2dbc.postgresql.codec.MockCodecs;
2425

@@ -39,7 +40,7 @@ public static final class Builder {
3940

4041
private Codecs codecs = MockCodecs.empty();
4142

42-
private Client client;
43+
private Client client = TestClient.NO_OP;
4344

4445
private PostgresqlConnection connection;
4546

0 commit comments

Comments
 (0)