diff --git a/src/sut/dumblog/dumblog.cabal b/src/sut/dumblog/dumblog.cabal index d65a6fbf..e405e67b 100644 --- a/src/sut/dumblog/dumblog.cabal +++ b/src/sut/dumblog/dumblog.cabal @@ -29,7 +29,7 @@ extra-source-files: library build-depends: - , base ^>=4.14.1.0 + , base ^>=4.14.1.0 , binary , bytestring , containers @@ -37,6 +37,7 @@ library , filepath , http-types , journal + , sqlite-simple , text , time , wai @@ -51,6 +52,7 @@ library Dumblog.Journal.StateMachine Dumblog.Journal.Types Dumblog.Journal.Worker + Dumblog.SQLite.DB Dumblog.SQLite.Main hs-source-dirs: src diff --git a/src/sut/dumblog/src/Dumblog/SQLite/DB.hs b/src/sut/dumblog/src/Dumblog/SQLite/DB.hs new file mode 100644 index 00000000..8624fa56 --- /dev/null +++ b/src/sut/dumblog/src/Dumblog/SQLite/DB.hs @@ -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