{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE LambdaCase #-}
module Database.CQL.IO
(
Settings
, S.defSettings
, addContact
, setCompression
, setConnectTimeout
, setContacts
, setIdleTimeout
, setKeyspace
, setMaxConnections
, setMaxStreams
, setMaxTimeouts
, setPolicy
, setPoolStripes
, setPortNumber
, PrepareStrategy (..)
, setPrepareStrategy
, setProtocolVersion
, setResponseTimeout
, setSendTimeout
, setRetrySettings
, setMaxRecvBuffer
, setSSLContext
, Logger (..)
, LogLevel (..)
, setLogger
, nullLogger
, stdoutLogger
, setAuthentication
, Authenticator (..)
, AuthContext
, ConnId
, authConnId
, authHost
, AuthMechanism (..)
, AuthUser (..)
, AuthPass (..)
, passwordAuthenticator
, RetrySettings
, noRetry
, defRetrySettings
, defRetryPolicy
, defRetryHandlers
, eagerRetrySettings
, eagerRetryPolicy
, eagerRetryHandlers
, setRetryPolicy
, setRetryHandlers
, adjustConsistency
, adjustSendTimeout
, adjustResponseTimeout
, Policy (..)
, random
, roundRobin
, Host
, HostEvent (..)
, InetAddr (..)
, hostAddr
, dataCentre
, rack
, Client
, MonadClient (..)
, ClientState
, DebugInfo (..)
, init
, runClient
, shutdown
, debugInfo
, R, W, S
, QueryParams (..)
, defQueryParams
, Consistency (..)
, SerialConsistency (..)
, Identity (..)
, QueryString (..)
, query
, query1
, write
, schema
, PrepQuery
, prepared
, queryString
, Page (..)
, emptyPage
, paginate
, Row
, fromRow
, trans
, BatchM
, addQuery
, addPrepQuery
, setType
, setConsistency
, setSerialConsistency
, batch
, retry
, once
, RunQ (..)
, HostResponse (..)
, request
, getResult
, ProtocolError (..)
, HostError (..)
, ConnectionError (..)
, ResponseError (..)
, AuthenticationError (..)
, HashCollision (..)
) where
import Control.Applicative
import Data.Functor.Identity
import Data.Maybe (isJust, listToMaybe)
import Database.CQL.Protocol
import Database.CQL.IO.Batch hiding (batch)
import Database.CQL.IO.Client
import Database.CQL.IO.Cluster.Host
import Database.CQL.IO.Cluster.Policies
import Database.CQL.IO.Connection.Settings as C
import Database.CQL.IO.Exception
import Database.CQL.IO.Log
import Database.CQL.IO.PrepQuery
import Database.CQL.IO.Settings as S
import Prelude hiding (init)
import qualified Database.CQL.IO.Batch as B
class RunQ q where
runQ :: (MonadClient m, Tuple a, Tuple b)
=> q k a b
-> QueryParams a
-> m (HostResponse k a b)
instance RunQ QueryString where
runQ :: forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
QueryString k a b -> QueryParams a -> m (HostResponse k a b)
runQ QueryString k a b
q QueryParams a
p = Request k a b -> m (HostResponse k a b)
forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
Request k a b -> m (HostResponse k a b)
request (Query k a b -> Request k a b
forall k a b. Query k a b -> Request k a b
RqQuery (QueryString k a b -> QueryParams a -> Query k a b
forall k a b. QueryString k a b -> QueryParams a -> Query k a b
Query QueryString k a b
q QueryParams a
p))
instance RunQ PrepQuery where
runQ :: forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
PrepQuery k a b -> QueryParams a -> m (HostResponse k a b)
runQ PrepQuery k a b
q = Client (HostResponse k a b) -> m (HostResponse k a b)
forall a. Client a -> m a
forall (m :: * -> *) a. MonadClient m => Client a -> m a
liftClient (Client (HostResponse k a b) -> m (HostResponse k a b))
-> (QueryParams a -> Client (HostResponse k a b))
-> QueryParams a
-> m (HostResponse k a b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrepQuery k a b -> QueryParams a -> Client (HostResponse k a b)
forall b a k.
(Tuple b, Tuple a) =>
PrepQuery k a b -> QueryParams a -> Client (HostResponse k a b)
execute PrepQuery k a b
q
query :: (MonadClient m, Tuple a, Tuple b, RunQ q) => q R a b -> QueryParams a -> m [b]
query :: forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m [b]
query q R a b
q QueryParams a
p = do
HostResponse R a b
r <- q R a b -> QueryParams a -> m (HostResponse R a b)
forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
forall (q :: * -> * -> * -> *) (m :: * -> *) a b k.
(RunQ q, MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
runQ q R a b
q QueryParams a
p
HostResponse R a b -> m (Result R a b)
forall (m :: * -> *) k a b.
MonadThrow m =>
HostResponse k a b -> m (Result k a b)
getResult HostResponse R a b
r m (Result R a b) -> (Result R a b -> m [b]) -> m [b]
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
RowsResult MetaData
_ [b]
b -> [b] -> m [b]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [b]
b
Result R a b
_ -> HostResponse R a b -> m [b]
forall (m :: * -> *) k a b c.
MonadThrow m =>
HostResponse k a b -> m c
unexpected HostResponse R a b
r
query1 :: (MonadClient m, Tuple a, Tuple b, RunQ q) => q R a b -> QueryParams a -> m (Maybe b)
query1 :: forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Maybe b)
query1 q R a b
q QueryParams a
p = [b] -> Maybe b
forall a. [a] -> Maybe a
listToMaybe ([b] -> Maybe b) -> m [b] -> m (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> q R a b -> QueryParams a -> m [b]
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m [b]
query q R a b
q QueryParams a
p
write :: (MonadClient m, Tuple a, RunQ q) => q W a () -> QueryParams a -> m ()
write :: forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write q W a ()
q QueryParams a
p = do
HostResponse W a ()
r <- q W a () -> QueryParams a -> m (HostResponse W a ())
forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
forall (q :: * -> * -> * -> *) (m :: * -> *) a b k.
(RunQ q, MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
runQ q W a ()
q QueryParams a
p
HostResponse W a () -> m (Result W a ())
forall (m :: * -> *) k a b.
MonadThrow m =>
HostResponse k a b -> m (Result k a b)
getResult HostResponse W a ()
r m (Result W a ()) -> (Result W a () -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Result W a ()
VoidResult -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Result W a ()
_ -> HostResponse W a () -> m ()
forall (m :: * -> *) k a b c.
MonadThrow m =>
HostResponse k a b -> m c
unexpected HostResponse W a ()
r
trans :: (MonadClient m, Tuple a, RunQ q) => q W a Row -> QueryParams a -> m [Row]
trans :: forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a Row -> QueryParams a -> m [Row]
trans q W a Row
q QueryParams a
p = do
HostResponse W a Row
r <- q W a Row -> QueryParams a -> m (HostResponse W a Row)
forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
forall (q :: * -> * -> * -> *) (m :: * -> *) a b k.
(RunQ q, MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
runQ q W a Row
q QueryParams a
p
HostResponse W a Row -> m (Result W a Row)
forall (m :: * -> *) k a b.
MonadThrow m =>
HostResponse k a b -> m (Result k a b)
getResult HostResponse W a Row
r m (Result W a Row) -> (Result W a Row -> m [Row]) -> m [Row]
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
RowsResult MetaData
_ [Row]
b -> [Row] -> m [Row]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Row]
b
Result W a Row
_ -> HostResponse W a Row -> m [Row]
forall (m :: * -> *) k a b c.
MonadThrow m =>
HostResponse k a b -> m c
unexpected HostResponse W a Row
r
schema :: (MonadClient m, Tuple a, RunQ q) => q S a () -> QueryParams a -> m (Maybe SchemaChange)
schema :: forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q S a () -> QueryParams a -> m (Maybe SchemaChange)
schema q S a ()
q QueryParams a
p = do
HostResponse S a ()
r <- q S a () -> QueryParams a -> m (HostResponse S a ())
forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
forall (q :: * -> * -> * -> *) (m :: * -> *) a b k.
(RunQ q, MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
runQ q S a ()
q QueryParams a
p
HostResponse S a () -> m (Result S a ())
forall (m :: * -> *) k a b.
MonadThrow m =>
HostResponse k a b -> m (Result k a b)
getResult HostResponse S a ()
r m (Result S a ())
-> (Result S a () -> m (Maybe SchemaChange))
-> m (Maybe SchemaChange)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
SchemaChangeResult SchemaChange
s -> Maybe SchemaChange -> m (Maybe SchemaChange)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe SchemaChange -> m (Maybe SchemaChange))
-> Maybe SchemaChange -> m (Maybe SchemaChange)
forall a b. (a -> b) -> a -> b
$ SchemaChange -> Maybe SchemaChange
forall a. a -> Maybe a
Just SchemaChange
s
Result S a ()
VoidResult -> Maybe SchemaChange -> m (Maybe SchemaChange)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe SchemaChange
forall a. Maybe a
Nothing
Result S a ()
_ -> HostResponse S a () -> m (Maybe SchemaChange)
forall (m :: * -> *) k a b c.
MonadThrow m =>
HostResponse k a b -> m c
unexpected HostResponse S a ()
r
batch :: MonadClient m => BatchM () -> m ()
batch :: forall (m :: * -> *). MonadClient m => BatchM () -> m ()
batch = Client () -> m ()
forall a. Client a -> m a
forall (m :: * -> *) a. MonadClient m => Client a -> m a
liftClient (Client () -> m ())
-> (BatchM () -> Client ()) -> BatchM () -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BatchM () -> Client ()
forall a. BatchM a -> Client ()
B.batch
data Page a = Page
{ forall a. Page a -> Bool
hasMore :: !Bool
, forall a. Page a -> [a]
result :: [a]
, forall a. Page a -> Client (Page a)
nextPage :: Client (Page a)
} deriving ((forall a b. (a -> b) -> Page a -> Page b)
-> (forall a b. a -> Page b -> Page a) -> Functor Page
forall a b. a -> Page b -> Page a
forall a b. (a -> b) -> Page a -> Page b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Page a -> Page b
fmap :: forall a b. (a -> b) -> Page a -> Page b
$c<$ :: forall a b. a -> Page b -> Page a
<$ :: forall a b. a -> Page b -> Page a
Functor)
emptyPage :: Page a
emptyPage :: forall a. Page a
emptyPage = Bool -> [a] -> Client (Page a) -> Page a
forall a. Bool -> [a] -> Client (Page a) -> Page a
Page Bool
False [] (Page a -> Client (Page a)
forall a. a -> Client a
forall (m :: * -> *) a. Monad m => a -> m a
return Page a
forall a. Page a
emptyPage)
paginate :: (MonadClient m, Tuple a, Tuple b, RunQ q) => q R a b -> QueryParams a -> m (Page b)
paginate :: forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Page b)
paginate q R a b
q QueryParams a
p = do
let p' :: QueryParams a
p' = QueryParams a
p { pageSize = pageSize p <|> Just 10000 }
HostResponse R a b
r <- q R a b -> QueryParams a -> m (HostResponse R a b)
forall (m :: * -> *) a b k.
(MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
forall (q :: * -> * -> * -> *) (m :: * -> *) a b k.
(RunQ q, MonadClient m, Tuple a, Tuple b) =>
q k a b -> QueryParams a -> m (HostResponse k a b)
runQ q R a b
q QueryParams a
p'
HostResponse R a b -> m (Result R a b)
forall (m :: * -> *) k a b.
MonadThrow m =>
HostResponse k a b -> m (Result k a b)
getResult HostResponse R a b
r m (Result R a b) -> (Result R a b -> m (Page b)) -> m (Page b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
RowsResult MetaData
m [b]
b ->
if Maybe PagingState -> Bool
forall a. Maybe a -> Bool
isJust (MetaData -> Maybe PagingState
pagingState MetaData
m) then
Page b -> m (Page b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Page b -> m (Page b)) -> Page b -> m (Page b)
forall a b. (a -> b) -> a -> b
$ Bool -> [b] -> Client (Page b) -> Page b
forall a. Bool -> [a] -> Client (Page a) -> Page a
Page Bool
True [b]
b (q R a b -> QueryParams a -> Client (Page b)
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Page b)
paginate q R a b
q QueryParams a
p' { queryPagingState = pagingState m })
else
Page b -> m (Page b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Page b -> m (Page b)) -> Page b -> m (Page b)
forall a b. (a -> b) -> a -> b
$ Bool -> [b] -> Client (Page b) -> Page b
forall a. Bool -> [a] -> Client (Page a) -> Page a
Page Bool
False [b]
b (Page b -> Client (Page b)
forall a. a -> Client a
forall (m :: * -> *) a. Monad m => a -> m a
return Page b
forall a. Page a
emptyPage)
Result R a b
_ -> HostResponse R a b -> m (Page b)
forall (m :: * -> *) k a b c.
MonadThrow m =>
HostResponse k a b -> m c
unexpected HostResponse R a b
r