{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}

module Hasura.RQL.IR.Update
  ( AnnotatedUpdate,
    AnnotatedUpdateG (..),
    auTable,
    auWhere,
    auCheck,
    auBackend,
    auOutput,
    auAllCols,
    auNamingConvention,
  )
where

import Control.Lens.TH (makeLenses)
import Data.Kind (Type)
import Hasura.GraphQL.Schema.NamingCase (NamingCase)
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.IR.Returning
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.Column
import Hasura.SQL.Backend

data AnnotatedUpdateG (b :: BackendType) (r :: Type) v = AnnotatedUpdateG
  { AnnotatedUpdateG b r v -> TableName b
_auTable :: TableName b,
    -- | The where clause for /update_table/ and /update_table_by_pk/ along with
    -- the permissions filter.
    -- In the case of /update_table_many/, this will be empty and the actual
    -- where clauses (one per update) are found in 'BackendUpdate'.
    AnnotatedUpdateG b r v -> (AnnBoolExp b v, AnnBoolExp b v)
_auWhere :: (AnnBoolExp b v, AnnBoolExp b v),
    AnnotatedUpdateG b r v -> AnnBoolExp b v
_auCheck :: AnnBoolExp b v,
    -- | All the backend-specific data related to an update mutation
    AnnotatedUpdateG b r v -> BackendUpdate b v
_auBackend :: BackendUpdate b v,
    -- we don't prepare the arguments for returning
    -- however the session variable can still be
    -- converted as desired

    -- | Selection set
    AnnotatedUpdateG b r v -> MutationOutputG b r v
_auOutput :: MutationOutputG b r v,
    AnnotatedUpdateG b r v -> [ColumnInfo b]
_auAllCols :: [ColumnInfo b],
    AnnotatedUpdateG b r v -> Maybe NamingCase
_auNamingConvention :: Maybe NamingCase
  }
  deriving stock (a -> AnnotatedUpdateG b r b -> AnnotatedUpdateG b r a
(a -> b) -> AnnotatedUpdateG b r a -> AnnotatedUpdateG b r b
(forall a b.
 (a -> b) -> AnnotatedUpdateG b r a -> AnnotatedUpdateG b r b)
-> (forall a b.
    a -> AnnotatedUpdateG b r b -> AnnotatedUpdateG b r a)
-> Functor (AnnotatedUpdateG b r)
forall a b. a -> AnnotatedUpdateG b r b -> AnnotatedUpdateG b r a
forall a b.
(a -> b) -> AnnotatedUpdateG b r a -> AnnotatedUpdateG b r b
forall (b :: BackendType) r a b.
Backend b =>
a -> AnnotatedUpdateG b r b -> AnnotatedUpdateG b r a
forall (b :: BackendType) r a b.
Backend b =>
(a -> b) -> AnnotatedUpdateG b r a -> AnnotatedUpdateG b r b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> AnnotatedUpdateG b r b -> AnnotatedUpdateG b r a
$c<$ :: forall (b :: BackendType) r a b.
Backend b =>
a -> AnnotatedUpdateG b r b -> AnnotatedUpdateG b r a
fmap :: (a -> b) -> AnnotatedUpdateG b r a -> AnnotatedUpdateG b r b
$cfmap :: forall (b :: BackendType) r a b.
Backend b =>
(a -> b) -> AnnotatedUpdateG b r a -> AnnotatedUpdateG b r b
Functor, AnnotatedUpdateG b r a -> Bool
(a -> m) -> AnnotatedUpdateG b r a -> m
(a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b
(forall m. Monoid m => AnnotatedUpdateG b r m -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> AnnotatedUpdateG b r a -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> AnnotatedUpdateG b r a -> m)
-> (forall a b. (a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b)
-> (forall a b. (a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b)
-> (forall b a. (b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b)
-> (forall b a. (b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b)
-> (forall a. (a -> a -> a) -> AnnotatedUpdateG b r a -> a)
-> (forall a. (a -> a -> a) -> AnnotatedUpdateG b r a -> a)
-> (forall a. AnnotatedUpdateG b r a -> [a])
-> (forall a. AnnotatedUpdateG b r a -> Bool)
-> (forall a. AnnotatedUpdateG b r a -> Int)
-> (forall a. Eq a => a -> AnnotatedUpdateG b r a -> Bool)
-> (forall a. Ord a => AnnotatedUpdateG b r a -> a)
-> (forall a. Ord a => AnnotatedUpdateG b r a -> a)
-> (forall a. Num a => AnnotatedUpdateG b r a -> a)
-> (forall a. Num a => AnnotatedUpdateG b r a -> a)
-> Foldable (AnnotatedUpdateG b r)
forall a. Eq a => a -> AnnotatedUpdateG b r a -> Bool
forall a. Num a => AnnotatedUpdateG b r a -> a
forall a. Ord a => AnnotatedUpdateG b r a -> a
forall m. Monoid m => AnnotatedUpdateG b r m -> m
forall a. AnnotatedUpdateG b r a -> Bool
forall a. AnnotatedUpdateG b r a -> Int
forall a. AnnotatedUpdateG b r a -> [a]
forall a. (a -> a -> a) -> AnnotatedUpdateG b r a -> a
forall m a. Monoid m => (a -> m) -> AnnotatedUpdateG b r a -> m
forall b a. (b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b
forall a b. (a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b
forall (b :: BackendType) r a.
(Backend b, Eq a) =>
a -> AnnotatedUpdateG b r a -> Bool
forall (b :: BackendType) r a.
(Backend b, Num a) =>
AnnotatedUpdateG b r a -> a
forall (b :: BackendType) r a.
(Backend b, Ord a) =>
AnnotatedUpdateG b r a -> a
forall (b :: BackendType) r m.
(Backend b, Monoid m) =>
AnnotatedUpdateG b r m -> m
forall (b :: BackendType) r a.
Backend b =>
AnnotatedUpdateG b r a -> Bool
forall (b :: BackendType) r a.
Backend b =>
AnnotatedUpdateG b r a -> Int
forall (b :: BackendType) r a.
Backend b =>
AnnotatedUpdateG b r a -> [a]
forall (b :: BackendType) r a.
Backend b =>
(a -> a -> a) -> AnnotatedUpdateG b r a -> a
forall (b :: BackendType) r m a.
(Backend b, Monoid m) =>
(a -> m) -> AnnotatedUpdateG b r a -> m
forall (b :: BackendType) r b a.
Backend b =>
(b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b
forall (b :: BackendType) r a b.
Backend b =>
(a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: AnnotatedUpdateG b r a -> a
$cproduct :: forall (b :: BackendType) r a.
(Backend b, Num a) =>
AnnotatedUpdateG b r a -> a
sum :: AnnotatedUpdateG b r a -> a
$csum :: forall (b :: BackendType) r a.
(Backend b, Num a) =>
AnnotatedUpdateG b r a -> a
minimum :: AnnotatedUpdateG b r a -> a
$cminimum :: forall (b :: BackendType) r a.
(Backend b, Ord a) =>
AnnotatedUpdateG b r a -> a
maximum :: AnnotatedUpdateG b r a -> a
$cmaximum :: forall (b :: BackendType) r a.
(Backend b, Ord a) =>
AnnotatedUpdateG b r a -> a
elem :: a -> AnnotatedUpdateG b r a -> Bool
$celem :: forall (b :: BackendType) r a.
(Backend b, Eq a) =>
a -> AnnotatedUpdateG b r a -> Bool
length :: AnnotatedUpdateG b r a -> Int
$clength :: forall (b :: BackendType) r a.
Backend b =>
AnnotatedUpdateG b r a -> Int
null :: AnnotatedUpdateG b r a -> Bool
$cnull :: forall (b :: BackendType) r a.
Backend b =>
AnnotatedUpdateG b r a -> Bool
toList :: AnnotatedUpdateG b r a -> [a]
$ctoList :: forall (b :: BackendType) r a.
Backend b =>
AnnotatedUpdateG b r a -> [a]
foldl1 :: (a -> a -> a) -> AnnotatedUpdateG b r a -> a
$cfoldl1 :: forall (b :: BackendType) r a.
Backend b =>
(a -> a -> a) -> AnnotatedUpdateG b r a -> a
foldr1 :: (a -> a -> a) -> AnnotatedUpdateG b r a -> a
$cfoldr1 :: forall (b :: BackendType) r a.
Backend b =>
(a -> a -> a) -> AnnotatedUpdateG b r a -> a
foldl' :: (b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b
$cfoldl' :: forall (b :: BackendType) r b a.
Backend b =>
(b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b
foldl :: (b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b
$cfoldl :: forall (b :: BackendType) r b a.
Backend b =>
(b -> a -> b) -> b -> AnnotatedUpdateG b r a -> b
foldr' :: (a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b
$cfoldr' :: forall (b :: BackendType) r a b.
Backend b =>
(a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b
foldr :: (a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b
$cfoldr :: forall (b :: BackendType) r a b.
Backend b =>
(a -> b -> b) -> b -> AnnotatedUpdateG b r a -> b
foldMap' :: (a -> m) -> AnnotatedUpdateG b r a -> m
$cfoldMap' :: forall (b :: BackendType) r m a.
(Backend b, Monoid m) =>
(a -> m) -> AnnotatedUpdateG b r a -> m
foldMap :: (a -> m) -> AnnotatedUpdateG b r a -> m
$cfoldMap :: forall (b :: BackendType) r m a.
(Backend b, Monoid m) =>
(a -> m) -> AnnotatedUpdateG b r a -> m
fold :: AnnotatedUpdateG b r m -> m
$cfold :: forall (b :: BackendType) r m.
(Backend b, Monoid m) =>
AnnotatedUpdateG b r m -> m
Foldable, Functor (AnnotatedUpdateG b r)
Foldable (AnnotatedUpdateG b r)
Functor (AnnotatedUpdateG b r)
-> Foldable (AnnotatedUpdateG b r)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> AnnotatedUpdateG b r a -> f (AnnotatedUpdateG b r b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    AnnotatedUpdateG b r (f a) -> f (AnnotatedUpdateG b r a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> AnnotatedUpdateG b r a -> m (AnnotatedUpdateG b r b))
-> (forall (m :: * -> *) a.
    Monad m =>
    AnnotatedUpdateG b r (m a) -> m (AnnotatedUpdateG b r a))
-> Traversable (AnnotatedUpdateG b r)
(a -> f b) -> AnnotatedUpdateG b r a -> f (AnnotatedUpdateG b r b)
forall (b :: BackendType) r.
Backend b =>
Functor (AnnotatedUpdateG b r)
forall (b :: BackendType) r.
Backend b =>
Foldable (AnnotatedUpdateG b r)
forall (b :: BackendType) r (m :: * -> *) a.
(Backend b, Monad m) =>
AnnotatedUpdateG b r (m a) -> m (AnnotatedUpdateG b r a)
forall (b :: BackendType) r (f :: * -> *) a.
(Backend b, Applicative f) =>
AnnotatedUpdateG b r (f a) -> f (AnnotatedUpdateG b r a)
forall (b :: BackendType) r (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b) -> AnnotatedUpdateG b r a -> m (AnnotatedUpdateG b r b)
forall (b :: BackendType) r (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b) -> AnnotatedUpdateG b r a -> f (AnnotatedUpdateG b r b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
AnnotatedUpdateG b r (m a) -> m (AnnotatedUpdateG b r a)
forall (f :: * -> *) a.
Applicative f =>
AnnotatedUpdateG b r (f a) -> f (AnnotatedUpdateG b r a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> AnnotatedUpdateG b r a -> m (AnnotatedUpdateG b r b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> AnnotatedUpdateG b r a -> f (AnnotatedUpdateG b r b)
sequence :: AnnotatedUpdateG b r (m a) -> m (AnnotatedUpdateG b r a)
$csequence :: forall (b :: BackendType) r (m :: * -> *) a.
(Backend b, Monad m) =>
AnnotatedUpdateG b r (m a) -> m (AnnotatedUpdateG b r a)
mapM :: (a -> m b) -> AnnotatedUpdateG b r a -> m (AnnotatedUpdateG b r b)
$cmapM :: forall (b :: BackendType) r (m :: * -> *) a b.
(Backend b, Monad m) =>
(a -> m b) -> AnnotatedUpdateG b r a -> m (AnnotatedUpdateG b r b)
sequenceA :: AnnotatedUpdateG b r (f a) -> f (AnnotatedUpdateG b r a)
$csequenceA :: forall (b :: BackendType) r (f :: * -> *) a.
(Backend b, Applicative f) =>
AnnotatedUpdateG b r (f a) -> f (AnnotatedUpdateG b r a)
traverse :: (a -> f b) -> AnnotatedUpdateG b r a -> f (AnnotatedUpdateG b r b)
$ctraverse :: forall (b :: BackendType) r (f :: * -> *) a b.
(Backend b, Applicative f) =>
(a -> f b) -> AnnotatedUpdateG b r a -> f (AnnotatedUpdateG b r b)
$cp2Traversable :: forall (b :: BackendType) r.
Backend b =>
Foldable (AnnotatedUpdateG b r)
$cp1Traversable :: forall (b :: BackendType) r.
Backend b =>
Functor (AnnotatedUpdateG b r)
Traversable)

deriving stock instance
  ( Backend b,
    Eq (AnnBoolExp b v),
    Eq (MutationOutputG b r v),
    Eq (BackendUpdate b v),
    Eq r,
    Eq v
  ) =>
  Eq (AnnotatedUpdateG b r v)

deriving stock instance
  ( Backend b,
    Show (AnnBoolExp b v),
    Show (MutationOutputG b r v),
    Show (BackendUpdate b v),
    Show r,
    Show v
  ) =>
  Show (AnnotatedUpdateG b r v)

type AnnotatedUpdate b = AnnotatedUpdateG b Void (SQLExpression b)

$(makeLenses ''AnnotatedUpdateG)