on dev help sign in
grouping and qualification
1cb41beEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+8−33
@@ -37,7 +37,6 @@ module Squeal.PostgreSQL.Expression
37 ( -- * Expression
37 ( -- * Expression
38 Expression (UnsafeExpression, renderExpression)
38 Expression (UnsafeExpression, renderExpression)
39 , HasParameter (param)
39 , HasParameter (param)
40− , GroupedBy (getGroup1, getGroup2)
41 -- ** Null
40 -- ** Null
42 , null_
41 , null_
43 , unNull
42 , unNull
@@ -178,7 +177,7 @@ values from primitive expression using arithmetic, logical,
178and other operations.
177and other operations.
179-}
178-}
180newtype Expression
179newtype Expression
181− (relation :: RelationsType)
180+ (relations :: RelationsType)
182 (grouping :: Grouping)
181 (grouping :: Grouping)
183 (params :: [NullityType])
182 (params :: [NullityType])
184 (ty :: NullityType)
183 (ty :: NullityType)
@@ -212,35 +211,9 @@ instance (HasUnique relation relations columns, Has column columns ty)
212 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
211 fromLabel = UnsafeExpression $ renderAlias (Alias @column)
213
212
214instance (Has relation relations columns, Has column columns ty)
213instance (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
216 relation ! column = UnsafeExpression $
215 relation ! column = UnsafeExpression $
217 renderAlias relation <> "." <> renderAlias column
216 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)
244
217
245instance
218instance
246 ( HasUnique relation relations columns
219 ( HasUnique relation relations columns
@@ -248,14 +221,16 @@ instance
248 , GroupedBy relation column bys
221 , GroupedBy relation column bys
249 ) => IsLabel column
222 ) => IsLabel column
250 (Expression relations ('Grouped bys) params ty) where
223 (Expression relations ('Grouped bys) params ty) where
251− fromLabel = getGroup1 (Alias @column)
224+ fromLabel = UnsafeExpression $ renderAlias (Alias @column)
252
225
253instance
226instance
254 ( Has relation relations columns
227 ( Has relation relations columns
255 , Has column columns ty
228 , Has column columns ty
256 , GroupedBy relation column bys
229 , GroupedBy relation column bys
257− ) => IsTableColumn relation column
230+ ) => IsQualified relation column
258− (Expression relations ('Grouped bys) params ty) where (!) = getGroup2
231+ (Expression relations ('Grouped bys) params ty) where
232+ relation ! column = UnsafeExpression $
233+ renderAlias relation <> "." <> renderAlias column
259
234
260-- | analagous to `Nothing`
235-- | analagous to `Nothing`
261--
236--
@@ -288,7 +263,7 @@ coalesce nullxs notNullx = UnsafeExpression $
288 "COALESCE" <> parenthesized (commaSeparated
263 "COALESCE" <> parenthesized (commaSeparated
289 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
264 ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
290
265
291−-- | analagous to `fromMaybe` using @COALESCE@
266+-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
292--
267--
293-- >>> renderExpression $ fromNull true null_
268-- >>> renderExpression $ fromNull true null_
294-- "COALESCE(NULL, TRUE)"
269-- "COALESCE(NULL, TRUE)"
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+20−4
@@ -46,7 +46,9 @@ module Squeal.PostgreSQL.Schema
46 , TableType
46 , TableType
47 , TablesType
47 , TablesType
48 , NilTables
48 , NilTables
49+ -- * Grouping
49 , Grouping (..)
50 , Grouping (..)
51+ , GroupedBy
50 -- * Constraints
52 -- * Constraints
51 , (:=>)
53 , (:=>)
52 , ColumnsToRelation
54 , ColumnsToRelation
@@ -67,7 +69,7 @@ module Squeal.PostgreSQL.Schema
67 , Has
69 , Has
68 , HasUnique
70 , HasUnique
69 , IsLabel (..)
71 , IsLabel (..)
70− , IsTableColumn (..)
72+ , IsQualified (..)
71 -- * Type Families
73 -- * Type Families
72 , In
74 , In
73 , PGNum
75 , PGNum
@@ -316,6 +318,20 @@ data Grouping
316 = Ungrouped -- ^ no aggregation permitted
318 = Ungrouped -- ^ no aggregation permitted
317 | Grouped [(Symbol,Symbol)] -- ^ aggregation required for any column which is not grouped
319 | Grouped [(Symbol,Symbol)] -- ^ aggregation required for any column which is not grouped
318
320
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+
319-- | `Alias`es are proxies for a type level string or `Symbol`
335-- | `Alias`es are proxies for a type level string or `Symbol`
320-- and have an `IsLabel` instance so that with @-XOverloadedLabels@
336-- and have an `IsLabel` instance so that with @-XOverloadedLabels@
321--
337--
@@ -380,12 +396,12 @@ instance {-# OVERLAPPABLE #-} (KnownSymbol alias, Has alias fields field)
380 => Has alias (field' ': fields) field
396 => Has alias (field' ': fields) field
381
397
382-- | Analagous to `IsLabel`, the constraint
398-- | Analagous to `IsLabel`, the constraint
383−-- `IsTableColumn` defines `!` for a column alias qualified
399+-- `IsQualified` defines `!` for a column alias qualified
384-- by a table alias.
400-- by a table alias.
385−class IsTableColumn table column expression where
401+class IsQualified table column expression where
386 (!) :: Alias table -> Alias column -> expression
402 (!) :: Alias table -> Alias column -> expression
387 infixl 9 !
403 infixl 9 !
388−instance IsTableColumn table column (Alias table, Alias column) where (!) = (,)
404+instance IsQualified table column (Alias table, Alias column) where (!) = (,)
389
405
390-- | @Elem@ is a promoted `elem`.
406-- | @Elem@ is a promoted `elem`.
391type family Elem x xs where
407type family Elem x xs where