module Database.Bolty.Connection.Type
( Config(..)
, ValidatedConfig(..)
, ServerState(..)
, Connection(..)
, Error(..)
, Scheme(..)
, Principal
, Credentials
, Routing(..)
, UserAgent(..)
, isTransient
, isRoutingError
) where
import Data.Kind (Type)
import Control.Exception (Exception, SomeException)
import Data.Default (Default(..))
import Data.IORef (IORef)
import Data.Text (Text)
import Data.Word (Word16, Word32, Word64)
import GHC.Generics (Generic)
import qualified Network.Connection as NC
import qualified Data.HashMap.Lazy as H
import qualified Data.Text as T
import Database.Bolty.Connection.Version (Version(..))
import Database.Bolty.Logging (QueryLog(..))
import Database.Bolty.Message.Response (QueryMeta)
import Database.Bolty.Notification (Notification)
type Config :: Type
data Config = Config
{ Config -> Text
host :: T.Text
, Config -> Word16
port :: Word16
, Config -> Scheme
scheme :: Scheme
, Config -> Bool
use_tls :: Bool
, Config -> [Version]
versions :: [Version]
, Config -> Int
timeout :: Int
, Config -> Routing
routing :: Routing
, Config -> UserAgent
user_agent :: UserAgent
, Config -> Maybe (QueryLog -> QueryMeta -> IO ())
queryLogger :: Maybe (QueryLog -> QueryMeta -> IO ())
, Config -> Maybe (Notification -> IO ())
notificationHandler :: Maybe (Notification -> IO ())
}
instance Default Config where
def :: Config
def = Config
{ host :: Text
host = Text
"127.0.0.1"
, port :: Word16
port = Word16
7687
, scheme :: Scheme
scheme = Scheme
None
, use_tls :: Bool
use_tls = Bool
True
, versions :: [Version]
versions = [ Version{major :: Word8
major = Word8
5, minor :: Word8
minor = Word8
4}
, Version{major :: Word8
major = Word8
5, minor :: Word8
minor = Word8
3}
, Version{major :: Word8
major = Word8
5, minor :: Word8
minor = Word8
2}
, Version{major :: Word8
major = Word8
5, minor :: Word8
minor = Word8
1}
, Version{major :: Word8
major = Word8
5, minor :: Word8
minor = Word8
0}
, Version{major :: Word8
major = Word8
4, minor :: Word8
minor = Word8
4}
]
, timeout :: Int
timeout = Int
10_000
, routing :: Routing
routing = Routing
NoRouting
, user_agent :: UserAgent
user_agent = UserAgent{name :: Text
name = Text
"bolty", version :: Text
version = Text
"2.0"}
, queryLogger :: Maybe (QueryLog -> QueryMeta -> IO ())
queryLogger = Maybe (QueryLog -> QueryMeta -> IO ())
forall a. Maybe a
Nothing
, notificationHandler :: Maybe (Notification -> IO ())
notificationHandler = Maybe (Notification -> IO ())
forall a. Maybe a
Nothing
}
type ValidatedConfig :: Type
data ValidatedConfig = ValidatedConfig
{ ValidatedConfig -> Text
host :: T.Text
, ValidatedConfig -> Word16
port :: Word16
, ValidatedConfig -> Scheme
scheme :: Scheme
, ValidatedConfig -> Bool
use_tls :: Bool
, ValidatedConfig -> [Word32]
versions :: [Word32]
, ValidatedConfig -> Int
timeout :: Int
, ValidatedConfig -> Routing
routing :: Routing
, ValidatedConfig -> UserAgent
user_agent :: UserAgent
, ValidatedConfig -> Maybe (QueryLog -> QueryMeta -> IO ())
queryLogger :: Maybe (QueryLog -> QueryMeta -> IO ())
, ValidatedConfig -> Maybe (Notification -> IO ())
notificationHandler :: Maybe (Notification -> IO ())
}
type ServerState :: Type
data ServerState
= Disconnected
| Connected
| Defunct
| Authentication
| Ready
| Streaming
| TXready
| TXstreaming
| Failed
| Interrupted
deriving stock (Int -> ServerState -> ShowS
[ServerState] -> ShowS
ServerState -> String
(Int -> ServerState -> ShowS)
-> (ServerState -> String)
-> ([ServerState] -> ShowS)
-> Show ServerState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ServerState -> ShowS
showsPrec :: Int -> ServerState -> ShowS
$cshow :: ServerState -> String
show :: ServerState -> String
$cshowList :: [ServerState] -> ShowS
showList :: [ServerState] -> ShowS
Show, ServerState -> ServerState -> Bool
(ServerState -> ServerState -> Bool)
-> (ServerState -> ServerState -> Bool) -> Eq ServerState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ServerState -> ServerState -> Bool
== :: ServerState -> ServerState -> Bool
$c/= :: ServerState -> ServerState -> Bool
/= :: ServerState -> ServerState -> Bool
Eq)
type Connection :: Type
data Connection = Connection
{ Connection -> Connection
rawConnection :: !NC.Connection
, Connection -> Int
timeout_milliseconds :: !Int
, Connection -> Word32
version :: Word32
, Connection -> IORef ServerState
server_state :: IORef ServerState
, Connection -> Text
server_agent :: Text
, Connection -> Text
connection_id :: Text
, Connection -> IORef Word64
lastActivity :: IORef Word64
, Connection -> Bool
telemetry_enabled :: Bool
, Connection -> Maybe Int
serverIdleTimeout :: Maybe Int
, Connection -> Maybe (QueryLog -> QueryMeta -> IO ())
queryLogger :: Maybe (QueryLog -> QueryMeta -> IO ())
, Connection -> Maybe (Notification -> IO ())
notificationHandler :: Maybe (Notification -> IO ())
}
instance Exception Error
type Error :: Type
data Error
= TimeOut T.Text
| AuthentificationFailed
| UnsupportedServerVersion Word32
| ResetFailed
| CannotReadResponse T.Text
| NonboltyError SomeException
| ResponseErrorRecords
| WrongMessageFormat T.Text
| ResponseErrorIgnored
| ResponseErrorFailure T.Text T.Text
| InvalidState ServerState T.Text
| RoutingTableError T.Text
deriving stock (Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
(Int -> Error -> ShowS)
-> (Error -> String) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Error -> ShowS
showsPrec :: Int -> Error -> ShowS
$cshow :: Error -> String
show :: Error -> String
$cshowList :: [Error] -> ShowS
showList :: [Error] -> ShowS
Show)
type Principal :: Type
type Principal = T.Text
type Credentials :: Type
type Credentials = T.Text
type Scheme :: Type
data Scheme
= None
| Basic !Principal !Credentials
| Kerberos
| Bearer !Credentials
deriving stock (Scheme -> Scheme -> Bool
(Scheme -> Scheme -> Bool)
-> (Scheme -> Scheme -> Bool) -> Eq Scheme
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Scheme -> Scheme -> Bool
== :: Scheme -> Scheme -> Bool
$c/= :: Scheme -> Scheme -> Bool
/= :: Scheme -> Scheme -> Bool
Eq, (forall x. Scheme -> Rep Scheme x)
-> (forall x. Rep Scheme x -> Scheme) -> Generic Scheme
forall x. Rep Scheme x -> Scheme
forall x. Scheme -> Rep Scheme x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Scheme -> Rep Scheme x
from :: forall x. Scheme -> Rep Scheme x
$cto :: forall x. Rep Scheme x -> Scheme
to :: forall x. Rep Scheme x -> Scheme
Generic)
type Routing :: Type
data Routing
= NoRouting
| Routing
| RoutingSpec !T.Text !(H.HashMap T.Text T.Text)
deriving stock (Routing -> Routing -> Bool
(Routing -> Routing -> Bool)
-> (Routing -> Routing -> Bool) -> Eq Routing
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Routing -> Routing -> Bool
== :: Routing -> Routing -> Bool
$c/= :: Routing -> Routing -> Bool
/= :: Routing -> Routing -> Bool
Eq)
type UserAgent :: Type
data UserAgent = UserAgent
{ UserAgent -> Text
name :: !T.Text
, UserAgent -> Text
version :: !T.Text
}
deriving stock (UserAgent -> UserAgent -> Bool
(UserAgent -> UserAgent -> Bool)
-> (UserAgent -> UserAgent -> Bool) -> Eq UserAgent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UserAgent -> UserAgent -> Bool
== :: UserAgent -> UserAgent -> Bool
$c/= :: UserAgent -> UserAgent -> Bool
/= :: UserAgent -> UserAgent -> Bool
Eq)
isTransient :: Error -> Bool
isTransient :: Error -> Bool
isTransient (ResponseErrorFailure Text
code Text
_) = Text
"Neo.TransientError." Text -> Text -> Bool
`T.isPrefixOf` Text
code
isTransient (RoutingTableError Text
_) = Bool
True
isTransient Error
_ = Bool
False
isRoutingError :: Error -> Bool
isRoutingError :: Error -> Bool
isRoutingError (ResponseErrorFailure Text
code Text
_) =
Text
"Neo.ClientError.Cluster.NotALeader" Text -> Text -> Bool
`T.isPrefixOf` Text
code
Bool -> Bool -> Bool
|| Text
"Neo.ClientError.General.ForbiddenOnReadOnlyDatabase" Text -> Text -> Bool
`T.isPrefixOf` Text
code
isRoutingError Error
_ = Bool
False