summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Database
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--haskell/src/SpaceTraders/Database.hs7
-rw-r--r--haskell/src/SpaceTraders/Database/Systems.hs23
2 files changed, 29 insertions, 1 deletions
diff --git a/haskell/src/SpaceTraders/Database.hs b/haskell/src/SpaceTraders/Database.hs
index ac3aa7b..4fffb07 100644
--- a/haskell/src/SpaceTraders/Database.hs
+++ b/haskell/src/SpaceTraders/Database.hs
@@ -31,7 +31,12 @@ migrations = [
id INTEGER PRIMARY KEY,
data TEXT NOT NULL
);|],
- [r|CREATE UNIQUE INDEX ships_data_symbol ON ships (json_extract(data, '$.symbol'));|]]
+ [r|CREATE UNIQUE INDEX ships_data_symbol ON ships (json_extract(data, '$.symbol'));|],
+ [r|CREATE TABLE systems (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+ );|],
+ [r|CREATE UNIQUE INDEX systems_data_symbol ON systems (json_extract(data, '$.symbol'));|]]
close :: S.Connection -> IO ()
close conn = S.close conn
diff --git a/haskell/src/SpaceTraders/Database/Systems.hs b/haskell/src/SpaceTraders/Database/Systems.hs
new file mode 100644
index 0000000..5312fd0
--- /dev/null
+++ b/haskell/src/SpaceTraders/Database/Systems.hs
@@ -0,0 +1,23 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module SpaceTraders.Database.Systems
+ ( addSystems
+ , getSystems
+ ) where
+
+import Data.Aeson
+import Data.Maybe
+import qualified Database.SQLite.Simple as S
+
+import Data.Text.Encoding (encodeUtf8Builder)
+import Data.ByteString.Builder(toLazyByteString)
+
+import SpaceTraders.Model.System
+
+addSystems :: S.Connection -> [System] -> IO ()
+addSystems conn systems = S.withTransaction conn $ S.executeMany conn "INSERT INTO systems(data) VALUES (json(?));" $ S.Only <$> map encode systems
+
+getSystems :: S.Connection -> IO [System]
+getSystems conn = do
+ ret <- S.query_ conn "SELECT data from systems;"
+ return . catMaybes $ map (decode . toLazyByteString . encodeUtf8Builder . head) ret