{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}

module Database.Esqueleto.Experimental.ToAlias
    where

import Database.Esqueleto.Internal.Internal hiding (From, from, on)
import Database.Esqueleto.Internal.PersistentImport

{-# DEPRECATED ToAliasT "This type alias doesn't do anything. Please delete it. Will be removed in the next release." #-}
type ToAliasT a = a

-- Tedious tuple magic
class ToAlias a where
    toAlias :: a -> SqlQuery a

instance ToAlias (SqlExpr (Value a)) where
    toAlias :: SqlExpr (Value a) -> SqlQuery (SqlExpr (Value a))
toAlias e :: SqlExpr (Value a)
e@(ERaw SqlExprMeta
m NeedParens -> IdentInfo -> (Builder, [PersistValue])
f)
      | Just Ident
_ <- SqlExprMeta -> Maybe Ident
sqlExprMetaAlias SqlExprMeta
m = forall (f :: * -> *) a. Applicative f => a -> f a
pure SqlExpr (Value a)
e
      | Bool
otherwise = do
            Ident
ident <- DBName -> SqlQuery Ident
newIdentFor (Text -> DBName
DBName Text
"v")
            forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a.
SqlExprMeta
-> (NeedParens -> IdentInfo -> (Builder, [PersistValue]))
-> SqlExpr a
ERaw SqlExprMeta
noMeta{sqlExprMetaAlias :: Maybe Ident
sqlExprMetaAlias = forall a. a -> Maybe a
Just Ident
ident} NeedParens -> IdentInfo -> (Builder, [PersistValue])
f

instance ToAlias (SqlExpr (Entity a)) where
    toAlias :: SqlExpr (Entity a) -> SqlQuery (SqlExpr (Entity a))
toAlias e :: SqlExpr (Entity a)
e@(ERaw SqlExprMeta
m NeedParens -> IdentInfo -> (Builder, [PersistValue])
f)
      | Just Ident
_ <- SqlExprMeta -> Maybe Ident
sqlExprMetaAlias SqlExprMeta
m = forall (f :: * -> *) a. Applicative f => a -> f a
pure SqlExpr (Entity a)
e
      | Bool
otherwise = do
           Ident
ident <- DBName -> SqlQuery Ident
newIdentFor (Text -> DBName
DBName Text
"v")
           forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a.
SqlExprMeta
-> (NeedParens -> IdentInfo -> (Builder, [PersistValue]))
-> SqlExpr a
ERaw SqlExprMeta
m{sqlExprMetaIsReference :: Bool
sqlExprMetaIsReference = Bool
False, sqlExprMetaAlias :: Maybe Ident
sqlExprMetaAlias = forall a. a -> Maybe a
Just Ident
ident} NeedParens -> IdentInfo -> (Builder, [PersistValue])
f

instance ToAlias (SqlExpr (Maybe (Entity a))) where
    -- FIXME: Code duplication because the compiler doesnt like half final encoding
    toAlias :: SqlExpr (Maybe (Entity a)) -> SqlQuery (SqlExpr (Maybe (Entity a)))
toAlias e :: SqlExpr (Maybe (Entity a))
e@(ERaw SqlExprMeta
m NeedParens -> IdentInfo -> (Builder, [PersistValue])
f)
      | Just Ident
_ <- SqlExprMeta -> Maybe Ident
sqlExprMetaAlias SqlExprMeta
m = forall (f :: * -> *) a. Applicative f => a -> f a
pure SqlExpr (Maybe (Entity a))
e
      | Bool
otherwise = do
           Ident
ident <- DBName -> SqlQuery Ident
newIdentFor (Text -> DBName
DBName Text
"v")
           forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a.
SqlExprMeta
-> (NeedParens -> IdentInfo -> (Builder, [PersistValue]))
-> SqlExpr a
ERaw SqlExprMeta
m{sqlExprMetaIsReference :: Bool
sqlExprMetaIsReference = Bool
False, sqlExprMetaAlias :: Maybe Ident
sqlExprMetaAlias = forall a. a -> Maybe a
Just Ident
ident} NeedParens -> IdentInfo -> (Builder, [PersistValue])
f

instance (ToAlias a, ToAlias b) => ToAlias (a,b) where
    toAlias :: (a, b) -> SqlQuery (a, b)
toAlias (a
a,b
b) = (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. ToAlias a => a -> SqlQuery a
toAlias a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. ToAlias a => a -> SqlQuery a
toAlias b
b

instance ( ToAlias a
         , ToAlias b
         , ToAlias c
         ) => ToAlias (a,b,c) where
    toAlias :: (a, b, c) -> SqlQuery (a, b, c)
toAlias (a, b, c)
x = forall a b c. ((a, b), c) -> (a, b, c)
to3 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. ToAlias a => a -> SqlQuery a
toAlias forall a b. (a -> b) -> a -> b
$ forall a b c. (a, b, c) -> ((a, b), c)
from3 (a, b, c)
x)

instance ( ToAlias a
         , ToAlias b
         , ToAlias c
         , ToAlias d
         ) => ToAlias (a,b,c,d) where
    toAlias :: (a, b, c, d) -> SqlQuery (a, b, c, d)
toAlias (a, b, c, d)
x = forall a b c d. ((a, b), (c, d)) -> (a, b, c, d)
to4 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. ToAlias a => a -> SqlQuery a
toAlias forall a b. (a -> b) -> a -> b
$ forall a b c d. (a, b, c, d) -> ((a, b), (c, d))
from4 (a, b, c, d)
x)

