JdbcTypesDefaultTransformer implements JdbcTypesTransformer { @Override public Object getColumnValue(ResultSet rs, int colIdx, Class<?> type) throws SQLException { if (type == String.class) return rs.getString(colIdx); if (type == int.class || type == Integer.class) { int res = rs.getInt(colIdx); return rs.wasNull() && type == Integer.class ? null : res; } if (type == long.class || type == Long.class) { long res = rs.getLong(colIdx); return rs.wasNull() && type == Long.class ? null : res; } if (type == double.class || type == Double.class) { double res = rs.getDouble(colIdx); return rs.wasNull() && type == Double.class ? null : res; } if (type == Date.class || type == java.util.Date.class) return rs.getDate(colIdx); if (type == Timestamp.class) return rs.getTimestamp(colIdx); if (type == Time.class) return rs.getTime(colIdx); if (type == boolean.class || type == Boolean.class) { boolean res = rs.getBoolean(colIdx); return rs.wasNull() && type == Boolean.class ? null : res; } if (type == byte.class || type == Byte.class) { byte res = rs.getByte(colIdx); return rs.wasNull() && type == Byte.class ? null : res; } if (type == short.class || type == Short.class) { short res = rs.getShort(colIdx); return rs.wasNull() && type == Short.class ? null : res; } if (type == float.class || type == Float.class) { float res = rs.getFloat(colIdx); return rs.wasNull() && type == Float.class ? null : res; } if (type == BigDecimal.class) return rs.getBigDecimal(colIdx); if (type == UUID.class) { Object res = rs.getObject(colIdx); if (res instanceof UUID) return res; if (res instanceof byte[]) { ByteBuffer bb = ByteBuffer.wrap((byte[])res); long most = bb.getLong(); long least = bb.getLong(); return new UUID(most, least); } if (res instanceof String) return UUID.fromString((String)res); } if (type.isEnum()) { if (NUMERIC_TYPES.contains(rs.getMetaData().getColumnType(colIdx))) { int ordinal = rs.getInt(colIdx); Object[] values = type.getEnumConstants(); return rs.wasNull() || ordinal >= values.length ? null : values[ordinal]; } String str = rs.getString(colIdx); try { return rs.wasNull() ? null : Enum.valueOf((Class<? extends Enum>) type, str.trim()); } catch (IllegalArgumentException ignore) { return null; } } return rs.getObject(colIdx); }  @Override Object getColumnValue(ResultSet rs, int colIdx, Class<?> type); static final JdbcTypesDefaultTransformer INSTANCE; }
@Test public void testTransformer() throws Exception { String jdbcUrl = "jdbc:gg-h2:mem:JdbcTypesDefaultTransformerTest"; String usr = "sa"; String pwd = ""; try (Connection conn = DriverManager.getConnection(jdbcUrl, usr, pwd)) { Statement stmt = conn.createStatement(); try { stmt.executeUpdate("DROP TABLE TEST_TRANSFORMER"); } catch (SQLException ignored) { } stmt.executeUpdate("CREATE TABLE TEST_TRANSFORMER(id INTEGER, " + "c1 BOOLEAN, c2 INTEGER, c3 TINYINT, c4 SMALLINT, c5 BIGINT, c6 DECIMAL(20, 2), c7 DOUBLE PRECISION, c8 REAL, " + "c9 TIME, c10 DATE, c11 TIMESTAMP, c12 VARCHAR(100), c13 UUID)"); stmt.executeUpdate("INSERT INTO TEST_TRANSFORMER(id, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13) " + "VALUES (1, true, 1, 2, 3, 4, 5.35, 6.15, 7.32, '00:01:08', '2016-01-01', '2016-01-01 00:01:08.296', " + "'100', '736bc956-090c-40d2-94da-916f2161f8a2')"); stmt.executeUpdate("INSERT INTO TEST_TRANSFORMER(id, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13) " + "VALUES (2, false, 10, 20, 30, 40, 50, 60, 70, current_time, current_date, current_timestamp, " + "'100.55', '736bc956-090c-40d2-94da-916f2161cdea')"); stmt.executeUpdate("INSERT INTO TEST_TRANSFORMER(id, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13) " + "VALUES (3, null, null, null, null, null, null, null, null, null, null, null, null, null)"); ResultSet rs = stmt.executeQuery("select c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13 from TEST_TRANSFORMER order by id"); assertTrue(rs.next()); JdbcTypesDefaultTransformer transformer = JdbcTypesDefaultTransformer.INSTANCE; assertTrue((Boolean)transformer.getColumnValue(rs, 1, boolean.class)); assertTrue((Boolean)transformer.getColumnValue(rs, 1, Boolean.class)); assertEquals(1, transformer.getColumnValue(rs, 1, int.class)); assertEquals(1.0, transformer.getColumnValue(rs, 1, double.class)); String s = (String)transformer.getColumnValue(rs, 1, String.class); assertTrue("true".equalsIgnoreCase(s) || "1".equals(s) || "t".equalsIgnoreCase(s)); assertEquals(1, transformer.getColumnValue(rs, 2, int.class)); assertEquals(1, transformer.getColumnValue(rs, 2, Integer.class)); assertEquals(1L, transformer.getColumnValue(rs, 2, Long.class)); assertEquals(1.0, transformer.getColumnValue(rs, 2, double.class)); assertEquals("1", transformer.getColumnValue(rs, 2, String.class)); byte b = 2; assertEquals(b, transformer.getColumnValue(rs, 3, byte.class)); assertEquals(b, transformer.getColumnValue(rs, 3, Byte.class)); assertEquals(2.0, transformer.getColumnValue(rs, 3, double.class)); assertEquals("2", transformer.getColumnValue(rs, 3, String.class)); short shrt = 3; assertEquals(shrt, transformer.getColumnValue(rs, 4, short.class)); assertEquals(shrt, transformer.getColumnValue(rs, 4, Short.class)); assertEquals(3.0, transformer.getColumnValue(rs, 4, double.class)); assertEquals("3", transformer.getColumnValue(rs, 4, String.class)); assertEquals(4L, transformer.getColumnValue(rs, 5, long.class)); assertEquals(4L, transformer.getColumnValue(rs, 5, Long.class)); assertEquals(4, transformer.getColumnValue(rs, 5, int.class)); assertEquals(4, transformer.getColumnValue(rs, 5, Integer.class)); assertEquals(4.0, transformer.getColumnValue(rs, 5, double.class)); assertEquals("4", transformer.getColumnValue(rs, 5, String.class)); assertEquals(new BigDecimal("4"), transformer.getColumnValue(rs, 5, BigDecimal.class)); assertEquals(new BigDecimal("5.35"), transformer.getColumnValue(rs, 6, BigDecimal.class)); assertEquals(5L, transformer.getColumnValue(rs, 6, long.class)); assertEquals("5.35", transformer.getColumnValue(rs, 6, String.class)); assertEquals(6.15, transformer.getColumnValue(rs, 7, double.class)); assertEquals(6.15, transformer.getColumnValue(rs, 7, Double.class)); assertEquals(6, transformer.getColumnValue(rs, 7, int.class)); assertEquals(6, transformer.getColumnValue(rs, 7, Integer.class)); assertTrue(transformer.getColumnValue(rs, 7, String.class).toString().startsWith("6.15")); assertTrue((7.32f - (Float)transformer.getColumnValue(rs, 8, float.class)) < 0.01); assertTrue((7.32f - (Float)transformer.getColumnValue(rs, 8, Float.class)) < 0.01); assertTrue((7.32 - (Double)transformer.getColumnValue(rs, 8, double.class)) < 0.01); assertTrue((7.32 - (Double)transformer.getColumnValue(rs, 8, Double.class)) < 0.01); assertEquals(7, transformer.getColumnValue(rs, 8, int.class)); assertEquals(7, transformer.getColumnValue(rs, 8, Integer.class)); assertTrue(transformer.getColumnValue(rs, 8, String.class).toString().startsWith("7.32")); assertTrue(transformer.getColumnValue(rs, 9, Time.class) instanceof Time); assertTrue(transformer.getColumnValue(rs, 9, String.class).toString().contains("00:01:08")); assertTrue(transformer.getColumnValue(rs, 10, Date.class) instanceof Date); assertTrue(transformer.getColumnValue(rs, 10, String.class).toString().startsWith("2016-01-01")); transformer.getColumnValue(rs, 11, Timestamp.class); assertTrue(transformer.getColumnValue(rs, 11, String.class).toString().startsWith("2016-01-01 00:01:08.29")); assertEquals("100", transformer.getColumnValue(rs, 12, String.class)); assertEquals(100, transformer.getColumnValue(rs, 12, int.class)); transformer.getColumnValue(rs, 13, UUID.class); assertEquals("736bc956-090c-40d2-94da-916f2161f8a2", transformer.getColumnValue(rs, 13, String.class)); assertTrue(rs.next()); assertFalse((Boolean)transformer.getColumnValue(rs, 1, boolean.class)); assertFalse((Boolean)transformer.getColumnValue(rs, 1, Boolean.class)); assertEquals(0, transformer.getColumnValue(rs, 1, int.class)); assertEquals(0.0, transformer.getColumnValue(rs, 1, double.class)); s = (String)transformer.getColumnValue(rs, 1, String.class); assertTrue("false".equalsIgnoreCase(s) || "0".equals(s) || "f".equalsIgnoreCase(s)); assertTrue(rs.next()); assertNotNull(transformer.getColumnValue(rs, 1, boolean.class)); assertNull(transformer.getColumnValue(rs, 1, Boolean.class)); assertEquals(0, transformer.getColumnValue(rs, 2, int.class)); assertNull(transformer.getColumnValue(rs, 2, Integer.class)); assertEquals((byte)0, transformer.getColumnValue(rs, 3, byte.class)); assertNull(transformer.getColumnValue(rs, 3, Byte.class)); assertEquals((short)0, transformer.getColumnValue(rs, 4, short.class)); assertNull(transformer.getColumnValue(rs, 4, Short.class)); assertEquals(0L, transformer.getColumnValue(rs, 5, long.class)); assertNull(transformer.getColumnValue(rs, 5, Long.class)); assertNull(transformer.getColumnValue(rs, 6, BigDecimal.class)); assertEquals(0d, transformer.getColumnValue(rs, 7, double.class)); assertNull(transformer.getColumnValue(rs, 7, Double.class)); assertEquals(0f, transformer.getColumnValue(rs, 8, float.class)); assertNull(transformer.getColumnValue(rs, 8, Float.class)); assertNull(transformer.getColumnValue(rs, 9, Time.class)); assertNull(transformer.getColumnValue(rs, 10, Date.class)); assertNull(transformer.getColumnValue(rs, 11, Timestamp.class)); assertNull(transformer.getColumnValue(rs, 12, String.class)); assertNull(transformer.getColumnValue(rs, 13, UUID.class)); } }