Skip to content

Commit 6c4710b

Browse files
committed
Move Oid and Notify to own modules
1 parent 509f0e9 commit 6c4710b

File tree

5 files changed

+83
-41
lines changed

5 files changed

+83
-41
lines changed

postgresql-libpq.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ library
6767
Database.PostgreSQL.LibPQ.Compat
6868
Database.PostgreSQL.LibPQ.Enums
6969
Database.PostgreSQL.LibPQ.Marshal
70+
Database.PostgreSQL.LibPQ.Notify
71+
Database.PostgreSQL.LibPQ.Oid
7072

7173
build-depends:
7274
base >=4.3 && <4.18

src/Database/PostgreSQL/LibPQ.hsc

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ import qualified Data.ByteString as B
242242

243243
import Control.Concurrent.MVar
244244

245-
import Data.Typeable
246-
247245
import Database.PostgreSQL.LibPQ.Compat
248246
import Database.PostgreSQL.LibPQ.Enums
249247
import Database.PostgreSQL.LibPQ.Internal
250248
import Database.PostgreSQL.LibPQ.Marshal
249+
import Database.PostgreSQL.LibPQ.Notify
250+
import Database.PostgreSQL.LibPQ.Oid
251251

252252
#if __GLASGOW_HASKELL__ >= 700
253253
import Control.Exception (mask_)
@@ -699,12 +699,6 @@ connectionUsedPassword connection =
699699
newtype Result = Result (ForeignPtr PGresult) deriving (Eq, Show)
700700
data PGresult
701701

