on dev help sign in
grouping and qualification
commit1cb41bee4c9ee1cf3934f48b6fd76e7657086e63 authorEitan Chatav <eitan.chatav@gmail.com> dateSun 25 Mar 2018, 12:11 (9 years ago) parentea6d1e7
2 files changed+28−37
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+8−33 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+20−4
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+8−33
@@ -37,7 +37,6 @@ module Squeal.PostgreSQL.Expression
3737 ( -- * Expression
3838 Expression (UnsafeExpression, renderExpression)
3939 , HasParameter (param)
40− , GroupedBy (getGroup1, getGroup2)
4140 -- ** Null
4241 , null_
4342 , unNull
@@ -178,7 +177,7 @@ values from primitive expression using arithmetic, logical,
178177and other operations.
179178-}
180179newtype Expression
181− (relation :: RelationsType)
180+ (relations :: RelationsType)
182181 (grouping :: Grouping)
183182 (params :: [NullityType])
184183 (ty :: NullityType)
@@ -212,35 +211,9 @@ instance (HasUnique relation relations columns, Has column columns ty)
212211 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
213212
214213instance (Has relation relations columns, Has column columns ty)
215− => IsTableColumn relation column (Expression relations 'Ungrouped params ty) where
214+ => IsQualified relation column (Expression relations 'Ungrouped params ty) where
216215 relation ! column = UnsafeExpression $
217216 renderAlias relation <> "." <> renderAlias column
218−
219−{- | A `GroupedBy` constraint indicates that a table qualified column is
220−a member of the auxiliary namespace created by @GROUP BY@ clauses and thus,
221−may be called in an output `Expression` without aggregating.
222−-}
223−class (KnownSymbol relation, KnownSymbol column)
224− => GroupedBy relation column bys where
225− getGroup1
226− :: (HasUnique relation relations columns, Has column columns ty)
227− => Alias column
228− -> Expression relations ('Grouped bys) params ty
229− getGroup1 column = UnsafeExpression $ renderAlias column
230− getGroup2
231− :: (Has relation relations columns, Has column columns ty)
232− => Alias relation
233− -> Alias column
234− -> Expression relations ('Grouped bys) params ty
235− getGroup2 relation column = UnsafeExpression $
236− renderAlias relation <> "." <> renderAlias column
237−instance {-# OVERLAPPING #-} (KnownSymbol relation, KnownSymbol column)
238− => GroupedBy relation column ('(table,column) ': bys)
239−instance {-# OVERLAPPABLE #-}
240− ( KnownSymbol relation
241− , KnownSymbol column
242− , GroupedBy relation column bys
243− ) => GroupedBy relation column (tabcol ': bys)
244217
245218instance
246219 ( HasUnique relation relations columns
@@ -248,14 +221,16 @@ instance
248221 , GroupedBy relation column bys
249222 ) => IsLabel column
250223 (Expression relations ('Grouped bys) params ty) where
251− fromLabel = getGroup1 (Alias @column)
224+ fromLabel = UnsafeExpression $ renderAlias (Alias @column)
252225
253226instance
254227 ( Has relation relations columns
255228 , Has column columns ty
256229 , GroupedBy relation column bys
257− ) => IsTableColumn relation column
258− (Expression relations ('Grouped bys) params ty) where (!) = getGroup2
230+ ) => IsQualified relation column
231+ (Expression relations ('Grouped bys) params ty) where
232+ relation ! column = UnsafeExpression $
233+ renderAlias relation <> "." <> renderAlias column
259234
260235-- | analagous to `Nothing`
261236--
@@ -288,7 +263,7 @@ coalesce nullxs notNullx = UnsafeExpression $
288263 "COALESCE" <> parenthesized (commaSeparated
289264 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
290265
291−-- | analagous to `fromMaybe` using @COALESCE@
266+-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
292267--
293268-- >>> renderExpression $ fromNull true null_
294269-- "COALESCE(NULL, TRUE)"
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+20−4
@@ -46,7 +46,9 @@ module Squeal.PostgreSQL.Schema
4646 , TableType
4747 , TablesType
4848 , NilTables
49+ -- * Grouping
4950 , Grouping (..)
51+ , GroupedBy
5052 -- * Constraints
5153 , (:=>)
5254 , ColumnsToRelation
@@ -67,7 +69,7 @@ module Squeal.PostgreSQL.Schema
6769 , Has
6870 , HasUnique
6971 , IsLabel (..)
70− , IsTableColumn (..)
72+ , IsQualified (..)
7173 -- * Type Families
7274 , In
7375 , PGNum
@@ -316,6 +318,20 @@ data Grouping
316318 = Ungrouped -- ^ no aggregation permitted
317319 | Grouped [(Symbol,Symbol)] -- ^ aggregation required for any column which is not grouped
318320
321+{- | A `GroupedBy` constraint indicates that a table qualified column is
322+a member of the auxiliary namespace created by @GROUP BY@ clauses and thus,
323+may be called in an output `Expression` without aggregating.
324+-}
325+class (KnownSymbol relation, KnownSymbol column)
326+ => GroupedBy relation column bys where
327+instance {-# OVERLAPPING #-} (KnownSymbol relation, KnownSymbol column)
328+ => GroupedBy relation column ('(table,column) ': bys)
329+instance {-# OVERLAPPABLE #-}
330+ ( KnownSymbol relation
331+ , KnownSymbol column
332+ , GroupedBy relation column bys
333+ ) => GroupedBy relation column (tabcol ': bys)
334+
319335-- | `Alias`es are proxies for a type level string or `Symbol`
320336-- and have an `IsLabel` instance so that with @-XOverloadedLabels@
321337--
@@ -380,12 +396,12 @@ instance {-# OVERLAPPABLE #-} (KnownSymbol alias, Has alias fields field)
380396 => Has alias (field' ': fields) field
381397
382398-- | Analagous to `IsLabel`, the constraint
383−-- `IsTableColumn` defines `!` for a column alias qualified
399+-- `IsQualified` defines `!` for a column alias qualified
384400-- by a table alias.
385−class IsTableColumn table column expression where
401+class IsQualified table column expression where
386402 (!) :: Alias table -> Alias column -> expression
387403 infixl 9 !
388−instance IsTableColumn table column (Alias table, Alias column) where (!) = (,)
404+instance IsQualified table column (Alias table, Alias column) where (!) = (,)
389405
390406-- | @Elem@ is a promoted `elem`.
391407type family Elem x xs where