summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Database
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--haskell/src/SpaceTraders/Database.hs56
-rw-r--r--haskell/src/SpaceTraders/Database/Agents.hs14
-rw-r--r--haskell/src/SpaceTraders/Database/Tokens.hs18
3 files changed, 88 insertions, 0 deletions
diff --git a/haskell/src/SpaceTraders/Database.hs b/haskell/src/SpaceTraders/Database.hs
new file mode 100644
index 0000000..fdc32d3
--- /dev/null
+++ b/haskell/src/SpaceTraders/Database.hs
@@ -0,0 +1,56 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE QuasiQuotes #-}
+
+module SpaceTraders.Database
+ ( close
+ , open
+ ) where
+
+import Control.Exception
+import qualified Database.SQLite.Simple as S
+import Text.RawString.QQ
+
+migrations :: [S.Query]
+migrations = [
+ [r|CREATE TABLE schema_version (
+ version INTEGER NOT NULL
+ );
+ |],
+ [r|CREATE TABLE tokens (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+ );
+ |],
+ [r|CREATE TABLE agents (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+ );
+ |],
+ [r|CREATE TABLE contracts (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+ );
+ |]]
+
+close :: S.Connection -> IO ()
+close conn = S.close conn
+
+open :: IO S.Connection
+open = do
+ conn <- S.open "spacetraders.db"
+ S.execute_ conn "PRAGMA foreign_keys = ON;"
+ S.execute_ conn "PRAGMA journal_mode = WAL;"
+ S.withTransaction conn $ do
+ version <- getSchemaVersion conn `catch` defaultVersion
+ mapM_ (S.execute_ conn) $ drop version migrations
+ S.execute_ conn "DELETE FROM schema_version;"
+ S.execute conn "INSERT INTO schema_version (version) VALUES (?);" (S.Only $ length migrations)
+ return conn
+
+getSchemaVersion :: S.Connection -> IO Int
+getSchemaVersion conn = do
+ [[v]] <- S.query_ conn "SELECT version FROM schema_version;"
+ return v
+
+defaultVersion :: SomeException -> IO Int
+defaultVersion _ = return 0
diff --git a/haskell/src/SpaceTraders/Database/Agents.hs b/haskell/src/SpaceTraders/Database/Agents.hs
new file mode 100644
index 0000000..5be7389
--- /dev/null
+++ b/haskell/src/SpaceTraders/Database/Agents.hs
@@ -0,0 +1,14 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE QuasiQuotes #-}
+
+module SpaceTraders.Database.Agents
+ ( setAgent
+ ) where
+
+import Data.Aeson
+import qualified Database.SQLite.Simple as S
+
+import SpaceTraders.Model.Agent
+
+setAgent :: S.Connection -> Agent -> IO ()
+setAgent conn agent = S.execute conn "INSERT INTO agents(data) VALUES (json(?));" (S.Only (encode agent))
diff --git a/haskell/src/SpaceTraders/Database/Tokens.hs b/haskell/src/SpaceTraders/Database/Tokens.hs
new file mode 100644
index 0000000..b907609
--- /dev/null
+++ b/haskell/src/SpaceTraders/Database/Tokens.hs
@@ -0,0 +1,18 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE QuasiQuotes #-}
+
+module SpaceTraders.Database.Tokens
+ ( getToken
+ , setToken
+ ) where
+
+import qualified Database.SQLite.Simple as S
+import qualified Data.Text as T
+
+getToken :: S.Connection -> IO (T.Text)
+getToken conn = do
+ [[token]] <- S.query_ conn "SELECT data FROM tokens;"
+ return token
+
+setToken :: S.Connection -> T.Text -> IO ()
+setToken conn value = S.execute conn "INSERT INTO tokens(data) VALUES (?);" (S.Only value)