Skip to content

Commit

Permalink
Add logging for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mirromutth committed Dec 15, 2023
1 parent 4517ade commit de202a7
Show file tree
Hide file tree
Showing 39 changed files with 203 additions and 33 deletions.
18 changes: 14 additions & 4 deletions src/main/java/io/asyncer/r2dbc/mysql/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,37 @@ void add(int index, MySqlParameter value) {
}

/**
* Converts bindings to a request message. If not need execute immediate, it will return a open cursor
* Converts bindings to a request message. If not need execute immediate, it will return an open cursor
* message.
*
* @param statementId prepared statement identifier.
* @param immediate if should be execute immediate, otherwise return a open cursor message
* @param immediate if it should be executed immediate, otherwise return an open cursor message
* @return an execute message or open cursor message
*/
PreparedExecuteMessage toExecuteMessage(int statementId, boolean immediate) {
if (values.length == 0) {
QueryLogger.log(statementId, EMPTY_VALUES);

return new PreparedExecuteMessage(statementId, immediate, EMPTY_VALUES);
}

if (values[0] == null) {
throw new IllegalStateException("Parameters has been used");
}

return new PreparedExecuteMessage(statementId, immediate, drainValues());
MySqlParameter[] values = drainValues();

QueryLogger.log(statementId, values);

return new PreparedExecuteMessage(statementId, immediate, values);
}

PreparedTextQueryMessage toTextMessage(Query query) {
return new PreparedTextQueryMessage(query, drainValues());
MySqlParameter[] values = drainValues();

QueryLogger.log(query, values);

return new PreparedTextQueryMessage(query, values);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/asyncer/r2dbc/mysql/QueryFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,6 @@ public boolean isDisposed() {
@Override
protected void tryNextOrComplete(@Nullable SynchronousSink<ServerMessage> sink) {
if (this.bindings.hasNext()) {
QueryLogger.log(this.query);

PreparedTextQueryMessage message = this.bindings.next().toTextMessage(this.query);
Sinks.EmitResult result = this.requests.tryEmitNext(message);

Expand Down Expand Up @@ -519,6 +517,7 @@ public void subscribe(CoreSubscriber<? super ClientMessage> actual) {
// Should reset only when it comes from cache.
this.shouldClose = false;
this.statementId = statementId;
QueryLogger.log(statementId, sql);
Sinks.EmitResult result = this.requests.tryEmitNext(new PreparedResetMessage(statementId));

if (result != Sinks.EmitResult.OK) {
Expand Down Expand Up @@ -553,6 +552,7 @@ public void accept(ServerMessage message, SynchronousSink<ServerMessage> sink) {
int parameters = ok.getTotalParameters();

this.statementId = statementId;
QueryLogger.log(statementId, sql);

// columns + parameters <= 0, has not metadata follow in,
if (columns <= -parameters) {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/io/asyncer/r2dbc/mysql/QueryLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ final class QueryLogger {
private static final InternalLogger logger = InternalLoggerFactory.getInstance("io.asyncer.r2dbc.mysql.QUERY");

static void log(String query) {
logger.debug("Executing direct query: {}", query);
logger.debug("Executing statement [{}]", query);
}

static void log(Query query) {
static void log(int statementId, String query) {
logger.debug("Prepared statement {} [{}]", statementId, query);
}

static void log(Query query, MySqlParameter[] values) {
if (logger.isDebugEnabled()) {
logger.debug("Executing format query: {}", query.getFormattedSql());
logger.debug("Executing statement [{}] with {}", query.getFormattedSql(), values);
}
}

static void log(int statementId, MySqlParameter[] values) {
logger.debug("Executing prepared statement {} with {}", statementId, values);
}

private QueryLogger() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@
abstract class AbstractMySqlParameter implements MySqlParameter {

@Override
public final String toString() {
// Hide parameter detail even its type.
return "Parameter{REDACTED}";
}
public abstract String toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BitSetCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,10 @@ public boolean equals(Object o) {
public int hashCode() {
return (int) (value ^ (value >>> 32));
}

@Override
public String toString() {
return Long.toBinaryString(value);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BlobCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,11 @@ protected Publisher<Void> getDiscard() {
Blob blob = this.blob.getAndSet(null);
return blob == null ? null : blob.discard();
}

@Override
public String toString() {
Blob blob = this.blob.get();
return blob == null ? "Blob[MOVED]" : blob.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/BooleanCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,10 @@ public boolean equals(Object o) {
public int hashCode() {
return (value ? 1 : 0);
}

@Override
public String toString() {
return Boolean.toString(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,10 @@ public boolean equals(Object o) {
public int hashCode() {
return Arrays.hashCode(value);
}

@Override
public String toString() {
return Arrays.toString(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,10 @@ public boolean equals(Object o) {
public int hashCode() {
return buffer.hashCode();
}

@Override
public String toString() {
return buffer.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/ByteCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value;
}

@Override
public String toString() {
return Byte.toString(value);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/ClobCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,11 @@ protected Publisher<Void> getDiscard() {
Clob clob = this.clob.getAndSet(null);
return clob == null ? null : clob.discard();
}

@Override
public String toString() {
Clob clob = this.clob.get();
return clob == null ? "Clob[MOVED]" : clob.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/DoubleCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,10 @@ public int hashCode() {
long temp = Double.doubleToLongBits(value);
return (int) (temp ^ (temp >>> 32));
}

@Override
public String toString() {
return Double.toString(value);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/DurationCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,10 @@ private void encodeTo(ParameterWriter writer) {

encodeTime(writer, isNegative, hours, minutes, seconds, micros);
}

@Override
public String toString() {
return value.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/EnumCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/FloatCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,10 @@ public boolean equals(Object o) {
public int hashCode() {
return (value != 0.0f ? Float.floatToIntBits(value) : 0);
}

@Override
public String toString() {
return Float.toString(value);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/InstantCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,10 @@ public int hashCode() {
private LocalDateTime serverValue() {
return LocalDateTime.ofInstant(value, context.getServerZoneId());
}

@Override
public String toString() {
return value.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/IntegerCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value;
}

@Override
public String toString() {
return Integer.toString(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/LongCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,10 @@ public boolean equals(Object o) {
public int hashCode() {
return (int) (value ^ (value >>> 32));
}

@Override
public String toString() {
return Long.toString(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void dispose() {
@Override
public String toString() {
// Hide parameter detail even it is null.
return "Parameter{REDACTED}";
return "null";
}

private NullMySqlParameter() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,10 @@ private LocalDateTime serverValue() {
value.withOffsetSameInstant((ZoneOffset) zone).toLocalDateTime() :
value.toZonedDateTime().withZoneSameInstant(zone).toLocalDateTime();
}

@Override
public String toString() {
return value.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,10 @@ private LocalTime serverValue() {
return value.toLocalTime()
.plusSeconds(offset.getTotalSeconds() - value.getOffset().getTotalSeconds());
}

@Override
public String toString() {
return value.toString();
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/SetCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}

private static final class StringArrayMySqlParameter extends AbstractMySqlParameter {
Expand Down Expand Up @@ -400,5 +405,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/ShortCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value;
}

@Override
public String toString() {
return Short.toString(value);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/asyncer/r2dbc/mysql/codec/StringCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,10 @@ public boolean equals(Object o) {
public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return value.toString();
}
}
}
Loading

0 comments on commit de202a7

Please sign in to comment.