diff --git a/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java b/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java index 207c04ede..a1175da80 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/sql/AbstractDhRepo.java @@ -258,7 +258,27 @@ public abstract class AbstractDhRepo throw new RuntimeException("SQL result set is missing a column name for column ["+resultMetaData.getTableName(columnIndex)+"."+columnIndex+"]."); } - Object columnValue = resultSet.getObject(columnIndex); + + // some values need explicit conversion + // Example: Long values that are within the bounds of an int would automatically be incorrectly returned as "Integer" objects + String columnType = resultMetaData.getColumnTypeName(columnIndex).toUpperCase(); + Object columnValue; + switch (columnType) + { + case "BIGINT": + columnValue = resultSet.getLong(columnIndex); + break; + case "SMALLINT": + columnValue = resultSet.getShort(columnIndex); + break; + case "TINYINT": + columnValue = resultSet.getByte(columnIndex); + break; + default: + columnValue = resultSet.getObject(columnIndex); + break; + } + object.put(columnName, columnValue); } diff --git a/core/src/test/java/testItems/sql/TestDataRepo.java b/core/src/test/java/testItems/sql/TestDataRepo.java index 4924e85c2..8a5a5961d 100644 --- a/core/src/test/java/testItems/sql/TestDataRepo.java +++ b/core/src/test/java/testItems/sql/TestDataRepo.java @@ -38,6 +38,8 @@ public class TestDataRepo extends AbstractDhRepo "Id INT NOT NULL PRIMARY KEY\n" + "\n" + ",Value TEXT NULL\n" + + ",LongValue BIGINT NULL\n" + + ",ByteValue TINYINT NULL\n" + ");"; this.queryDictionaryFirst(createTableSql); } @@ -55,8 +57,10 @@ public class TestDataRepo extends AbstractDhRepo { int id = (int) objectMap.get("Id"); String value = (String) objectMap.get("Value"); + long longValue = (Long) objectMap.get("LongValue"); + byte byteValue = (Byte) objectMap.get("ByteValue"); - return new TestDto(id, value); + return new TestDto(id, value, longValue, byteValue); } @Override @@ -69,8 +73,8 @@ public class TestDataRepo extends AbstractDhRepo String value = (dto.value != null) ? dto.value+"" : "NULL"; return - "INSERT INTO "+this.getTableName()+" (Id, Value) " + - "VALUES("+id+",'"+value+"');"; + "INSERT INTO "+this.getTableName()+" (Id, Value, LongValue, ByteValue) " + + "VALUES("+id+",'"+value+"',"+dto.longValue+","+dto.byteValue+");"; } @Override @@ -81,7 +85,10 @@ public class TestDataRepo extends AbstractDhRepo return "UPDATE "+this.getTableName()+" " + - "SET Value = '"+value+"' " + + "SET " + + " Value = '"+value+"' \n" + + " ,LongValue = "+dto.longValue + " \n" + + " ,ByteValue = "+dto.byteValue + " \n" + "WHERE Id = "+id; } diff --git a/core/src/test/java/testItems/sql/TestDto.java b/core/src/test/java/testItems/sql/TestDto.java index b3660fa07..488184eb5 100644 --- a/core/src/test/java/testItems/sql/TestDto.java +++ b/core/src/test/java/testItems/sql/TestDto.java @@ -20,19 +20,49 @@ package testItems.sql; import com.seibel.distanthorizons.core.sql.IBaseDTO; +import org.junit.Assert; public class TestDto implements IBaseDTO { public int id; public String value; + public long longValue; + public byte byteValue; - public TestDto(int id, String value) + public TestDto(int id, String value, long longValue, byte byteValue) { this.id = id; this.value = value; + this.longValue = longValue; + this.byteValue = byteValue; } @Override public String getPrimaryKeyString() { return this.id+""; } + + @Override + public boolean equals(Object other) + { + if (other.getClass() != this.getClass()) + { + return false; + } + else + { + TestDto otherDto = (TestDto) other; + + return otherDto.id == this.id + && otherDto.value.equals(this.value) + && otherDto.longValue == this.longValue + && otherDto.byteValue == this.byteValue; + } + } + + @Override + public String toString() + { + return this.id + ", " + this.value + ", " + this.longValue + ", " + this.byteValue; + } + } diff --git a/core/src/test/java/tests/DhRepoSqliteTest.java b/core/src/test/java/tests/DhRepoSqliteTest.java index bba8ece67..0a2030a8a 100644 --- a/core/src/test/java/tests/DhRepoSqliteTest.java +++ b/core/src/test/java/tests/DhRepoSqliteTest.java @@ -82,14 +82,13 @@ public class DhRepoSqliteTest //===========// // insert - TestDto insertDto = new TestDto(0, "a"); + TestDto insertDto = new TestDto(0, "a", 0L, (byte) 0); testDataRepo.save(insertDto); // get TestDto getDto = testDataRepo.getByPrimaryKey("0"); Assert.assertNotNull("get failed, null returned", getDto); - Assert.assertEquals("get/insert failed, not equal", insertDto.id, getDto.id); - Assert.assertEquals("get/insert failed, not equal", insertDto.value, getDto.value); + Assert.assertEquals("get/insert failed, not equal", insertDto, getDto); // exists - DTO present Assert.assertTrue("DTO exists failed", testDataRepo.exists(insertDto)); @@ -97,14 +96,13 @@ public class DhRepoSqliteTest // update - TestDto updateMetaFile = new TestDto(0, "b"); + TestDto updateMetaFile = new TestDto(0, "b", Long.MAX_VALUE, Byte.MAX_VALUE); testDataRepo.save(updateMetaFile); // get getDto = testDataRepo.getByPrimaryKey("0"); Assert.assertNotNull("get failed, null returned", getDto); - Assert.assertEquals("get/insert failed, not equal", updateMetaFile.id, getDto.id); - Assert.assertEquals("get/insert failed, not equal", updateMetaFile.value, getDto.value); + Assert.assertEquals("get/insert failed, not equal", updateMetaFile, getDto); // delete