module Database.CQL.IO
(
Settings
, defSettings
, addContact
, setCompression
, setConnectTimeout
, setContacts
, setIdleTimeout
, setKeyspace
, setMaxConnections
, setMaxStreams
, setMaxTimeouts
, setMaxWaitQueue
, setPolicy
, setPoolStripes
, setPortNumber
, setProtocolVersion
, setResponseTimeout
, setSendTimeout
, Client
, ClientState
, DebugInfo (..)
, init
, runClient
, shutdown
, debugInfo
, query
, write
, schema
, batch
, Page (..)
, emptyPage
, paginate
, request
, command
, Policy (..)
, random
, roundRobin
, Host
, HostEvent (..)
, InetAddr (..)
, hostAddr
, dataCentre
, rack
, InvalidSettings (..)
, InternalError (..)
, HostError (..)
, ConnectionError (..)
, UnexpectedResponse (..)
, Timeout (..)
) where
import Control.Applicative
import Control.Monad.Catch
import Control.Monad (void)
import Data.Maybe (isJust)
import Database.CQL.Protocol
import Database.CQL.IO.Client
import Database.CQL.IO.Cluster.Host
import Database.CQL.IO.Cluster.Policies
import Database.CQL.IO.Settings
import Database.CQL.IO.Types
import Prelude hiding (init)
runQuery :: (Tuple a, Tuple b) => QueryString k a b -> QueryParams a -> Client (Response k a b)
runQuery q p = do
r <- request (RqQuery (Query q p))
case r of
RsError _ e -> throwM e
_ -> return r
query :: (Tuple a, Tuple b) => QueryString R a b -> QueryParams a -> Client [b]
query q p = do
r <- runQuery q p
case r of
RsResult _ (RowsResult _ b) -> return b
_ -> throwM UnexpectedResponse
write :: Tuple a => QueryString W a () -> QueryParams a -> Client ()
write q p = void $ runQuery q p
schema :: Tuple a => QueryString S a () -> QueryParams a -> Client (Maybe SchemaChange)
schema x y = do
r <- runQuery x y
case r of
RsResult _ (SchemaChangeResult s) -> return $ Just s
RsResult _ VoidResult -> return Nothing
_ -> throwM UnexpectedResponse
batch :: Batch -> Client ()
batch b = command (RqBatch b)
data Page a = Page
{ hasMore :: !Bool
, result :: [a]
, nextPage :: Client (Page a)
} deriving (Functor)
emptyPage :: Page a
emptyPage = Page False [] (return emptyPage)
paginate :: (Tuple a, Tuple b) => QueryString R a b -> QueryParams a -> Client (Page b)
paginate q p = do
let p' = p { pageSize = pageSize p <|> Just 10000 }
r <- runQuery q p'
case r of
RsResult _ (RowsResult m b) ->
if isJust (pagingState m) then
return $ Page True b (paginate q p' { queryPagingState = pagingState m })
else
return $ Page False b (return emptyPage)
_ -> throwM UnexpectedResponse