Deprecate ToAliasT and ToAliasReferenceT (#221)

* Make ToAliasT and ToAliasReferenceT associated types of the corresponding classes

* Update changelog

* Remove ToAliasT and ToAliasReferenceT

* Update changelog and deprecate type families instead of deleting them

* Apply suggestions from code review

Co-authored-by: Ben Levy <benjaminlevy007@gmail.com>

Co-authored-by: Ben Levy <benjaminlevy007@gmail.com>
This commit is contained in:
Arthur Xavier 2020-10-29 18:35:18 -03:00 committed by GitHub
parent 4ea3d5da59
commit 4f6b02298c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 114 deletions

View File

@ -1,3 +1,9 @@
3.4.0.1
=======
- @arthurxavierx
- [#221](https://github.com/bitemyapp/esqueleto/pull/221)
- Deprecate `ToAliasT` and `ToAliasReferenceT`
3.4.0.0 3.4.0.0
======= =======
- @belevy, @charukiewicz - @belevy, @charukiewicz

View File

@ -1,7 +1,7 @@
cabal-version: 1.12 cabal-version: 1.12
name: esqueleto name: esqueleto
version: 3.4.0.0 version: 3.4.0.1
synopsis: Type-safe EDSL for SQL queries on persistent backends. synopsis: Type-safe EDSL for SQL queries on persistent backends.
description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime. description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime.
. .

View File

@ -57,9 +57,7 @@ module Database.Esqueleto.Experimental
, ToMaybe(..) , ToMaybe(..)
, ToMaybeT , ToMaybeT
, ToAlias(..) , ToAlias(..)
, ToAliasT
, ToAliasReference(..) , ToAliasReference(..)
, ToAliasReferenceT
-- * The Normal Stuff -- * The Normal Stuff
, where_, groupBy, orderBy, rand, asc, desc, limit, offset , where_, groupBy, orderBy, rand, asc, desc, limit, offset
, distinct, distinctOn, don, distinctOnOrderBy, having, locking , distinct, distinctOn, don, distinctOnOrderBy, having, locking
@ -577,75 +575,61 @@ data From a where
:: PersistEntity ent :: PersistEntity ent
=> From (SqlExpr (Entity ent)) => From (SqlExpr (Entity ent))
SubQuery SubQuery
:: ( SqlSelect a' r :: ( SqlSelect a r
, SqlSelect a'' r'
, ToAlias a , ToAlias a
, a' ~ ToAliasT a , ToAliasReference a
, ToAliasReference a'
, ToAliasReferenceT a' ~ a''
) )
=> SqlQuery a => SqlQuery a
-> From a'' -> From a
FromCte FromCte
:: Ident :: Ident
-> a -> a
-> From a -> From a
SqlSetOperation SqlSetOperation
:: ( SqlSelect a' r :: ( SqlSelect a r
, ToAlias a , ToAlias a
, a' ~ ToAliasT a , ToAliasReference a
, ToAliasReference a'
, ToAliasReferenceT a' ~ a''
) )
=> SqlSetOperation a => SqlSetOperation a
-> From a'' -> From a
InnerJoinFrom InnerJoinFrom
:: From a :: From a
-> (From b, (a :& b) -> SqlExpr (Value Bool)) -> (From b, (a :& b) -> SqlExpr (Value Bool))
-> From (a :& b) -> From (a :& b)
InnerJoinFromLateral InnerJoinFromLateral
:: ( SqlSelect b' r :: ( SqlSelect b r
, SqlSelect b'' r'
, ToAlias b , ToAlias b
, b' ~ ToAliasT b , ToAliasReference b
, ToAliasReference b'
, ToAliasReferenceT b' ~ b''
) )
=> From a => From a
-> ((a -> SqlQuery b), (a :& b'') -> SqlExpr (Value Bool)) -> ((a -> SqlQuery b), (a :& b) -> SqlExpr (Value Bool))
-> From (a :& b'') -> From (a :& b)
CrossJoinFrom CrossJoinFrom
:: From a :: From a
-> From b -> From b
-> From (a :& b) -> From (a :& b)
CrossJoinFromLateral CrossJoinFromLateral
:: ( SqlSelect b' r :: ( SqlSelect b r
, SqlSelect b'' r'
, ToAlias b , ToAlias b
, b' ~ ToAliasT b , ToAliasReference b
, ToAliasReference b'
, ToAliasReferenceT b' ~ b''
) )
=> From a => From a
-> (a -> SqlQuery b) -> (a -> SqlQuery b)
-> From (a :& b'') -> From (a :& b)
LeftJoinFrom LeftJoinFrom
:: ToMaybe b :: ToMaybe b
=> From a => From a
-> (From b, (a :& ToMaybeT b) -> SqlExpr (Value Bool)) -> (From b, (a :& ToMaybeT b) -> SqlExpr (Value Bool))
-> From (a :& ToMaybeT b) -> From (a :& ToMaybeT b)
LeftJoinFromLateral LeftJoinFromLateral
:: ( SqlSelect b' r :: ( SqlSelect b r
, SqlSelect b'' r'
, ToAlias b , ToAlias b
, b' ~ ToAliasT b , ToAliasReference b
, ToAliasReference b' , ToMaybe b
, ToAliasReferenceT b' ~ b''
, ToMaybe b''
) )
=> From a => From a
-> ((a -> SqlQuery b), (a :& ToMaybeT b'') -> SqlExpr (Value Bool)) -> ((a -> SqlQuery b), (a :& ToMaybeT b) -> SqlExpr (Value Bool))
-> From (a :& ToMaybeT b'') -> From (a :& ToMaybeT b)
RightJoinFrom RightJoinFrom
:: ToMaybe a :: ToMaybe a
=> From a => From a
@ -687,17 +671,17 @@ type JoinErrorMsg jk = 'Text "Missing on statement for " ':<>: 'Text jk
type family ToFromT a where type family ToFromT a where
ToFromT (From a) = a ToFromT (From a) = a
ToFromT (SqlQuery a) = ToAliasReferenceT (ToAliasT a) ToFromT (SqlQuery a) = a
ToFromT (Union a b) = ToAliasReferenceT (ToAliasT (SetOperationT a)) ToFromT (Union a b) = SetOperationT a
ToFromT (UnionAll a b) = ToAliasReferenceT (ToAliasT (SetOperationT a)) ToFromT (UnionAll a b) = SetOperationT a
ToFromT (Except a b) = ToAliasReferenceT (ToAliasT (SetOperationT a)) ToFromT (Except a b) = SetOperationT a
ToFromT (Intersect a b) = ToAliasReferenceT (ToAliasT (SetOperationT a)) ToFromT (Intersect a b) = SetOperationT a
ToFromT (SqlSetOperation a) = ToAliasReferenceT (ToAliasT a) ToFromT (SqlSetOperation a) = a
ToFromT (InnerJoin a (b, c -> SqlExpr (Value Bool))) = c ToFromT (InnerJoin a (b, c -> SqlExpr (Value Bool))) = c
ToFromT (LeftOuterJoin a (b, c -> SqlExpr (Value Bool))) = c ToFromT (LeftOuterJoin a (b, c -> SqlExpr (Value Bool))) = c
ToFromT (RightOuterJoin a (b, c -> SqlExpr (Value Bool))) = c ToFromT (RightOuterJoin a (b, c -> SqlExpr (Value Bool))) = c
ToFromT (FullOuterJoin a (b, c -> SqlExpr (Value Bool))) = c ToFromT (FullOuterJoin a (b, c -> SqlExpr (Value Bool))) = c
ToFromT (CrossJoin a (c -> SqlQuery b)) = ToFromT a :& ToAliasReferenceT (ToAliasT b) ToFromT (CrossJoin a (c -> SqlQuery b)) = ToFromT a :& b
ToFromT (CrossJoin a b) = ToFromT a :& ToFromT b ToFromT (CrossJoin a b) = ToFromT a :& ToFromT b
ToFromT (InnerJoin a b) = TypeError (JoinErrorMsg "InnerJoin") ToFromT (InnerJoin a b) = TypeError (JoinErrorMsg "InnerJoin")
ToFromT (LeftOuterJoin a b) = TypeError (JoinErrorMsg "LeftOuterJoin") ToFromT (LeftOuterJoin a b) = TypeError (JoinErrorMsg "LeftOuterJoin")
@ -732,39 +716,30 @@ instance {-# OVERLAPPABLE #-} ToFrom (FullOuterJoin a b) where
toFrom = undefined toFrom = undefined
instance ( ToAlias a instance ( ToAlias a
, a' ~ ToAliasT a , ToAliasReference a
, ToAliasReference a' , SqlSelect a r
, a'' ~ ToAliasReferenceT a'
, SqlSelect a' r'
, SqlSelect a'' r'
) => ToFrom (SqlQuery a) where ) => ToFrom (SqlQuery a) where
toFrom = SubQuery toFrom = SubQuery
instance ( SqlSelect c' r instance ( SqlSelect c r
, SqlSelect c'' r'
, ToAlias c , ToAlias c
, c' ~ ToAliasT c , ToAliasReference c
, ToAliasReference c'
, ToAliasReferenceT c' ~ c''
, ToSetOperation a c , ToSetOperation a c
, ToSetOperation b c , ToSetOperation b c
, c ~ SetOperationT a , c ~ SetOperationT a
) => ToFrom (Union a b) where ) => ToFrom (Union a b) where
toFrom u = SqlSetOperation $ toSetOperation u toFrom u = SqlSetOperation $ toSetOperation u
instance ( SqlSelect c' r instance ( SqlSelect c r
, SqlSelect c'' r'
, ToAlias c , ToAlias c
, c' ~ ToAliasT c , ToAliasReference c
, ToAliasReference c'
, ToAliasReferenceT c' ~ c''
, ToSetOperation a c , ToSetOperation a c
, ToSetOperation b c , ToSetOperation b c
, c ~ SetOperationT a , c ~ SetOperationT a
) => ToFrom (UnionAll a b) where ) => ToFrom (UnionAll a b) where
toFrom u = SqlSetOperation $ toSetOperation u toFrom u = SqlSetOperation $ toSetOperation u
instance (SqlSelect a' r,SqlSelect a'' r', ToAlias a, a' ~ ToAliasT a, ToAliasReference a', ToAliasReferenceT a' ~ a'') => ToFrom (SqlSetOperation a) where instance (SqlSelect a r, ToAlias a, ToAliasReference a) => ToFrom (SqlSetOperation a) where
-- If someone uses just a plain SelectQuery it should behave like a normal subquery -- If someone uses just a plain SelectQuery it should behave like a normal subquery
toFrom (SelectQueryP _ q) = SubQuery q toFrom (SelectQueryP _ q) = SubQuery q
-- Otherwise use the SqlSetOperation -- Otherwise use the SqlSetOperation
@ -773,15 +748,12 @@ instance (SqlSelect a' r,SqlSelect a'' r', ToAlias a, a' ~ ToAliasT a, ToAliasRe
class ToInnerJoin lateral lhs rhs res where class ToInnerJoin lateral lhs rhs res where
toInnerJoin :: Proxy lateral -> lhs -> rhs -> (res -> SqlExpr (Value Bool)) -> From res toInnerJoin :: Proxy lateral -> lhs -> rhs -> (res -> SqlExpr (Value Bool)) -> From res
instance ( SqlSelect bAlias r instance ( SqlSelect b r
, SqlSelect bAliasRef r'
, ToAlias b , ToAlias b
, bAlias ~ ToAliasT b , ToAliasReference b
, ToAliasReference bAlias
, bAliasRef ~ ToAliasReferenceT bAlias
, ToFrom a , ToFrom a
, ToFromT a ~ a' , ToFromT a ~ a'
) => ToInnerJoin Lateral a (a' -> SqlQuery b) (a' :& bAliasRef) where ) => ToInnerJoin Lateral a (a' -> SqlQuery b) (a' :& b) where
toInnerJoin _ lhs q on' = InnerJoinFromLateral (toFrom lhs) (q, on') toInnerJoin _ lhs q on' = InnerJoinFromLateral (toFrom lhs) (q, on')
instance (ToFrom a, ToFromT a ~ a', ToFrom b, ToFromT b ~ b') instance (ToFrom a, ToFromT a ~ a', ToFrom b, ToFromT b ~ b')
@ -807,12 +779,9 @@ instance ( ToFrom a
instance {-# OVERLAPPING #-} instance {-# OVERLAPPING #-}
( ToFrom a ( ToFrom a
, ToFromT a ~ a' , ToFromT a ~ a'
, SqlSelect bAlias r , SqlSelect b r
, SqlSelect bAliasRef r'
, ToAlias b , ToAlias b
, bAlias ~ ToAliasT b , ToAliasReference b
, ToAliasReference bAlias
, bAliasRef ~ ToAliasReferenceT bAlias
) )
=> ToFrom (CrossJoin a (a' -> SqlQuery b)) where => ToFrom (CrossJoin a (a' -> SqlQuery b)) where
toFrom (CrossJoin lhs q) = CrossJoinFromLateral (toFrom lhs) q toFrom (CrossJoin lhs q) = CrossJoinFromLateral (toFrom lhs) q
@ -822,14 +791,11 @@ class ToLeftJoin lateral lhs rhs res where
instance ( ToFrom a instance ( ToFrom a
, ToFromT a ~ a' , ToFromT a ~ a'
, SqlSelect bAlias r , SqlSelect b r
, SqlSelect bAliasRef r'
, ToAlias b , ToAlias b
, bAlias ~ ToAliasT b , ToAliasReference b
, ToAliasReference bAlias , ToMaybe b
, bAliasRef ~ ToAliasReferenceT bAlias , mb ~ ToMaybeT b
, ToMaybe bAliasRef
, mb ~ ToMaybeT bAliasRef
) => ToLeftJoin Lateral a (a' -> SqlQuery b) (a' :& mb) where ) => ToLeftJoin Lateral a (a' -> SqlQuery b) (a' :& mb) where
toLeftJoin _ lhs q on' = LeftJoinFromLateral (toFrom lhs) (q, on') toLeftJoin _ lhs q on' = LeftJoinFromLateral (toFrom lhs) (q, on')
@ -1087,14 +1053,11 @@ from parts = do
let ret = (toMaybe leftVal) :& (toMaybe rightVal) let ret = (toMaybe leftVal) :& (toMaybe rightVal)
pure $ (ret, FromJoin leftFrom FullOuterJoinKind rightFrom (Just (on' ret))) pure $ (ret, FromJoin leftFrom FullOuterJoinKind rightFrom (Just (on' ret)))
fromSubQuery :: ( SqlSelect a' r fromSubQuery :: ( SqlSelect a r
, SqlSelect a'' r'
, ToAlias a , ToAlias a
, a' ~ ToAliasT a , ToAliasReference a
, ToAliasReference a'
, ToAliasReferenceT a' ~ a''
) )
=> SubQueryType -> SqlQuery a -> SqlQuery (ToAliasReferenceT (ToAliasT a), FromClause) => SubQueryType -> SqlQuery a -> SqlQuery (a, FromClause)
fromSubQuery subqueryType subquery = do fromSubQuery subqueryType subquery = do
-- We want to update the IdentState without writing the query to side data -- We want to update the IdentState without writing the query to side data
(ret, sideData) <- Q $ W.censor (\_ -> mempty) $ W.listen $ unQ subquery (ret, sideData) <- Q $ W.censor (\_ -> mempty) $ W.listen $ unQ subquery
@ -1133,9 +1096,9 @@ fromSubQuery subqueryType subquery = do
-- --
-- /Since: 3.4.0.0/ -- /Since: 3.4.0.0/
with :: ( ToAlias a with :: ( ToAlias a
, ToAliasReference (ToAliasT a) , ToAliasReference a
, SqlSelect (ToAliasT a) r , SqlSelect a r
) => SqlQuery a -> SqlQuery (From (ToAliasReferenceT (ToAliasT a))) ) => SqlQuery a -> SqlQuery (From a)
with query = do with query = do
(ret, sideData) <- Q $ W.censor (\_ -> mempty) $ W.listen $ unQ query (ret, sideData) <- Q $ W.censor (\_ -> mempty) $ W.listen $ unQ query
aliasedValue <- toAlias ret aliasedValue <- toAlias ret
@ -1179,16 +1142,14 @@ with query = do
-- --
-- /Since: 3.4.0.0/ -- /Since: 3.4.0.0/
withRecursive :: ( ToAlias a withRecursive :: ( ToAlias a
, ToAliasReference (ToAliasT a) , ToAliasReference a
, SqlSelect a r , SqlSelect a r
, SqlSelect (ToAliasT a) r
, ref ~ ToAliasReferenceT (ToAliasT a)
, RecursiveCteUnion unionKind , RecursiveCteUnion unionKind
) )
=> SqlQuery a => SqlQuery a
-> unionKind -> unionKind
-> (From ref -> SqlQuery a) -> (From a -> SqlQuery a)
-> SqlQuery (From ref) -> SqlQuery (From a)
withRecursive baseCase unionKind recursiveCase = do withRecursive baseCase unionKind recursiveCase = do
(ret, sideData) <- Q $ W.censor (\_ -> mempty) $ W.listen $ unQ baseCase (ret, sideData) <- Q $ W.censor (\_ -> mempty) $ W.listen $ unQ baseCase
aliasedValue <- toAlias ret aliasedValue <- toAlias ret
@ -1205,21 +1166,12 @@ withRecursive baseCase unionKind recursiveCase = do
Q $ W.tell mempty{sdCteClause = [clause]} Q $ W.tell mempty{sdCteClause = [clause]}
pure refFrom pure refFrom
type family ToAliasT a where {-# DEPRECATED ToAliasT "This type alias doesn't do anything. Please delete it. Will be removed in the next release." #-}
ToAliasT (SqlExpr (Value a)) = SqlExpr (Value a) type ToAliasT a = a
ToAliasT (SqlExpr (Entity a)) = SqlExpr (Entity a)
ToAliasT (SqlExpr (Maybe (Entity a))) = SqlExpr (Maybe (Entity a))
ToAliasT (a, b) = (ToAliasT a, ToAliasT b)
ToAliasT (a, b, c) = (ToAliasT a, ToAliasT b, ToAliasT c)
ToAliasT (a, b, c, d) = (ToAliasT a, ToAliasT b, ToAliasT c, ToAliasT d)
ToAliasT (a, b, c, d, e) = (ToAliasT a, ToAliasT b, ToAliasT c, ToAliasT d, ToAliasT e)
ToAliasT (a, b, c, d, e, f) = (ToAliasT a, ToAliasT b, ToAliasT c, ToAliasT d, ToAliasT e, ToAliasT f)
ToAliasT (a, b, c, d, e, f, g) = (ToAliasT a, ToAliasT b, ToAliasT c, ToAliasT d, ToAliasT e, ToAliasT f, ToAliasT g)
ToAliasT (a, b, c, d, e, f, g, h) = (ToAliasT a, ToAliasT b, ToAliasT c, ToAliasT d, ToAliasT e, ToAliasT f, ToAliasT g, ToAliasT h)
-- Tedious tuple magic -- Tedious tuple magic
class ToAlias a where class ToAlias a where
toAlias :: a -> SqlQuery (ToAliasT a) toAlias :: a -> SqlQuery a
instance ToAlias (SqlExpr (Value a)) where instance ToAlias (SqlExpr (Value a)) where
toAlias v@(EAliasedValue _ _) = pure v toAlias v@(EAliasedValue _ _) = pure v
@ -1292,21 +1244,12 @@ instance ( ToAlias a
toAlias x = to8 <$> (toAlias $ from8 x) toAlias x = to8 <$> (toAlias $ from8 x)
type family ToAliasReferenceT a where {-# DEPRECATED ToAliasReferenceT "This type alias doesn't do anything. Please delete it. Will be removed in the next release." #-}
ToAliasReferenceT (SqlExpr (Value a)) = SqlExpr (Value a) type ToAliasReferenceT a = a
ToAliasReferenceT (SqlExpr (Entity a)) = SqlExpr (Entity a)
ToAliasReferenceT (SqlExpr (Maybe (Entity a))) = SqlExpr (Maybe (Entity a))
ToAliasReferenceT (a,b) = (ToAliasReferenceT a, ToAliasReferenceT b)
ToAliasReferenceT (a,b,c) = (ToAliasReferenceT a, ToAliasReferenceT b, ToAliasReferenceT c)
ToAliasReferenceT (a, b, c, d) = (ToAliasReferenceT a, ToAliasReferenceT b, ToAliasReferenceT c, ToAliasReferenceT d)
ToAliasReferenceT (a, b, c, d, e) = (ToAliasReferenceT a, ToAliasReferenceT b, ToAliasReferenceT c, ToAliasReferenceT d, ToAliasReferenceT e)
ToAliasReferenceT (a, b, c, d, e, f) = (ToAliasReferenceT a, ToAliasReferenceT b, ToAliasReferenceT c, ToAliasReferenceT d, ToAliasReferenceT e, ToAliasReferenceT f)
ToAliasReferenceT (a, b, c, d, e, f, g) = (ToAliasReferenceT a, ToAliasReferenceT b, ToAliasReferenceT c, ToAliasReferenceT d, ToAliasReferenceT e, ToAliasReferenceT f, ToAliasReferenceT g)
ToAliasReferenceT (a, b, c, d, e, f, g, h) = (ToAliasReferenceT a, ToAliasReferenceT b, ToAliasReferenceT c, ToAliasReferenceT d, ToAliasReferenceT e, ToAliasReferenceT f, ToAliasReferenceT g, ToAliasReferenceT h)
-- more tedious tuple magic -- more tedious tuple magic
class ToAliasReference a where class ToAliasReference a where
toAliasReference :: Ident -> a -> SqlQuery (ToAliasReferenceT a) toAliasReference :: Ident -> a -> SqlQuery a
instance ToAliasReference (SqlExpr (Value a)) where instance ToAliasReference (SqlExpr (Value a)) where
toAliasReference aliasSource (EAliasedValue aliasIdent _) = pure $ EValueReference aliasSource (\_ -> aliasIdent) toAliasReference aliasSource (EAliasedValue aliasIdent _) = pure $ EValueReference aliasSource (\_ -> aliasIdent)
@ -1321,6 +1264,7 @@ instance ToAliasReference (SqlExpr (Entity a)) where
instance ToAliasReference (SqlExpr (Maybe (Entity a))) where instance ToAliasReference (SqlExpr (Maybe (Entity a))) where
toAliasReference s (EMaybe e) = EMaybe <$> toAliasReference s e toAliasReference s (EMaybe e) = EMaybe <$> toAliasReference s e
instance (ToAliasReference a, ToAliasReference b) => ToAliasReference (a, b) where instance (ToAliasReference a, ToAliasReference b) => ToAliasReference (a, b) where
toAliasReference ident (a,b) = (,) <$> (toAliasReference ident a) <*> (toAliasReference ident b) toAliasReference ident (a,b) = (,) <$> (toAliasReference ident a) <*> (toAliasReference ident b)