on dev help sign in
docs
commit8b44f7f29a2b9548755f97a3abc8f0bc361258a6 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateSat 13 Apr 2019, 18:12 (8 years ago) parent4a7f34b
3 files changed+36−20
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Window.hs+11−6 squeal-postgresql/src/Squeal/PostgreSQL/List.hs+4−4 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+21−10
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Window.hs+11−6
@@ -23,9 +23,8 @@ Window functions and definitions
2323#-}
2424
2525module Squeal.PostgreSQL.Expression.Window
26− ( WindowDefinition (..)
27− , partitionBy
28− , WindowFunction (..)
26+ ( -- * functions
27+ partitionBy
2928 , rank
3029 , rowNumber
3130 , denseRank
@@ -37,6 +36,11 @@ module Squeal.PostgreSQL.Expression.Window
3736 , firstValue
3837 , lastValue
3938 , nthValue
39+ , unsafeWindowFunction1
40+ , unsafeWindowFunctionN
41+ -- * types
42+ , WindowFunction (..)
43+ , WindowDefinition (..)
4044 , WinFun0
4145 , WinFun1
4246 , WinFunN
@@ -93,16 +97,15 @@ instance Aggregate
9397 varPop = unsafeWindowFunction1 "var_pop"
9498 varSamp = unsafeWindowFunction1 "var_samp"
9599
96−
97100-- | A `WindowDefinition` is a set of table rows that are somehow related
98101-- to the current row
99102data WindowDefinition outer commons grp schemas params from where
100103 WindowDefinition
101104 :: SOP.SListI bys
102105 => NP (Expression outer commons grp schemas params from) bys
103− -- ^ partitions
106+ -- ^ `partitionBy` clause
104107 -> [SortExpression outer commons grp schemas params from]
105− -- ^ ordering
108+ -- ^ `Squeal.PostgreSQL.Expression.Sort.orderBy` clause
106109 -> WindowDefinition outer commons grp schemas params from
107110
108111instance OrderBy WindowDefinition where
@@ -186,10 +189,12 @@ type WinFunN xs y
186189 -> WindowFunction outer commons grp schemas params from y
187190 -- ^ output
188191
192+-- | escape hatch for defining window functions
189193unsafeWindowFunction1 :: ByteString -> WinFun1 x y
190194unsafeWindowFunction1 fun x
191195 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
192196
197+-- | escape hatch for defining multi-argument window functions
193198unsafeWindowFunctionN :: SOP.SListI xs => ByteString -> WinFunN xs y
194199unsafeWindowFunctionN fun xs = UnsafeWindowFunction $ fun <>
195200 parenthesized (renderCommaSeparated renderSQL xs)
squeal-postgresql/src/Squeal/PostgreSQL/List.hs+4−4
@@ -32,7 +32,7 @@ module Squeal.PostgreSQL.List
3232 , (*:)
3333 , Join
3434 , disjoin
35− , Additionally (..)
35+ , Additional (..)
3636 , AlignedList (..)
3737 , single
3838 , extractList
@@ -68,12 +68,12 @@ disjoin = case sList @xs of
6868 SCons -> \(x :* xsys) ->
6969 case disjoin xsys of (xs,ys) -> (x :* xs, ys)
7070
71−-- | The `Additionally` class is for appending
71+-- | The `Additional` class is for appending
7272-- type-level list parameterized constructors such as `NP`,
7373-- `Squeal.PostgreSQL.Query.Selection`, and `Squeal.PostgreSQL.Query.FromClause`.
74−class Additionally expr where
74+class Additional expr where
7575 also :: expr ys -> expr xs -> expr (Join xs ys)
76−instance Additionally (NP expr) where
76+instance Additional (NP expr) where
7777 also ys = \case
7878 Nil -> ys
7979 x :* xs -> x :* (xs & also ys)
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+21−10
@@ -387,28 +387,39 @@ q1 `exceptAll` q2 = UnsafeQuery $
387387SELECT queries
388388-----------------------------------------}
389389
390+{- | The simplest kinds of `Selection` are `Star` and `DotStar` which
391+emits all columns that a `TableExpression` produces. A select `List`
392+is a list of `Expression`s. A `Selection` could be a list of
393+`WindowFunction`s `Over` `WindowDefinition`. `Additional` `Selection`s can
394+be selected with `Also`.
395+-}
390396data Selection outer commons grp schemas params from row where
391− List
392− :: SListI row
393− => NP (Aliased (Expression outer commons grp schemas params from)) row
394− -> Selection outer commons grp schemas params from row
395397 Star
396398 :: HasUnique tab from row
397399 => Selection outer commons 'Ungrouped schemas params from row
400+ -- ^ `HasUnique` table in the `FromClause`
398401 DotStar
399402 :: Has tab from row
400403 => Alias tab
404+ -- ^ `Has` table with `Alias`
401405 -> Selection outer commons 'Ungrouped schemas params from row
402− Also
403− :: Selection outer commons grp schemas params from right
404− -> Selection outer commons grp schemas params from left
405− -> Selection outer commons grp schemas params from (Join left right)
406+ List
407+ :: SListI row
408+ => NP (Aliased (Expression outer commons grp schemas params from)) row
409+ -- ^ `NP` list of `Aliased` `Expression`s
410+ -> Selection outer commons grp schemas params from row
406411 Over
407412 :: SListI row
408413 => NP (Aliased (WindowFunction outer commons grp schemas params from)) row
414+ -- ^ `NP` list of `Aliased` `WindowFunction`s
409415 -> WindowDefinition outer commons grp schemas params from
410416 -> Selection outer commons grp schemas params from row
411−instance Additionally (Selection outer commons grp schemas params from) where
417+ Also
418+ :: Selection outer commons grp schemas params from right
419+ -- ^ `Additional` `Selection`
420+ -> Selection outer commons grp schemas params from left
421+ -> Selection outer commons grp schemas params from (Join left right)
422+instance Additional (Selection outer commons grp schemas params from) where
412423 also = Also
413424instance (KnownSymbol col, row ~ '[col ::: ty])
414425 => Aliasable col
@@ -741,7 +752,7 @@ common
741752common (cte `As` alias) = UnsafeFromClause $
742753 renderSQL cte <+> "AS" <+> renderSQL alias
743754
744−instance Additionally (FromClause outer commons schemas params) where
755+instance Additional (FromClause outer commons schemas params) where
745756 also right left = UnsafeFromClause $
746757 renderSQL left <> ", " <> renderSQL right
747758