-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sut): add db module for sqlite dumblog
- Loading branch information
1 parent
9a84f89
commit 2019aaa
Showing
2 changed files
with
36 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -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 |