@@ -747,13 +747,26 @@ 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
-- | Convert a list of parameters to the format expected by libpq FFI calls.
751
764
withParams :: [Maybe (Oid , B. ByteString , Format )]
752
765
-> (CInt -> Ptr Oid -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a )
753
766
-> IO a
754
767
withParams params action =
755
768
withArray oids $ \ ts ->
756
- withMany (maybeWith B. useAsCString ) values $ \ c_values ->
769
+ withMany (maybeWith unsafeUseParamAsCString ) values $ \ c_values ->
757
770
withArray c_values $ \ vs ->
758
771
withArray c_lengths $ \ ls ->
759
772
withArrayLen formats $ \ n fs ->
@@ -768,7 +781,7 @@ withParams params action =
768
781
, 0 : d
769
782
)
770
783
accum (! a,! b,! c,! d) (Just (t,v,f)) = ( t: a
771
- , (Just v ): b
784
+ , (Just (v,f) ): b
772
785
, (toEnum $ B. length v): c
773
786
, (toEnum $ fromEnum f): d
774
787
)
@@ -779,7 +792,7 @@ withParamsPrepared :: [Maybe (B.ByteString, Format)]
779
792
-> (CInt -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a )
780
793
-> IO a
781
794
withParamsPrepared params action =
782
- withMany (maybeWith B. useAsCString ) values $ \ c_values ->
795
+ withMany (maybeWith unsafeUseParamAsCString ) values $ \ c_values ->
783
796
withArray c_values $ \ vs ->
784
797
withArray c_lengths $ \ ls ->
785
798
withArrayLen formats $ \ n fs ->
@@ -791,7 +804,7 @@ withParamsPrepared params action =
791
804
, 0 : b
792
805
, 0 : c
793
806
)
794
- accum (! a,! b,! c) (Just (v, f)) = ( (Just v ): a
807
+ accum (! a,! b,! c) (Just (v, f)) = ( (Just (v,f) ): a
795
808
, (toEnum $ B. length v): b
796
809
, (toEnum $ fromEnum f): c
797
810
)
0 commit comments