module Database.CQL.IO
(
Settings
, defSettings
, addContact
, setCompression
, setConnectTimeout
, setContacts
, setIdleTimeout
, setKeyspace
, setMaxConnections
, setMaxStreams
, setMaxTimeouts
, setPolicy
, setPoolStripes
, setPortNumber
, setProtocolVersion
, setResponseTimeout
, setSendTimeout
, setRetrySettings
, setMaxRecvBuffer
, RetrySettings
, noRetry
, retryForever
, maxRetries
, adjustConsistency
, constDelay
, expBackoff
, fibBackoff
, adjustSendTimeout
, adjustResponseTimeout
, Client
, MonadClient (..)
, ClientState
, DebugInfo (..)
, init
, runClient
, retry
, shutdown
, debugInfo
, query
, query1
, 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, listToMaybe)
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 :: (MonadClient m, Tuple a, Tuple b) => QueryString k a b -> QueryParams a -> m (Response k a b)
runQuery q p = do
r <- request (RqQuery (Query q p))
case r of
RsError _ e -> throwM e
_ -> return r
query :: (MonadClient m, Tuple a, Tuple b) => QueryString R a b -> QueryParams a -> m [b]
query q p = do
r <- runQuery q p
case r of
RsResult _ (RowsResult _ b) -> return b
_ -> throwM UnexpectedResponse
query1 :: (MonadClient m, Tuple a, Tuple b) => QueryString R a b -> QueryParams a -> m (Maybe b)
query1 q p = listToMaybe <$> query q p
write :: (MonadClient m, Tuple a) => QueryString W a () -> QueryParams a -> m ()
write q p = void $ runQuery q p
schema :: (MonadClient m, Tuple a) => QueryString S a () -> QueryParams a -> m (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 :: MonadClient m => Batch -> m ()
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 :: (MonadClient m, Tuple a, Tuple b) => QueryString R a b -> QueryParams a -> m (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