Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Eq, Show instances, use safe foreign imports, and add utility module #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 80 additions & 76 deletions Database/PQ.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ data PollingStatus
= PollingFailed
| PollingReading
| PollingWriting
| PollingOk deriving Show
| PollingOk deriving (Eq, Show)

pollHelper :: (Ptr PGconn -> IO CInt)
-> Connection
Expand Down Expand Up @@ -398,7 +398,7 @@ data ConnStatus
| ConnectionSetEnv -- ^ Negotiating environment-driven
-- parameter settings.
| ConnectionSSLStartup -- ^ Negotiating SSL encryption.
deriving Show
deriving (Eq, Show)


-- | Returns the status of the connection.
Expand Down Expand Up @@ -436,6 +436,7 @@ data TransactionStatus = TransIdle -- ^ currently idle
| TransInTrans -- ^ idle, in a valid transaction block
| TransInError -- ^ idle, in a failed transaction block
| TransUnknown -- ^ the connection is bad
deriving (Eq, Show)

-- | Returns the current in-transaction status of the server.
--
Expand Down Expand Up @@ -580,7 +581,7 @@ connectionUsedPassword connection =
newtype Result = Result (ForeignPtr PGresult) deriving (Eq, Show)
data PGresult

data Format = Text | Binary deriving Enum
data Format = Text | Binary deriving (Enum, Eq, Show)

type Oid = CUInt

Expand Down Expand Up @@ -835,7 +836,7 @@ data ExecStatus = EmptyQuery -- ^ The string sent to the server was empty.
| NonfatalError -- ^ A nonfatal error (a notice or
-- warning) occurred.
| FatalError -- ^ A fatal error occurred.
deriving Show
deriving (Eq, Show)

