Skip to content

Commit

Permalink
Support tinyInt1isBit
Browse files Browse the repository at this point in the history
Motivation:
Aligning with MySQL connector.

Modifications:
Implemented `tinyInt1isBit` flag.

Result:
Improved compatibility with MySQL connectors.
  • Loading branch information
jchrys committed Jan 31, 2025
1 parent ef62618 commit 81adf3a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ public Builder metrics(boolean enabled) {
* option to whether the driver should interpret MySQL's TINYINT(1) as a BIT type.
* When enabled, TINYINT(1) columns (both SIGNED and UNSIGNED) will be treated as
* {@link Boolean} by default.
*
*
* @param tinyInt1isBit {@code true} to treat TINYINT(1) as BIT
* @return this {@link Builder}
* @since 1.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public final class MySqlConnectionFactoryProvider implements ConnectionFactoryPr
/**
* Since the MySQL server silently converts BIT to TINYINT(1) when creating tables,
* should the driver treat the datatype TINYINT(1) as the BIT type?
* </p>
* <p>
* Note: If {@code tinyInt1isBit=true}, TINYINT(1) columns, whether SIGNED or UNSIGNED,
* will be represented as {@link Boolean} by default.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void getTimeZone() {
String id = i < 0 ? "UTC" + i : "UTC+" + i;
ConnectionContext context = new ConnectionContext(
ZeroDateOption.USE_NULL, null,
8192, true, ZoneId.of(id));
8192, true, true, ZoneId.of(id));

assertThat(context.getTimeZone()).isEqualTo(ZoneId.of(id));
}
Expand All @@ -48,7 +48,7 @@ void getTimeZone() {
@Test
void setTwiceTimeZone() {
ConnectionContext context = new ConnectionContext(ZeroDateOption.USE_NULL, null,
8192, true, null);
8192, true, true, null);

context.initSession(
Caches.createPrepareCache(0),
Expand All @@ -70,7 +70,7 @@ void setTwiceTimeZone() {
@Test
void badSetTimeZone() {
ConnectionContext context = new ConnectionContext(ZeroDateOption.USE_NULL, null,
8192, true, ZoneId.systemDefault());
8192,true, true, ZoneId.systemDefault());
assertThatIllegalStateException().isThrownBy(() -> context.initSession(
Caches.createPrepareCache(0),
IsolationLevel.REPEATABLE_READ,
Expand All @@ -91,7 +91,7 @@ public static ConnectionContext mock(boolean isMariaDB) {

public static ConnectionContext mock(boolean isMariaDB, ZoneId zoneId) {
ConnectionContext context = new ConnectionContext(ZeroDateOption.USE_NULL, null,
8192, true, zoneId);
8192, true, true, zoneId);

context.initHandshake(1, ServerVersion.parse(isMariaDB ? "11.2.22.MOCKED" : "8.0.11.MOCKED"),
Capability.of(~(isMariaDB ? 1 : 0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,17 @@ void loadDataLocalInfile(String name) throws URISyntaxException, IOException {
.doOnNext(it -> assertThat(it).isEqualTo(json)));
}

@Test
public void tinyInt1isBitTrue() {
complete(connection -> Mono.from(connection.createStatement("CREATE TEMPORARY TABLE `test` (`id` INT NOT NULL PRIMARY KEY, `value` TINYINT(1))").execute())
.flatMap(IntegrationTestSupport::extractRowsUpdated)
.thenMany(connection.createStatement("INSERT INTO `test` VALUES (1, 1)").execute())
.flatMap(IntegrationTestSupport::extractRowsUpdated)
.thenMany(connection.createStatement("SELECT `value` FROM `test`").execute())
.flatMap(result -> result.map((row, metadata) -> row.get("value", Object.class)))
.doOnNext(value -> assertThat(value).isInstanceOf(Boolean.class)));
}

@Test
void batchCrud() {
// TODO: spilt it to multiple test cases and move it to BatchIntegrationTest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.asyncer.r2dbc.mysql;


import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

import static org.assertj.core.api.Assertions.assertThat;

class TinyInt1isBitFalseTest extends IntegrationTestSupport{
TinyInt1isBitFalseTest() {
super(configuration(builder -> builder.tinyInt1isBit(false)));
}

@Test
public void tinyInt1isBitFalse() {
complete(connection -> Mono.from(connection.createStatement("CREATE TEMPORARY TABLE `test` (`id` INT NOT NULL PRIMARY KEY, `value` TINYINT(1))").execute())
.flatMap(IntegrationTestSupport::extractRowsUpdated)
.thenMany(connection.createStatement("INSERT INTO `test` VALUES (1, 1)").execute())
.flatMap(IntegrationTestSupport::extractRowsUpdated)
.thenMany(connection.createStatement("SELECT `value` FROM `test`").execute())
.flatMap(result -> result.map((row, metadata) -> row.get("value", Object.class)))
.doOnNext(value -> assertThat(value).isInstanceOf(Byte.class)));
}

}

0 comments on commit 81adf3a

Please sign in to comment.