-----------------------------------------------------------------------------
-- |
-- 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, EmptyDataDecls #-}

module Database.PostgreSQL.LibPQ.Internal where

import Foreign
import Control.Concurrent.MVar ( MVar )


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

instance Eq Connection where
    (Conn c :: ForeignPtr PGconn
c _) == :: Connection -> Connection -> Bool
== (Conn d :: ForeignPtr PGconn
d _) = ForeignPtr PGconn
c ForeignPtr PGconn -> ForeignPtr PGconn -> Bool
forall a. Eq a => a -> a -> Bool
== ForeignPtr PGconn
d
    (Conn c :: ForeignPtr PGconn
c _) /= :: Connection -> Connection -> Bool
/= (Conn d :: ForeignPtr PGconn
d _) = 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 :: Connection -> (Ptr PGconn -> IO b) -> IO b
withConn (Conn !ForeignPtr PGconn
fp _) f :: 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