Skip to content

Commit

Permalink
Merge branch 'trunk' into 1x-tc
Browse files Browse the repository at this point in the history
  • Loading branch information
jchrys authored Mar 31, 2024
2 parents c7cafc9 + aa90297 commit 84d635f
Show file tree
Hide file tree
Showing 22 changed files with 202 additions and 216 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@ public final boolean canDecode(MySqlReadableMetadata metadata, Class<?> target)
return target.isAssignableFrom(this.type) && doCanDecode(metadata);
}

@Override
public final Class<? extends T> getMainClass() {
return this.type;
}

protected abstract boolean doCanDecode(MySqlReadableMetadata metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @param <T> the boxed type of handling primitive data.
*/
abstract class AbstractPrimitiveCodec<T> implements PrimitiveCodec<T> {
abstract class AbstractPrimitiveCodec<T> implements Codec<T> {

private final Class<T> primitiveClass;

Expand All @@ -41,11 +41,17 @@ abstract class AbstractPrimitiveCodec<T> implements PrimitiveCodec<T> {

@Override
public final boolean canDecode(MySqlReadableMetadata metadata, Class<?> target) {
return target.isAssignableFrom(boxedClass) && canPrimitiveDecode(metadata);
return (target.isAssignableFrom(boxedClass) || target.equals(primitiveClass)) && doCanDecode(metadata);
}

@Override
public final Class<T> getPrimitiveClass() {
return primitiveClass;
}

@Override
public final Class<? extends T> getMainClass() {
return boxedClass;
}

protected abstract boolean doCanDecode(MySqlReadableMetadata metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ final class BlobCodec implements MassiveCodec<Blob> {
private BlobCodec() {
}

@Override
public Class<? extends Blob> getMainClass() {
return Blob.class;
}

@Override
public Blob decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
CodecContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
}

@Override
public boolean canPrimitiveDecode(MySqlReadableMetadata metadata) {
public boolean doCanDecode(MySqlReadableMetadata metadata) {
MySqlType type = metadata.getType();
return (type == MySqlType.BIT || type == MySqlType.TINYINT) &&
Integer.valueOf(1).equals(metadata.getPrecision());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MySqlParameter encode(Object value, CodecContext context) {
}

@Override
public boolean canPrimitiveDecode(MySqlReadableMetadata metadata) {
public boolean doCanDecode(MySqlReadableMetadata metadata) {
return metadata.getType().isNumeric();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ final class ClobCodec implements MassiveCodec<Clob> {
private ClobCodec() {
}

@Override
public Class<? extends Clob> getMainClass() {
return Clob.class;
}

@Override
public Clob decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
CodecContext context) {
Expand Down
15 changes: 13 additions & 2 deletions r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/codec/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public interface Codec<T> {
* @return the decoded result.
*/
@Nullable
T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
CodecContext context);
T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary, CodecContext context);

/**
* Checks if the field value can be decoded as specified {@link Class}.
Expand All @@ -69,4 +68,16 @@ T decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean
* @return encoded {@link MySqlParameter}.
*/
MySqlParameter encode(Object value, CodecContext context);

/**
* Gets the main {@link Class} that is handled by this codec. It is used to fast path the codec lookup if it is not
* {@code null}. If same main {@link Class} is handled by multiple codecs, the codec with the highest priority will
* be used. The priority of the fast path is determined by its order in {@link Codecs}.
*
* @return the main {@link Class}, or {@code null} if it is not in fast path.
*/
@Nullable
default Class<? extends T> getMainClass() {
return null;
}
}
Loading

0 comments on commit 84d635f

Please sign in to comment.