aboutsummaryrefslogtreecommitdiffstats
path: root/src/Store/Query/Parser.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Store/Query/Parser.hs')
-rw-r--r--src/Store/Query/Parser.hs115
1 files changed, 104 insertions, 11 deletions
diff --git a/src/Store/Query/Parser.hs b/src/Store/Query/Parser.hs
index cb02f32..e16c926 100644
--- a/src/Store/Query/Parser.hs
+++ b/src/Store/Query/Parser.hs
@@ -4,10 +4,14 @@ module Store.Query.Parser () where
import Control.Exception (throw)
import Control.Monad (void)
+import Data.Aeson qualified as J
+import Data.Aeson.Key qualified as JK
+import Data.Aeson.KeyMap qualified as JM
import Data.Char (isSpace)
import Data.Map qualified as M
import Data.String (IsString (fromString))
import Data.Text qualified as T
+import Data.Vector qualified as V
import Data.Void (Void)
import Store.Exception (ParseError (ParseError))
import Store.Query.Type
@@ -22,21 +26,53 @@ instance IsString Query where
where
parser = do
void $ P.many P.space1
- select
- fs <- fieldSelector
- from
- c <- collection
- js <- joinClauses
- es <- embedClauses
- w <- P.optional do
- where_
- whereClause
- P.eof
- pure $ Select fs c js es w
+ P.choice
+ [ do
+ delete
+ from
+ c <- collection
+ w <- P.optional do
+ where_
+ whereClause
+ pure $ Delete c w,
+ do
+ insert
+ vs <- objects
+ into
+ c <- collection
+ pure $ Insert vs c,
+ do
+ select
+ fs <- fieldSelector
+ from
+ c <- collection
+ js <- joinClauses
+ es <- embedClauses
+ w <- P.optional do
+ where_
+ whereClause
+ pure $ Select fs c js es w,
+ do
+ update
+ c <- collection
+ set
+ v <- object
+ w <-
+ P.optional do
+ where_
+ whereClause
+ pure $ Update c v w
+ ]
+ <* P.eof
lexeme :: P.Parsec Void String a -> P.Parsec Void String a
lexeme = P.lexeme $ void $ P.takeWhileP Nothing isSpace
+ set = void $ lexeme (P.string "SET")
+ update = void $ lexeme (P.string "UPDATE")
+ into = void $ lexeme (P.string "INTO")
+ insert = void $ lexeme (P.string "INSERT")
+ delete = void $ lexeme (P.string "DELETE")
and = void $ lexeme (P.string "AND")
colon = void $ lexeme (P.string ":")
comma = void $ lexeme (P.string ",")
@@ -120,6 +156,63 @@ instance IsString Query where
SelectField <$> field
]
+ objects = P.sepBy object comma
+
+ object = lexeme do
+ void $ lexeme $ P.string "{"
+ kvs <-
+ P.sepBy
+ ( do
+ k <-
+ lexeme $ do
+ void $ lexeme $ P.string "\""
+ P.takeWhile1P
+ Nothing
+ (/= '\"')
+ <* (void $ lexeme $ P.string "\"")
+ lexeme $ colon
+ v <- value
+ pure (JK.fromString k, v)
+ )
+ comma
+ void $ lexeme $ P.string "}"
+ pure $ J.Object (JM.fromList kvs)
+
+ value =
+ P.choice
+ [ object,
+ array,
+ string,
+ number,
+ bool,
+ null_
+ ]
+
+ array = lexeme do
+ void $ lexeme $ P.string "["
+ vs <- P.sepBy value comma
+ void $ lexeme $ P.string "]"
+ pure $ J.Array (V.fromList vs)
+
+ string = lexeme do
+ void $ lexeme $ P.string "\""
+ s <- P.takeWhileP Nothing (\c -> c /= '\"')
+ void $ lexeme $ P.string "\""
+ pure $ J.String (T.pack s)
+
+ number = lexeme do
+ J.Number <$> P.scientific
+
+ bool = lexeme do
+ J.Bool
+ <$> P.choice
+ [ const True <$> P.string "true",
+ const False <$> P.string "false"
+ ]
+
+ null_ = lexeme do
+ const J.Null <$> P.string "null"
+
field :: P.Parsec Void String Field
field =
lexeme do