add support for limit and offset
This commit is contained in:
parent
1a8551825d
commit
a81f62b940
5 changed files with 88 additions and 31 deletions
Language/SQL/SimpleSQL
|
@ -73,7 +73,8 @@ digitse[+-]digits
|
|||
> blacklist :: [String]
|
||||
> blacklist = ["as", "from", "where", "having", "group", "order"
|
||||
> ,"inner", "left", "right", "full", "natural", "join"
|
||||
> ,"on", "using", "when", "then", "case", "end", "order"]
|
||||
> ,"on", "using", "when", "then", "case", "end", "order"
|
||||
> ,"limit", "offset"]
|
||||
|
||||
TODO: talk about what must be in the blacklist, and what doesn't need
|
||||
to be.
|
||||
|
@ -209,12 +210,16 @@ to be.
|
|||
|
||||
= query expressions
|
||||
|
||||
> duplicates :: P Duplicates
|
||||
> duplicates = option All $ try $ choice [All <$ keyword_ "all"
|
||||
> ,Distinct <$ keyword "distinct"]
|
||||
|
||||
> selectItem :: P (Maybe String, ScalarExpr)
|
||||
> selectItem = flip (,) <$> scalarExpr <*> optionMaybe (try alias)
|
||||
> where alias = optional (try (keyword_ "as")) *> identifierString
|
||||
|
||||
> selectList :: P [(Maybe String,ScalarExpr)]
|
||||
> selectList = try (keyword_ "select") *> commaSep1 selectItem
|
||||
> selectList = commaSep1 selectItem
|
||||
|
||||
> from :: P [TableRef]
|
||||
> from = option [] (try (keyword_ "from") *> commaSep1 tref)
|
||||
|
@ -256,8 +261,11 @@ to be.
|
|||
> alias j = let a1 = optional (try (keyword_ "as")) *> identifierString
|
||||
> in option j (JoinAlias j <$> try a1)
|
||||
|
||||
> optionalScalarExpr :: String -> P (Maybe ScalarExpr)
|
||||
> optionalScalarExpr k = optionMaybe (try (keyword_ k) *> scalarExpr)
|
||||
|
||||
> swhere :: P (Maybe ScalarExpr)
|
||||
> swhere = optionMaybe (try (keyword_ "where") *> scalarExpr)
|
||||
> swhere = optionalScalarExpr "where"
|
||||
|
||||
> sgroupBy :: P [ScalarExpr]
|
||||
> sgroupBy = option [] (try (keyword_ "group")
|
||||
|
@ -265,7 +273,7 @@ to be.
|
|||
> *> commaSep1 scalarExpr)
|
||||
|
||||
> having :: P (Maybe ScalarExpr)
|
||||
> having = optionMaybe (try (keyword_ "having") *> scalarExpr)
|
||||
> having = optionalScalarExpr "having"
|
||||
|
||||
> orderBy :: P [(ScalarExpr,Direction)]
|
||||
> orderBy = option [] (try (keyword_ "order")
|
||||
|
@ -276,16 +284,26 @@ to be.
|
|||
> <*> option Asc (choice [Asc <$ keyword_ "asc"
|
||||
> ,Desc <$ keyword_ "desc"])
|
||||
|
||||
> limit :: P (Maybe ScalarExpr)
|
||||
> limit = optionalScalarExpr "limit"
|
||||
|
||||
> offset :: P (Maybe ScalarExpr)
|
||||
> offset = optionalScalarExpr "offset"
|
||||
|
||||
|
||||
> queryExpr :: P QueryExpr
|
||||
> queryExpr =
|
||||
> try (keyword_ "select") >>
|
||||
> Select
|
||||
> <$> selectList
|
||||
> <$> duplicates
|
||||
> <*> selectList
|
||||
> <*> from
|
||||
> <*> swhere
|
||||
> <*> sgroupBy
|
||||
> <*> having
|
||||
> <*> orderBy
|
||||
|
||||
> <*> limit
|
||||
> <*> offset
|
||||
|
||||
------------------------------------------------
|
||||
|
||||
|
|
|
@ -51,14 +51,20 @@ back into SQL source text. It attempts to format the output nicely.
|
|||
= query expressions
|
||||
|
||||
> queryExpr :: QueryExpr -> Doc
|
||||
> queryExpr (Select sl fr wh gb hv od) =
|
||||
> queryExpr (Select d sl fr wh gb hv od lm off) =
|
||||
> sep [text "select"
|
||||
> ,case d of
|
||||
> All -> empty
|
||||
> Distinct -> text "distinct"
|
||||
> ,nest 4 $ sep [selectList sl]
|
||||
> ,from fr
|
||||
> ,whr wh
|
||||
> ,maybeScalarExpr "where" wh
|
||||
> ,grpBy gb
|
||||
> ,having hv
|
||||
> ,orderBy od]
|
||||
> ,maybeScalarExpr "having" hv
|
||||
> ,orderBy od
|
||||
> ,maybeScalarExpr "limit" lm
|
||||
> ,maybeScalarExpr "offset" off
|
||||
> ]
|
||||
|
||||
> selectList :: [(Maybe String, ScalarExpr)] -> Doc
|
||||
> selectList is = commaSep $ map si is
|
||||
|
@ -97,20 +103,16 @@ back into SQL source text. It attempts to format the output nicely.
|
|||
> joinCond Nothing = empty
|
||||
> joinCond (Just JoinNatural) = empty
|
||||
|
||||
> whr :: Maybe ScalarExpr -> Doc
|
||||
> whr = maybe empty
|
||||
> (\w -> sep [text "where"
|
||||
> ,nest 4 $ scalarExpr w])
|
||||
> maybeScalarExpr :: String -> Maybe ScalarExpr -> Doc
|
||||
> maybeScalarExpr k = maybe empty
|
||||
> (\e -> sep [text k
|
||||
> ,nest 4 $ scalarExpr e])
|
||||
|
||||
> grpBy :: [ScalarExpr] -> Doc
|
||||
> grpBy [] = empty
|
||||
> grpBy gs = sep [text "group by"
|
||||
> ,nest 4 $ commaSep $ map scalarExpr gs]
|
||||
|
||||
> having :: Maybe ScalarExpr -> Doc
|
||||
> having = maybe empty
|
||||
> (\w -> sep [text "having"
|
||||
> ,nest 4 $ scalarExpr w])
|
||||
> orderBy :: [(ScalarExpr,Direction)] -> Doc
|
||||
> orderBy [] = empty
|
||||
> orderBy os = sep [text "order by"
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
> ,TableRef(..)
|
||||
> ,JoinType(..)
|
||||
> ,JoinCondition(..)
|
||||
> ,Duplicates(..)
|
||||
> ,Direction(..)
|
||||
> ) where
|
||||
|
||||
|
@ -26,23 +27,30 @@
|
|||
|
||||
> data QueryExpr
|
||||
> = Select
|
||||
> {qeSelectList :: [(Maybe String,ScalarExpr)]
|
||||
> {qeDuplicates :: Duplicates
|
||||
> ,qeSelectList :: [(Maybe String,ScalarExpr)]
|
||||
> ,qeFrom :: [TableRef]
|
||||
> ,qeWhere :: Maybe ScalarExpr
|
||||
> ,qeGroupBy :: [ScalarExpr]
|
||||
> ,qeHaving :: Maybe ScalarExpr
|
||||
> ,qeOrderBy :: [(ScalarExpr,Direction)]
|
||||
> ,qeLimit :: Maybe ScalarExpr
|
||||
> ,qeOffset :: Maybe ScalarExpr
|
||||
> } deriving (Eq,Show)
|
||||
|
||||
> data Duplicates = Distinct | All deriving (Eq,Show)
|
||||
> data Direction = Asc | Desc deriving (Eq,Show)
|
||||
|
||||
> makeSelect :: QueryExpr
|
||||
> makeSelect = Select {qeSelectList = []
|
||||
> makeSelect = Select {qeDuplicates = All
|
||||
> ,qeSelectList = []
|
||||
> ,qeFrom = []
|
||||
> ,qeWhere = Nothing
|
||||
> ,qeGroupBy = []
|
||||
> ,qeHaving = Nothing
|
||||
> ,qeOrderBy = []}
|
||||
> ,qeOrderBy = []
|
||||
> ,qeLimit = Nothing
|
||||
> ,qeOffset = Nothing}
|
||||
|
||||
|
||||
> data TableRef = SimpleTableRef String
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue