Skip to content

Commit b9f4378

Browse files
committed
exec, query: factor out common parameter handling helpers
There were two identical copies each of the parameter mangling code.
1 parent 7441ca8 commit b9f4378

File tree

1 file changed

+75
-89
lines changed

1 file changed

+75
-89
lines changed

src/Database/PostgreSQL/LibPQ.hsc

Lines changed: 75 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,58 @@ newtype Oid = Oid CUInt deriving (Eq, Ord, Read, Show, Storable, Typeable)
746746
invalidOid :: Oid
747747
invalidOid = Oid (#const InvalidOid)
748748

749+
750+
withParams :: [Maybe (Oid, B.ByteString, Format)]
751+
-> (CInt -> Ptr Oid -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a)
752+
-> IO a
753+
withParams params x =
754+
withArray oids $ \ts ->
755+
withMany (maybeWith B.useAsCString) values $ \c_values ->
756+
withArray c_values $ \vs ->
757+
withArray c_lengths $ \ls ->
758+
withArray formats $ \fs ->
759+
x n ts vs ls fs
760+
where
761+
(oids, values, lengths, formats) =
762+
foldl' accum ([],[],[],[]) $ reverse params
763+
!c_lengths = map toEnum lengths :: [CInt]
764+
!n = toEnum $ length params
765+
766+
accum (!a,!b,!c,!d) Nothing = ( invalidOid:a
767+
, Nothing:b
768+
, 0:c
769+
, 0:d
770+
)
771+
accum (!a,!b,!c,!d) (Just (t,v,f)) = ( t:a
772+
, (Just v):b
773+
, (B.length v):c
774+
, (toEnum $ fromEnum f):d
775+
)
776+
777+
withParamsPrepared :: [Maybe (B.ByteString, Format)]
778+
-> (CInt -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a)
779+
-> IO a
780+
withParamsPrepared params x =
781+
withMany (maybeWith B.useAsCString) values $ \c_values ->
782+
withArray c_values $ \vs ->
783+
withArray c_lengths $ \ls ->
784+
withArray formats $ \fs ->
785+
x n vs ls fs
786+
where
787+
(values, lengths, formats) = foldl' accum ([],[],[]) $ reverse params
788+
!c_lengths = map toEnum lengths :: [CInt]
789+
!n = toEnum $ length params
790+
791+
accum (!a,!b,!c) Nothing = ( Nothing:a
792+
, 0:b
793+
, 0:c
794+
)
795+
accum (!a,!b,!c) (Just (v, f)) = ( (Just v):a
796+
, (B.length v):b
797+
, (toEnum $ fromEnum f):c
798+
)
799+
800+
749801
-- | Submits a command to the server and waits for the result.
750802
--
751803
-- Returns a 'Result' or possibly 'Nothing'. A 'Result' will generally
@@ -812,31 +864,12 @@ execParams :: Connection -- ^ connection
812864
-> Format -- ^ result format
813865
-> IO (Maybe Result) -- ^ result
814866
execParams connection statement params rFmt =
815-
do let (oids, values, lengths, formats) =
816-
foldl' accum ([],[],[],[]) $ reverse params
817-
!c_lengths = map toEnum lengths :: [CInt]
818-
!n = toEnum $ length params
819-
!f = toEnum $ fromEnum rFmt
820-
resultFromConn connection $ \c ->
821-
B.useAsCString statement $ \s ->
822-
withArray oids $ \ts ->
823-
withMany (maybeWith B.useAsCString) values $ \c_values ->
824-
withArray c_values $ \vs ->
825-
withArray c_lengths $ \ls ->
826-
withArray formats $ \fs ->
827-
c_PQexecParams c s n ts vs ls fs f
828-
829-
where
830-
accum (!a,!b,!c,!d) Nothing = ( invalidOid:a
831-
, Nothing:b
832-
, 0:c
833-
, 0:d
834-
)
835-
accum (!a,!b,!c,!d) (Just (t,v,f)) = ( t:a
836-
, (Just v):b
837-
, (B.length v):c
838-
, (toEnum $ fromEnum f):d
839-
)
867+
resultFromConn connection $ \c ->
868+
B.useAsCString statement $ \s ->
869+
withParams params $ \n ts vs ls fs ->
870+
c_PQexecParams c s n ts vs ls fs f
871+
where
872+
!f = toEnum $ fromEnum rFmt
840873

841874

842875
-- | Submits a request to create a prepared statement with the given
@@ -911,28 +944,13 @@ execPrepared :: Connection -- ^ connection
911944
-> [Maybe (B.ByteString, Format)] -- ^ parameters
912945
-> Format -- ^ result format
913946
-> IO (Maybe Result) -- ^ result
914-
execPrepared connection stmtName mPairs rFmt =
915-
do let (values, lengths, formats) = foldl' accum ([],[],[]) $ reverse mPairs
916-
!c_lengths = map toEnum lengths :: [CInt]
917-
!n = toEnum $ length mPairs
918-
!f = toEnum $ fromEnum rFmt
919-
resultFromConn connection $ \c ->
920-
B.useAsCString stmtName $ \s ->
921-
withMany (maybeWith B.useAsCString) values $ \c_values ->
922-
withArray c_values $ \vs ->
923-
withArray c_lengths $ \ls ->
924-
withArray formats $ \fs ->
925-
c_PQexecPrepared c s n vs ls fs f
926-
947+
execPrepared connection stmtName params rFmt =
948+
resultFromConn connection $ \c ->
949+
B.useAsCString stmtName $ \s ->
950+
withParamsPrepared params $ \n vs ls fs ->
951+
c_PQexecPrepared c s n vs ls fs f
927952
where
928-
accum (!a,!b,!c) Nothing = ( Nothing:a
929-
, 0:b
930-
, 0:c
931-
)
932-
accum (!a,!b,!c) (Just (v, f)) = ( (Just v):a
933-
, (B.length v):b
934-
, (toEnum $ fromEnum f):c
935-
)
953+
!f = toEnum $ fromEnum rFmt
936954

937955

938956
-- | Submits a request to obtain information about the specified
@@ -1676,31 +1694,13 @@ sendQueryParams :: Connection
16761694
-> Format
16771695
-> IO Bool
16781696
sendQueryParams connection statement params rFmt =
1679-
do let (oids, values, lengths, formats) =
1680-
foldl' accum ([],[],[],[]) $ reverse params
1681-
!c_lengths = map toEnum lengths :: [CInt]
1682-
!n = toEnum $ length params
1683-
!f = toEnum $ fromEnum rFmt
1684-
enumFromConn connection $ \c ->
1685-
B.useAsCString statement $ \s ->
1686-
withArray oids $ \ts ->
1687-
withMany (maybeWith B.useAsCString) values $ \c_values ->
1688-
withArray c_values $ \vs ->
1689-
withArray c_lengths $ \ls ->
1690-
withArray formats $ \fs ->
1691-
c_PQsendQueryParams c s n ts vs ls fs f
1697+
enumFromConn connection $ \c ->
1698+
B.useAsCString statement $ \s ->
1699+
withParams params $ \n ts vs ls fs ->
1700+
c_PQsendQueryParams c s n ts vs ls fs f
16921701

16931702
where
1694-
accum (!a,!b,!c,!d) Nothing = ( invalidOid:a
1695-
, Nothing:b
1696-
, 0:c
1697-
, 0:d
1698-
)
1699-
accum (!a,!b,!c,!d) (Just (t,v,f)) = ( t:a
1700-
, (Just v):b
1701-
, (B.length v):c
1702-
, (toEnum $ fromEnum f):d
1703-
)
1703+
!f = toEnum $ fromEnum rFmt
17041704

17051705

17061706
-- | Sends a request to create a prepared statement with the given
@@ -1726,28 +1726,14 @@ sendQueryPrepared :: Connection
17261726
-> [Maybe (B.ByteString, Format)]
17271727
-> Format
17281728
-> IO Bool
1729-
sendQueryPrepared connection stmtName mPairs rFmt =
1730-
do let (values, lengths, formats) = foldl' accum ([],[],[]) $ reverse mPairs
1731-
!c_lengths = map toEnum lengths :: [CInt]
1732-
!n = toEnum $ length mPairs
1733-
!f = toEnum $ fromEnum rFmt
1734-
enumFromConn connection $ \c ->
1735-
B.useAsCString stmtName $ \s ->
1736-
withMany (maybeWith B.useAsCString) values $ \c_values ->
1737-
withArray c_values $ \vs ->
1738-
withArray c_lengths $ \ls ->
1739-
withArray formats $ \fs ->
1740-
c_PQsendQueryPrepared c s n vs ls fs f
1729+
sendQueryPrepared connection stmtName params rFmt =
1730+
enumFromConn connection $ \c ->
1731+
B.useAsCString stmtName $ \s ->
1732+
withParamsPrepared params $ \n vs ls fs ->
1733+
c_PQsendQueryPrepared c s n vs ls fs f
17411734

17421735
where
1743-
accum (!a,!b,!c) Nothing = ( Nothing:a
1744-
, 0:b
1745-
, 0:c
1746-
)
1747-
accum (!a,!b,!c) (Just (v, f)) = ( (Just v):a
1748-
, (B.length v):b
1749-
, (toEnum $ fromEnum f):c
1750-
)
1736+
!f = toEnum $ fromEnum rFmt
17511737

17521738

17531739
-- | Submits a request to obtain information about the specified

0 commit comments

Comments
 (0)