1
Fork 0

add support for ansi standard offset and fetch syntax

This commit is contained in:
Jake Wheat 2013-12-17 16:00:17 +02:00
parent 8093498f2d
commit b2728ec9bf
5 changed files with 47 additions and 39 deletions
Language/SQL/SimpleSQL

View file

@ -12,6 +12,7 @@
> import Data.Char
> import Text.Parsec hiding (ParseError)
> import qualified Text.Parsec as P
> import Text.Parsec.Perm
> import Language.SQL.SimpleSQL.Syntax
> import Language.SQL.SimpleSQL.Fixity
@ -572,11 +573,26 @@ where, having, limit, offset).
> <*> option Asc (choice [Asc <$ keyword_ "asc"
> ,Desc <$ keyword_ "desc"])
> limit :: P ScalarExpr
> limit = keywordScalarExpr "limit"
allows offset and fetch in either order
+ postgresql offset without row(s) and limit instead of fetch also
> offsetFetch :: P (Maybe ScalarExpr, Maybe ScalarExpr)
> offsetFetch = permute ((,) <$?> (Nothing, Just <$> offset)
> <|?> (Nothing, Just <$> fetch))
> offset :: P ScalarExpr
> offset = keywordScalarExpr "offset"
> offset = try (keyword_ "offset") *> scalarExpr
> <* option () (try $ choice [try (keyword_ "rows"),keyword_ "row"])
> fetch :: P ScalarExpr
> fetch = choice [ansiFetch, limit]
> where
> ansiFetch = try (keyword_ "fetch") >>
> choice [keyword_ "first",keyword_ "next"]
> *> scalarExpr
> <* choice [keyword_ "rows",keyword_ "row"]
> <* keyword_ "only"
> limit = try (keyword_ "limit") *> scalarExpr
== common table expressions
@ -601,7 +617,7 @@ and union, etc..
> >>= optionSuffix queryExprSuffix]
> where
> select = try (keyword_ "select") >>
> Select
> mkSelect
> <$> (fromMaybe All <$> duplicates)
> <*> selectList
> <*> option [] from
@ -609,8 +625,9 @@ and union, etc..
> <*> option [] sgroupBy
> <*> optionMaybe having
> <*> option [] orderBy
> <*> optionMaybe limit
> <*> optionMaybe offset
> <*> offsetFetch
> mkSelect d sl f w g h od (ofs,fe) =
> Select d sl f w g h od ofs fe
> values = try (keyword_ "values")
> >> Values <$> commaSep (parens (commaSep scalarExpr))
> table = try (keyword_ "table") >> Table <$> name
@ -698,7 +715,7 @@ keyword parser also
> blacklist :: [String]
> blacklist =
> ["select", "as", "from", "where", "having", "group", "order"
> ,"limit", "offset"
> ,"limit", "offset", "fetch"
> ,"inner", "left", "right", "full", "natural", "join"
> ,"cross", "on", "using", "lateral"
> ,"when", "then", "case", "end", "in"

View file

@ -149,7 +149,7 @@
= query expressions
> queryExpr :: QueryExpr -> Doc
> queryExpr (Select d sl fr wh gb hv od lm off) =
> queryExpr (Select d sl fr wh gb hv od off fe) =
> sep [text "select"
> ,case d of
> All -> empty
@ -160,8 +160,9 @@
> ,grpBy gb
> ,maybeScalarExpr "having" hv
> ,orderBy od
> ,maybeScalarExpr "limit" lm
> ,maybeScalarExpr "offset" off
> ,maybe empty (\e -> text "offset" <+> scalarExpr e <+> text "rows") off
> ,maybe empty (\e -> text "fetch next" <+> scalarExpr e
> <+> text "rows only") fe
> ]
> queryExpr (CombineQueryExpr q1 ct d c q2) =
> sep [queryExpr q1

View file

@ -145,8 +145,8 @@
> ,qeGroupBy :: [ScalarExpr]
> ,qeHaving :: Maybe ScalarExpr
> ,qeOrderBy :: [(ScalarExpr,Direction)]
> ,qeLimit :: Maybe ScalarExpr
> ,qeOffset :: Maybe ScalarExpr
> ,qeFetch :: Maybe ScalarExpr
> }
> | CombineQueryExpr
> {qe0 :: QueryExpr
@ -177,8 +177,8 @@ I'm not sure if this is valid syntax or not.
> ,qeGroupBy = []
> ,qeHaving = Nothing
> ,qeOrderBy = []
> ,qeLimit = Nothing
> ,qeOffset = Nothing}
> ,qeOffset = Nothing
> ,qeFetch = Nothing}
> -- | represents the Distinct or All keywords, which can be used