{-# LANGUAGE OverloadedStrings #-}
module Database.CQL.IO.Protocol where
import Control.Monad.Catch
import Data.ByteString.Lazy (ByteString)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Database.CQL.Protocol
import Database.CQL.IO.Exception
import qualified Data.Text.Lazy as LT
data Frame = Frame !Header !ByteString
parse :: (Tuple a, Tuple b, MonadThrow m)
=> Compression
-> Frame
-> m (Response k a b)
parse x (Frame h b) =
case unpack x h b of
Left e -> throwM $ ParseError ("response body reading: " ++ e)
Right r -> return r
serialise :: (Tuple a, MonadThrow m)
=> Version
-> Compression
-> Request k a b
-> Int
-> m ByteString
serialise v f r i =
let c = case getOpCode r of
OcStartup -> noCompression
OcOptions -> noCompression
_ -> f
s = mkStreamId i
in either (throwM . SerialiseError) return (pack v c (isTracing r) s r)
where
isTracing :: Request k a b -> Bool
isTracing (RqQuery (Query _ p)) = fromMaybe False $ enableTracing p
isTracing (RqExecute (Execute _ p)) = fromMaybe False $ enableTracing p
isTracing _ = False
quoted :: LT.Text -> LT.Text
quoted s = "\"" <> LT.replace "\"" "\"\"" s <> "\""