-- | Postgres Types Table
--
-- Defines a single combinator to throw an error if the view is not mutable.
module Hasura.Backends.Postgres.Types.Table
  ( mutableView,
  )
where

import Data.Text.Extended
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Base.Error
import Hasura.Prelude
import Hasura.RQL.Types.Table

mutableView ::
  (MonadError QErr m) =>
  QualifiedTable ->
  (ViewInfo -> Bool) ->
  Maybe ViewInfo ->
  Text ->
  m ()
mutableView :: QualifiedTable
-> (ViewInfo -> Bool) -> Maybe ViewInfo -> Text -> m ()
mutableView QualifiedTable
qt ViewInfo -> Bool
f Maybe ViewInfo
mVI Text
operation =
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((ViewInfo -> Bool) -> Maybe ViewInfo -> Bool
isMutable ViewInfo -> Bool
f Maybe ViewInfo
mVI) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
"view " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> QualifiedTable
qt QualifiedTable -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" is not " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
operation