module Database.PostgreSQL.Simple.Notification
( Notification(..)
, getNotification
) where
import Control.Concurrent ( threadWaitRead )
import Control.Concurrent.MVar ( takeMVar, putMVar )
import qualified Data.ByteString as B
import Database.PostgreSQL.Simple.Internal
import qualified Database.PostgreSQL.LibPQ as PQ
import System.Posix.Types ( CPid )
data Notification = Notification
{ notificationPid :: CPid
, notificationChannel :: B.ByteString
, notificationData :: B.ByteString
}
errfd = "Database.PostgreSQL.Simple.Notification.getNotification: \
\failed to fetch file descriptor"
errconn = "Database.PostgreSQL.Simple.Notification.getNotification: \
\not connected"
lockConn :: Connection -> IO (PQ.Connection)
lockConn Connection{..} = do
mconn <- takeMVar connectionHandle
case mconn of
Nothing -> do
putMVar connectionHandle mconn
fail errconn
Just conn -> return conn
unlockConn :: Connection -> PQ.Connection -> IO ()
unlockConn Connection{..} c = putMVar connectionHandle (Just c)
getNotification :: Connection -> IO Notification
getNotification conn = do
c <- lockConn conn
loop conn c
where
loop conn c = do
mmsg <- PQ.notifies c
case mmsg of
Nothing -> do
mfd <- PQ.socket c
unlockConn conn c
case mfd of
Nothing -> fail errfd
Just fd -> do
threadWaitRead fd
c <- lockConn conn
_ <- PQ.consumeInput c
loop conn c
Just PQ.Notify{..} -> do
unlockConn conn c
return Notification { notificationPid = notifyBePid
, notificationChannel = notifyRelname
, notificationData = notifyExtra }