Add FromField Word

This commit is contained in:
Paul Brinkmeier 2023-09-17 05:31:32 +02:00
parent e307ee73d8
commit 83d2c07396
2 changed files with 21 additions and 0 deletions

View File

@ -56,6 +56,7 @@ instance FromField Char where
parseField = anyChar
-- | See https://www.postgresql.org/docs/current/datatype-numeric.html.
-- We assume that 'Int' has 64 bits. This is not guaranteed but reasonable enough.
instance FromField Int where
validOid Proxy = Oid.smallint \/ Oid.integer \/ Oid.bigint
parseField = signed decimal
@ -65,6 +66,10 @@ instance FromField Integer where
validOid Proxy = Oid.smallint \/ Oid.integer \/ Oid.bigint
parseField = signed decimal
instance FromField Word where
validOid Proxy = Oid.smallint \/ Oid.integer \/ Oid.bigint
parseField = decimal
doubleParser :: Parser Double
doubleParser = choice
[ string "NaN" $> nan

View File

@ -24,6 +24,12 @@ newtype AnInteger = AnInteger
instance FromRow AnInteger where
newtype AWord = AWord
{ word :: Word
} deriving (Eq, Generic, Show)
instance FromRow AWord where
newtype AText = AText
{ text :: Text
} deriving (Eq, Generic, Show)
@ -90,6 +96,16 @@ spec = do
it "Decodes bigint" $ \conn -> do
shouldFetch conn "SELECT pow(2, 48)::BIGINT AS integer" [AnInteger $ (2 :: Integer) ^ (48 :: Integer)]
describe "FromField Word" $ do
it "Decodes smallint" $ \conn -> do
shouldFetch conn "SELECT 42::SMALLINT AS word" [AWord 42]
it "Decodes integer" $ \conn -> do
shouldFetch conn "SELECT pow(2, 20)::INTEGER AS word" [AWord $ (2 :: Word) ^ (20 :: Word)]
it "Decodes bigint" $ \conn -> do
shouldFetch conn "SELECT pow(2, 48)::BIGINT AS word" [AWord $ (2 :: Word) ^ (48 :: Word)]
describe "FromField Text" $ do
it "Decodes text" $ \conn -> do
shouldFetch conn "SELECT 'Hello, World!'::TEXT AS text" [AText "Hello, World!"]