    {-# LINE 1 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    {-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-}
    {-# LINE 2 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    module Database.TokyoCabinet.TDB.Query.C where

    import Data.Word
    import Data.Bits

    import Foreign.Ptr
    import Foreign.ForeignPtr
    import Foreign.C.Types
    import Foreign.C.String

    import Database.TokyoCabinet.TDB.C
    import Database.TokyoCabinet.Map.C
    import Database.TokyoCabinet.List.C

    {-# LINE 17 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}

    data Condition =
        QCSTREQ   |
        QCSTRINC  |
        QCSTRBW   |
        QCSTREW   |
        QCSTRAND  |
        QCSTROR   |
        QCSTROREQ |
        QCSTRRX   |
        QCNUMEQ   |
        QCNUMGT   |
        QCNUMGE   |
        QCNUMLT   |
        QCNUMLE   |
        QCNUMBT   |
        QCNUMOREQ |
        QCNEGATE Condition |
        QCNOIDX  Condition
        deriving (Eq, Ord, Show)

    data OrderType =
        QOSTRASC  |
        QOSTRDESC |
        QONUMASC  |
        QONUMDESC
        deriving (Eq, Ord, Show)

    data PostTreatment m k v =
        QPPUT (m k v) |
        QPOUT  |
        QPNOP  |
        QPSTOP
        deriving (Eq, Ord, Show)

    condToCInt :: Condition -> CInt
    condToCInt QCSTREQ   = 0
    {-# LINE 54 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCSTRINC  = 1
    {-# LINE 55 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCSTRBW   = 2
    {-# LINE 56 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCSTREW   = 3
    {-# LINE 57 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCSTRAND  = 4
    {-# LINE 58 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCSTROR   = 5
    {-# LINE 59 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCSTROREQ = 6
    {-# LINE 60 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCSTRRX   = 7
    {-# LINE 61 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCNUMEQ   = 8
    {-# LINE 62 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCNUMGT   = 9
    {-# LINE 63 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCNUMGE   = 10
    {-# LINE 64 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCNUMLT   = 11
    {-# LINE 65 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCNUMLE   = 12
    {-# LINE 66 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCNUMBT   = 13
    {-# LINE 67 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt QCNUMOREQ = 14
    {-# LINE 68 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt (QCNEGATE c) = (16777216) .|. (condToCInt c)
    {-# LINE 69 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    condToCInt (QCNOIDX  c) = (33554432) .|. (condToCInt c)
    {-# LINE 70 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}

    orderToCInt :: OrderType -> CInt
    orderToCInt QOSTRASC  = 0
    {-# LINE 73 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    orderToCInt QOSTRDESC = 1
    {-# LINE 74 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    orderToCInt QONUMASC  = 2
    {-# LINE 75 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    orderToCInt QONUMDESC = 3
    {-# LINE 76 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}

    ptToCInt :: PostTreatment m k v -> CInt
    ptToCInt QPNOP  = 0
    ptToCInt QPOUT  = 2
    {-# LINE 80 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    ptToCInt QPSTOP = 16777216
    {-# LINE 81 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}
    ptToCInt (QPPUT _) = 1
    {-# LINE 82 "Database/TokyoCabinet/TDB/Query/C.hsc" #-}

    data TDBQRY = TDBQRY { unTDBQRY :: !(ForeignPtr QRY)
                         , unTDBOBJ :: TDB }

    data QRY

    foreign import ccall safe "tctdbqrynew"
      c_tctdbqrynew :: Ptr TDB' -> IO (Ptr QRY)

    foreign import ccall safe "tctdbqrydel"
      c_tctdbqrydel :: Ptr QRY -> IO ()

    foreign import ccall safe "&tctdbqrydel"
      tctdbqryFinalizer :: FunPtr (Ptr QRY -> IO ())

    foreign import ccall safe "tctdbqryaddcond"
      c_tctdbqryaddcond :: Ptr QRY -> CString -> CInt -> CString -> IO ()

    foreign import ccall safe "tctdbqrysetorder"
      c_tctdbqrysetorder :: Ptr QRY -> CString -> CInt -> IO ()

    foreign import ccall safe "tctdbqrysetlimit"
      c_tctdbqrysetlimit :: Ptr QRY -> CInt -> CInt -> IO ()

    foreign import ccall safe "tctdbqrysearch"
      c_tctdbqrysearch :: Ptr QRY -> IO (Ptr LIST)

    foreign import ccall safe "tctdbqrysearchout"
      c_tctdbqrysearchout :: Ptr QRY -> IO Bool

    foreign import ccall safe "tctdbqryhint"
      c_tctdbqryhint :: Ptr QRY -> IO CString

    type TDBQRYPROC' = Ptr Word8 -> CInt -> Ptr MAP -> Ptr Word8 -> IO CInt

    foreign import ccall safe "tctdbqryproc"
      c_tctdbqryproc :: Ptr QRY
                     -> FunPtr TDBQRYPROC'
                     -> Ptr Word8
                     -> IO Bool

    foreign import ccall "wrapper"
      mkProc :: TDBQRYPROC' -> IO (FunPtr TDBQRYPROC')