aboutsummaryrefslogtreecommitdiffstats
path: root/src/Store/Query.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Store/Query.hs')
-rw-r--r--src/Store/Query.hs42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/Store/Query.hs b/src/Store/Query.hs
index 091aae7..4c0f829 100644
--- a/src/Store/Query.hs
+++ b/src/Store/Query.hs
@@ -16,6 +16,7 @@ import Data.Map qualified as M
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
import Data.Vector qualified as V
+import GHC.Generics (Generic)
import Store.Exception (DecodeException (DecodeException), MissingFileName (MissingFileName))
import Store.Query.Parser ()
import Store.Query.Printer ()
@@ -24,7 +25,17 @@ import Store.Query.Type
import Store.Store qualified as S
import System.FilePath ((</>))
-query :: Query -> S.StoreM [J.Value]
+data Paginated = Paginated
+ { count :: Int,
+ data_ :: [J.Value]
+ }
+ deriving (Show, Generic)
+
+instance J.ToJSON Paginated
+
+instance J.FromJSON Paginated
+
+query :: Query -> S.StoreM J.Value
query (Delete c w) = do
c' <-
mapM (\fn -> fmap ((fn,) . fromValue c) . decodeFile c $ fn)
@@ -33,7 +44,7 @@ query (Delete c w) = do
lift $ print fps
mapM_ S.deleteFile (map (c </>) fps)
S.commit
- pure []
+ pure (J.toJSON ([] @()))
query (Insert vs c) = do
let vs' = map (\v -> ((c, fileName v), v)) vs
@@ -43,8 +54,8 @@ query (Insert vs c) = do
_ -> throw (MissingFileName v)
mapM_ (\((c, fn), v) -> encodeFile c fn v) vs'
S.commit
- pure []
-query (Select fs c js es w) = do
+ pure (J.toJSON ([] @()))
+query (Select fs c js es w l o) = do
c' <-
mapM (\fn -> fromValue c <$> decodeFile c fn)
=<< S.listFiles c
@@ -64,7 +75,18 @@ query (Select fs c js es w) = do
=<< S.listFiles c
)
es
- pure $ map (Store.Query.select fs) $ where_ w $ embeds es' $ joins js' c'
+ let rs =
+ map (Store.Query.select fs)
+ . where_ w
+ . embeds es'
+ . joins js'
+ $ c'
+ rs' =
+ case l >> o >> pure () of
+ (Just _) ->
+ J.toJSON (Paginated (length rs) . applyLimit l . applyOffset o $ rs)
+ _ -> J.toJSON rs
+ pure rs'
query (Update c v w) = do
c' <-
mapM (\fn -> fmap (((c, fn),) . fromValue c) . decodeFile c $ fn)
@@ -76,7 +98,15 @@ query (Update c v w) = do
)
c''
S.commit
- pure []
+ pure (J.toJSON ([] @()))
+
+applyLimit :: Maybe LimitClause -> [a] -> [a]
+applyLimit Nothing = id
+applyLimit (Just (Limit n)) = take n
+
+applyOffset :: Maybe OffsetClause -> [a] -> [a]
+applyOffset Nothing = id
+applyOffset (Just (Offset n)) = drop n
embeds ::
EmbedClauses (Record [J.Value]) ->