From 8a9e4b3e3bd06f5e166ce1e66e89b12b952a25f0 Mon Sep 17 00:00:00 2001 From: bernd Date: Wed, 14 Nov 2012 21:24:31 +0100 Subject: [PATCH 09/25] Fix DefaultTypeMappers for primitive values to return null when the result set contains SQL NULL and the result type supports it. --- .../net/lemnik/eodsql/impl/DefaultTypeMappers.java | 94 +++++++++++++++++----- eodsql/test/net/lemnik/eodsql/PrimitiveQuery.java | 6 +- .../net/lemnik/eodsql/SelectPrimitiveTest.java | 33 ++++++++ 3 files changed, 110 insertions(+), 23 deletions(-) diff --git a/eodsql/src/net/lemnik/eodsql/impl/DefaultTypeMappers.java b/eodsql/src/net/lemnik/eodsql/impl/DefaultTypeMappers.java index bde0d21..74260ad 100644 --- a/eodsql/src/net/lemnik/eodsql/impl/DefaultTypeMappers.java +++ b/eodsql/src/net/lemnik/eodsql/impl/DefaultTypeMappers.java @@ -23,20 +23,20 @@ class DefaultTypeMappers { static void register() { final Map typeMap = QueryTool.getTypeMap(); - typeMap.put(Byte.class, new ByteMapper()); - typeMap.put(Byte.TYPE, new ByteMapper()); - typeMap.put(Short.class, new ShortMapper()); - typeMap.put(Short.TYPE, new ShortMapper()); - typeMap.put(Integer.class, new IntegerMapper()); - typeMap.put(Integer.TYPE, new IntegerMapper()); - typeMap.put(Long.class, new LongMapper()); - typeMap.put(Long.TYPE, new LongMapper()); - typeMap.put(Float.class, new FloatMapper()); - typeMap.put(Float.TYPE, new FloatMapper()); - typeMap.put(Double.class, new DoubleMapper()); - typeMap.put(Double.TYPE, new DoubleMapper()); - typeMap.put(Boolean.class, new BooleanMapper()); - typeMap.put(Boolean.TYPE, new BooleanMapper()); + typeMap.put(Byte.class, new ByteMapper(true)); + typeMap.put(Byte.TYPE, new ByteMapper(false)); + typeMap.put(Short.class, new ShortMapper(true)); + typeMap.put(Short.TYPE, new ShortMapper(false)); + typeMap.put(Integer.class, new IntegerMapper(true)); + typeMap.put(Integer.TYPE, new IntegerMapper(false)); + typeMap.put(Long.class, new LongMapper(true)); + typeMap.put(Long.TYPE, new LongMapper(false)); + typeMap.put(Float.class, new FloatMapper(true)); + typeMap.put(Float.TYPE, new FloatMapper(false)); + typeMap.put(Double.class, new DoubleMapper(true)); + typeMap.put(Double.TYPE, new DoubleMapper(false)); + typeMap.put(Boolean.class, new BooleanMapper(true)); + typeMap.put(Boolean.TYPE, new BooleanMapper(false)); typeMap.put(Character.class, new CharacterMapper()); typeMap.put(Character.TYPE, new CharacterMapper()); typeMap.put(String.class, new StringMapper()); @@ -52,12 +52,19 @@ class DefaultTypeMappers { static class ByteMapper implements TypeMapper { + private final boolean mayReturnNull; + + ByteMapper(boolean mayReturnNull) { + this.mayReturnNull = mayReturnNull; + } + public Byte get( final ResultSet results, final int column) throws SQLException { - return Byte.valueOf(results.getByte(column)); + final byte result = results.getByte(column); + return (mayReturnNull && results.wasNull()) ? null : Byte.valueOf(result); } public void set( @@ -86,12 +93,19 @@ class DefaultTypeMappers { static class ShortMapper implements TypeMapper { + private final boolean mayReturnNull; + + ShortMapper(boolean mayReturnNull) { + this.mayReturnNull = mayReturnNull; + } + public Short get( final ResultSet results, final int column) throws SQLException { - return Short.valueOf(results.getShort(column)); + final short result = results.getShort(column); + return (mayReturnNull && results.wasNull()) ? null : Short.valueOf(result); } public void set( @@ -120,12 +134,19 @@ class DefaultTypeMappers { static class IntegerMapper implements TypeMapper { + private final boolean mayReturnNull; + + IntegerMapper(boolean mayReturnNull) { + this.mayReturnNull = mayReturnNull; + } + public Integer get( final ResultSet results, final int column) throws SQLException { - return Integer.valueOf(results.getInt(column)); + final int result = results.getInt(column); + return (mayReturnNull && results.wasNull()) ? null : Integer.valueOf(result); } public void set( @@ -154,12 +175,19 @@ class DefaultTypeMappers { static class LongMapper implements TypeMapper { + private final boolean mayReturnNull; + + LongMapper(boolean mayReturnNull) { + this.mayReturnNull = mayReturnNull; + } + public Long get( final ResultSet results, final int column) throws SQLException { - return Long.valueOf(results.getLong(column)); + final long result = results.getLong(column); + return (mayReturnNull && results.wasNull()) ? null : Long.valueOf(result); } public void set( @@ -188,12 +216,19 @@ class DefaultTypeMappers { static class FloatMapper implements TypeMapper { + private final boolean mayReturnNull; + + FloatMapper(boolean mayReturnNull) { + this.mayReturnNull = mayReturnNull; + } + public Float get( final ResultSet results, final int column) throws SQLException { - return Float.valueOf(results.getFloat(column)); + final float result = results.getFloat(column); + return (mayReturnNull && results.wasNull()) ? null : Float.valueOf(result); } public void set( @@ -222,12 +257,19 @@ class DefaultTypeMappers { static class DoubleMapper implements TypeMapper { + private final boolean mayReturnNull; + + DoubleMapper(boolean mayReturnNull) { + this.mayReturnNull = mayReturnNull; + } + public Double get( final ResultSet results, final int column) throws SQLException { - return Double.valueOf(results.getDouble(column)); + final double result = results.getDouble(column); + return (mayReturnNull && results.wasNull()) ? null : Double.valueOf(result); } public void set( @@ -256,12 +298,19 @@ class DefaultTypeMappers { static class BooleanMapper implements TypeMapper { + private final boolean mayReturnNull; + + BooleanMapper(boolean mayReturnNull) { + this.mayReturnNull = mayReturnNull; + } + public Boolean get( final ResultSet results, final int column) throws SQLException { - return Boolean.valueOf(results.getBoolean(column)); + final boolean result = results.getBoolean(column); + return (mayReturnNull && results.wasNull()) ? null : Boolean.valueOf(result); } public void set( @@ -498,7 +547,8 @@ class DefaultTypeMappers { final int column) throws SQLException { - return new BigInteger(results.getBytes(column)); + final byte[] bytes = results.getBytes(column); + return (bytes == null) ? null : new BigInteger(bytes); } public void set( diff --git a/eodsql/test/net/lemnik/eodsql/PrimitiveQuery.java b/eodsql/test/net/lemnik/eodsql/PrimitiveQuery.java index 6af7ab7..8f94df5 100644 --- a/eodsql/test/net/lemnik/eodsql/PrimitiveQuery.java +++ b/eodsql/test/net/lemnik/eodsql/PrimitiveQuery.java @@ -9,6 +9,7 @@ package net.lemnik.eodsql; import java.sql.SQLException; +import java.util.List; /** * @@ -18,7 +19,7 @@ public interface PrimitiveQuery extends BaseQuery { @Update( "INSERT INTO primitive_table (my_integer, my_double) VALUES (?1, ?2)") - public void insert(int intValue, double doubleValue); + public void insert(Integer intValue, Double doubleValue); @Select("SELECT MAX(my_integer) FROM primitive_table") public int maxInt(); @@ -32,6 +33,9 @@ public interface PrimitiveQuery extends BaseQuery { @Select("SELECT my_integer FROM primitive_table ORDER BY my_integer ASC") public int[] sortedInts(); + @Select("SELECT my_integer FROM primitive_table ORDER BY my_integer ASC") + public List sortedIntList(); + @Update("DELETE FROM primitive_table") public void deleteAll(); diff --git a/eodsql/test/net/lemnik/eodsql/SelectPrimitiveTest.java b/eodsql/test/net/lemnik/eodsql/SelectPrimitiveTest.java index 38f456b..946a184 100644 --- a/eodsql/test/net/lemnik/eodsql/SelectPrimitiveTest.java +++ b/eodsql/test/net/lemnik/eodsql/SelectPrimitiveTest.java @@ -1,5 +1,6 @@ package net.lemnik.eodsql; +import java.util.List; import java.util.Random; /** @@ -53,6 +54,38 @@ public class SelectPrimitiveTest extends EoDTestCase { assertEquals(0, array.length); } + public void testPrimitiveListWithNull() throws Exception { + query.deleteAll(); + query.insert(17, 3.0); + query.insert(-1, 2.0); + query.insert(null, 1.0); + + final List numbers = query.sortedIntList(); + + assertNotNull(numbers); + assertEquals(numbers.size(), 3); + + assertEquals(null, numbers.get(0)); + assertEquals(Integer.valueOf(-1), numbers.get(1)); + assertEquals(Integer.valueOf(17), numbers.get(2)); + } + + public void testPrimitiveArrayWithNull() throws Exception { + query.deleteAll(); + query.insert(17, 3.0); + query.insert(-1, 2.0); + query.insert(null, 1.0); + + final int[] numbers = query.sortedInts(); + + assertNotNull(numbers); + assertEquals(numbers.length, 3); + + assertEquals(0, numbers[0]); + assertEquals(-1, numbers[1]); + assertEquals(17, numbers[2]); + } + @Override protected void tearDown() throws Exception { query.dropPrimitiveTable(); -- 1.8.0