summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Database
diff options
context:
space:
mode:
Diffstat (limited to 'haskell/src/SpaceTraders/Database')
-rw-r--r--haskell/src/SpaceTraders/Database/Systems.hs23
1 files changed, 23 insertions, 0 deletions
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