@@ -747,12 +747,25 @@ invalidOid :: Oid
747
747
invalidOid = Oid (# const InvalidOid )
748
748
749
749
750
+ -- | Prepare the given parameter bytestring for passing on to libpq,
751
+ -- without copying for binary parameters.
752
+ --
753
+ -- This safe to use to pass parameters to libpq considering:
754
+ -- * libpq treats the parameter data as read-only
755
+ -- * 'ByteString' uses pinned memory
756
+ -- * the reference to the 'CString' doesn't escape
757
+ unsafeUseParamAsCString :: (B. ByteString , Format ) -> (CString -> IO a ) -> IO a
758
+ unsafeUseParamAsCString (bs, format) =
759
+ case format of
760
+ Binary -> B. unsafeUseAsCString bs
761
+ Text -> B. useAsCString bs
762
+
750
763
withParams :: [Maybe (Oid , B. ByteString , Format )]
751
764
-> (CInt -> Ptr Oid -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a )
752
765
-> IO a
753
766
withParams params action =
754
767
withArray oids $ \ ts ->
755
- withMany (maybeWith B. useAsCString ) values $ \ c_values ->
768
+ withMany (maybeWith unsafeUseParamAsCString ) values $ \ c_values ->
756
769
withArray c_values $ \ vs ->
757
770
withArray c_lengths $ \ ls ->
758
771
withArrayLen formats $ \ n fs ->
@@ -767,7 +780,7 @@ withParams params action =
767
780
, 0 : d
768
781
)
769
782
accum (! a,! b,! c,! d) (Just (t,v,f)) = ( t: a
770
- , (Just v ): b
783
+ , (Just (v,f) ): b
771
784
, (toEnum $ B. length v): c
772
785
, (toEnum $ fromEnum f): d
773
786
)
@@ -776,7 +789,7 @@ withParamsPrepared :: [Maybe (B.ByteString, Format)]
776
789
-> (CInt -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a )
777
790
-> IO a
778
791
withParamsPrepared params action =
779
- withMany (maybeWith B. useAsCString ) values $ \ c_values ->
792
+ withMany (maybeWith unsafeUseParamAsCString ) values $ \ c_values ->
780
793
withArray c_values $ \ vs ->
781
794
withArray c_lengths $ \ ls ->
782
795
withArrayLen formats $ \ n fs ->
@@ -788,7 +801,7 @@ withParamsPrepared params action =
788
801
, 0 : b
789
802
, 0 : c
790
803
)
791
- accum (! a,! b,! c) (Just (v, f)) = ( (Just v ): a
804
+ accum (! a,! b,! c) (Just (v, f)) = ( (Just (v,f) ): a
792
805
, (toEnum $ B. length v): b
793
806
, (toEnum $ fromEnum f): c
794
807
)
0 commit comments