on dev help sign in
aliased aliases
commitb65851c5b911818fedd9561862e8c2532834b1b9 authorEitan Chatav <eitan.chatav@gmail.com> dateSat 2 Jun 2018, 15:57 (8 years ago) parentf0e5644
3 files changed+31−55
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs−32 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+28−23 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+3
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs−32
@@ -92,9 +92,6 @@ module Squeal.PostgreSQL.Expression
9292 , count, countDistinct
9393 , every, everyDistinct
9494 , max_, maxDistinct, min_, minDistinct
95− -- * Tables
96− , Table (UnsafeTable, renderTable)
97− , View (UnsafeView, renderView)
9895 -- * Types
9996 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
10097 , PGTyped (pgtype)
@@ -1080,35 +1077,6 @@ min_ = unsafeAggregate "min"
10801077maxDistinct = unsafeAggregateDistinct "max"
10811078minDistinct = unsafeAggregateDistinct "min"
10821079
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−
11121080{-----------------------------------------
11131081type expressions
11141082-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+28−23
@@ -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
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+3
@@ -395,6 +395,9 @@ deriving instance Eq (expression ty)
395395 => Eq (Aliased expression (alias ::: ty))
396396deriving instance Ord (expression ty)
397397 => 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
398401
399402-- | >>> let renderMaybe = fromString . maybe "Nothing" (const "Just")
400403-- >>> renderAliasedAs renderMaybe (Just (3::Int) `As` #an_int)