702-
703-
newtype Oid = Oid CUInt deriving (Eq, Ord, Read, Show, Storable, Typeable)
704-
705-
invalidOid :: Oid
706-
invalidOid = Oid (#const InvalidOid)
707-
708702
-- | Convert a list of parameters to the format expected by libpq FFI calls.
709703
withParams :: [Maybe (Oid, B.ByteString, Format)]
710704
-> (CInt -> Ptr Oid -> Ptr CString -> Ptr CInt -> Ptr CInt -> IO a)
@@ -1739,35 +1733,7 @@ cancel (Cancel fp) =
17391733
-- ordinary SQL commands. The arrival of NOTIFY messages can
17401734
-- subsequently be detected by calling 'notifies'.
17411735

1742-
data Notify = Notify {
1743-
notifyRelname :: {-# UNPACK #-} !B.ByteString -- ^ notification channel name
1744-
, notifyBePid :: {-# UNPACK #-} !CPid -- ^ process ID of notifying server process
1745-
, notifyExtra :: {-# UNPACK #-} !B.ByteString -- ^ notification payload string
1746-
} deriving Show
17471736

1748-
#if __GLASGOW_HASKELL__ < 800
1749-
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
1750-
#endif
1751-
instance Storable Notify where
1752-
sizeOf _ = #{size PGnotify}
1753-
1754-
alignment _ = #{alignment PGnotify}
1755-
1756-
peek ptr = do
1757-
relname <- B.packCString =<< #{peek PGnotify, relname} ptr
1758-
extra <- B.packCString =<< #{peek PGnotify, extra} ptr
1759-
be_pid <- fmap f $ #{peek PGnotify, be_pid} ptr
1760-
return $! Notify relname be_pid extra
1761-
where
1762-
f :: CInt -> CPid
1763-
f = fromIntegral
1764-
1765-
poke ptr (Notify a b c) =
1766-
B.useAsCString a $ \a' ->
1767-
B.useAsCString c $ \c' ->
1768-
do #{poke PGnotify, relname} ptr a'
1769-
#{poke PGnotify, be_pid} ptr (fromIntegral b :: CInt)
1770-
#{poke PGnotify, extra} ptr c'
17711737

17721738

17731739
-- | Returns the next notification from a list of unhandled
@@ -1915,8 +1881,6 @@ maybeBsFromForeignPtr fp f =
19151881

19161882
type NoticeReceiver = NoticeBuffer -> Ptr PGresult -> IO ()
19171883

1918-
data PGnotice
1919-
19201884
-- | Upon connection initialization, any notices received from the server are
19211885
-- normally written to the console. Notices are akin to warnings, and
19221886
-- are distinct from notifications. This function suppresses notices.
@@ -1954,8 +1918,8 @@ getNotice (Conn _ nbRef) =
19541918
then return Nothing
19551919
else do
19561920
fp <- newForeignPtr finalizerFree (castPtr np)
1957-
len <- #{peek PGnotice, len} np
1958-
return $! Just $! mkPS fp (#offset PGnotice, str) len
1921+
len <- pgNoticePeekLen np
1922+
return $! Just $! mkPS fp pgNoticeOffsetStr (fromIntegral len)
19591923

19601924
-- $largeobjects
19611925

src/Database/PostgreSQL/LibPQ/Internal.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
{-# LANGUAGE BangPatterns #-}
1515
{-# LANGUAGE EmptyDataDecls #-}
1616

17-
module Database.PostgreSQL.LibPQ.Internal where
17+
module Database.PostgreSQL.LibPQ.Internal (
18+
Connection (..),
19+
withConn,
20+
PGconn,
21+
CNoticeBuffer,
22+
NoticeBuffer,
23+
) where
1824

1925
import Control.Concurrent.MVar (MVar)
2026
import Foreign
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module Database.PostgreSQL.LibPQ.Notify where
2+
3+
#include <libpq-fe.h>
4+
#include "noticehandlers.h"
5+
6+
import Foreign (Ptr, Storable (..))
7+
import Foreign.C.Types (CInt, CSize)
8+
import System.Posix.Types (CPid)
9+
10+
import qualified Data.ByteString as B
11+
12+
-------------------------------------------------------------------------------
13+
-- Notify
14+
-------------------------------------------------------------------------------
15+
16+
data Notify = Notify {
17+
notifyRelname :: {-# UNPACK #-} !B.ByteString -- ^ notification channel name
18+
, notifyBePid :: {-# UNPACK #-} !CPid -- ^ process ID of notifying server process
19+
, notifyExtra :: {-# UNPACK #-} !B.ByteString -- ^ notification payload string
20+
} deriving Show
21+
22+
#if __GLASGOW_HASKELL__ < 800
23+
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
24+
#endif
25+
instance Storable Notify where
26+
sizeOf _ = #{size PGnotify}
27+
28+
alignment _ = #{alignment PGnotify}
29+
30+
peek ptr = do
31+
relname <- B.packCString =<< #{peek PGnotify, relname} ptr
32+
extra <- B.packCString =<< #{peek PGnotify, extra} ptr
33+
be_pid <- fmap f $ #{peek PGnotify, be_pid} ptr
34+
return $! Notify relname be_pid extra
35+
where
36+
f :: CInt -> CPid
37+
f = fromIntegral
38+
39+
poke ptr (Notify a b c) =
40+
B.useAsCString a $ \a' ->
41+
B.useAsCString c $ \c' ->
42+
do #{poke PGnotify, relname} ptr a'
43+
#{poke PGnotify, be_pid} ptr (fromIntegral b :: CInt)
44+
#{poke PGnotify, extra} ptr c'
45+
46+
-------------------------------------------------------------------------------
47+
-- Notice
48+
-------------------------------------------------------------------------------
49+
50+
data PGnotice
51+
52+
pgNoticePeekLen :: Ptr PGnotice -> IO CSize
53+
pgNoticePeekLen = #{peek PGnotice, len}
54+
55+
pgNoticeOffsetStr :: Int
56+
pgNoticeOffsetStr = #{offset PGnotice, str}

src/Database/PostgreSQL/LibPQ/Oid.hsc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE DeriveDataTypeable #-}
2+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3+
module Database.PostgreSQL.LibPQ.Oid where
4+
5+
#include <libpq-fe.h>
6+
7+
import Data.Typeable (Typeable)
8+
import Foreign.C.Types (CUInt)
9+
import Foreign.Storable (Storable)
10+
11+
newtype Oid = Oid CUInt deriving (Eq, Ord, Read, Show, Storable, Typeable)
12+
13+
invalidOid :: Oid
14+
invalidOid = Oid (#const InvalidOid)

0 commit comments

Comments
 (0)