Skip to content

Commit

Permalink
feat(sut): add db module for sqlite dumblog
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Feb 18, 2022
1 parent 9a84f89 commit 2019aaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/sut/dumblog/dumblog.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ extra-source-files:

library
build-depends:
, base ^>=4.14.1.0
, base ^>=4.14.1.0
, binary
, bytestring
, containers
, directory
, filepath
, http-types
, journal
, sqlite-simple
, text
, time
, wai
Expand All @@ -51,6 +52,7 @@ library
Dumblog.Journal.StateMachine
Dumblog.Journal.Types
Dumblog.Journal.Worker
Dumblog.SQLite.DB
Dumblog.SQLite.Main

hs-source-dirs: src
Expand Down
33 changes: 33 additions & 0 deletions src/sut/dumblog/src/Dumblog/SQLite/DB.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Dumblog.SQLite.DB where

import Data.ByteString (ByteString)
import Database.SQLite.Simple
( Connection
, Only(Only)
, execute
, execute_
, lastInsertRowId
, open
, query
)

------------------------------------------------------------------------

sQLITE_DB_PATH :: FilePath
sQLITE_DB_PATH = "/tmp/dumblog.sqlite3"

initDB :: IO Connection
initDB = do
conn <- open sQLITE_DB_PATH
execute_ conn "CREATE TABLE IF NOT EXISTS dumblog (ix INTEGER PRIMARY KEY, value BLOB)"
return conn

writeDB :: Connection -> ByteString -> IO Int
writeDB conn bs = do
execute conn "INSERT INTO dumblog (value) VALUES (?)" (Only bs)
fromIntegral <$> lastInsertRowId conn

readDB :: Connection -> Int -> IO ByteString
readDB conn ix = do
[[bs]] <- query conn "SELECT value from dumblog WHERE ix = ?" (Only ix)
return bs

0 comments on commit 2019aaa

Please sign in to comment.