@@ -246,6 +246,7 @@ import Data.Typeable
246
246
247
247
import Database.PostgreSQL.LibPQ.Compat
248
248
import Database.PostgreSQL.LibPQ.Internal
249
+ import Database.PostgreSQL.LibPQ.Marshal
249
250
250
251
#if __GLASGOW_HASKELL__ >= 700
251
252
import Control.Exception (mask_ )
@@ -750,27 +751,24 @@ withParams :: [Maybe (Oid, B.ByteString, Format)]
750
751
-> (CInt -> Ptr Oid -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a )
751
752
-> IO a
752
753
withParams params action =
753
- withArray oids $ \ ts ->
754
+ unsafeWithArray n oids $ \ ts ->
754
755
withMany (maybeWith B. useAsCString) values $ \ c_values ->
755
- withArray c_values $ \ vs ->
756
- withArray c_lengths $ \ ls ->
757
- withArray formats $ \ fs ->
758
- action n ts vs ls fs
756
+ unsafeWithArray n c_values $ \ vs ->
757
+ unsafeWithArray n c_lengths $ \ ls ->
758
+ unsafeWithArray n formats $ \ fs ->
759
+ action (intToCInt n) ts vs ls fs
759
760
where
760
- AccumParams oids values c_lengths formats =
761
- foldr accum (AccumParams [] [] [] [] ) params
762
-
763
- n :: CInt
764
- ! n = intToCInt $ length params
761
+ AccumParams n oids values c_lengths formats =
762
+ foldr accum (AccumParams 0 [] [] [] [] ) params
765
763
766
764
accum :: Maybe (Oid , B. ByteString , Format ) -> AccumParams -> AccumParams
767
- accum Nothing ~ (AccumParams a b c d) =
768
- AccumParams (invalidOid : a) (Nothing : b) (0 : c) (0 : d)
765
+ accum Nothing ~ (AccumParams i a b c d) =
766
+ AccumParams (i + 1 ) ( invalidOid : a) (Nothing : b) (0 : c) (0 : d)
769
767
770
- accum (Just (t,v,f)) ~ (AccumParams xs ys zs ws) =
771
- let z = intToCInt (B. length v)
772
- w = formatToCInt f
773
- in z `seq` w `seq` AccumParams (t : xs) (Just v : ys) (z : zs) (w : ws)
768
+ accum (Just (t,v,f)) ~ (AccumParams i xs ys zs ws) =
769
+ let ! z = intToCInt (B. length v)
770
+ ! w = formatToCInt f
771
+ in AccumParams (i + 1 ) (t : xs) (Just v : ys) (z : zs) (w : ws)
774
772
775
773
formatToCInt :: Format -> CInt
776
774
formatToCInt Text = 0
@@ -779,7 +777,7 @@ formatToCInt Binary = 1
779
777
intToCInt :: Int -> CInt
780
778
intToCInt = toEnum
781
779
782
- data AccumParams = AccumParams ! [Oid ] ! [Maybe B. ByteString ] ! [CInt ] ! [CInt ]
780
+ data AccumParams = AccumParams ! Int ! [Oid ] ! [Maybe B. ByteString ] ! [CInt ] ! [CInt ]
783
781
784
782
-- | Convert a list of parameters to the format expected by libpq FFI calls,
785
783
-- prepared statement variant.
@@ -788,27 +786,24 @@ withParamsPrepared :: [Maybe (B.ByteString, Format)]
788
786
-> IO a
789
787
withParamsPrepared params action =
790
788
withMany (maybeWith B. useAsCString) values $ \ c_values ->
791
- withArray c_values $ \ vs ->
792
- withArray c_lengths $ \ ls ->
793
- withArray formats $ \ fs ->
794
- action n vs ls fs
789
+ unsafeWithArray n c_values $ \ vs ->
790
+ unsafeWithArray n c_lengths $ \ ls ->
791
+ unsafeWithArray n formats $ \ fs ->
792
+ action (intToCInt n) vs ls fs
795
793
where
796
- AccumPrepParams values c_lengths formats =
797
- foldr accum (AccumPrepParams [] [] [] ) params
798
-
799
- n :: CInt
800
- n = intToCInt $ length params
794
+ AccumPrepParams n values c_lengths formats =
795
+ foldr accum (AccumPrepParams 0 [] [] [] ) params
801
796
802
797
accum :: Maybe (B. ByteString ,Format ) -> AccumPrepParams -> AccumPrepParams
803
- accum Nothing ~ (AccumPrepParams a b c) =
804
- AccumPrepParams (Nothing : a) (0 : b) (0 : c)
798
+ accum Nothing ~ (AccumPrepParams i a b c) =
799
+ AccumPrepParams (i + 1 ) ( Nothing : a) (0 : b) (0 : c)
805
800
806
- accum (Just (v, f)) ~ (AccumPrepParams xs ys zs) =
807
- let y = intToCInt (B. length v)
808
- z = formatToCInt f
809
- in y `seq` z `seq` AccumPrepParams (Just v : xs) (y : ys) (z : zs)
801
+ accum (Just (v, f)) ~ (AccumPrepParams i xs ys zs) =
802
+ let ! y = intToCInt (B. length v)
803
+ ! z = formatToCInt f
804
+ in AccumPrepParams (i + 1 ) (Just v : xs) (y : ys) (z : zs)
810
805
811
- data AccumPrepParams = AccumPrepParams ! [Maybe B. ByteString ] ! [CInt ] ! [CInt ]
806
+ data AccumPrepParams = AccumPrepParams ! Int ! [Maybe B. ByteString ] ! [CInt ] ! [CInt ]
812
807
813
808
-- | Submits a command to the server and waits for the result.
814
809
--
@@ -929,9 +924,8 @@ prepare connection stmtName query mParamTypes =
929
924
resultFromConn connection $ \ c ->
930
925
B. useAsCString stmtName $ \ s ->
931
926
B. useAsCString query $ \ q ->
932
- maybeWith withArray mParamTypes $ \ o ->
933
- let l = maybe 0 (intToCInt . length ) mParamTypes
934
- in c_PQprepare c s q l o
927
+ maybeWithInt withArrayLen mParamTypes $ \ l o ->
928
+ c_PQprepare c s q (intToCInt l) o
935
929
936
930
937
931
-- | Sends a request to execute a prepared statement with given
@@ -1726,9 +1720,8 @@ sendPrepare connection stmtName query mParamTypes =
1726
1720
enumFromConn connection $ \ c ->
1727
1721
B. useAsCString stmtName $ \ s ->
1728
1722
B. useAsCString query $ \ q ->
1729
- maybeWith withArray mParamTypes $ \ o ->
1730
- let l = maybe 0 (intToCInt . length ) mParamTypes
1731
- in c_PQsendPrepare c s q l o
1723
+ maybeWithInt withArrayLen mParamTypes $ \ l o ->
1724
+ c_PQsendPrepare c s q (intToCInt l) o
1732
1725
1733
1726
1734
1727
-- | Sends a request to execute a prepared statement with given
0 commit comments