{-# language DuplicateRecordFields #-}
{-# language GADTs #-}
{-# language NamedFieldPuns #-}
{-# language RecordWildCards #-}
{-# language StandaloneKindSignatures #-}
{-# language StrictData #-}

module Rel8.Statement.Update
  ( Update(..)
  , update
  , ppUpdate
  )
where

-- base
import Data.Kind ( Type )
import Prelude

-- hasql
import qualified Hasql.Encoders as Hasql
import qualified Hasql.Statement as Hasql

-- pretty
import Text.PrettyPrint ( Doc, (<+>), ($$), text )

-- rel8
import Rel8.Expr ( Expr )
import Rel8.Query ( Query )
import Rel8.Schema.Name ( Selects )
import Rel8.Schema.Table ( TableSchema(..), ppTable )
import Rel8.Statement.Returning ( Returning, decodeReturning, ppReturning )
import Rel8.Statement.Set ( ppSet )
import Rel8.Statement.Using ( ppFrom )
import Rel8.Statement.Where ( ppWhere )

-- text
import qualified Data.Text as Text
import Data.Text.Encoding ( encodeUtf8 )


-- | The constituent parts of an @UPDATE@ statement.
type Update :: Type -> Type
data Update a where
  Update :: Selects names exprs =>
    { ()
target :: TableSchema names
      -- ^ Which table to update.
    , ()
from :: Query from
      -- ^ @FROM@ clause — this can be used to join against other tables,
      -- and its results can be referenced in the @SET@ and @WHERE@ clauses.
    , ()
set :: from -> exprs -> exprs
      -- ^ How to update each selected row.
    , ()
updateWhere :: from -> exprs -> Expr Bool
      -- ^ Which rows to select for update.
    , ()
returning :: Returning names a
      -- ^ What to return from the @UPDATE@ statement.
    }
    -> Update a


ppUpdate :: Update a -> Doc
ppUpdate :: Update a -> Doc
ppUpdate Update {Query from
TableSchema names
Returning names a
from -> exprs -> exprs
from -> exprs -> Expr Bool
returning :: Returning names a
updateWhere :: from -> exprs -> Expr Bool
set :: from -> exprs -> exprs
from :: Query from
target :: TableSchema names
$sel:returning:Update :: ()
$sel:updateWhere:Update :: ()
$sel:set:Update :: ()
$sel:from:Update :: ()
$sel:target:Update :: ()
..} = case Query from -> Maybe (Doc, from)
forall a. Query a -> Maybe (Doc, a)
ppFrom Query from
from of
  Maybe (Doc, from)
Nothing ->
    String -> Doc
text String
"UPDATE" Doc -> Doc -> Doc
<+> TableSchema names -> Doc
forall a. TableSchema a -> Doc
ppTable TableSchema names
target Doc -> Doc -> Doc
$$
    TableSchema names -> (exprs -> exprs) -> Doc
forall names exprs.
Selects names exprs =>
TableSchema names -> (exprs -> exprs) -> Doc
ppSet TableSchema names
target exprs -> exprs
forall a. a -> a
id Doc -> Doc -> Doc
$$
    String -> Doc
text String
"WHERE false"
  Just (Doc
fromDoc, from
i) ->
    String -> Doc
text String
"UPDATE" Doc -> Doc -> Doc
<+> TableSchema names -> Doc
forall a. TableSchema a -> Doc
ppTable TableSchema names
target Doc -> Doc -> Doc
$$
    TableSchema names -> (exprs -> exprs) -> Doc
forall names exprs.
Selects names exprs =>
TableSchema names -> (exprs -> exprs) -> Doc
ppSet TableSchema names
target (from -> exprs -> exprs
set from
i) Doc -> Doc -> Doc
$$
    Doc
fromDoc Doc -> Doc -> Doc
$$
    TableSchema names -> (exprs -> Expr Bool) -> Doc
forall names exprs.
Selects names exprs =>
TableSchema names -> (exprs -> Expr Bool) -> Doc
ppWhere TableSchema names
target (from -> exprs -> Expr Bool
updateWhere from
i) Doc -> Doc -> Doc
$$
    TableSchema names -> Returning names a -> Doc
forall names a. TableSchema names -> Returning names a -> Doc
ppReturning TableSchema names
target Returning names a
returning


-- | Run an @UPDATE@ statement.
update :: Update a -> Hasql.Statement () a
update :: Update a -> Statement () a
update u :: Update a
u@Update {Returning names a
returning :: Returning names a
$sel:returning:Update :: ()
returning} = ByteString -> Params () -> Result a -> Bool -> Statement () a
forall a b.
ByteString -> Params a -> Result b -> Bool -> Statement a b
Hasql.Statement ByteString
bytes Params ()
params Result a
decode Bool
prepare
  where
    bytes :: ByteString
bytes = Text -> ByteString
encodeUtf8 (Text -> ByteString) -> Text -> ByteString
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
sql
    params :: Params ()
params = Params ()
Hasql.noParams
    decode :: Result a
decode = Returning names a -> Result a
forall names a. Returning names a -> Result a
decodeReturning Returning names a
returning
    prepare :: Bool
prepare = Bool
False
    sql :: String
sql = Doc -> String
forall a. Show a => a -> String
show Doc
doc
    doc :: Doc
doc = Update a -> Doc
forall a. Update a -> Doc
ppUpdate Update a
u