add support for nulls first/last
This commit is contained in:
parent
e85ab8b831
commit
ebe522b21d
7 changed files with 66 additions and 34 deletions
Language/SQL/SimpleSQL
|
@ -84,15 +84,15 @@ the fixity code.
|
|||
> Star -> str ('v':show e)
|
||||
> AggregateApp nm d es od ->
|
||||
> HSE.App (var ('a':name nm))
|
||||
> $ HSE.List [str $ show (d,map snd od)
|
||||
> $ HSE.List [str $ show (d,orderInf od)
|
||||
> ,HSE.List $ map toHaskell es
|
||||
> ,HSE.List $ map (toHaskell . fst) od]
|
||||
> ,HSE.List $ orderExps od]
|
||||
> WindowApp nm es pb od r ->
|
||||
> HSE.App (var ('w':name nm))
|
||||
> $ HSE.List [str $ show (map snd od, r)
|
||||
> $ HSE.List [str $ show (orderInf od, r)
|
||||
> ,HSE.List $ map toHaskell es
|
||||
> ,HSE.List $ map toHaskell pb
|
||||
> ,HSE.List $ map (toHaskell . fst) od]
|
||||
> ,HSE.List $ orderExps od]
|
||||
> PrefixOp nm e0 ->
|
||||
> HSE.App (HSE.Var $ sym $ name nm) (toHaskell e0)
|
||||
> PostfixOp nm e0 ->
|
||||
|
@ -119,6 +119,10 @@ the fixity code.
|
|||
> name n = case n of
|
||||
> QName q -> "\"" ++ q
|
||||
> Name m -> m
|
||||
> orderExps = map (toHaskell . (\(OrderField a _ _) -> a))
|
||||
> orderInf = map (\(OrderField _ b c) -> (b,c))
|
||||
|
||||
|
||||
|
||||
|
||||
> toSql :: HSE.Exp -> ScalarExpr
|
||||
|
@ -135,17 +139,17 @@ the fixity code.
|
|||
> (HSE.List [HSE.Lit (HSE.String vs)
|
||||
> ,HSE.List es
|
||||
> ,HSE.List od]) ->
|
||||
> let (d,dir) = read vs
|
||||
> let (d,oinf) = read vs
|
||||
> in AggregateApp (unname i) d (map toSql es)
|
||||
> $ zip (map toSql od) dir
|
||||
> $ sord oinf od
|
||||
> HSE.App (HSE.Var (HSE.UnQual (HSE.Ident ('w':i))))
|
||||
> (HSE.List [HSE.Lit (HSE.String vs)
|
||||
> ,HSE.List es
|
||||
> ,HSE.List pb
|
||||
> ,HSE.List od]) ->
|
||||
> let (dir,r) = read vs
|
||||
> let (oinf,r) = read vs
|
||||
> in WindowApp (unname i) (map toSql es) (map toSql pb)
|
||||
> (zip (map toSql od) dir) r
|
||||
> (sord oinf od) r
|
||||
> HSE.App (HSE.Var (HSE.UnQual (HSE.Symbol ('p':nm)))) e0 ->
|
||||
> PostfixOp (unname nm) $ toSql e0
|
||||
> HSE.App (HSE.Var (HSE.UnQual (HSE.Symbol nm))) e0 ->
|
||||
|
@ -165,6 +169,7 @@ the fixity code.
|
|||
> in In b (toSql e0) sq
|
||||
> _ -> err e
|
||||
> where
|
||||
> sord = zipWith (\(i0,i1) ce -> OrderField (toSql ce) i0 i1)
|
||||
> ltom (HSE.List []) = Nothing
|
||||
> ltom (HSE.List [ex]) = Just $ toSql ex
|
||||
> ltom ex = err ex
|
||||
|
|
|
@ -605,12 +605,17 @@ where, having, limit, offset).
|
|||
> having :: P ScalarExpr
|
||||
> having = keywordScalarExpr "having"
|
||||
|
||||
> orderBy :: P [(ScalarExpr,Direction)]
|
||||
> orderBy :: P [OrderField]
|
||||
> orderBy = try (keyword_ "order") *> keyword_ "by" *> commaSep1 ob
|
||||
> where
|
||||
> ob = (,) <$> scalarExpr
|
||||
> <*> option Asc (choice [Asc <$ keyword_ "asc"
|
||||
> ,Desc <$ keyword_ "desc"])
|
||||
> ob = OrderField
|
||||
> <$> scalarExpr
|
||||
> <*> option Asc (choice [Asc <$ keyword_ "asc"
|
||||
> ,Desc <$ keyword_ "desc"])
|
||||
> <*> option NullsOrderDefault
|
||||
> (try (keyword_ "nulls" >>
|
||||
> choice [NullsFirst <$ keyword "first"
|
||||
> ,NullsLast <$ keyword "last"]))
|
||||
|
||||
allows offset and fetch in either order
|
||||
+ postgresql offset without row(s) and limit instead of fetch also
|
||||
|
|
|
@ -260,13 +260,18 @@
|
|||
> grpBy gs = sep [text "group by"
|
||||
> ,nest 9 $ commaSep $ map scalarExpr gs]
|
||||
|
||||
> orderBy :: [(ScalarExpr,Direction)] -> Doc
|
||||
> orderBy :: [OrderField] -> Doc
|
||||
> orderBy [] = empty
|
||||
> orderBy os = sep [text "order by"
|
||||
> ,nest 9 $ commaSep $ map f os]
|
||||
> where
|
||||
> f (e,Asc) = scalarExpr e
|
||||
> f (e,Desc) = scalarExpr e <+> text "desc"
|
||||
> f (OrderField e d n) =
|
||||
> scalarExpr e
|
||||
> <+> (if d == Asc then empty else text "desc")
|
||||
> <+> (case n of
|
||||
> NullsOrderDefault -> empty
|
||||
> NullsFirst -> text "nulls" <+> text "first"
|
||||
> NullsLast -> text "nulls" <+> text "last")
|
||||
|
||||
= utils
|
||||
|
||||
|
|
|
@ -6,7 +6,9 @@
|
|||
> ,Name(..)
|
||||
> ,TypeName(..)
|
||||
> ,Duplicates(..)
|
||||
> ,OrderField(..)
|
||||
> ,Direction(..)
|
||||
> ,NullsOrder(..)
|
||||
> ,InThing(..)
|
||||
> ,SubQueryExprType(..)
|
||||
> ,Frame(..)
|
||||
|
@ -65,7 +67,7 @@
|
|||
> {aggName :: Name -- ^ aggregate function name
|
||||
> ,aggDistinct :: (Maybe Duplicates)-- ^ distinct
|
||||
> ,aggArgs :: [ScalarExpr]-- ^ args
|
||||
> ,aggOrderBy :: [(ScalarExpr,Direction)] -- ^ order by
|
||||
> ,aggOrderBy :: [OrderField] -- ^ order by
|
||||
> }
|
||||
> -- | window application, which adds over (partition by a order
|
||||
> -- by b) to regular function application. Explicit frames are
|
||||
|
@ -74,7 +76,7 @@
|
|||
> {wnName :: Name -- ^ window function name
|
||||
> ,wnArgs :: [ScalarExpr] -- ^ args
|
||||
> ,wnPartition :: [ScalarExpr] -- ^ partition by
|
||||
> ,wnOrderBy :: [(ScalarExpr,Direction)] -- ^ order by
|
||||
> ,wnOrderBy :: [OrderField] -- ^ order by
|
||||
> ,wnFrame :: Maybe Frame -- ^ frame clause
|
||||
> }
|
||||
> -- | Infix binary operators. This is used for symbol operators
|
||||
|
@ -141,6 +143,14 @@
|
|||
> | SqAny
|
||||
> deriving (Eq,Show,Read)
|
||||
|
||||
> data OrderField = OrderField ScalarExpr Direction NullsOrder
|
||||
> deriving (Eq,Show,Read)
|
||||
|
||||
> data NullsOrder = NullsOrderDefault
|
||||
> | NullsFirst
|
||||
> | NullsLast
|
||||
> deriving (Eq,Show,Read)
|
||||
|
||||
> -- | Represents the frame clause of a window
|
||||
> -- this can be [range | rows] frame_start
|
||||
> -- or [range | rows] between frame_start and frame_end
|
||||
|
@ -181,7 +191,7 @@
|
|||
> ,qeWhere :: Maybe ScalarExpr
|
||||
> ,qeGroupBy :: [ScalarExpr]
|
||||
> ,qeHaving :: Maybe ScalarExpr
|
||||
> ,qeOrderBy :: [(ScalarExpr,Direction)]
|
||||
> ,qeOrderBy :: [OrderField]
|
||||
> ,qeOffset :: Maybe ScalarExpr
|
||||
> ,qeFetch :: Maybe ScalarExpr
|
||||
> }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue