From 41908469677cda65a4c4b510bb39976cad62566f Mon Sep 17 00:00:00 2001 From: Alexey Khudyakov Date: Fri, 24 Jun 2016 00:18:55 +0300 Subject: [PATCH] Convert NULLs to NaN for Double/Floats SQLite stores NaN as NULL so we convert them back as NaN --- Database/SQLite/Simple/FromField.hs | 2 ++ test/ParamConv.hs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Database/SQLite/Simple/FromField.hs b/Database/SQLite/Simple/FromField.hs index 8821d23..f7779c5 100644 --- a/Database/SQLite/Simple/FromField.hs +++ b/Database/SQLite/Simple/FromField.hs @@ -145,10 +145,12 @@ instance FromField Word where instance FromField Double where fromField (Field (SQLFloat flt) _) = Ok flt + fromField (Field SQLNull _) = Ok (0/0) fromField f = returnError ConversionFailed f "expecting an SQLFloat column type" instance FromField Float where fromField (Field (SQLFloat flt) _) = Ok . double2Float $ flt + fromField (Field SQLNull _) = Ok (0/0) fromField f = returnError ConversionFailed f "expecting an SQLFloat column type" instance FromField Bool where diff --git a/test/ParamConv.hs b/test/ParamConv.hs index ccbc3aa..d0da1a1 100644 --- a/test/ParamConv.hs +++ b/test/ParamConv.hs @@ -37,6 +37,11 @@ testParamConvNull TestEnv{..} = TestCase $ do execute conn "INSERT INTO nulltype (id, t1) VALUES (?,?)" (two, "foo" :: String) [mr2] <- query_ conn "SELECT t1 FROM nulltype WHERE id = 2" :: IO [Only (Maybe String)] assertEqual "nulls" (Just "foo") (fromOnly mr2) + -- Test null conversion for Float/Double + [Only nanD] <- query_ conn "SELECT NULL" :: IO [Only Double] + assertBool "NULL for Double" $ isNaN nanD + [Only nanF] <- query_ conn "SELECT NULL" :: IO [Only Float] + assertBool "NULL for Float" $ isNaN nanF testParamConvInt :: TestEnv -> Test testParamConvInt TestEnv{..} = TestCase $ do