-- | Postgres Translate Delete
--
-- Tranlates an IR delete term to a Postgres SQL DELETE statement.
--
-- See 'Hasura.Backends.Postgres.Execute.Mutation.execDeleteQuery'.
module Hasura.Backends.Postgres.Translate.Delete
  ( mkDelete,
  )
where

import Hasura.Backends.Postgres.SQL.DML qualified as S
import Hasura.Backends.Postgres.Translate.BoolExp
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.IR.Delete
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.BackendType

mkDelete ::
  (Backend ('Postgres pgKind)) =>
  AnnDel ('Postgres pgKind) ->
  S.SQLDelete
mkDelete :: forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
AnnDel ('Postgres pgKind) -> SQLDelete
mkDelete (AnnDel TableName ('Postgres pgKind)
tn (AnnBoolExp ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
fltr, AnnBoolExp ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
wc) MutationOutputG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
_ [ColumnInfo ('Postgres pgKind)]
_ Maybe NamingCase
_ Maybe (ValidateInput ResolvedWebhook)
_ Bool
_) =
  QualifiedTable
-> Maybe UsingExp -> Maybe WhereFrag -> Maybe RetExp -> SQLDelete
S.SQLDelete TableName ('Postgres pgKind)
QualifiedTable
tn Maybe UsingExp
forall a. Maybe a
Nothing Maybe WhereFrag
tableFltr (Maybe RetExp -> SQLDelete) -> Maybe RetExp -> SQLDelete
forall a b. (a -> b) -> a -> b
$ RetExp -> Maybe RetExp
forall a. a -> Maybe a
Just RetExp
S.returningStar
  where
    tableFltr :: Maybe WhereFrag
tableFltr =
      WhereFrag -> Maybe WhereFrag
forall a. a -> Maybe a
Just
        (WhereFrag -> Maybe WhereFrag)
-> (AnnBoolExp ('Postgres pgKind) SQLExp -> WhereFrag)
-> AnnBoolExp ('Postgres pgKind) SQLExp
-> Maybe WhereFrag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BoolExp -> WhereFrag
S.WhereFrag
        (BoolExp -> WhereFrag)
-> (AnnBoolExp ('Postgres pgKind) SQLExp -> BoolExp)
-> AnnBoolExp ('Postgres pgKind) SQLExp
-> WhereFrag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BoolExp -> BoolExp
S.simplifyBoolExp
        (BoolExp -> BoolExp)
-> (AnnBoolExp ('Postgres pgKind) SQLExp -> BoolExp)
-> AnnBoolExp ('Postgres pgKind) SQLExp
-> BoolExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Qual
-> AnnBoolExp ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
-> BoolExp
forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
toSQLBoolExp (QualifiedTable -> Qual
S.QualTable TableName ('Postgres pgKind)
QualifiedTable
tn)
        (AnnBoolExp ('Postgres pgKind) SQLExp -> Maybe WhereFrag)
-> AnnBoolExp ('Postgres pgKind) SQLExp -> Maybe WhereFrag
forall a b. (a -> b) -> a -> b
$ AnnBoolExp ('Postgres pgKind) SQLExp
-> AnnBoolExp ('Postgres pgKind) SQLExp
-> AnnBoolExp ('Postgres pgKind) SQLExp
forall (backend :: BackendType) scalar.
AnnBoolExp backend scalar
-> AnnBoolExp backend scalar -> AnnBoolExp backend scalar
andAnnBoolExps AnnBoolExp ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
AnnBoolExp ('Postgres pgKind) SQLExp
fltr AnnBoolExp ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
AnnBoolExp ('Postgres pgKind) SQLExp
wc