Skip to content

Commit

Permalink
Fixed #3 : TINYINT issue with MariaDB/MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Jul 5, 2024
1 parent 69e3fd7 commit d71c6f5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/rife/database/types/databasedrivers/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ protected Object getTypedObject(Object result, Class targetType) {
return result;
} else {
try {
// handle conversion between boolean in numbers
if (ClassUtils.isNumeric(targetType) && result_class == Boolean.class) {
result = (Boolean) result ? 1 : 0;
result_class = result.getClass();
}

// handle common primitive types and standard classes
if (targetType == boolean.class || targetType == Boolean.class) {
return StringUtils.convertToBoolean(result.toString());
} else if (targetType == byte.class || targetType == Byte.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import rife.database.querymanagers.generic.exceptions.MissingDefaultConstructorException;

import static org.junit.jupiter.api.Assertions.*;
import static rife.database.TestDatasources.MYSQL;

public class TestGenericQueryManagerSimple {
protected GenericQueryManager<SimpleBean> setup(Datasource datasource) {
Expand Down Expand Up @@ -65,6 +66,57 @@ void testInstallCustomQuery(Datasource datasource) {
}
}

public static class TestTinyBean {
private int id_ = -1;
private int tiny_ = 0;

public void setId(int id) {
id_ = id;
}

public int getId() {
return id_;
}

public void setTiny(int tiny) {
tiny_ = tiny;
}

public int getTiny() {
return tiny_;
}
}

@DatasourceEnabledIf(TestDatasourceIdentifier.MYSQL)
void testSaveRestoreTinyInt1AsInteger() {
var ddl = new DbQueryManager(MYSQL);
ddl.executeUpdate("""
create table testtinybean
(
id int auto_increment primary key,
tiny tinyint(1) null
)
""");
try {
var manager = GenericQueryManagerFactory.instance(MYSQL, TestTinyBean.class, "testtinybean");
var bean = new TestTinyBean();

bean.setTiny(1);

var id = manager.save(bean);

var new_bean = manager.restore(id);

assertNotNull(new_bean);
assertNotSame(new_bean, bean);
assertEquals(bean.getId(), new_bean.getId());
assertEquals(bean.getTiny(), new_bean.getTiny());
}
finally {
ddl.executeUpdate("drop table testtinybean");
}
}

@ParameterizedTest
@ArgumentsSource(TestDatasources.class)
void testSaveRestore(Datasource datasource) {
Expand Down

0 comments on commit d71c6f5

Please sign in to comment.