aboutsummaryrefslogtreecommitdiffstats
path: root/src/Store/Query
diff options
context:
space:
mode:
Diffstat (limited to 'src/Store/Query')
-rw-r--r--src/Store/Query/Parser.hs16
-rw-r--r--src/Store/Query/Printer.hs14
-rw-r--r--src/Store/Query/Type.hs10
3 files changed, 37 insertions, 3 deletions
diff --git a/src/Store/Query/Parser.hs b/src/Store/Query/Parser.hs
index 99ddc79..5b7bc1b 100644
--- a/src/Store/Query/Parser.hs
+++ b/src/Store/Query/Parser.hs
@@ -51,7 +51,11 @@ instance IsString Query where
w <- P.optional do
where_
whereClause
- pure $ Select fs c js es w,
+ l <- P.optional do
+ limitClause
+ o <- P.optional do
+ offsetClause
+ pure $ Select fs c js es w l o,
do
update
c <- collection
@@ -83,6 +87,8 @@ instance IsString Query where
or = void $ lexeme (P.string "OR")
select = void $ lexeme (P.string "SELECT")
where_ = void $ lexeme (P.string "WHERE")
+ offset_ = void $ lexeme (P.string "OFFSET")
+ limit_ = void $ lexeme (P.string "LIMIT")
collection = lexeme $ P.takeWhile1P (Just "collection") (not . isSpace)
@@ -119,6 +125,14 @@ instance IsString Query where
b <- P.choice [Left <$> value, Right <$> field]
pure $ Eq a b
+ offsetClause = do
+ offset_
+ Offset <$> lexeme P.decimal
+
+ limitClause = do
+ limit_
+ Limit <$> lexeme P.decimal
+
fieldSelector =
lexeme $
P.choice
diff --git a/src/Store/Query/Printer.hs b/src/Store/Query/Printer.hs
index cff543f..ecde378 100644
--- a/src/Store/Query/Printer.hs
+++ b/src/Store/Query/Printer.hs
@@ -26,7 +26,7 @@ instance Show Query where
Just "INTO",
Just (showCollection c)
]
- show (Select fs c js es w) =
+ show (Select fs c js es w l o) =
intercalate " " . catMaybes $
[ Just "SELECT",
Just (showFieldSelector fs),
@@ -34,7 +34,9 @@ instance Show Query where
Just (showCollection c),
showJoinClauses js,
showEmbedClauses es,
- showWhereClause w
+ showWhereClause w,
+ showLimitClause l,
+ showOffsetClause o
]
show (Update c v w) =
intercalate " " . catMaybes $
@@ -106,6 +108,14 @@ showComparison (Eq a b) = intercalate " " [showArg a, "==", showArg b]
where
showArg = either showValue showField
+showOffsetClause :: Maybe OffsetClause -> Maybe String
+showOffsetClause (Just (Offset n)) = Just (" OFFSET %d" <> show n)
+showOffsetClause Nothing = Nothing
+
+showLimitClause :: Maybe LimitClause -> Maybe String
+showLimitClause (Just (Limit n)) = Just (" LIMIT %d" <> show n)
+showLimitClause Nothing = Nothing
+
showValues :: [J.Value] -> Maybe String
showValues [] = Nothing
showValues vs = Just (intercalate ", " (map showValue vs))
diff --git a/src/Store/Query/Type.hs b/src/Store/Query/Type.hs
index 912d020..2f1f5ec 100644
--- a/src/Store/Query/Type.hs
+++ b/src/Store/Query/Type.hs
@@ -11,6 +11,8 @@ module Store.Query.Type
JoinType (..),
Query (..),
WhereClause (..),
+ LimitClause (..),
+ OffsetClause (..),
)
where
@@ -28,6 +30,8 @@ data Query
(JoinClauses FilePath)
(EmbedClauses FilePath)
(Maybe WhereClause)
+ (Maybe LimitClause)
+ (Maybe OffsetClause)
| Update Collection J.Value (Maybe WhereClause)
data FieldSelector
@@ -64,3 +68,9 @@ data WhereClause
data Comparison
= Eq (Either J.Value Field) (Either J.Value Field)
deriving (Show)
+
+data LimitClause = Limit Int
+ deriving (Show)
+
+data OffsetClause = Offset Int
+ deriving (Show)