@@ -74,8 +74,10 @@ import Data.Foldable
74import Data.Function ((&))
74import Data.Function ((&))
75import Data.Kind
75import Data.Kind
76import Data.Monoid
76import Data.Monoid
77+import Data.Text (unpack)
77import Data.Traversable
78import Data.Traversable
78import Generics.SOP
79import Generics.SOP
80+import PostgreSQL.Binary.Encoding (encodingBytes)
80import qualified Database.PostgreSQL.LibPQ as LibPQ
82import qualified Database.PostgreSQL.LibPQ as LibPQ
@@ -393,7 +395,8 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
393 (UnsafeManipulation q :: Manipulation schema ps ys) (params :: x) =
395 (UnsafeManipulation q :: Manipulation schema ps ys) (params :: x) =
394 PQ $ \ (K conn) -> do
396 PQ $ \ (K conn) -> do
396− toParam' bytes = (LibPQ.invalidOid,bytes,LibPQ.Binary)
399+ (LibPQ.invalidOid, encodingBytes encoding, LibPQ.Binary)
397 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
400 params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
398 q' = q <> ";"
401 q' = q <> ";"
399 resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
402 resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
@@ -416,7 +419,7 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
416 "traversePrepared: LibPQ.prepare status " <> show status
419 "traversePrepared: LibPQ.prepare status " <> show status
417 results <- for list $ \ params -> do
420 results <- for list $ \ params -> do
419− toParam' bytes = (bytes,LibPQ.Binary)
422+ toParam' encoding = (encodingBytes encoding,LibPQ.Binary)
420 params' = fmap (fmap toParam') (hcollapse (toParams @x @xs params))
423 params' = fmap (fmap toParam') (hcollapse (toParams @x @xs params))
421 resultMaybe <- LibPQ.execPrepared conn temp params' LibPQ.Binary
424 resultMaybe <- LibPQ.execPrepared conn temp params' LibPQ.Binary
422 case resultMaybe of
425 case resultMaybe of
@@ -447,7 +450,7 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
447 "traversePrepared: LibPQ.prepare status " <> show status
450 "traversePrepared: LibPQ.prepare status " <> show status
448 for_ list $ \ params -> do
451 for_ list $ \ params -> do
450− toParam' bytes = (bytes,LibPQ.Binary)
453+ toParam' encoding = (encodingBytes encoding, LibPQ.Binary)
451 params' = fmap (fmap toParam') (hcollapse (toParams @x @xs params))
454 params' = fmap (fmap toParam') (hcollapse (toParams @x @xs params))
452 resultMaybe <- LibPQ.execPrepared conn temp params' LibPQ.Binary
455 resultMaybe <- LibPQ.execPrepared conn temp params' LibPQ.Binary
453 case resultMaybe of
456 case resultMaybe of
@@ -541,7 +544,9 @@ getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
541 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
544 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
542 case fromList row' of
545 case fromList row' of
543 Nothing -> error "getRow: found unexpected length"
546 Nothing -> error "getRow: found unexpected length"
544− Just row -> return $ fromRow @columns row
547+ Just row -> case fromRow @columns row of
548+ Left parseError -> error $ "fromRow: " <> unpack parseError
546-- | Intended to be used for unfolding in streaming libraries, `nextRow`
551-- | Intended to be used for unfolding in streaming libraries, `nextRow`
547-- takes a total number of rows (which can be found with `ntuples`)
552-- takes a total number of rows (which can be found with `ntuples`)
@@ -559,7 +564,9 @@ nextRow total (K result :: K LibPQ.Result columns) r
559 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
564 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
560 case fromList row' of
565 case fromList row' of
561 Nothing -> error "nextRow: found unexpected length"
566 Nothing -> error "nextRow: found unexpected length"
567+ Just row -> case fromRow @columns row of
568+ Left parseError -> error $ "nextRow: " <> unpack parseError
562− Just row -> return $ Just (r+1, fromRow @columns row)
569+ Right y -> return $ Just (r+1, y)
564-- | Get all rows from a `LibPQ.Result`.
571-- | Get all rows from a `LibPQ.Result`.
@@ -573,7 +580,9 @@ getRows (K result :: K LibPQ.Result columns) = liftBase $ do
573 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
580 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
574 case fromList row' of
581 case fromList row' of
575 Nothing -> error "getRows: found unexpected length"
582 Nothing -> error "getRows: found unexpected length"
576− Just row -> return $ fromRow @columns row
583+ Just row -> case fromRow @columns row of
584+ Left parseError -> error $ "getRows: " <> unpack parseError
578-- | Get the first row if possible from a `LibPQ.Result`.
587-- | Get the first row if possible from a `LibPQ.Result`.
@@ -587,7 +596,9 @@ firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
587 row' <- traverse (LibPQ.getvalue result 0) [0 .. len - 1]
596 row' <- traverse (LibPQ.getvalue result 0) [0 .. len - 1]
588 case fromList row' of
597 case fromList row' of
589 Nothing -> error "firstRow: found unexpected length"
598 Nothing -> error "firstRow: found unexpected length"
590− Just row -> return . Just $ fromRow @columns row
599+ Just row -> case fromRow @columns row of
600+ Left parseError -> error $ "firstRow: " <> unpack parseError
601+ Right y -> return $ Just y
592-- | Lifts actions on results from @LibPQ@.
603-- | Lifts actions on results from @LibPQ@.
593liftResult
604liftResult