From a980a128c54dff021ec21478e60b5e241749d504 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Thu, 8 Feb 2024 02:03:40 +0100 Subject: `SELECT .` -> `SELECT *` --- app/Main.hs | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index c0e6d11..493a807 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -27,19 +27,21 @@ debug s x = trace (printf "%s: %s" s (show x)) x main :: IO () main = do setCurrentDirectory "./data" - putStrLn "> SELECT . FROM c" - query' $ Select [Unqualified "."] "c" [] [] + putStrLn "> SELECT * FROM c" + query' $ Select All "c" [] [] - putStrLn "\n> SELECT id FROM c" - query' $ Select [Unqualified "id"] "c" [] [] + putStrLn "\n> SELECT * FROM j" + query' $ Select All "j" [] [] putStrLn "\n> SELECT c.id, j.id, is_j FROM c LEFT JOIN j ON j.id == c.j_id" query' $ Select - [ Qualified "c" "id", - Qualified "j" "id", - Unqualified "is_j" - ] + ( Only + [ Qualified "c" "id", + Qualified "j" "id", + Unqualified "is_j" + ] + ) "c" [ LeftJoin "j" @@ -51,9 +53,11 @@ main = do putStrLn "\n> SELECT c.id, j.id FROM c RIGHT JOIN j ON j.id == c.j_id" query' $ Select - [ Qualified "c" "id", - Qualified "j" "id" - ] + ( Only + [ Qualified "c" "id", + Qualified "j" "id" + ] + ) "c" [ RightJoin "j" @@ -65,9 +69,11 @@ main = do putStrLn "\n> SELECT c.id, j.id FROM c FULL JOIN j ON j.id == c.j_id" query' $ Select - [ Qualified "c" "id", - Qualified "j" "id" - ] + ( Only + [ Qualified "c" "id", + Qualified "j" "id" + ] + ) "c" [ FullJoin "j" @@ -77,7 +83,12 @@ main = do [] data Query - = Select [Field] Collection [Join FilePath] Where + = Select FieldSelector Collection [Join FilePath] Where + deriving (Show) + +data FieldSelector + = All + | Only [Field] deriving (Show) data Field @@ -179,8 +190,10 @@ decodeFile :: J.FromJSON a => FilePath -> IO a decodeFile = fmap (fromMaybe (throw DecodeException)) . J.decodeFileStrict -select :: [Field] -> [Record J.Value] -> J.Value -select fs vs = +select :: FieldSelector -> [Record J.Value] -> J.Value +select All vs = + join' (map (\(Record _ v) -> v) vs) +select (Only fs) vs = mergeUnsafe (join' (map ((\(Record _ v) -> v) . select' fs) vs)) v0 where v0 = @@ -188,13 +201,11 @@ select fs vs = mapMaybe ( \f -> case f of Qualified c k -> Just $ J.Object $ JM.singleton (JK.fromText (T.pack c <> "." <> k)) J.Null - Unqualified "." -> Nothing Unqualified k -> Just $ J.Object $ JM.singleton (JK.fromText k) J.Null ) fs select' :: [Field] -> Record J.Value -> Record J.Value -select' [Unqualified "."] v = v select' fs (Record c (J.Object kvs)) = Record c . J.Object $ JM.fromList . mapMaybe match . JM.toList $ -- cgit v1.2.3