{-# LANGUAGE
DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, GeneralizedNewtypeDeriving
, LambdaCase
, MultiParamTypeClasses
, OverloadedStrings
, PatternSynonyms
, QuantifiedConstraints
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, TypeFamilies
, TypeInType
, TypeOperators
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Manipulation.Update
(
update
, update_
) where
import Data.ByteString hiding (foldr)
import GHC.TypeLits
import qualified Generics.SOP as SOP
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Default
import Squeal.PostgreSQL.Expression.Logic
import Squeal.PostgreSQL.Manipulation
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
renderUpdate
:: (forall x. RenderSQL (expr x))
=> Aliased (Optional expr) ty
-> ByteString
renderUpdate (expr `As` col) = renderSQL col <+> "=" <+> renderSQL expr
update
:: ( Has sch db schema
, Has tab0 schema ('Table table)
, Updatable table updates
, SOP.SListI row )
=> Aliased (QualifiedAlias sch) (tab ::: tab0)
-> NP (Aliased (Optional (Expression 'Ungrouped '[] with db params (tab ::: TableToRow table ': from)))) updates
-> 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
update (tab0 `As` tab) columns using wh returning = UnsafeManipulation $
"UPDATE"
<+> renderSQL tab0 <+> "AS" <+> renderSQL tab
<+> "SET"
<+> renderCommaSeparated renderUpdate columns
<> case using of
NoUsing -> ""
Using tables -> " FROM" <+> renderSQL tables
<+> "WHERE" <+> renderSQL wh
<> renderSQL returning
update_
:: ( Has sch db schema
, Has tab0 schema ('Table table)
, KnownSymbol tab
, Updatable table updates )
=> Aliased (QualifiedAlias sch) (tab ::: tab0)
-> NP (Aliased (Optional (Expression 'Ungrouped '[] with db params '[tab ::: TableToRow table]))) updates
-> Condition 'Ungrouped '[] with db params '[tab ::: TableToRow table]
-> Manipulation with db params '[]
update_ tab columns wh = update tab columns NoUsing wh (Returning_ Nil)