-----------------------------------------------------------------------------
-- |
-- Module      :  Database.PostgreSQL.LibPQ.Internal
-- Copyright   :  (c) 2010 Grant Monroe,
--                (c) 2011 Leon P Smith
-- License     :  BSD3
--
-- This module exports the data constructor for the database connection
-- object so that people may create their own foreign bindings to libpq
-- functions that may not exist yet in vanilla postgresql-libpq.
--
-----------------------------------------------------------------------------

{-# LANGUAGE BangPatterns   #-}
{-# LANGUAGE EmptyDataDecls #-}

module Database.PostgreSQL.LibPQ.Internal (
    Connection (..),
    withConn,
    PGconn,
    CNoticeBuffer,
    NoticeBuffer,
) where

import Control.Concurrent.MVar (MVar)
import Foreign                 (ForeignPtr, Ptr, withForeignPtr)

-- | 'Connection' encapsulates a connection to the backend.
data Connection = Conn {-# UNPACK #-} !(ForeignPtr PGconn)
                       {-# UNPACK #-} !(MVar NoticeBuffer)

instance Eq Connection where
    (Conn ForeignPtr PGconn
c MVar NoticeBuffer
_) == :: Connection -> Connection -> Bool
== (Conn ForeignPtr PGconn
d MVar NoticeBuffer
_) = ForeignPtr PGconn
c ForeignPtr PGconn -> ForeignPtr PGconn -> Bool
forall a. Eq a => a -> a -> Bool
== ForeignPtr PGconn
d
    (Conn ForeignPtr PGconn
c MVar NoticeBuffer
_) /= :: Connection -> Connection -> Bool
/= (Conn ForeignPtr PGconn
d MVar NoticeBuffer
_) = ForeignPtr PGconn
c ForeignPtr PGconn -> ForeignPtr PGconn -> Bool
forall a. Eq a => a -> a -> Bool
/= ForeignPtr PGconn
d

withConn :: Connection
         -> (Ptr PGconn -> IO b)
         -> IO b
withConn :: forall b. Connection -> (Ptr PGconn -> IO b) -> IO b
withConn (Conn !ForeignPtr PGconn
fp MVar NoticeBuffer
_) Ptr PGconn -> IO b
f = ForeignPtr PGconn -> (Ptr PGconn -> IO b) -> IO b
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr PGconn
fp Ptr PGconn -> IO b
f
{-# INLINE withConn #-}

data PGconn

data CNoticeBuffer
type NoticeBuffer = Ptr CNoticeBuffer