Skip to content

Commit fc19817

Browse files
authored
Merge pull request #35 from haskellari/less-hsc
Less hsc2hs: Convert main module into ordinary Haskell file
2 parents 509f0e9 + cae2893 commit fc19817

File tree

5 files changed

+84
-45
lines changed

5 files changed

+84
-45
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 renamed to src/Database/PostgreSQL/LibPQ.hs

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
--
3838
-----------------------------------------------------------------------------
3939

40+
{-# LANGUAGE CPP #-}
4041
{-# LANGUAGE ForeignFunctionInterface #-}
4142
{-# LANGUAGE EmptyDataDecls #-}
4243
{-# LANGUAGE OverloadedStrings #-}
@@ -212,10 +213,6 @@ module Database.PostgreSQL.LibPQ
212213
)
213214
where
214215

215-
#include <libpq-fe.h>
216-
#include <libpq/libpq-fs.h>
217-
#include "noticehandlers.h"
218-
219216
import Prelude hiding ( print )
220217
import Foreign
221218
import Foreign.C.Types
@@ -242,12 +239,12 @@ import qualified Data.ByteString as B
242239

243240
import Control.Concurrent.MVar
244241

245-
import Data.Typeable
246-
247242
import Database.PostgreSQL.LibPQ.Compat
248243
import Database.PostgreSQL.LibPQ.Enums
249244
import Database.PostgreSQL.LibPQ.Internal
250245
import Database.PostgreSQL.LibPQ.Marshal
246+
import Database.PostgreSQL.LibPQ.Notify
247+
import Database.PostgreSQL.LibPQ.Oid
251248

252249
#if __GLASGOW_HASKELL__ >= 700
253250
import Control.Exception (mask_)
@@ -699,12 +696,6 @@ connectionUsedPassword connection =
699696
newtype Result = Result (ForeignPtr PGresult) deriving (Eq, Show)
700697
data PGresult
701698

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

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
17471733

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'
17711734

17721735

17731736
-- | Returns the next notification from a list of unhandled
@@ -1915,8 +1878,6 @@ maybeBsFromForeignPtr fp f =
19151878

19161879
type NoticeReceiver = NoticeBuffer -> Ptr PGresult -> IO ()
19171880

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

19601921
-- $largeobjects
19611922

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)