diff --git a/src/main/java/rife/database/types/databasedrivers/Common.java b/src/main/java/rife/database/types/databasedrivers/Common.java index 8cab28c..ff721d2 100644 --- a/src/main/java/rife/database/types/databasedrivers/Common.java +++ b/src/main/java/rife/database/types/databasedrivers/Common.java @@ -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) { diff --git a/src/test/java/rife/database/querymanagers/generic/TestGenericQueryManagerSimple.java b/src/test/java/rife/database/querymanagers/generic/TestGenericQueryManagerSimple.java index 8352152..1c08945 100644 --- a/src/test/java/rife/database/querymanagers/generic/TestGenericQueryManagerSimple.java +++ b/src/test/java/rife/database/querymanagers/generic/TestGenericQueryManagerSimple.java @@ -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 setup(Datasource datasource) { @@ -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) {