instance Enum ExecStatus where
toEnum (#const PGRES_EMPTY_QUERY) = EmptyQuery
Expand Down Expand Up @@ -952,6 +953,8 @@ data FieldCode = DiagSeverity
-- ^ The name of the source-code function reporting the
-- error.

deriving (Eq, Show)


instance Enum FieldCode where
toEnum (#const PG_DIAG_SEVERITY) = DiagSeverity
Expand Down Expand Up @@ -1651,6 +1654,7 @@ isnonblocking connection = enumFromConn connection c_PQisnonblocking
data FlushStatus = FlushOk
| FlushFailed
| FlushWriting
deriving (Eq, Show)

-- | Attempts to flush any queued output data to the server. Returns
-- 'FlushOk' if successful (or if the send queue is empty),
Expand Down Expand Up @@ -1984,141 +1988,141 @@ maybeBsFromForeignPtr fp f =
foreign import ccall safe "libpq-fe.h PQconnectdb"
c_PQconnectdb :: CString ->IO (Ptr PGconn)

foreign import ccall unsafe "libpq-fe.h PQconnectStart"
foreign import ccall safe "libpq-fe.h PQconnectStart"
c_PQconnectStart :: CString ->IO (Ptr PGconn)

foreign import ccall unsafe "libpq-fe.h PQconnectPoll"
foreign import ccall safe "libpq-fe.h PQconnectPoll"
c_PQconnectPoll :: Ptr PGconn ->IO CInt

foreign import ccall unsafe "libpq-fe.h PQdb"
foreign import ccall safe "libpq-fe.h PQdb"
c_PQdb :: Ptr PGconn -> IO CString

foreign import ccall unsafe "libpq-fe.h PQuser"
foreign import ccall safe "libpq-fe.h PQuser"
c_PQuser :: Ptr PGconn -> IO CString

foreign import ccall unsafe "libpq-fe.h PQpass"
foreign import ccall safe "libpq-fe.h PQpass"
c_PQpass :: Ptr PGconn -> IO CString

foreign import ccall unsafe "libpq-fe.h PQhost"
foreign import ccall safe "libpq-fe.h PQhost"
c_PQhost :: Ptr PGconn -> IO CString

foreign import ccall unsafe "libpq-fe.h PQport"
foreign import ccall safe "libpq-fe.h PQport"
c_PQport :: Ptr PGconn -> IO CString

foreign import ccall unsafe "libpq-fe.h PQoptions"
foreign import ccall safe "libpq-fe.h PQoptions"
c_PQoptions :: Ptr PGconn -> IO CString

foreign import ccall unsafe "libpq-fe.h PQbackendPID"
foreign import ccall safe "libpq-fe.h PQbackendPID"
c_PQbackendPID :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQconnectionNeedsPassword"
foreign import ccall safe "libpq-fe.h PQconnectionNeedsPassword"
c_PQconnectionNeedsPassword :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQconnectionUsedPassword"
foreign import ccall safe "libpq-fe.h PQconnectionUsedPassword"
c_PQconnectionUsedPassword :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQstatus"
foreign import ccall safe "libpq-fe.h PQstatus"
c_PQstatus :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQtransactionStatus"
foreign import ccall safe "libpq-fe.h PQtransactionStatus"
c_PQtransactionStatus :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQparameterStatus"
foreign import ccall safe "libpq-fe.h PQparameterStatus"
c_PQparameterStatus :: Ptr PGconn -> CString -> IO CString

foreign import ccall unsafe "libpq-fe.h PQprotocolVersion"
foreign import ccall safe "libpq-fe.h PQprotocolVersion"
c_PQprotocolVersion :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQserverVersion"
foreign import ccall safe "libpq-fe.h PQserverVersion"
c_PQserverVersion :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQsocket"
foreign import ccall safe "libpq-fe.h PQsocket"
c_PQsocket :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQerrorMessage"
foreign import ccall safe "libpq-fe.h PQerrorMessage"
c_PQerrorMessage :: Ptr PGconn -> IO CString

foreign import ccall unsafe "libpq-fe.h &PQfinish"
foreign import ccall safe "libpq-fe.h &PQfinish"
p_PQfinish :: FunPtr (Ptr PGconn -> IO ())

foreign import ccall safe "libpq-fe.h PQreset"
c_PQreset :: Ptr PGconn -> IO ()

foreign import ccall unsafe "libpq-fe.h PQresetStart"
foreign import ccall safe "libpq-fe.h PQresetStart"
c_PQresetStart :: Ptr PGconn ->IO CInt

foreign import ccall unsafe "libpq-fe.h PQresetPoll"
foreign import ccall safe "libpq-fe.h PQresetPoll"
c_PQresetPoll :: Ptr PGconn ->IO CInt

foreign import ccall unsafe "libpq-fe.h PQclientEncoding"
foreign import ccall safe "libpq-fe.h PQclientEncoding"
c_PQclientEncoding :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h pg_encoding_to_char"
foreign import ccall safe "libpq-fe.h pg_encoding_to_char"
c_pg_encoding_to_char :: CInt -> IO CString

foreign import ccall unsafe "libpq-fe.h PQsetClientEncoding"
foreign import ccall safe "libpq-fe.h PQsetClientEncoding"
c_PQsetClientEncoding :: Ptr PGconn -> CString -> IO CInt

type PGVerbosity = CInt
foreign import ccall unsafe "libpq-fe.h PQsetErrorVerbosity"
foreign import ccall safe "libpq-fe.h PQsetErrorVerbosity"
c_PQsetErrorVerbosity :: Ptr PGconn -> PGVerbosity -> IO PGVerbosity

foreign import ccall unsafe "libpq-fe.h PQtrace"
foreign import ccall safe "libpq-fe.h PQtrace"
c_PQtrace :: Ptr PGconn -> Ptr CFile -> IO ()

foreign import ccall unsafe "libpq-fe.h PQuntrace"
foreign import ccall safe "libpq-fe.h PQuntrace"
c_PQuntrace :: Ptr PGconn -> IO ()

foreign import ccall unsafe "libpq-fe.h PQsendQuery"
foreign import ccall safe "libpq-fe.h PQsendQuery"
c_PQsendQuery :: Ptr PGconn -> CString ->IO CInt

foreign import ccall unsafe "libpq-fe.h PQsendQueryParams"
foreign import ccall safe "libpq-fe.h PQsendQueryParams"
c_PQsendQueryParams :: Ptr PGconn -> CString -> CInt -> Ptr Oid
-> Ptr CString -> Ptr CInt -> Ptr CInt -> CInt
-> IO CInt

foreign import ccall unsafe "libpq-fe.h PQsendPrepare"
foreign import ccall safe "libpq-fe.h PQsendPrepare"
c_PQsendPrepare :: Ptr PGconn -> CString -> CString -> CInt -> Ptr Oid
-> IO CInt

foreign import ccall unsafe "libpq-fe.h PQsendQueryPrepared"
foreign import ccall safe "libpq-fe.h PQsendQueryPrepared"
c_PQsendQueryPrepared :: Ptr PGconn -> CString -> CInt -> Ptr CString
-> Ptr CInt -> Ptr CInt -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQsendDescribePrepared"
foreign import ccall safe "libpq-fe.h PQsendDescribePrepared"
c_PQsendDescribePrepared :: Ptr PGconn -> CString -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQsendDescribePortal"
foreign import ccall safe "libpq-fe.h PQsendDescribePortal"
c_PQsendDescribePortal :: Ptr PGconn -> CString -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQflush"
foreign import ccall safe "libpq-fe.h PQflush"
c_PQflush :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQgetCancel"
foreign import ccall safe "libpq-fe.h PQgetCancel"
c_PQgetCancel :: Ptr PGconn -> IO (Ptr PGcancel)

foreign import ccall unsafe "libpq-fe.h &PQfreeCancel"
foreign import ccall safe "libpq-fe.h &PQfreeCancel"
p_PQfreeCancel :: FunPtr (Ptr PGcancel -> IO ())

foreign import ccall unsafe "libpq-fe.h PQcancel"
foreign import ccall safe "libpq-fe.h PQcancel"
c_PQcancel :: Ptr PGcancel -> CString -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQnotifies"
foreign import ccall safe "libpq-fe.h PQnotifies"
c_PQnotifies :: Ptr PGconn -> IO (Ptr Notify)

foreign import ccall unsafe "libpq-fe.h PQconsumeInput"
foreign import ccall safe "libpq-fe.h PQconsumeInput"
c_PQconsumeInput :: Ptr PGconn ->IO CInt

foreign import ccall unsafe "libpq-fe.h PQisBusy"
foreign import ccall safe "libpq-fe.h PQisBusy"
c_PQisBusy :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQsetnonblocking"
foreign import ccall safe "libpq-fe.h PQsetnonblocking"
c_PQsetnonblocking :: Ptr PGconn -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQisnonblocking"
foreign import ccall safe "libpq-fe.h PQisnonblocking"
c_PQisnonblocking :: Ptr PGconn -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQgetResult"
foreign import ccall safe "libpq-fe.h PQgetResult"
c_PQgetResult :: Ptr PGconn -> IO (Ptr PGresult)

foreign import ccall safe "libpq-fe.h PQexec"
Expand All @@ -2143,100 +2147,100 @@ foreign import ccall safe "libpq-fe.h PQdescribePrepared"
foreign import ccall safe "libpq-fe.h PQdescribePortal"
c_PQdescribePortal :: Ptr PGconn -> CString -> IO (Ptr PGresult)

foreign import ccall unsafe "libpq-fe.h &PQclear"
foreign import ccall safe "libpq-fe.h &PQclear"
p_PQclear :: FunPtr (Ptr PGresult ->IO ())

foreign import ccall unsafe "libpq-fe.h PQresultStatus"
foreign import ccall safe "libpq-fe.h PQresultStatus"
c_PQresultStatus :: Ptr PGresult -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQresStatus"
foreign import ccall safe "libpq-fe.h PQresStatus"
c_PQresStatus :: CInt -> IO CString

foreign import ccall unsafe "libpq-fe.h PQresultErrorMessage"
foreign import ccall safe "libpq-fe.h PQresultErrorMessage"
c_PQresultErrorMessage :: Ptr PGresult -> IO CString

foreign import ccall unsafe "libpq-fe.h PQresultErrorField"
foreign import ccall safe "libpq-fe.h PQresultErrorField"
c_PQresultErrorField :: Ptr PGresult -> CInt -> IO CString

foreign import ccall unsafe "libpq-fe.h PQntuples"
foreign import ccall safe "libpq-fe.h PQntuples"
c_PQntuples :: Ptr PGresult -> CInt

foreign import ccall unsafe "libpq-fe.h PQnfields"
foreign import ccall safe "libpq-fe.h PQnfields"
c_PQnfields :: Ptr PGresult -> CInt

foreign import ccall unsafe "libpq-fe.h PQfname"
foreign import ccall safe "libpq-fe.h PQfname"
c_PQfname :: Ptr PGresult -> CInt -> IO CString

foreign import ccall unsafe "libpq-fe.h PQfnumber"
foreign import ccall safe "libpq-fe.h PQfnumber"
c_PQfnumber :: Ptr PGresult -> CString -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQftable"
foreign import ccall safe "libpq-fe.h PQftable"
c_PQftable :: Ptr PGresult -> CInt -> IO Oid

foreign import ccall unsafe "libpq-fe.h PQftablecol"
foreign import ccall safe "libpq-fe.h PQftablecol"
c_PQftablecol :: Ptr PGresult -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQfformat"
foreign import ccall safe "libpq-fe.h PQfformat"
c_PQfformat :: Ptr PGresult -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQftype"
foreign import ccall safe "libpq-fe.h PQftype"
c_PQftype :: Ptr PGresult -> CInt -> IO Oid

foreign import ccall unsafe "libpq-fe.h PQfmod"
foreign import ccall safe "libpq-fe.h PQfmod"
c_PQfmod :: Ptr PGresult -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQfsize"
foreign import ccall safe "libpq-fe.h PQfsize"
c_PQfsize :: Ptr PGresult -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQgetvalue"
foreign import ccall safe "libpq-fe.h PQgetvalue"
c_PQgetvalue :: Ptr PGresult -> CInt -> CInt -> IO CString

foreign import ccall unsafe "libpq-fe.h PQgetisnull"
foreign import ccall safe "libpq-fe.h PQgetisnull"
c_PQgetisnull :: Ptr PGresult -> CInt -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQgetlength"
foreign import ccall safe "libpq-fe.h PQgetlength"
c_PQgetlength :: Ptr PGresult -> CInt -> CInt -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQnparams"
foreign import ccall safe "libpq-fe.h PQnparams"
c_PQnparams :: Ptr PGresult -> IO CInt

foreign import ccall unsafe "libpq-fe.h PQparamtype"
foreign import ccall safe "libpq-fe.h PQparamtype"
c_PQparamtype :: Ptr PGresult -> CInt -> IO Oid

foreign import ccall unsafe "stdio.h fdopen"
foreign import ccall safe "stdio.h fdopen"
c_fdopen :: CInt -> CString -> IO (Ptr CFile)

foreign import ccall unsafe "libpq-fe.h PQprint"
foreign import ccall safe "libpq-fe.h PQprint"
c_PQprint :: Ptr CFile -> Ptr PGresult -> Ptr PrintOpt -> IO ()

foreign import ccall unsafe "libpq-fe.h PQcmdStatus"
foreign import ccall safe "libpq-fe.h PQcmdStatus"
c_PQcmdStatus :: Ptr PGresult -> IO CString

foreign import ccall unsafe "libpq-fe.h PQcmdTuples"
foreign import ccall safe "libpq-fe.h PQcmdTuples"
c_PQcmdTuples :: Ptr PGresult -> IO CString

foreign import ccall unsafe "libpq-fe.h PQoidValue"
foreign import ccall safe "libpq-fe.h PQoidValue"
c_PQoidValue :: Ptr PGresult -> IO Oid

foreign import ccall unsafe "libpq-fe.h PQescapeStringConn"
foreign import ccall safe "libpq-fe.h PQescapeStringConn"
c_PQescapeStringConn :: Ptr PGconn
-> Ptr Word8 -- Actually (CString)
-> CString
-> CSize
-> Ptr CInt
-> IO CSize

foreign import ccall unsafe "libpq-fe.h PQescapeByteaConn"
foreign import ccall safe "libpq-fe.h PQescapeByteaConn"
c_PQescapeByteaConn :: Ptr PGconn
-> CString -- Actually (Ptr CUChar)
-> CSize
-> Ptr CSize
-> IO (Ptr Word8) -- Actually (IO (Ptr CUChar))

foreign import ccall unsafe "libpq-fe.h PQunescapeBytea"
foreign import ccall safe "libpq-fe.h PQunescapeBytea"
c_PQunescapeBytea :: CString -- Actually (Ptr CUChar)
-> Ptr CSize
-> IO (Ptr Word8) -- Actually (IO (Ptr CUChar))

foreign import ccall unsafe "libpq-fe.h &PQfreemem"
foreign import ccall safe "libpq-fe.h &PQfreemem"
p_PQfreemem :: FunPtr (Ptr a -> IO ())
Loading