Skip to content

Commit b468460

Browse files
From/ToSQL instances for Word16, Word32, Word64
1 parent 55dfd04 commit b468460

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

cabal.project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ packages: .
44
allow-newer: *:base
55
, *:ghc-prim
66
, *:template-haskell
7+
8+
tests: True

src/Database/PostgreSQL/PQTypes/Format.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ instance PQFormat Float where
8585
instance PQFormat Double where
8686
pqFormat = BS.pack "%float8"
8787

88+
instance PQFormat Word16 where
89+
pqFormat = BS.pack "%int2"
90+
91+
instance PQFormat Word32 where
92+
pqFormat = BS.pack "%int4"
93+
94+
instance PQFormat Word64 where
95+
pqFormat = BS.pack "%int8"
96+
8897
-- CHAR
8998

9099
instance PQFormat Char where

src/Database/PostgreSQL/PQTypes/FromSQL.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ instance FromSQL Double where
6868
fromSQL Nothing = unexpectedNULL
6969
fromSQL (Just n) = pure . realToFrac $ n
7070

71+
instance FromSQL Word16 where
72+
type PQBase Word16 = CUShort
73+
fromSQL Nothing = unexpectedNULL
74+
fromSQL (Just n) = pure . fromIntegral $ n
75+
76+
instance FromSQL Word32 where
77+
type PQBase Word32 = CUInt
78+
fromSQL Nothing = unexpectedNULL
79+
fromSQL (Just n) = pure . fromIntegral $ n
80+
81+
instance FromSQL Word64 where
82+
type PQBase Word64 = CULLong
83+
fromSQL Nothing = unexpectedNULL
84+
fromSQL (Just n) = pure . fromIntegral $ n
85+
7186
-- CHAR
7287

7388
instance FromSQL Char where

src/Database/PostgreSQL/PQTypes/ToSQL.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ instance ToSQL Double where
8484
type PQDest Double = CDouble
8585
toSQL n _ = putAsPtr (realToFrac n)
8686

87+
instance ToSQL Word16 where
88+
type PQDest Word16 = CUShort
89+
toSQL n _ = putAsPtr (fromIntegral n)
90+
91+
instance ToSQL Word32 where
92+
type PQDest Word32 = CUInt
93+
toSQL n _ = putAsPtr (fromIntegral n)
94+
95+
instance ToSQL Word64 where
96+
type PQDest Word64 = CULLong
97+
toSQL n _ = putAsPtr (fromIntegral n)
98+
8799
-- CHAR
88100

89101
instance ToSQL Char where

test/Main.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ tests td =
593593
, nullTest td (u :: Int64)
594594
, nullTest td (u :: Float)
595595
, nullTest td (u :: Double)
596+
, nullTest td (u :: Word16)
597+
, nullTest td (u :: Word32)
598+
, nullTest td (u :: Word64)
596599
, nullTest td (u :: Bool)
597600
, nullTest td (u :: AsciiChar)
598601
, nullTest td (u :: Word8)
@@ -618,6 +621,9 @@ tests td =
618621
, putGetTest td 100 (u :: Int64) (==)
619622
, putGetTest td 10000 (u :: Float) (==)
620623
, putGetTest td 10000 (u :: Double) (==)
624+
, putGetTest td 100 (u :: Word16) (==)
625+
, putGetTest td 100 (u :: Word32) (==)
626+
, putGetTest td 100 (u :: Word64) (==)
621627
, putGetTest td 100 (u :: Bool) (==)
622628
, putGetTest td 100 (u :: AsciiChar) (==)
623629
, putGetTest td 100 (u :: Word8) (==)
@@ -681,9 +687,9 @@ tests td =
681687
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple))
682688
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested))
683689
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested))
684-
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16))
685-
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32))
686-
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64))
690+
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Word16))
691+
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Word16, Word32))
692+
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Word16, Word32, Word64))
687693
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float))
688694
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double))
689695
, rowTest td (u :: (Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool, AsciiChar, Word8, String0, BS.ByteString, T.Text, BS.ByteString, Day, Array1 Int32, Composite Simple, CompositeArray1 Simple, Composite Nested, CompositeArray1 Nested, Int16, Int32, Int64, Float, Double, Bool))

test/Prelude/Instances.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,3 @@ deriving instance
505505
, Show a41, Show a42, Show a43, Show a44, Show a45, Show a46, Show a47, Show a48
506506
, Show a49, Show a50
507507
) => Show (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, a41, a42, a43, a44, a45, a46, a47, a48, a49, a50)
508-

0 commit comments

Comments
 (0)