{-|
Module: Squeal.PostgreSQL.Delete
Description: delete statements
Copyright: (c) Eitan Chatav, 2019
Maintainer: eitan@morphism.tech
Stability: experimental

delete statements
-}

{-# LANGUAGE
    DeriveGeneric
  , DerivingStrategies
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , LambdaCase
  , MultiParamTypeClasses
  , OverloadedStrings
  , PatternSynonyms
  , QuantifiedConstraints
  , RankNTypes
  , ScopedTypeVariables
  , TypeApplications
  , TypeFamilies
  , DataKinds
  , PolyKinds
  , TypeOperators
  , UndecidableInstances
#-}

module Squeal.PostgreSQL.Manipulation.Delete
  ( -- * Delete
    deleteFrom
  , deleteFrom_
  ) where

import qualified Generics.SOP as SOP

import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Expression.Logic
import Squeal.PostgreSQL.Manipulation
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema

-- $setup
-- >>> import Squeal.PostgreSQL

{-----------------------------------------
DELETE statements
-----------------------------------------}

{- | Delete rows from a table.

>>> type Columns = '["col1" ::: 'Def :=> 'NotNull 'PGint4, "col2" ::: 'NoDef :=> 'NotNull 'PGint4]
>>> type Schema = '["tab1" ::: 'Table ('[] :=> Columns), "tab2" ::: 'Table ('[] :=> Columns)]
>>> :{
let
  manp :: Manipulation with (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
  manp = deleteFrom #tab1 (Using (table #tab2)) (#tab1 ! #col1 .== #tab2 ! #col2) (Returning (#tab1 & DotStar))
in printSQL manp
:}
DELETE FROM "tab1" AS "tab1" USING "tab2" AS "tab2" WHERE ("tab1"."col1" = "tab2"."col2") RETURNING "tab1".*
-}
deleteFrom
  :: ( SOP.SListI row
     , Has sch db schema
     , Has tab0 schema ('Table table) )
  => Aliased (QualifiedAlias sch) (tab ::: tab0) -- ^ table to delete from
  -> UsingClause with db params from
  -> Condition  'Ungrouped '[] with db params (tab ::: TableToRow table ': from)
  -- ^ condition under which to delete a row
  -> ReturningClause with db params (tab ::: TableToRow table ': from) row
  -- ^ results to return
  -> Manipulation with db params row
deleteFrom :: forall (row :: [(Symbol, NullType)]) (sch :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab0 :: Symbol)
       (table :: TableType) (tab :: Symbol) (with :: FromType)
       (params :: [NullType]) (from :: FromType).
(SListI row, Has sch db schema, Has tab0 schema ('Table table)) =>
Aliased (QualifiedAlias sch) (tab ::: tab0)
-> UsingClause with db params from
-> Condition
     'Ungrouped '[] with db params ((tab ::: TableToRow table) : from)
-> ReturningClause
     with db params ((tab ::: TableToRow table) : from) row
-> Manipulation with db params row
deleteFrom (QualifiedAlias sch ty
tab0 `As` Alias alias
tab) UsingClause with db params from
using Condition
  'Ungrouped '[] with db params ((tab ::: TableToRow table) : from)
wh ReturningClause
  with db params ((tab ::: TableToRow table) : from) row
returning = forall (with :: FromType)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (params :: [NullType])
       (columns :: [(Symbol, NullType)]).
ByteString -> Manipulation with db params columns
UnsafeManipulation forall a b. (a -> b) -> a -> b
$
  ByteString
"DELETE FROM"
  ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch ty
tab0 ByteString -> ByteString -> ByteString
<+> ByteString
"AS" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias alias
tab
  forall a. Semigroup a => a -> a -> a
<> case UsingClause with db params from
using of
    UsingClause with db params from
NoUsing -> ByteString
""
    Using FromClause '[] with db params from
tables -> ByteString
" USING" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL FromClause '[] with db params from
tables
  ByteString -> ByteString -> ByteString
<+> ByteString
"WHERE" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Condition
  'Ungrouped '[] with db params ((tab ::: TableToRow table) : from)
wh
  forall a. Semigroup a => a -> a -> a
<> forall sql. RenderSQL sql => sql -> ByteString
renderSQL ReturningClause
  with db params ((tab ::: TableToRow table) : from) row
returning

{- | Delete rows returning `Nil`.

>>> type Columns = '["col1" ::: 'Def :=> 'NotNull 'PGint4]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> :{
let
  manp :: Manipulation with (Public Schema) '[ 'NotNull 'PGint4] '[]
  manp = deleteFrom_ (#tab `as` #t) (#t ! #col1 .== param @1)
in printSQL manp
:}
DELETE FROM "tab" AS "t" WHERE ("t"."col1" = ($1 :: int4))
-}
deleteFrom_
  :: ( Has sch db schema
     , Has tab0 schema ('Table table) )
  => Aliased (QualifiedAlias sch) (tab ::: tab0) -- ^ table to delete from
  -> Condition  'Ungrouped '[] with db params '[tab ::: TableToRow table]
  -- ^ condition under which to delete a row
  -> Manipulation with db params '[]
deleteFrom_ :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab0 :: Symbol)
       (table :: TableType) (tab :: Symbol) (with :: FromType)
       (params :: [NullType]).
(Has sch db schema, Has tab0 schema ('Table table)) =>
Aliased (QualifiedAlias sch) (tab ::: tab0)
-> Condition
     'Ungrouped '[] with db params '[tab ::: TableToRow table]
-> Manipulation with db params '[]
deleteFrom_ Aliased (QualifiedAlias sch) (tab ::: tab0)
tab Condition 'Ungrouped '[] with db params '[tab ::: TableToRow table]
wh = forall (row :: [(Symbol, NullType)]) (sch :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab0 :: Symbol)
       (table :: TableType) (tab :: Symbol) (with :: FromType)
       (params :: [NullType]) (from :: FromType).
(SListI row, Has sch db schema, Has tab0 schema ('Table table)) =>
Aliased (QualifiedAlias sch) (tab ::: tab0)
-> UsingClause with db params from
-> Condition
     'Ungrouped '[] with db params ((tab ::: TableToRow table) : from)
-> ReturningClause
     with db params ((tab ::: TableToRow table) : from) row
-> Manipulation with db params row
deleteFrom Aliased (QualifiedAlias sch) (tab ::: tab0)
tab forall (with :: FromType)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (params :: [NullType]).
UsingClause with db params '[]
NoUsing Condition 'Ungrouped '[] with db params '[tab ::: TableToRow table]
wh (forall (row :: [(Symbol, NullType)]) (with :: FromType)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (params :: [NullType])
       (from :: FromType).
SListI row =>
NP (Aliased (Expression 'Ungrouped '[] with db params from)) row
-> ReturningClause with db params from row
Returning_ forall {k} (a :: k -> *). NP a '[]
Nil)