summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Database
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-04 19:37:50 +0200
committerJulien Dessaux2023-07-04 19:37:50 +0200
commit78c5467c4ac39869314c686d1d026482e7b3fd8f (patch)
tree154bc89cd8f9ea87e93d7d638e41f6557daddf22 /haskell/src/SpaceTraders/Database
parent[haskell] Implemented contract (diff)
downloadspacetraders-78c5467c4ac39869314c686d1d026482e7b3fd8f.tar.gz
spacetraders-78c5467c4ac39869314c686d1d026482e7b3fd8f.tar.bz2
spacetraders-78c5467c4ac39869314c686d1d026482e7b3fd8f.zip
[haskell] Implemented ship
Diffstat (limited to '')
-rw-r--r--haskell/src/SpaceTraders/Database.hs17
-rw-r--r--haskell/src/SpaceTraders/Database/Ships.hs13
2 files changed, 22 insertions, 8 deletions
diff --git a/haskell/src/SpaceTraders/Database.hs b/haskell/src/SpaceTraders/Database.hs
index fdc32d3..ac3aa7b 100644
--- a/haskell/src/SpaceTraders/Database.hs
+++ b/haskell/src/SpaceTraders/Database.hs
@@ -14,23 +14,24 @@ 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
- );
- |]]
+ );|],
+ [r|CREATE TABLE ships (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+ );|],
+ [r|CREATE UNIQUE INDEX ships_data_symbol ON ships (json_extract(data, '$.symbol'));|]]
close :: S.Connection -> IO ()
close conn = S.close conn
diff --git a/haskell/src/SpaceTraders/Database/Ships.hs b/haskell/src/SpaceTraders/Database/Ships.hs
new file mode 100644
index 0000000..81c422d
--- /dev/null
+++ b/haskell/src/SpaceTraders/Database/Ships.hs
@@ -0,0 +1,13 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module SpaceTraders.Database.Ships
+ ( addShip
+ ) where
+
+import Data.Aeson
+import qualified Database.SQLite.Simple as S
+
+import SpaceTraders.Model.Ship
+
+addShip :: S.Connection -> Ship -> IO ()
+addShip conn ship = S.execute conn "INSERT INTO ships(data) VALUES (json(?));" (S.Only (encode ship))