summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/Utils.hs
blob: 2f54a81966050e698291a89c033341d6ba623e3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module SpaceTraders.Utils
  ( count
  , decodeText
  , execute
  , fromJSONValue
  , int2ByteString
  , one_
  , query
  , query_
  ) where

import Control.Monad.Reader
import Data.Aeson
import Data.Aeson.Types
import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Internal as B
import Data.Maybe
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Database.SQLite.Simple as S

import SpaceTraders

count :: (HasDatabaseConn env, MonadFail m, MonadReader env m, MonadIO m, S.ToRow t) => S.Query -> t -> m Int
count q t = do
  env <- ask
  [[ret]] <- liftIO (S.query (getConn env) q t :: IO [[Int]])
  return ret

decodeText :: FromJSON a => T.Text -> Maybe a
decodeText = decode . B.toLazyByteString . T.encodeUtf8Builder

execute :: (HasDatabaseConn env, MonadReader env m, MonadIO m, S.ToRow t) => S.Query -> t -> m ()
execute q t = do
  env <- ask
  liftIO $ S.execute (getConn env) q t

fromJSONValue :: FromJSON a => Value -> Either String a
fromJSONValue = parseEither parseJSON

int2ByteString :: Int -> B.ByteString
int2ByteString = B.pack . map B.c2w . show

one_ :: (FromJSON b, HasDatabaseConn env, MonadReader env m, MonadIO m) => S.Query -> m b
one_ q = query_ q >>= pure . head

query :: (FromJSON b, HasDatabaseConn env, MonadReader env m, MonadIO m, S.ToRow t) => S.Query -> t -> m [b]
query q t = do
  env <- ask
  ret <- liftIO $ S.query (getConn env) q t
  return . catMaybes $ map (decodeText . head) ret

query_ :: (FromJSON b, HasDatabaseConn env, MonadReader env m, MonadIO m) => S.Query -> m [b]
query_ q = do
  env <- ask
  ret <- liftIO $ S.query_ (getConn env) q
  return . catMaybes $ map (decodeText . head) ret