on dev help sign in
aliased aliases
b65851cEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs−32
@@ -92,9 +92,6 @@ module Squeal.PostgreSQL.Expression
92 , count, countDistinct
92 , count, countDistinct
93 , every, everyDistinct
93 , every, everyDistinct
94 , max_, maxDistinct, min_, minDistinct
94 , max_, maxDistinct, min_, minDistinct
95− -- * Tables
96− , Table (UnsafeTable, renderTable)
97− , View (UnsafeView, renderView)
98 -- * Types
95 -- * Types
99 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
96 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
100 , PGTyped (pgtype)
97 , PGTyped (pgtype)
@@ -1080,35 +1077,6 @@ min_ = unsafeAggregate "min"
1080maxDistinct = unsafeAggregateDistinct "max"
1077maxDistinct = unsafeAggregateDistinct "max"
1081minDistinct = unsafeAggregateDistinct "min"
1078minDistinct = unsafeAggregateDistinct "min"
1082
1079
1083−{-----------------------------------------
1084−tables
1085−-----------------------------------------}
1086−
1087−-- | A `Table` from a table expression is a way
1088−-- to call a table reference by its alias.
1089−newtype Table
1090− (schema :: SchemaType)
1091− (columns :: RelationType)
1092− = UnsafeTable { renderTable :: ByteString }
1093− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1094−instance
1095− ( Has alias schema ('Table table)
1096− , relation ~ TableToRelation table
1097− ) => IsLabel alias (Table schema relation) where
1098− fromLabel = UnsafeTable $ renderAlias (Alias @alias)
1099−
1100−-- | A `View` from a table expression is a way
1101−-- to call a table reference by its alias.
1102−newtype View
1103− (schema :: SchemaType)
1104− (columns :: RelationType)
1105− = UnsafeView { renderView :: ByteString }
1106− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1107−instance
1108− ( Has alias schema ('View columns)
1109− ) => IsLabel alias (View schema columns) where
1110− fromLabel = UnsafeView $ renderAlias (Alias @alias)
1111−
1112{-----------------------------------------
1080{-----------------------------------------
1113type expressions
1081type expressions
1114-----------------------------------------}
1082-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+28−23
@@ -10,6 +10,7 @@ Squeal queries.
10
10
11{-# LANGUAGE
11{-# LANGUAGE
12 DeriveGeneric
12 DeriveGeneric
13+ , FlexibleContexts
13 , GADTs
14 , GADTs
14 , GeneralizedNewtypeDeriving
15 , GeneralizedNewtypeDeriving
15 , LambdaCase
16 , LambdaCase
@@ -96,10 +97,10 @@ let
96 '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]
97 '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]
97 '[]
98 '[]
98 '["col" ::: 'Null 'PGint4]
99 '["col" ::: 'Null 'PGint4]
99− query = selectStar (from (table (#tab `As` #t)))
100+ query = selectStar (from (table #tab))
100in renderQuery query
101in renderQuery query
101:}
102:}
102−"SELECT * FROM \"tab\" AS \"t\""
103+"SELECT * FROM \"tab\" AS \"tab\""
103
104
104restricted query:
105restricted query:
105
106
@@ -115,12 +116,12 @@ let
115 query =
116 query =
116 select
117 select
117 ((#col1 + #col2) `As` #sum :* #col1 :* Nil)
118 ((#col1 + #col2) `As` #sum :* #col1 :* Nil)
118− ( from (table (#tab `As` #t))
119+ ( from (table #tab)
119 & where_ (#col1 .> #col2)
120 & where_ (#col1 .> #col2)
120 & where_ (#col2 .> 0) )
121 & where_ (#col2 .> 0) )
121in renderQuery query
122in renderQuery query
122:}
123:}
123−"SELECT (\"col1\" + \"col2\") AS \"sum\", \"col1\" AS \"col1\" FROM \"tab\" AS \"t\" WHERE ((\"col1\" > \"col2\") AND (\"col2\" > 0))"
124+"SELECT (\"col1\" + \"col2\") AS \"sum\", \"col1\" AS \"col1\" FROM \"tab\" AS \"tab\" WHERE ((\"col1\" > \"col2\") AND (\"col2\" > 0))"
124
125
125subquery:
126subquery:
126
127
@@ -132,10 +133,10 @@ let
132 '["col" ::: 'Null 'PGint4]
133 '["col" ::: 'Null 'PGint4]
133 query =
134 query =
134 selectStar
135 selectStar
135− (from (subquery (selectStar (from (table (#tab `As` #t))) `As` #sub)))
136+ (from (subquery (selectStar (from (table #tab)) `As` #sub)))
136in renderQuery query
137in renderQuery query
137:}
138:}
138−"SELECT * FROM (SELECT * FROM \"tab\" AS \"t\") AS \"sub\""
139+"SELECT * FROM (SELECT * FROM \"tab\" AS \"tab\") AS \"sub\""
139
140
140limits and offsets:
141limits and offsets:
141
142
@@ -146,10 +147,10 @@ let
146 '[]
147 '[]
147 '["col" ::: 'Null 'PGint4]
148 '["col" ::: 'Null 'PGint4]
148 query = selectStar
149 query = selectStar
149− (from (table (#tab `As` #t)) & limit 100 & offset 2 & limit 50 & offset 2)
150+ (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
150in renderQuery query
151in renderQuery query
151:}
152:}
152−"SELECT * FROM \"tab\" AS \"t\" LIMIT 50 OFFSET 4"
153+"SELECT * FROM \"tab\" AS \"tab\" LIMIT 50 OFFSET 4"
153
154
154parameterized query:
155parameterized query:
155
156
@@ -160,10 +161,10 @@ let
160 '[ 'NotNull 'PGfloat8]
161 '[ 'NotNull 'PGfloat8]
161 '["col" ::: 'NotNull 'PGfloat8]
162 '["col" ::: 'NotNull 'PGfloat8]
162 query = selectStar
163 query = selectStar
163− (from (table (#tab `As` #t)) & where_ (#col .> param @1))
164+ (from (table #tab) & where_ (#col .> param @1))
164in renderQuery query
165in renderQuery query
165:}
166:}
166−"SELECT * FROM \"tab\" AS \"t\" WHERE (\"col\" > ($1 :: float8))"
167+"SELECT * FROM \"tab\" AS \"tab\" WHERE (\"col\" > ($1 :: float8))"
167
168
168aggregation query:
169aggregation query:
169
170
@@ -194,10 +195,10 @@ let
194 '[]
195 '[]
195 '["col" ::: 'Null 'PGint4]
196 '["col" ::: 'Null 'PGint4]
196 query = selectStar
197 query = selectStar
197− (from (table (#tab `As` #t)) & orderBy [#col & AscNullsFirst])
198+ (from (table #tab) & orderBy [#col & AscNullsFirst])
198in renderQuery query
199in renderQuery query
199:}
200:}
200−"SELECT * FROM \"tab\" AS \"t\" ORDER BY \"col\" ASC NULLS FIRST"
201+"SELECT * FROM \"tab\" AS \"tab\" ORDER BY \"col\" ASC NULLS FIRST"
201
202
202joins:
203joins:
203
204
@@ -266,12 +267,12 @@ let
266 '[]
267 '[]
267 '["col" ::: 'Null 'PGint4]
268 '["col" ::: 'Null 'PGint4]
268 query =
269 query =
269− selectStar (from (table (#tab `As` #t)))
270+ selectStar (from (table #tab))
270 `unionAll`
271 `unionAll`
271− selectStar (from (table (#tab `As` #t)))
272+ selectStar (from (table #tab))
272in renderQuery query
273in renderQuery query
273:}
274:}
274−"(SELECT * FROM \"tab\" AS \"t\") UNION ALL (SELECT * FROM \"tab\" AS \"t\")"
275+"(SELECT * FROM \"tab\" AS \"tab\") UNION ALL (SELECT * FROM \"tab\" AS \"tab\")"
275-}
276-}
276newtype Query
277newtype Query
277 (schema :: SchemaType)
278 (schema :: SchemaType)
@@ -614,21 +615,25 @@ newtype FromClause schema params relations
614
615
615-- | A real `table` is a table from the schema.
616-- | A real `table` is a table from the schema.
616table
617table
617− :: Aliased (Table schema) table
618+ :: Has tab schema ('Table table)
619+ => Aliased Alias (alias ::: tab)
618− -> FromClause schema params '[table]
620+ -> FromClause schema params '[alias ::: TableToRelation table]
619−table = UnsafeFromClause . renderAliasedAs renderTable
621+table (tab `As` alias) = UnsafeFromClause $
622+ renderAlias tab <+> "AS" <+> renderAlias alias
620
623
621-- | `subquery` derives a table from a `Query`.
624-- | `subquery` derives a table from a `Query`.
622subquery
625subquery
623− :: Aliased (Query schema params) table
626+ :: Aliased (Query schema params) rel
624− -> FromClause schema params '[table]
627+ -> FromClause schema params '[rel]
625subquery = UnsafeFromClause . renderAliasedAs (parenthesized . renderQuery)
628subquery = UnsafeFromClause . renderAliasedAs (parenthesized . renderQuery)
626
629
627-- | `view` derives a table from a `View`.
630-- | `view` derives a table from a `View`.
628view
631view
629− :: Aliased (View schema) table
632+ :: Has view schema ('View rel)
633+ => Aliased Alias (alias ::: view)
630− -> FromClause schema params '[table]
634+ -> FromClause schema params '[alias ::: rel]
631−view = UnsafeFromClause . renderAliasedAs renderView
635+view (vw `As` alias) = UnsafeFromClause $
636+ renderAlias vw <+> "AS" <+> renderAlias alias
632
637
633{- | @left & crossJoin right@. For every possible combination of rows from
638{- | @left & crossJoin right@. For every possible combination of rows from
634 @left@ and @right@ (i.e., a Cartesian product), the joined table will contain
639 @left@ and @right@ (i.e., a Cartesian product), the joined table will contain
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+3
@@ -395,6 +395,9 @@ deriving instance Eq (expression ty)
395 => Eq (Aliased expression (alias ::: ty))
395 => Eq (Aliased expression (alias ::: ty))
396deriving instance Ord (expression ty)
396deriving instance Ord (expression ty)
397 => Ord (Aliased expression (alias ::: ty))
397 => Ord (Aliased expression (alias ::: ty))
398+instance (alias0 ~ alias1, alias0 ~ alias2, KnownSymbol alias2)
399+ => IsLabel alias0 (Aliased Alias (alias1 ::: alias2)) where
400+ fromLabel = fromLabel @alias2 `As` fromLabel @alias1
398
401
399-- | >>> let renderMaybe = fromString . maybe "Nothing" (const "Just")
402-- | >>> let renderMaybe = fromString . maybe "Nothing" (const "Just")
400-- >>> renderAliasedAs renderMaybe (Just (3::Int) `As` #an_int)
403-- >>> renderAliasedAs renderMaybe (Just (3::Int) `As` #an_int)