From 5f71b4c044a382b89bd05577ef6fed86ed593c31 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 16 Apr 2015 19:11:57 -0400 Subject: [PATCH] Allow SQLite the chance to deserialize DateTime objects --- src/main/java/com/iciql/SQLDialectSQLite.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main/java/com/iciql/SQLDialectSQLite.java b/src/main/java/com/iciql/SQLDialectSQLite.java index 4234e9f..4777234 100644 --- a/src/main/java/com/iciql/SQLDialectSQLite.java +++ b/src/main/java/com/iciql/SQLDialectSQLite.java @@ -16,6 +16,13 @@ package com.iciql; +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; + +import com.iciql.Iciql.DataTypeAdapter; import com.iciql.TableDefinition.FieldDefinition; import com.iciql.TableDefinition.IndexDefinition; import com.iciql.util.IciqlLogger; @@ -135,4 +142,42 @@ public class SQLDialectSQLite extends SQLDialectDefault { stat.setSQL(buff.toString()); } + @Override + public Object deserialize(ResultSet rs, int columnIndex, Class targetType, Class> typeAdapter) { + try { + return super.deserialize(rs, columnIndex, targetType, typeAdapter); + } catch (IciqlException e) { + if (typeAdapter == null && e.getMessage().startsWith("Can not convert")) { + try { + // give the SQLite JDBC driver an opportunity to deserialize DateTime objects + if (Timestamp.class.equals(targetType)) { + return rs.getTimestamp(columnIndex); + } else if (Time.class.equals(targetType)) { + return rs.getTime(columnIndex); + } else if (Date.class.equals(targetType)) { + return rs.getDate(columnIndex); + } else if (java.util.Date.class.equals(targetType)) { + Timestamp timestamp = rs.getTimestamp(columnIndex); + return new java.util.Date(timestamp.getTime()); + } + } catch (SQLException x) { + throw new IciqlException(x, "Can not convert the value at column {0} to {1}", + columnIndex, targetType.getName()); + } + } + + // rethrow e + throw e; + } + } + + @Override + public String prepareStringParameter(Object o) { + if (o instanceof Boolean) { + // SQLite does not have an explicit BOOLEAN type + Boolean bool = (Boolean) o; + return bool ? "1" : "0"; + } + return super.prepareStringParameter(o); + } } \ No newline at end of file -- 2.39.5