Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# hpqtypes-1.xx.x.x (xxxx-xx-xx)
* Add `FromRow`/`ToRow` instances for `Solo`

# hpqtypes-1.14.0.0 (2025-12-10)
* Make `begin`, `commit` and `rollback` do nothing instead of throwing an error
if the on demand connection acquisition mode is active.
Expand Down
7 changes: 7 additions & 0 deletions src/Database/PostgreSQL/PQTypes/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Proxy
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Data.Time
import Data.Tuple
import Data.UUID.Types
import Data.Word

Expand Down Expand Up @@ -157,6 +158,12 @@ instance
pqFormat = pqFormat @t
pqVariables = 1

instance
( PQFormat t
) => PQFormat (Solo t) where
pqFormat = pqFormat @t
pqVariables = 1

instance
( PQFormat t1, PQFormat t2
) => PQFormat (t1, t2) where
Expand Down
7 changes: 7 additions & 0 deletions src/Database/PostgreSQL/PQTypes/FromRow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Database.PostgreSQL.PQTypes.FromRow
import Control.Exception qualified as E
import Data.ByteString.Unsafe qualified as BS
import Data.Functor.Identity
import Data.Tuple
import Foreign.C
import Foreign.Marshal.Alloc
import Foreign.Ptr
Expand Down Expand Up @@ -84,6 +85,12 @@ instance FromSQL t => FromRow (Identity t) where
t <- peek p1 >>= convert res i b
pure (Identity t)

instance FromSQL t => FromRow (Solo t) where
fromRow res err b i = withFormat $ \fmt -> alloca $ \p1 -> do
verify err =<< c_PQgetf1 res err i fmt b p1
t <- peek p1 >>= convert res i b
pure (MkSolo t)

instance
( FromSQL t1, FromSQL t2
) => FromRow (t1, t2) where
Expand Down
7 changes: 7 additions & 0 deletions src/Database/PostgreSQL/PQTypes/ToRow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Database.PostgreSQL.PQTypes.ToRow

import Data.ByteString.Unsafe qualified as BS
import Data.Functor.Identity
import Data.Tuple
import Foreign.C
import Foreign.Marshal.Alloc
import Foreign.Ptr
Expand Down Expand Up @@ -61,6 +62,12 @@ instance ToSQL t => ToRow (Identity t) where
where
Identity t = row

instance ToSQL t => ToRow (Solo t) where
toRow row pa param err = withFormat row $ \fmt -> toSQL t pa $ \base ->
verify err =<< c_PQputf1 param err fmt base
where
t = getSolo row

instance
( ToSQL t1, ToSQL t2
) => ToRow (t1, t2) where
Expand Down
5 changes: 5 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Int
import Data.Maybe
import Data.Text qualified as T
import Data.Time
import Data.Tuple
import Data.Typeable
import Data.UUID.Types qualified as U
import Data.Word
Expand Down Expand Up @@ -112,6 +113,9 @@ instance Arbitrary Interval where
instance (Arbitrary a1, Arbitrary a2) => Arbitrary (a1 :*: a2) where
arbitrary = (:*:) <$> arbitrary <*> arbitrary

instance Arbitrary a => Arbitrary (Solo a) where
arbitrary = MkSolo <$> arbitrary

instance Arbitrary a => Arbitrary (Composite a) where
arbitrary = Composite <$> arbitrary

Expand Down Expand Up @@ -688,6 +692,7 @@ tests td =
, rowTest td (u :: Identity T.Text :*: (Double, Int16))
, rowTest td (u :: (T.Text, Double) :*: Identity Int16)
, rowTest td (u :: (Int16, T.Text, Int64, Double) :*: Identity Bool :*: (String0, AsciiChar))
, rowTest td (u :: Solo Int16)
, rowTest td (u :: (Int16, Int32))
, rowTest td (u :: (Int16, Int32, Int64))
, rowTest td (u :: (Int16, Int32, Int64, Float))
Expand Down
Loading