@@ -10,6 +10,7 @@ Squeal queries.
1010
1111{-# LANGUAGE
1212 DeriveGeneric
13+ , FlexibleContexts
1314 , GADTs
1415 , GeneralizedNewtypeDeriving
1516 , LambdaCase
@@ -96,10 +97,10 @@ let
9697 '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]
9798 '[]
9899 '["col" ::: 'Null 'PGint4]
99− query = selectStar (from (table (#tab `As` #t)))
100+ query = selectStar (from (table #tab))
100101in renderQuery query
101102:}
102−"SELECT * FROM \"tab\" AS \"t\""
103+"SELECT * FROM \"tab\" AS \"tab\""
103104
104105restricted query:
105106
@@ -115,12 +116,12 @@ let
115116 query =
116117 select
117118 ((#col1 + #col2) `As` #sum :* #col1 :* Nil)
118− ( from (table (#tab `As` #t))
119+ ( from (table #tab)
119120 & where_ (#col1 .> #col2)
120121 & where_ (#col2 .> 0) )
121122in renderQuery query
122123:}
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))"
124125
125126subquery:
126127
@@ -132,10 +133,10 @@ let
132133 '["col" ::: 'Null 'PGint4]
133134 query =
134135 selectStar
135− (from (subquery (selectStar (from (table (#tab `As` #t))) `As` #sub)))
136+ (from (subquery (selectStar (from (table #tab)) `As` #sub)))
136137in renderQuery query
137138:}
138−"SELECT * FROM (SELECT * FROM \"tab\" AS \"t\") AS \"sub\""
139+"SELECT * FROM (SELECT * FROM \"tab\" AS \"tab\") AS \"sub\""
139140
140141limits and offsets:
141142
@@ -146,10 +147,10 @@ let
146147 '[]
147148 '["col" ::: 'Null 'PGint4]
148149 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)
150151in renderQuery query
151152:}
152−"SELECT * FROM \"tab\" AS \"t\" LIMIT 50 OFFSET 4"
153+"SELECT * FROM \"tab\" AS \"tab\" LIMIT 50 OFFSET 4"
153154
154155parameterized query:
155156
@@ -160,10 +161,10 @@ let
160161 '[ 'NotNull 'PGfloat8]
161162 '["col" ::: 'NotNull 'PGfloat8]
162163 query = selectStar
163− (from (table (#tab `As` #t)) & where_ (#col .> param @1))
164+ (from (table #tab) & where_ (#col .> param @1))
164165in renderQuery query
165166:}
166−"SELECT * FROM \"tab\" AS \"t\" WHERE (\"col\" > ($1 :: float8))"
167+"SELECT * FROM \"tab\" AS \"tab\" WHERE (\"col\" > ($1 :: float8))"
167168
168169aggregation query:
169170
@@ -194,10 +195,10 @@ let
194195 '[]
195196 '["col" ::: 'Null 'PGint4]
196197 query = selectStar
197− (from (table (#tab `As` #t)) & orderBy [#col & AscNullsFirst])
198+ (from (table #tab) & orderBy [#col & AscNullsFirst])
198199in renderQuery query
199200:}
200−"SELECT * FROM \"tab\" AS \"t\" ORDER BY \"col\" ASC NULLS FIRST"
201+"SELECT * FROM \"tab\" AS \"tab\" ORDER BY \"col\" ASC NULLS FIRST"
201202
202203joins:
203204
@@ -266,12 +267,12 @@ let
266267 '[]
267268 '["col" ::: 'Null 'PGint4]
268269 query =
269− selectStar (from (table (#tab `As` #t)))
270+ selectStar (from (table #tab))
270271 `unionAll`
271− selectStar (from (table (#tab `As` #t)))
272+ selectStar (from (table #tab))
272273in renderQuery query
273274:}
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\")"
275276-}
276277newtype Query
277278 (schema :: SchemaType)
@@ -614,21 +615,25 @@ newtype FromClause schema params relations
614615
615616-- | A real `table` is a table from the schema.
616617table
617− :: Aliased (Table schema) table
618− -> FromClause schema params '[table]
619−table = UnsafeFromClause . renderAliasedAs renderTable
618+ :: Has tab schema ('Table table)
619+ => Aliased Alias (alias ::: tab)
620+ -> FromClause schema params '[alias ::: TableToRelation table]
621+table (tab `As` alias) = UnsafeFromClause $
622+ renderAlias tab <+> "AS" <+> renderAlias alias
620623
621624-- | `subquery` derives a table from a `Query`.
622625subquery
623− :: Aliased (Query schema params) table
624− -> FromClause schema params '[table]
626+ :: Aliased (Query schema params) rel
627+ -> FromClause schema params '[rel]
625628subquery = UnsafeFromClause . renderAliasedAs (parenthesized . renderQuery)
626629
627630-- | `view` derives a table from a `View`.
628631view
629− :: Aliased (View schema) table
630− -> FromClause schema params '[table]
631−view = UnsafeFromClause . renderAliasedAs renderView
632+ :: Has view schema ('View rel)
633+ => Aliased Alias (alias ::: view)
634+ -> FromClause schema params '[alias ::: rel]
635+view (vw `As` alias) = UnsafeFromClause $
636+ renderAlias vw <+> "AS" <+> renderAlias alias
632637
633638{- | @left & crossJoin right@. For every possible combination of rows from
634639 @left@ and @right@ (i.e., a Cartesian product), the joined table will contain