module FoundationDB.Internal.Database
  ( FoundationDBOptions (..),
    defaultOptions,
    Database (..),
    apiVersionInUse,
  )
where

import qualified FoundationDB.Internal.Bindings as FDB
import FoundationDB.Options.DatabaseOption (DatabaseOption (..))
import FoundationDB.Options.NetworkOption (NetworkOption (..))

-- | Options set at the connection level for FoundationDB.
data FoundationDBOptions = FoundationDBOptions
  { -- | Desired API version. See 'currentAPIVersion' for the latest
    -- version installed on your system. The C API (and this library) allow you
    -- to choose any version earlier than 'currentAPIVersion' to get the client
    -- behavior of that version of the FoundationDB client library.
    FoundationDBOptions -> Int
apiVersion :: Int,
    -- | Path to your @fdb.cluster@ file. If 'Nothing', uses
    -- default location.
    FoundationDBOptions -> Maybe FilePath
clusterFile :: Maybe FilePath,
    -- | Additional network options. Each will be set in order.
    FoundationDBOptions -> [NetworkOption]
networkOptions :: [NetworkOption],
    -- | Additional database options. Each will be set in order.
    FoundationDBOptions -> [DatabaseOption]
databaseOptions :: [DatabaseOption]
  }
  deriving (Int -> FoundationDBOptions -> ShowS
[FoundationDBOptions] -> ShowS
FoundationDBOptions -> FilePath
(Int -> FoundationDBOptions -> ShowS)
-> (FoundationDBOptions -> FilePath)
-> ([FoundationDBOptions] -> ShowS)
-> Show FoundationDBOptions
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [FoundationDBOptions] -> ShowS
$cshowList :: [FoundationDBOptions] -> ShowS
show :: FoundationDBOptions -> FilePath
$cshow :: FoundationDBOptions -> FilePath
showsPrec :: Int -> FoundationDBOptions -> ShowS
$cshowsPrec :: Int -> FoundationDBOptions -> ShowS
Show, FoundationDBOptions -> FoundationDBOptions -> Bool
(FoundationDBOptions -> FoundationDBOptions -> Bool)
-> (FoundationDBOptions -> FoundationDBOptions -> Bool)
-> Eq FoundationDBOptions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FoundationDBOptions -> FoundationDBOptions -> Bool
$c/= :: FoundationDBOptions -> FoundationDBOptions -> Bool
== :: FoundationDBOptions -> FoundationDBOptions -> Bool
$c== :: FoundationDBOptions -> FoundationDBOptions -> Bool
Eq, Eq FoundationDBOptions
Eq FoundationDBOptions
-> (FoundationDBOptions -> FoundationDBOptions -> Ordering)
-> (FoundationDBOptions -> FoundationDBOptions -> Bool)
-> (FoundationDBOptions -> FoundationDBOptions -> Bool)
-> (FoundationDBOptions -> FoundationDBOptions -> Bool)
-> (FoundationDBOptions -> FoundationDBOptions -> Bool)
-> (FoundationDBOptions
    -> FoundationDBOptions -> FoundationDBOptions)
-> (FoundationDBOptions
    -> FoundationDBOptions -> FoundationDBOptions)
-> Ord FoundationDBOptions
FoundationDBOptions -> FoundationDBOptions -> Bool
FoundationDBOptions -> FoundationDBOptions -> Ordering
FoundationDBOptions -> FoundationDBOptions -> FoundationDBOptions
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FoundationDBOptions -> FoundationDBOptions -> FoundationDBOptions
$cmin :: FoundationDBOptions -> FoundationDBOptions -> FoundationDBOptions
max :: FoundationDBOptions -> FoundationDBOptions -> FoundationDBOptions
$cmax :: FoundationDBOptions -> FoundationDBOptions -> FoundationDBOptions
>= :: FoundationDBOptions -> FoundationDBOptions -> Bool
$c>= :: FoundationDBOptions -> FoundationDBOptions -> Bool
> :: FoundationDBOptions -> FoundationDBOptions -> Bool
$c> :: FoundationDBOptions -> FoundationDBOptions -> Bool
<= :: FoundationDBOptions -> FoundationDBOptions -> Bool
$c<= :: FoundationDBOptions -> FoundationDBOptions -> Bool
< :: FoundationDBOptions -> FoundationDBOptions -> Bool
$c< :: FoundationDBOptions -> FoundationDBOptions -> Bool
compare :: FoundationDBOptions -> FoundationDBOptions -> Ordering
$ccompare :: FoundationDBOptions -> FoundationDBOptions -> Ordering
$cp1Ord :: Eq FoundationDBOptions
Ord)

-- | Uses the current API version, the default cluster file location, and no
-- additional options.
defaultOptions :: FoundationDBOptions
defaultOptions :: FoundationDBOptions
defaultOptions = Int
-> Maybe FilePath
-> [NetworkOption]
-> [DatabaseOption]
-> FoundationDBOptions
FoundationDBOptions Int
FDB.currentAPIVersion Maybe FilePath
forall a. Maybe a
Nothing [] []

data Database = Database
  { Database -> DatabasePtr
databasePtr :: FDB.DatabasePtr,
    Database -> FoundationDBOptions
databaseFoundationDBOptions :: FoundationDBOptions
  }
  deriving (Int -> Database -> ShowS
[Database] -> ShowS
Database -> FilePath
(Int -> Database -> ShowS)
-> (Database -> FilePath) -> ([Database] -> ShowS) -> Show Database
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [Database] -> ShowS
$cshowList :: [Database] -> ShowS
show :: Database -> FilePath
$cshow :: Database -> FilePath
showsPrec :: Int -> Database -> ShowS
$cshowsPrec :: Int -> Database -> ShowS
Show, Database -> Database -> Bool
(Database -> Database -> Bool)
-> (Database -> Database -> Bool) -> Eq Database
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Database -> Database -> Bool
$c/= :: Database -> Database -> Bool
== :: Database -> Database -> Bool
$c== :: Database -> Database -> Bool
Eq)

-- | Returns the API version that was specified in the 'apiVersion' field when
-- the FDB client was initialized.
apiVersionInUse :: Database -> Int
apiVersionInUse :: Database -> Int
apiVersionInUse = FoundationDBOptions -> Int
apiVersion (FoundationDBOptions -> Int)
-> (Database -> FoundationDBOptions) -> Database -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Database -> FoundationDBOptions
databaseFoundationDBOptions