Skip to content

Commit

Permalink
Handle BIT(1) as Boolean by Default
Browse files Browse the repository at this point in the history
Motivation:
BIT(1) should be treated as `Boolean`.

Modification:
set `Boolean.class` as the default java type for `BIT(1)`.

Result:
Ensures that `BIT(1)` is handled as `Boolean` by default, similar to the implementation in MySQL Connector/j.
Resolves #277
  • Loading branch information
jchrys committed Aug 3, 2024
1 parent bc227f1 commit 8c092bd
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.asyncer.r2dbc.mysql.MySqlParameter;
import io.asyncer.r2dbc.mysql.api.MySqlReadableMetadata;
import io.asyncer.r2dbc.mysql.constant.MySqlType;
import io.asyncer.r2dbc.mysql.internal.util.InternalArrays;
import io.asyncer.r2dbc.mysql.message.FieldValue;
import io.asyncer.r2dbc.mysql.message.LargeFieldValue;
Expand Down Expand Up @@ -358,11 +359,21 @@ private <T> T decodeMassive(LargeFieldValue value, MySqlReadableMetadata metadat
* @param type the {@link Class} specified by the user.
* @return the {@link Class} to use for decoding.
*/
private static Class<?> chooseClass(MySqlReadableMetadata metadata, Class<?> type) {
Class<?> javaType = metadata.getType().getJavaType();
private static Class<?> chooseClass(final MySqlReadableMetadata metadata, Class<?> type) {
final Class<?> javaType = getDefaultJavaType(metadata);
return type.isAssignableFrom(javaType) ? javaType : type;
}

private static Class<?> getDefaultJavaType(final MySqlReadableMetadata metadata) {
final MySqlType type = metadata.getType();
// ref: https://github.com/asyncer-io/r2dbc-mysql/issues/277
// BIT(1) should be treated as Boolean by default.
if (type == MySqlType.BIT && Integer.valueOf(1).equals(metadata.getPrecision())) {
return Boolean.class;
}
return type.getJavaType();
}

static final class Builder implements CodecsBuilder {

@GuardedBy("lock")
Expand Down

0 comments on commit 8c092bd

Please sign in to comment.