  GENERATED by C->Haskell Compiler , version 0.16.2 Crystal Seed , 24 Jan 2009 ( Haskell )
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "PQ.chs" #-}


 simple wrapper around  , focused on supporting copy from stdin/ to 
 stdout 



simple wrapper around libpq, focused on supporting copy from stdin/ to
stdout

-}

# LANGUAGE ForeignFunctionInterface #
module Database.HsSqlPpp.Utils.PQ where


import Foreign
import Database.HsSqlPpp.Utils.C2HS

data ConnStatusType = CONNECTION_OK
                    | CONNECTION_BAD
                    | CONNECTION_STARTED
                    | CONNECTION_MADE
                    | CONNECTION_AWAITING_RESPONSE
                    | CONNECTION_AUTH_OK
                    | CONNECTION_SETENV
                    | CONNECTION_SSL_STARTUP
                    | CONNECTION_NEEDED
                    deriving (Enum,Eq,Show)

{-# LINE 22 "PQ.chs" #-}
data ExecStatusType = PGRES_EMPTY_QUERY
                    | PGRES_COMMAND_OK
                    | PGRES_TUPLES_OK
                    | PGRES_COPY_OUT
                    | PGRES_COPY_IN
                    | PGRES_BAD_RESPONSE
                    | PGRES_NONFATAL_ERROR
                    | PGRES_FATAL_ERROR
                    deriving (Eq,Show)
instance Enum ExecStatusType where
  fromEnum PGRES_EMPTY_QUERY = 0
  fromEnum PGRES_COMMAND_OK = 1
  fromEnum PGRES_TUPLES_OK = 2
  fromEnum PGRES_COPY_OUT = 3
  fromEnum PGRES_COPY_IN = 4
  fromEnum PGRES_BAD_RESPONSE = 5
  fromEnum PGRES_NONFATAL_ERROR = 6
  fromEnum PGRES_FATAL_ERROR = 7

  toEnum 0 = PGRES_EMPTY_QUERY
  toEnum 1 = PGRES_COMMAND_OK
  toEnum 2 = PGRES_TUPLES_OK
  toEnum 3 = PGRES_COPY_OUT
  toEnum 4 = PGRES_COPY_IN
  toEnum 5 = PGRES_BAD_RESPONSE
  toEnum 6 = PGRES_NONFATAL_ERROR
  toEnum 7 = PGRES_FATAL_ERROR
  toEnum unmatched = error ("ExecStatusType.toEnum: Cannot match " ++ show unmatched)

{-# LINE 23 "PQ.chs" #-}

newtype PGconn = PGconn (Ptr (PGconn))
{-# LINE 25 "PQ.chs" #-}

newtype PGresult = PGresult (Ptr (PGresult))
{-# LINE 27 "PQ.chs" #-}

makePGConn :: Ptr () -> PGconn
makePGConn p = PGconn (castPtr p)

pqConnectdb :: String -> IO (PGconn)
pqConnectdb a1 =
  withCString a1 $ \a1' -> 
  pqConnectdb'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')
# LINE 33 " PQ.chs " #

pqFinish :: PGconn -> IO ()
pqFinish a1 =
  let {a1' = id a1} in 
  pqFinish'_ a1' >>= \res ->
  return ()
{-# LINE 36 "PQ.chs" #-}

pqStatus :: PGconn -> IO (ConnStatusType)
pqStatus a1 =
  let {a1' = id a1} in 
  pqStatus'_ a1' >>= \res ->
  let {res' = cToEnum res} in
  return (res')
# LINE 39 " PQ.chs " #

pqErrorMessage :: PGconn -> IO (String)
pqErrorMessage a1 =
  let {a1' = id a1} in 
  pqErrorMessage'_ a1' >>= \res ->
  peekCString res >>= \res' ->
  return (res')
{-# LINE 42 "PQ.chs" #-}

pqExec :: PGconn -> String -> IO (PGresult)
pqExec a1 a2 =
  let {a1' = id a1} in 
  withCString a2 $ \a2' -> 
  pqExec'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')
{-# LINE 47 "PQ.chs" #-}

pqClear :: PGresult -> IO ()
pqClear a1 =
  let {a1' = id a1} in 
  pqClear'_ a1' >>= \res ->
  return ()
{-# LINE 50 "PQ.chs" #-}

pqResultStatus :: PGresult -> IO (ExecStatusType)
pqResultStatus a1 =
  let {a1' = id a1} in 
  pqResultStatus'_ a1' >>= \res ->
  let {res' = cToEnum res} in
  return (res')
# LINE 53 " PQ.chs " #

pqResStatus :: ExecStatusType -> IO (String)
pqResStatus a1 =
  let {a1' = cFromEnum a1} in 
  pqResStatus'_ a1' >>= \res ->
  peekCString res >>= \res' ->
  return (res')
{-# LINE 56 "PQ.chs" #-}

pqResultErrorMessage :: PGresult -> IO (String)
pqResultErrorMessage a1 =
  let {a1' = id a1} in 
  pqResultErrorMessage'_ a1' >>= \res ->
  peekCString res >>= \res' ->
  return (res')
# LINE 59 " PQ.chs " #


pqPutCopyData :: PGconn -> String -> Int -> IO (Int)
pqPutCopyData a1 a2 a3 =
  let {a1' = id a1} in 
  withCString a2 $ \a2' -> 
  let {a3' = cIntConv a3} in 
  pqPutCopyData'_ a1' a2' a3' >>= \res ->
  let {res' = cIntConv res} in
  return (res')
{-# LINE 65 "PQ.chs" #-}

pqPutCopyEnd :: PGconn -> Maybe String -> IO (Int)
pqPutCopyEnd a1 a2 =
  let {a1' = id a1} in 
  withMCString a2 $ \a2' -> 
  pqPutCopyEnd'_ a1' a2' >>= \res ->
  let {res' = cIntConv res} in
  return (res')
# LINE 69 " PQ.chs " #

withMCString :: Maybe String -> (CString -> IO a) -> IO a
withMCString m f = do
  case m of
    Nothing -> f nullPtr
    Just s -> withCString s f

pqGetCopyData :: PGconn -> Ptr (Ptr CChar) -> Int -> IO (Int)
pqGetCopyData a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = cIntConv a3} in 
  pqGetCopyData'_ a1' a2' a3' >>= \res ->
  let {res' = cIntConv res} in
  return (res')
{-# LINE 80 "PQ.chs" #-}

pqGetResult :: PGconn -> IO (Maybe PGresult)
pqGetResult a1 =
  let {a1' = id a1} in 
  pqGetResult'_ a1' >>= \res ->
  let {res' = mPGresult res} in
  return (res')
{-# LINE 83 "PQ.chs" #-}

mPGresult :: PGresult -> Maybe PGresult
mPGresult p@(PGresult x) = if x == nullPtr
                 then Nothing
                 else Just p

pqFreemem :: Ptr () -> IO ()
pqFreemem a1 =
  let {a1' = id a1} in 
  pqFreemem'_ a1' >>= \res ->
  return ()
{-# LINE 91 "PQ.chs" #-}


foreign import ccall unsafe "PQ.chs.h PQconnectdb"
  pqConnectdb'_ :: ((Ptr CChar) -> (IO (PGconn)))

foreign import ccall unsafe "PQ.chs.h PQfinish"
  pqFinish'_ :: ((PGconn) -> (IO ()))

foreign import ccall unsafe "PQ.chs.h PQstatus"
  pqStatus'_ :: ((PGconn) -> (IO CInt))

foreign import ccall unsafe "PQ.chs.h PQerrorMessage"
  pqErrorMessage'_ :: ((PGconn) -> (IO (Ptr CChar)))

foreign import ccall unsafe "PQ.chs.h PQexec"
  pqExec'_ :: ((PGconn) -> ((Ptr CChar) -> (IO (PGresult))))

foreign import ccall unsafe "PQ.chs.h PQclear"
  pqClear'_ :: ((PGresult) -> (IO ()))

foreign import ccall unsafe "PQ.chs.h PQresultStatus"
  pqResultStatus'_ :: ((PGresult) -> (IO CInt))

foreign import ccall unsafe "PQ.chs.h PQresStatus"
  pqResStatus'_ :: (CInt -> (IO (Ptr CChar)))

foreign import ccall unsafe "PQ.chs.h PQresultErrorMessage"
  pqResultErrorMessage'_ :: ((PGresult) -> (IO (Ptr CChar)))

foreign import ccall unsafe "PQ.chs.h PQputCopyData"
  pqPutCopyData'_ :: ((PGconn) -> ((Ptr CChar) -> (CInt -> (IO CInt))))

foreign import ccall unsafe "PQ.chs.h PQputCopyEnd"
  pqPutCopyEnd'_ :: ((PGconn) -> ((Ptr CChar) -> (IO CInt)))

foreign import ccall unsafe "PQ.chs.h PQgetCopyData"
  pqGetCopyData'_ :: ((PGconn) -> ((Ptr (Ptr CChar)) -> (CInt -> (IO CInt))))

foreign import ccall unsafe "PQ.chs.h PQgetResult"
  pqGetResult'_ :: ((PGconn) -> (IO (PGresult)))

foreign import ccall unsafe "PQ.chs.h PQfreemem"
  pqFreemem'_ :: ((Ptr ()) -> (IO ()))

 Edit the ORIGNAL .chs file instead!
# LINE 1 "PQ.chs" #
# LINE 22 "PQ.chs" #
# LINE 23 "PQ.chs" #
# LINE 25 "PQ.chs" #
# LINE 27 "PQ.chs" #
# LINE 36 "PQ.chs" #
# LINE 42 "PQ.chs" #
# LINE 47 "PQ.chs" #
# LINE 50 "PQ.chs" #
# LINE 56 "PQ.chs" #
# LINE 65 "PQ.chs" #
# LINE 80 "PQ.chs" #
# LINE 83 "PQ.chs" #
# LINE 91 "PQ.chs" #
  GENERATED by C->Haskell Compiler , version 0.16.2 Crystal Seed , 24 Jan 2009 ( Haskell )




 simple wrapper around  , focused on supporting copy from stdin/ to 
 stdout 



simple wrapper around libpq, focused on supporting copy from stdin/ to
stdout

-}

# LANGUAGE ForeignFunctionInterface #
module Database.HsSqlPpp.Utils.PQ where


import Foreign
import Database.HsSqlPpp.Utils.C2HS

data ConnStatusType = CONNECTION_OK
                    | CONNECTION_BAD
                    | CONNECTION_STARTED
                    | CONNECTION_MADE
                    | CONNECTION_AWAITING_RESPONSE
                    | CONNECTION_AUTH_OK
                    | CONNECTION_SETENV
                    | CONNECTION_SSL_STARTUP
                    | CONNECTION_NEEDED
                    deriving (Enum,Eq,Show)

data ExecStatusType = PGRES_EMPTY_QUERY
                    | PGRES_COMMAND_OK
                    | PGRES_TUPLES_OK
                    | PGRES_COPY_OUT
                    | PGRES_COPY_IN
                    | PGRES_BAD_RESPONSE
                    | PGRES_NONFATAL_ERROR
                    | PGRES_FATAL_ERROR
                    deriving (Eq,Show)
instance Enum ExecStatusType where
  fromEnum PGRES_EMPTY_QUERY = 0
  fromEnum PGRES_COMMAND_OK = 1
  fromEnum PGRES_TUPLES_OK = 2
  fromEnum PGRES_COPY_OUT = 3
  fromEnum PGRES_COPY_IN = 4
  fromEnum PGRES_BAD_RESPONSE = 5
  fromEnum PGRES_NONFATAL_ERROR = 6
  fromEnum PGRES_FATAL_ERROR = 7

  toEnum 0 = PGRES_EMPTY_QUERY
  toEnum 1 = PGRES_COMMAND_OK
  toEnum 2 = PGRES_TUPLES_OK
  toEnum 3 = PGRES_COPY_OUT
  toEnum 4 = PGRES_COPY_IN
  toEnum 5 = PGRES_BAD_RESPONSE
  toEnum 6 = PGRES_NONFATAL_ERROR
  toEnum 7 = PGRES_FATAL_ERROR
  toEnum unmatched = error ("ExecStatusType.toEnum: Cannot match " ++ show unmatched)


newtype PGconn = PGconn (Ptr (PGconn))

newtype PGresult = PGresult (Ptr (PGresult))

makePGConn :: Ptr () -> PGconn
makePGConn p = PGconn (castPtr p)

pqConnectdb :: String -> IO (PGconn)
pqConnectdb a1 =
  withCString a1 $ \a1' -> 
  pqConnectdb'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')
# LINE 33 " PQ.chs " #

pqFinish :: PGconn -> IO ()
pqFinish a1 =
  let {a1' = id a1} in 
  pqFinish'_ a1' >>= \res ->
  return ()

pqStatus :: PGconn -> IO (ConnStatusType)
pqStatus a1 =
  let {a1' = id a1} in 
  pqStatus'_ a1' >>= \res ->
  let {res' = cToEnum res} in
  return (res')
# LINE 39 " PQ.chs " #

pqErrorMessage :: PGconn -> IO (String)
pqErrorMessage a1 =
  let {a1' = id a1} in 
  pqErrorMessage'_ a1' >>= \res ->
  peekCString res >>= \res' ->
  return (res')

pqExec :: PGconn -> String -> IO (PGresult)
pqExec a1 a2 =
  let {a1' = id a1} in 
  withCString a2 $ \a2' -> 
  pqExec'_ a1' a2' >>= \res ->
  let {res' = id res} in
  return (res')

pqClear :: PGresult -> IO ()
pqClear a1 =
  let {a1' = id a1} in 
  pqClear'_ a1' >>= \res ->
  return ()

pqResultStatus :: PGresult -> IO (ExecStatusType)
pqResultStatus a1 =
  let {a1' = id a1} in 
  pqResultStatus'_ a1' >>= \res ->
  let {res' = cToEnum res} in
  return (res')
# LINE 53 " PQ.chs " #

pqResStatus :: ExecStatusType -> IO (String)
pqResStatus a1 =
  let {a1' = cFromEnum a1} in 
  pqResStatus'_ a1' >>= \res ->
  peekCString res >>= \res' ->
  return (res')

pqResultErrorMessage :: PGresult -> IO (String)
pqResultErrorMessage a1 =
  let {a1' = id a1} in 
  pqResultErrorMessage'_ a1' >>= \res ->
  peekCString res >>= \res' ->
  return (res')
# LINE 59 " PQ.chs " #


pqPutCopyData :: PGconn -> String -> Int -> IO (Int)
pqPutCopyData a1 a2 a3 =
  let {a1' = id a1} in 
  withCString a2 $ \a2' -> 
  let {a3' = cIntConv a3} in 
  pqPutCopyData'_ a1' a2' a3' >>= \res ->
  let {res' = cIntConv res} in
  return (res')

pqPutCopyEnd :: PGconn -> Maybe String -> IO (Int)
pqPutCopyEnd a1 a2 =
  let {a1' = id a1} in 
  withMCString a2 $ \a2' -> 
  pqPutCopyEnd'_ a1' a2' >>= \res ->
  let {res' = cIntConv res} in
  return (res')
# LINE 69 " PQ.chs " #

withMCString :: Maybe String -> (CString -> IO a) -> IO a
withMCString m f = do
  case m of
    Nothing -> f nullPtr
    Just s -> withCString s f

pqGetCopyData :: PGconn -> Ptr (Ptr CChar) -> Int -> IO (Int)
pqGetCopyData a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = cIntConv a3} in 
  pqGetCopyData'_ a1' a2' a3' >>= \res ->
  let {res' = cIntConv res} in
  return (res')

pqGetResult :: PGconn -> IO (Maybe PGresult)
pqGetResult a1 =
  let {a1' = id a1} in 
  pqGetResult'_ a1' >>= \res ->
  let {res' = mPGresult res} in
  return (res')

mPGresult :: PGresult -> Maybe PGresult
mPGresult p@(PGresult x) = if x == nullPtr
                 then Nothing
                 else Just p

pqFreemem :: Ptr () -> IO ()
pqFreemem a1 =
  let {a1' = id a1} in 
  pqFreemem'_ a1' >>= \res ->
  return ()


foreign import ccall unsafe "PQ.chs.h PQconnectdb"
  pqConnectdb'_ :: ((Ptr CChar) -> (IO (PGconn)))

foreign import ccall unsafe "PQ.chs.h PQfinish"
  pqFinish'_ :: ((PGconn) -> (IO ()))

foreign import ccall unsafe "PQ.chs.h PQstatus"
  pqStatus'_ :: ((PGconn) -> (IO CInt))

foreign import ccall unsafe "PQ.chs.h PQerrorMessage"
  pqErrorMessage'_ :: ((PGconn) -> (IO (Ptr CChar)))

foreign import ccall unsafe "PQ.chs.h PQexec"
  pqExec'_ :: ((PGconn) -> ((Ptr CChar) -> (IO (PGresult))))

foreign import ccall unsafe "PQ.chs.h PQclear"
  pqClear'_ :: ((PGresult) -> (IO ()))

foreign import ccall unsafe "PQ.chs.h PQresultStatus"
  pqResultStatus'_ :: ((PGresult) -> (IO CInt))

foreign import ccall unsafe "PQ.chs.h PQresStatus"
  pqResStatus'_ :: (CInt -> (IO (Ptr CChar)))

foreign import ccall unsafe "PQ.chs.h PQresultErrorMessage"
  pqResultErrorMessage'_ :: ((PGresult) -> (IO (Ptr CChar)))

foreign import ccall unsafe "PQ.chs.h PQputCopyData"
  pqPutCopyData'_ :: ((PGconn) -> ((Ptr CChar) -> (CInt -> (IO CInt))))

foreign import ccall unsafe "PQ.chs.h PQputCopyEnd"
  pqPutCopyEnd'_ :: ((PGconn) -> ((Ptr CChar) -> (IO CInt)))

foreign import ccall unsafe "PQ.chs.h PQgetCopyData"
  pqGetCopyData'_ :: ((PGconn) -> ((Ptr (Ptr CChar)) -> (CInt -> (IO CInt))))

foreign import ccall unsafe "PQ.chs.h PQgetResult"
  pqGetResult'_ :: ((PGconn) -> (IO (PGresult)))

foreign import ccall unsafe "PQ.chs.h PQfreemem"
  pqFreemem'_ :: ((Ptr ()) -> (IO ()))
