Implement FromField Bool
This commit is contained in:
parent
66202b1e34
commit
60f7f1c091
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
- [x] Implement `String` and `Text` decoding
|
- [x] Implement `String` and `Text` decoding
|
||||||
- [x] Implement `Int` decoding
|
- [x] Implement `Int` decoding
|
||||||
- [ ] Implement error reporting i.e. use `Either OpiumError` instead of `Maybe`
|
- [x] Implement error reporting i.e. use `Either OpiumError` instead of `Maybe`
|
||||||
- [ ] Implement `Float` and `Double` decoding
|
- [x] Implement `Float` and `Double` decoding
|
||||||
- [ ] Implement `UTCTime` and zoned time decoding
|
- [ ] Implement `UTCTime` and zoned time decoding
|
||||||
|
- [ ] Implement `ByteString` decoding (`bytea`)
|
||||||
|
- Can we make let the fromField instance choose whether it wants binary or text?
|
||||||
|
@ -85,3 +85,15 @@ instance FromField Double where
|
|||||||
fromField = fromParser
|
fromField = fromParser
|
||||||
(Oid.real \/ Oid.doublePrecision)
|
(Oid.real \/ Oid.doublePrecision)
|
||||||
floatParser
|
floatParser
|
||||||
|
|
||||||
|
boolParser :: Parser Bool
|
||||||
|
boolParser = choice
|
||||||
|
[ string "t" $> True
|
||||||
|
, string "f" $> False
|
||||||
|
]
|
||||||
|
|
||||||
|
-- | See https://www.postgresql.org/docs/current/datatype-boolean.html.
|
||||||
|
instance FromField Bool where
|
||||||
|
fromField = fromParser
|
||||||
|
Oid.boolean
|
||||||
|
boolParser
|
||||||
|
@ -39,3 +39,7 @@ real = eq $ Oid 700
|
|||||||
-- | 64-bit IEEE float
|
-- | 64-bit IEEE float
|
||||||
doublePrecision :: Oid -> Bool
|
doublePrecision :: Oid -> Bool
|
||||||
doublePrecision = eq $ Oid 701
|
doublePrecision = eq $ Oid 701
|
||||||
|
|
||||||
|
-- | Boolean
|
||||||
|
boolean :: Oid -> Bool
|
||||||
|
boolean = eq $ Oid 16
|
||||||
|
@ -42,6 +42,12 @@ newtype ADouble = ADouble
|
|||||||
|
|
||||||
instance FromRow ADouble where
|
instance FromRow ADouble where
|
||||||
|
|
||||||
|
newtype ABool = ABool
|
||||||
|
{ bool :: Bool
|
||||||
|
} deriving (Eq, Generic, Show)
|
||||||
|
|
||||||
|
instance FromRow ABool where
|
||||||
|
|
||||||
shouldFetch :: (Eq a, FromRow a, Show a) => Connection -> ByteString -> [a] -> IO ()
|
shouldFetch :: (Eq a, FromRow a, Show a) => Connection -> ByteString -> [a] -> IO ()
|
||||||
shouldFetch conn query expectedRows = do
|
shouldFetch conn query expectedRows = do
|
||||||
actualRows <- Opium.fetch_ conn query
|
actualRows <- Opium.fetch_ conn query
|
||||||
@ -125,3 +131,16 @@ spec = do
|
|||||||
|
|
||||||
Right [ADouble value1] <- Opium.fetch_ conn "SELECT '-inf'::double precision AS double"
|
Right [ADouble value1] <- Opium.fetch_ conn "SELECT '-inf'::double precision AS double"
|
||||||
value1 `shouldSatisfy` (isInfinite /\ (< 0))
|
value1 `shouldSatisfy` (isInfinite /\ (< 0))
|
||||||
|
|
||||||
|
describe "FromField Bool" $ do
|
||||||
|
it "Decodes boolean" $ \conn -> do
|
||||||
|
shouldFetch conn "SELECT true AS bool" [ABool True]
|
||||||
|
shouldFetch conn "SELECT 't'::boolean AS bool" [ABool True]
|
||||||
|
shouldFetch conn "SELECT 'yes'::boolean AS bool" [ABool True]
|
||||||
|
shouldFetch conn "SELECT 'on'::boolean AS bool" [ABool True]
|
||||||
|
shouldFetch conn "SELECT 1::boolean AS bool" [ABool True]
|
||||||
|
shouldFetch conn "SELECT false AS bool" [ABool False]
|
||||||
|
shouldFetch conn "SELECT 'f'::boolean AS bool" [ABool False]
|
||||||
|
shouldFetch conn "SELECT 'no'::boolean AS bool" [ABool False]
|
||||||
|
shouldFetch conn "SELECT 'off'::boolean AS bool" [ABool False]
|
||||||
|
shouldFetch conn "SELECT 0::boolean AS bool" [ABool False]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user