aboutsummaryrefslogtreecommitdiffstats
path: root/app/Main.hs
blob: d650a33d30de3a547407f8499120a9d76e792ce3 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Exception (Exception, throw)
import Data.Aeson qualified as J
import Data.Aeson.Key qualified as JK
import Data.Aeson.KeyMap qualified as JM
import Data.ByteString.Lazy.Char8 qualified as LB
import Data.List (foldl')
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Set qualified as S
import Data.String (IsString (fromString))
import Data.Text qualified as T
import Debug.Trace (trace)
import System.Directory (listDirectory, setCurrentDirectory)
import System.FilePath (takeExtension, (</>))
import Text.Megaparsec qualified as P
import Text.Megaparsec.Char qualified as P
import Text.Megaparsec.Char.Lexer qualified as P
import Text.Printf (printf)

debug :: Show a => String -> a -> a
debug s x = trace (printf "%s: %s" s (show x)) x

main = do
  setCurrentDirectory "./data"
  -- query "SELECT . FROM c"
  query' $ Select [Unqualified "."] "c" [] []
  putStrLn ""
  -- query "SELECT id FROM c"
  query' $ Select [Qualified "c" "id"] "c" [] []
  putStrLn ""
  -- query "SELECT c.id, j.id, is_j FROM c JOIN j WHERE j.id == c.j_id"
  query' $
    Select
      [ Qualified "c" "id",
        Qualified "j" "id",
        Unqualified "is_j"
      ]
      "c"
      ["j"]
      [ Eq (Qualified "j" "id") (Qualified "c" "j_id")
      ]

data Query
  = Select [Field] Collection Join Where
  deriving (Show)

data Field
  = Qualified FilePath T.Text
  | Unqualified T.Text
  deriving (Show)

type Collection = FilePath

type Join = [FilePath]

type Where = [Cmp]

data Cmp
  = Eq Field Field
  deriving (Show)

data Record a
  = Record FilePath a
  deriving (Show, Eq)

{-
instance IsString Query where
  fromString =
    either throw id (P.parse parser "" s)-}

query :: Query -> IO [J.Value]
query (Select fs c js ws) = do
  c' <- mapM (fmap (Record c) . decodeFile . (c </>)) =<< ls c
  js' <- mapM (\j -> mapM (fmap (Record j) . decodeFile . (j </>)) =<< ls j) js
  pure $ map (select fs) $ where_ ws $ combine c' js'

combine c = combine' (map (: []) c)
  where
    combine' cs [] = cs
    combine' cs (js : jss) = combine' [c ++ [j] | c <- cs, j <- js] jss

ls :: FilePath -> IO [FilePath]
ls =
  fmap (filter ((== ".json") . takeExtension)) . listDirectory

data DecodeException = DecodeException
  deriving (Show)

instance Exception DecodeException

decodeFile :: J.FromJSON a => FilePath -> IO a
decodeFile =
  fmap (fromMaybe (throw DecodeException)) . J.decodeFileStrict

select :: [Field] -> [Record J.Value] -> J.Value
select fs vs = join' $ map (select' fs) vs

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 $
      kvs
  where
    match (k, v) = case filter (matches (Record c (JK.toText k))) fs of
      (Qualified _ _ : _) -> Just (JK.fromString (c <> "." <> JK.toString k), v)
      (Unqualified _ : _) -> Just (k, v)
      _ -> Nothing

matches :: Record T.Text -> Field -> Bool
matches (Record c k) (Qualified c' k') = c == c' && k == k'
matches (Record _ k) (Unqualified k') = k == k'

join' :: [Record J.Value] -> J.Value
join' vs = foldl' merge (J.Object JM.empty) (map (\(Record _ v) -> v) vs)

where_ :: Where -> [[Record J.Value]] -> [[Record J.Value]]
where_ ws vss = filter (\vs -> all (\w -> satisfy w vs) ws) vss
  where
    satisfy (Eq f f') vs = unique f vs == unique f' vs

data DuplicateField' = DuplicateField'
  deriving (Show)

instance Exception DuplicateField'

unique :: Field -> [Record J.Value] -> J.Value
unique f as = case mapMaybe (get f) as of
  [Record _ v] -> v
  (_ : _) -> throw DuplicateField'

get :: Field -> Record J.Value -> Maybe (Record J.Value)
get (Unqualified k) (Record c (J.Object kvs)) =
  Record c <$> JM.lookup (JK.fromText k) kvs
get (Qualified c' k) (Record c (J.Object kvs))
  | c' == c = Record c <$> JM.lookup (JK.fromText k) kvs
  | otherwise = Nothing

data DuplicateField = DuplicateField
  deriving (Show)

instance Exception DuplicateField

merge :: J.Value -> J.Value -> J.Value
merge (J.Object kvs) (J.Object kvs') =
  case disjoint kvs kvs' of
    True -> J.Object (JM.union kvs kvs')
    False -> throw DuplicateField

disjoint :: JM.KeyMap v -> JM.KeyMap v -> Bool
disjoint kvs kvs' =
  let ks = S.fromList (JM.keys kvs)
      ks' = S.fromList (JM.keys kvs')
   in S.size ks + S.size ks' == S.size (ks `S.union` ks')

query' :: Query -> IO ()
query' q = mapM_ (LB.putStrLn . J.encode) =<< query q