module Database.PostgreSQL.PQTypes.Internal.C.Types (
PGcancel
, PGconn
, PGparam
, PGresult
, PGtypeArgs
, ConnStatusType(..)
, c_CONNECTION_OK, c_CONNECTION_BAD, c_CONNECTION_STARTED
, c_CONNECTION_MADE, c_CONNECTION_AWAITING_RESPONSE, c_CONNECTION_AUTH_OK
, c_CONNECTION_SETENV, c_CONNECTION_SSL_STARTUP, c_CONNECTION_NEEDED
, PostgresPollingStatusType(..)
, c_PGRES_POLLING_FAILED, c_PGRES_POLLING_READING, c_PGRES_POLLING_WRITING
, c_PGRES_POLLING_OK, c_PGRES_POLLING_ACTIVE
, ResultFormat(..)
, c_RESULT_TEXT, c_RESULT_BINARY
, ExecStatusType(..)
, c_PGRES_EMPTY_QUERY, c_PGRES_COMMAND_OK, c_PGRES_TUPLES_OK
, c_PGRES_COPY_OUT, c_PGRES_COPY_IN, c_PGRES_BAD_RESPONSE
, c_PGRES_NONFATAL_ERROR, c_PGRES_FATAL_ERROR, c_PGRES_COPY_BOTH
, ErrorField(..)
, c_PG_DIAG_SEVERITY, c_PG_DIAG_SQLSTATE, c_PG_DIAG_MESSAGE_PRIMARY
, c_PG_DIAG_MESSAGE_DETAIL, c_PG_DIAG_MESSAGE_HINT
, c_PG_DIAG_STATEMENT_POSITION, c_PG_DIAG_INTERNAL_POSITION
, c_PG_DIAG_INTERNAL_QUERY, c_PG_DIAG_CONTEXT, c_PG_DIAG_SOURCE_FILE
, c_PG_DIAG_SOURCE_LINE, c_PG_DIAG_SOURCE_FUNCTION
, TypeClass(..)
, c_PQT_SUBCLASS, c_PQT_COMPOSITE, c_PQT_USERDEFINED
, PGerror(..)
, PGregisterType(..)
, PGarray(..)
, PGbytea(..)
, PGdate(..)
, PGtime(..)
, PGtimestamp(..)
) where
import Control.Applicative
import Data.ByteString.Unsafe
import Foreign.C
import Foreign.ForeignPtr
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Storable
import Prelude
import qualified Data.ByteString as BS
import qualified Data.Vector.Storable as V
data PGcancel
data PGconn
data PGparam
data PGresult
data PGtypeArgs
newtype ConnStatusType = ConnStatusType CInt
deriving Eq
c_CONNECTION_OK :: ConnStatusType
c_CONNECTION_OK = ConnStatusType 0
c_CONNECTION_BAD :: ConnStatusType
c_CONNECTION_BAD = ConnStatusType 1
c_CONNECTION_STARTED :: ConnStatusType
c_CONNECTION_STARTED = ConnStatusType 2
c_CONNECTION_MADE :: ConnStatusType
c_CONNECTION_MADE = ConnStatusType 3
c_CONNECTION_AWAITING_RESPONSE :: ConnStatusType
c_CONNECTION_AWAITING_RESPONSE = ConnStatusType 4
c_CONNECTION_AUTH_OK :: ConnStatusType
c_CONNECTION_AUTH_OK = ConnStatusType 5
c_CONNECTION_SETENV :: ConnStatusType
c_CONNECTION_SETENV = ConnStatusType 6
c_CONNECTION_SSL_STARTUP :: ConnStatusType
c_CONNECTION_SSL_STARTUP = ConnStatusType 7
c_CONNECTION_NEEDED :: ConnStatusType
c_CONNECTION_NEEDED = ConnStatusType 8
newtype PostgresPollingStatusType = PostgresPollingStatusType CInt
deriving Eq
c_PGRES_POLLING_FAILED :: PostgresPollingStatusType
c_PGRES_POLLING_FAILED = PostgresPollingStatusType 0
c_PGRES_POLLING_READING :: PostgresPollingStatusType
c_PGRES_POLLING_READING = PostgresPollingStatusType 1
c_PGRES_POLLING_WRITING :: PostgresPollingStatusType
c_PGRES_POLLING_WRITING = PostgresPollingStatusType 2
c_PGRES_POLLING_OK :: PostgresPollingStatusType
c_PGRES_POLLING_OK = PostgresPollingStatusType 3
c_PGRES_POLLING_ACTIVE :: PostgresPollingStatusType
c_PGRES_POLLING_ACTIVE = PostgresPollingStatusType 4
newtype ResultFormat = ResultFormat CInt
c_RESULT_TEXT :: ResultFormat
c_RESULT_TEXT = ResultFormat 0
c_RESULT_BINARY :: ResultFormat
c_RESULT_BINARY = ResultFormat 1
newtype ExecStatusType = ExecStatusType CInt
deriving Eq
c_PGRES_EMPTY_QUERY :: ExecStatusType
c_PGRES_EMPTY_QUERY = ExecStatusType 0
c_PGRES_COMMAND_OK :: ExecStatusType
c_PGRES_COMMAND_OK = ExecStatusType 1
c_PGRES_TUPLES_OK :: ExecStatusType
c_PGRES_TUPLES_OK = ExecStatusType 2
c_PGRES_COPY_OUT :: ExecStatusType
c_PGRES_COPY_OUT = ExecStatusType 3
c_PGRES_COPY_IN :: ExecStatusType
c_PGRES_COPY_IN = ExecStatusType 4
c_PGRES_BAD_RESPONSE :: ExecStatusType
c_PGRES_BAD_RESPONSE = ExecStatusType 5
c_PGRES_NONFATAL_ERROR :: ExecStatusType
c_PGRES_NONFATAL_ERROR = ExecStatusType 6
c_PGRES_FATAL_ERROR :: ExecStatusType
c_PGRES_FATAL_ERROR = ExecStatusType 7
c_PGRES_COPY_BOTH :: ExecStatusType
c_PGRES_COPY_BOTH = ExecStatusType 8
newtype ErrorField = ErrorField CInt
deriving Eq
c_PG_DIAG_SEVERITY :: ErrorField
c_PG_DIAG_SEVERITY = ErrorField 83
c_PG_DIAG_SQLSTATE :: ErrorField
c_PG_DIAG_SQLSTATE = ErrorField 67
c_PG_DIAG_MESSAGE_PRIMARY :: ErrorField
c_PG_DIAG_MESSAGE_PRIMARY = ErrorField 77
c_PG_DIAG_MESSAGE_DETAIL :: ErrorField
c_PG_DIAG_MESSAGE_DETAIL = ErrorField 68
c_PG_DIAG_MESSAGE_HINT :: ErrorField
c_PG_DIAG_MESSAGE_HINT = ErrorField 72
c_PG_DIAG_STATEMENT_POSITION :: ErrorField
c_PG_DIAG_STATEMENT_POSITION = ErrorField 80
c_PG_DIAG_INTERNAL_POSITION :: ErrorField
c_PG_DIAG_INTERNAL_POSITION = ErrorField 112
c_PG_DIAG_INTERNAL_QUERY :: ErrorField
c_PG_DIAG_INTERNAL_QUERY = ErrorField 113
c_PG_DIAG_CONTEXT :: ErrorField
c_PG_DIAG_CONTEXT = ErrorField 87
c_PG_DIAG_SOURCE_FILE :: ErrorField
c_PG_DIAG_SOURCE_FILE = ErrorField 70
c_PG_DIAG_SOURCE_LINE :: ErrorField
c_PG_DIAG_SOURCE_LINE = ErrorField 76
c_PG_DIAG_SOURCE_FUNCTION :: ErrorField
c_PG_DIAG_SOURCE_FUNCTION = ErrorField 82
newtype TypeClass = TypeClass CInt
deriving Eq
c_PQT_SUBCLASS :: TypeClass
c_PQT_SUBCLASS = TypeClass 0
c_PQT_COMPOSITE :: TypeClass
c_PQT_COMPOSITE = TypeClass 1
c_PQT_USERDEFINED :: TypeClass
c_PQT_USERDEFINED = TypeClass 2
newtype PGerror = PGerror {
pgErrorMsg :: String
} deriving Show
instance Storable PGerror where
sizeOf _ = (2048)
alignment _ = 1
peek ptr = PGerror <$> peekCString ((\hsc_ptr -> hsc_ptr `plusPtr` 0) ptr)
poke _ _ = error "Storable PGerror: poke is not supposed to be used"
data PGregisterType = PGregisterType {
pgRegisterTypeTypName :: !CString
, pgRegisterTypeTypPut :: !(FunPtr (Ptr PGtypeArgs -> IO CInt))
, pgRegisterTypeTypGet :: !(FunPtr (Ptr PGtypeArgs -> IO CInt))
} deriving Show
instance Storable PGregisterType where
sizeOf _ = (24)
alignment _ = 8
peek ptr = PGregisterType
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 16) ptr
poke ptr PGregisterType{..} = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr pgRegisterTypeTypName
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr pgRegisterTypeTypPut
(\hsc_ptr -> pokeByteOff hsc_ptr 16) ptr pgRegisterTypeTypGet
c_MAXDIM :: Int
c_MAXDIM = 6
data PGarray = PGarray {
pgArrayNDims :: !CInt
, pgArrayLBound :: !(V.Vector CInt)
, pgArrayDims :: !(V.Vector CInt)
, pgArrayParam :: !(Ptr PGparam)
, pgArrayRes :: !(Ptr PGresult)
} deriving Show
instance Storable PGarray where
sizeOf _ = (72)
alignment _ = 8
peek ptr = PGarray
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
<*> readVector ((\hsc_ptr -> hsc_ptr `plusPtr` 4) ptr)
<*> readVector ((\hsc_ptr -> hsc_ptr `plusPtr` 28) ptr)
<*> (\hsc_ptr -> peekByteOff hsc_ptr 56) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 64) ptr
where
readVector :: Ptr CInt -> IO (V.Vector CInt)
readVector src = do
let len = c_MAXDIM
fptr <- mallocForeignPtrArray len
withForeignPtr fptr $ \dest -> copyArray dest src len
return (V.unsafeFromForeignPtr0 fptr len)
poke ptr PGarray{..} = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr pgArrayNDims
writeVector pgArrayLBound $ (\hsc_ptr -> hsc_ptr `plusPtr` 4) ptr
writeVector pgArrayDims $ (\hsc_ptr -> hsc_ptr `plusPtr` 28) ptr
(\hsc_ptr -> pokeByteOff hsc_ptr 56) ptr pgArrayParam
(\hsc_ptr -> pokeByteOff hsc_ptr 64) ptr pgArrayRes
where
writeVector :: V.Vector CInt -> Ptr CInt -> IO ()
writeVector v dest = do
let (fptr, baseLen) = V.unsafeToForeignPtr0 v
withForeignPtr fptr $ \src -> do
let len = min baseLen c_MAXDIM
copyArray dest src len
data PGbytea = PGbytea {
pgByteaLen :: !CInt
, pgByteaData :: !CString
} deriving Show
instance Storable PGbytea where
sizeOf _ = (16)
alignment _ = 8
peek ptr = PGbytea
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
poke ptr PGbytea{..} = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr pgByteaLen
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr pgByteaData
data PGdate = PGdate {
pgDateIsBC :: !CInt
, pgDateYear :: !CInt
, pgDateMon :: !CInt
, pgDateMDay :: !CInt
, pgDateJDay :: !CInt
, pgDateYDay :: !CInt
, pgDateWDay :: !CInt
} deriving Show
instance Storable PGdate where
sizeOf _ = (28)
alignment _ = 4
peek ptr = PGdate
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 4) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 12) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 16) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 20) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 24) ptr
poke ptr PGdate{..} = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr pgDateIsBC
(\hsc_ptr -> pokeByteOff hsc_ptr 4) ptr pgDateYear
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr pgDateMon
(\hsc_ptr -> pokeByteOff hsc_ptr 12) ptr pgDateMDay
(\hsc_ptr -> pokeByteOff hsc_ptr 16) ptr pgDateJDay
(\hsc_ptr -> pokeByteOff hsc_ptr 20) ptr pgDateYDay
(\hsc_ptr -> pokeByteOff hsc_ptr 24) ptr pgDateWDay
data PGtime = PGtime {
pgTimeHour :: !CInt
, pgTimeMin :: !CInt
, pgTimeSec :: !CInt
, pgTimeUSec :: !CInt
, pgTimeWithTZ :: !CInt
, pgTimeIsDST :: !CInt
, pgTimeGMTOff :: !CInt
, pgTimeTZAbbr :: !BS.ByteString
} deriving Show
instance Storable PGtime where
sizeOf _ = (44)
alignment _ = 4
peek ptr = PGtime
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 4) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 12) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 16) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 20) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 24) ptr
<*> BS.packCString ((\hsc_ptr -> hsc_ptr `plusPtr` 28) ptr)
poke ptr PGtime{..} = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr pgTimeHour
(\hsc_ptr -> pokeByteOff hsc_ptr 4) ptr pgTimeMin
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr pgTimeSec
(\hsc_ptr -> pokeByteOff hsc_ptr 12) ptr pgTimeUSec
(\hsc_ptr -> pokeByteOff hsc_ptr 16) ptr pgTimeWithTZ
(\hsc_ptr -> pokeByteOff hsc_ptr 20) ptr pgTimeIsDST
(\hsc_ptr -> pokeByteOff hsc_ptr 24) ptr pgTimeGMTOff
unsafeUseAsCStringLen pgTimeTZAbbr $ \(cs, len) -> do
let tzabbr = (\hsc_ptr -> hsc_ptr `plusPtr` 28) ptr
copyArray tzabbr cs (min len 16)
pokeElemOff tzabbr (min len 15) (0::CChar)
data PGtimestamp = PGtimestamp {
pgTimestampEpoch :: !CLLong
, pgTimestampDate :: !PGdate
, pgTimestampTime :: !PGtime
} deriving Show
instance Storable PGtimestamp where
sizeOf _ = (80)
alignment _ = 8
peek ptr = PGtimestamp
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
<*> (\hsc_ptr -> peekByteOff hsc_ptr 36) ptr
poke ptr PGtimestamp{..} = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr pgTimestampEpoch
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr pgTimestampDate
(\hsc_ptr -> pokeByteOff hsc_ptr 36) ptr pgTimestampTime