-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
Database.Table.SQLite.Simple
in Deposit Wallet
- Loading branch information
1 parent
6bb0184
commit 8c0211b
Showing
2 changed files
with
32 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 31 additions & 46 deletions
77
lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO/DB.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,61 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE Rank2Types #-} | ||
module Cardano.Wallet.Deposit.IO.DB | ||
( SqlM | ||
, SqlContext (..) | ||
( Connection | ||
, withSqliteFile | ||
, withSqlContextInMemory | ||
, withSqliteInMemory | ||
|
||
, SqlM | ||
, runSqlM | ||
|
||
, DBLog (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.BM.Extra | ||
( bracketTracer | ||
) | ||
import Cardano.DB.Sqlite | ||
( DBLog (..) | ||
) | ||
import Control.Concurrent.MVar | ||
( newMVar | ||
, withMVar | ||
) | ||
import Control.Monad.Trans.Reader | ||
( ReaderT (..) | ||
) | ||
import Control.Tracer | ||
( Tracer | ||
, contramap | ||
, traceWith | ||
) | ||
|
||
import qualified Database.SQLite.Simple as Sqlite | ||
import Database.Table.SQLite.Simple | ||
( Connection | ||
, SqlM | ||
, runSqlM | ||
, withConnection | ||
) | ||
|
||
{----------------------------------------------------------------------------- | ||
SqlContext | ||
------------------------------------------------------------------------------} | ||
-- | Monad to run SQL queries in. | ||
type SqlM = ReaderT Sqlite.Connection IO | ||
|
||
-- | A facility to run 'SqlM' computations. | ||
-- Importantly, computations are not run in parallel, but sequenced. | ||
newtype SqlContext = SqlContext | ||
{ runSqlM :: forall a. SqlM a -> IO a | ||
} | ||
|
||
-- | Acquire and release an 'SqlContext' in memory. | ||
withSqlContextInMemory | ||
-- | Acquire and release an SQLite 'Connection' in memory. | ||
withSqliteInMemory | ||
:: Tracer IO DBLog | ||
-- ^ Logging | ||
-> (SqlContext -> IO a) | ||
-> (Connection -> IO a) | ||
-- ^ Action to run | ||
-> IO a | ||
withSqlContextInMemory tr = withSqliteFile tr ":memory:" | ||
withSqliteInMemory tr = withSqliteFile tr ":memory:" | ||
|
||
-- | Use sqlite to open a database file | ||
-- and provide an 'SqlContext' for running 'SqlM' actions. | ||
-- | Acquire and release an SQLite 'Connection' from a file. | ||
withSqliteFile | ||
:: Tracer IO DBLog | ||
-- ^ Logging | ||
-> FilePath | ||
-- ^ Database file | ||
-> (SqlContext -> IO a) | ||
-> (Connection -> IO a) | ||
-- ^ Action to run | ||
-> IO a | ||
withSqliteFile tr filepath action = | ||
Sqlite.withConnection filepath $ \connection0 -> do | ||
traceWith tr $ MsgOpenSingleConnection filepath | ||
-- The lock ensures that database operations are sequenced. | ||
lock <- newMVar connection0 | ||
let runSqlM :: SqlM a -> IO a | ||
runSqlM cmd = withMVar lock (observe . runReaderT cmd) | ||
action SqlContext{runSqlM} | ||
where | ||
observe :: IO a -> IO a | ||
observe = bracketTracer (contramap MsgRun tr) | ||
withConnection filepath $ \conn -> do | ||
traceWith tr $ MsgStartConnection filepath | ||
result <- action conn | ||
traceWith tr $ MsgDoneConnection filepath | ||
pure result | ||
|
||
{------------------------------------------------------------------------------- | ||
Logging | ||
-------------------------------------------------------------------------------} | ||
|
||
data DBLog | ||
= MsgStartConnection FilePath | ||
| MsgDoneConnection FilePath | ||
deriving (Show, Eq) |