instance ( ToAlias a
         , ToAlias b
         , ToAlias c
         , ToAlias d
         , ToAlias e
         ) => ToAlias (a,b,c,d,e) where
    toAlias :: (a, b, c, d, e) -> SqlQuery (a, b, c, d, e)
toAlias (a, b, c, d, e)
x = forall a b c d e. ((a, b), (c, d), e) -> (a, b, c, d, e)
to5 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. ToAlias a => a -> SqlQuery a
toAlias forall a b. (a -> b) -> a -> b
$ forall a b c d e. (a, b, c, d, e) -> ((a, b), (c, d), e)
from5 (a, b, c, d, e)
x)

instance ( ToAlias a
         , ToAlias b
         , ToAlias c
         , ToAlias d
         , ToAlias e
         , ToAlias f
         ) => ToAlias (a,b,c,d,e,f) where
    toAlias :: (a, b, c, d, e, f) -> SqlQuery (a, b, c, d, e, f)
toAlias (a, b, c, d, e, f)
x = forall a b c d e f. ((a, b), (c, d), (e, f)) -> (a, b, c, d, e, f)
to6 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. ToAlias a => a -> SqlQuery a
toAlias forall a b. (a -> b) -> a -> b
$ forall a b c d e f. (a, b, c, d, e, f) -> ((a, b), (c, d), (e, f))
from6 (a, b, c, d, e, f)
x)

instance ( ToAlias a
         , ToAlias b
         , ToAlias c
         , ToAlias d
         , ToAlias e
         , ToAlias f
         , ToAlias g
         ) => ToAlias (a,b,c,d,e,f,g) where
    toAlias :: (a, b, c, d, e, f, g) -> SqlQuery (a, b, c, d, e, f, g)
toAlias (a, b, c, d, e, f, g)
x = forall a b c d e f g.
((a, b), (c, d), (e, f), g) -> (a, b, c, d, e, f, g)
to7 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. ToAlias a => a -> SqlQuery a
toAlias forall a b. (a -> b) -> a -> b
$ forall a b c d e f g.
(a, b, c, d, e, f, g) -> ((a, b), (c, d), (e, f), g)
from7 (a, b, c, d, e, f, g)
x)

instance ( ToAlias a
         , ToAlias b
         , ToAlias c
         , ToAlias d
         , ToAlias e
         , ToAlias f
         , ToAlias g
         , ToAlias h
         ) => ToAlias (a,b,c,d,e,f,g,h) where
    toAlias :: (a, b, c, d, e, f, g, h) -> SqlQuery (a, b, c, d, e, f, g, h)
toAlias (a, b, c, d, e, f, g, h)
x = forall a b c d e f g h.
((a, b), (c, d), (e, f), (g, h)) -> (a, b, c, d, e, f, g, h)
to8 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. ToAlias a => a -> SqlQuery a
toAlias forall a b. (a -> b) -> a -> b
$ forall a b c d e f g h.
(a, b, c, d, e, f, g, h) -> ((a, b), (c, d), (e, f), (g, h))
from8 (a, b, c, d, e, f, g, h)
x)