module Hasura.Backends.DataConnector.Plan.MutationPlan
  ( mkMutationPlan,
  )
where

import Control.Monad.Trans.Writer.CPS qualified as CPS
import Data.Aeson qualified as J
import Data.Aeson.Encoding qualified as JE
import Data.Has (Has, modifier)
import Data.HashMap.Strict qualified as HashMap
import Data.Semigroup.Foldable (toNonEmpty)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text.Extended (toTxt)
import Hasura.Backends.DataConnector.API qualified as API
import Hasura.Backends.DataConnector.Adapter.Backend
import Hasura.Backends.DataConnector.Adapter.Types
import Hasura.Backends.DataConnector.Adapter.Types.Mutations
import Hasura.Backends.DataConnector.Plan.Common
import Hasura.Backends.DataConnector.Plan.QueryPlan (reshapeAnnFields, translateAnnFields)
import Hasura.Base.Error (Code (..), QErr, throw400, throw500)
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp (GBoolExp (..))
import Hasura.RQL.IR.Delete
import Hasura.RQL.IR.Insert hiding (Single)
import Hasura.RQL.IR.Returning
import Hasura.RQL.IR.Root
import Hasura.RQL.IR.Select
import Hasura.RQL.IR.Update
import Hasura.RQL.IR.Update.Batch
import Hasura.RQL.IR.Value
import Hasura.RQL.Types.BackendType (BackendType (..))
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.Session (SessionVariables)
import Language.GraphQL.Draft.Syntax qualified as G
import Witch qualified

--------------------------------------------------------------------------------

newtype TableInsertSchemas = TableInsertSchemas
  {TableInsertSchemas -> HashMap TableName TableInsertSchema
unTableInsertSchemas :: HashMap API.TableName TableInsertSchema}
  deriving stock (TableInsertSchemas -> TableInsertSchemas -> Bool
(TableInsertSchemas -> TableInsertSchemas -> Bool)
-> (TableInsertSchemas -> TableInsertSchemas -> Bool)
-> Eq TableInsertSchemas
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TableInsertSchemas -> TableInsertSchemas -> Bool
== :: TableInsertSchemas -> TableInsertSchemas -> Bool
$c/= :: TableInsertSchemas -> TableInsertSchemas -> Bool
/= :: TableInsertSchemas -> TableInsertSchemas -> Bool
Eq, Int -> TableInsertSchemas -> ShowS
[TableInsertSchemas] -> ShowS
TableInsertSchemas -> String
(Int -> TableInsertSchemas -> ShowS)
-> (TableInsertSchemas -> String)
-> ([TableInsertSchemas] -> ShowS)
-> Show TableInsertSchemas
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TableInsertSchemas -> ShowS
showsPrec :: Int -> TableInsertSchemas -> ShowS
$cshow :: TableInsertSchemas -> String
show :: TableInsertSchemas -> String
$cshowList :: [TableInsertSchemas] -> ShowS
showList :: [TableInsertSchemas] -> ShowS
Show)

instance Semigroup TableInsertSchemas where
  (TableInsertSchemas HashMap TableName TableInsertSchema
l) <> :: TableInsertSchemas -> TableInsertSchemas -> TableInsertSchemas
<> (TableInsertSchemas HashMap TableName TableInsertSchema
r) = HashMap TableName TableInsertSchema -> TableInsertSchemas
TableInsertSchemas (HashMap TableName TableInsertSchema -> TableInsertSchemas)
-> HashMap TableName TableInsertSchema -> TableInsertSchemas
forall a b. (a -> b) -> a -> b
$ (TableInsertSchema -> TableInsertSchema -> TableInsertSchema)
-> HashMap TableName TableInsertSchema
-> HashMap TableName TableInsertSchema
-> HashMap TableName TableInsertSchema
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v
HashMap.unionWith TableInsertSchema -> TableInsertSchema -> TableInsertSchema
forall a. Semigroup a => a -> a -> a
(<>) HashMap TableName TableInsertSchema
l HashMap TableName TableInsertSchema
r

instance Monoid TableInsertSchemas where
  mempty :: TableInsertSchemas
mempty = HashMap TableName TableInsertSchema -> TableInsertSchemas
TableInsertSchemas HashMap TableName TableInsertSchema
forall a. Monoid a => a
mempty

data TableInsertSchema = TableInsertSchema
  { TableInsertSchema -> Maybe (NonEmpty ColumnName)
_tisPrimaryKey :: Maybe (NonEmpty API.ColumnName),
    TableInsertSchema -> HashMap FieldName InsertFieldSchema
_tisFields :: HashMap API.FieldName API.InsertFieldSchema
  }
  deriving stock (TableInsertSchema -> TableInsertSchema -> Bool
(TableInsertSchema -> TableInsertSchema -> Bool)
-> (TableInsertSchema -> TableInsertSchema -> Bool)
-> Eq TableInsertSchema
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TableInsertSchema -> TableInsertSchema -> Bool
== :: TableInsertSchema -> TableInsertSchema -> Bool
$c/= :: TableInsertSchema -> TableInsertSchema -> Bool
/= :: TableInsertSchema -> TableInsertSchema -> Bool
Eq, Int -> TableInsertSchema -> ShowS
[TableInsertSchema] -> ShowS
TableInsertSchema -> String
(Int -> TableInsertSchema -> ShowS)
-> (TableInsertSchema -> String)
-> ([TableInsertSchema] -> ShowS)
-> Show TableInsertSchema
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TableInsertSchema -> ShowS
showsPrec :: Int -> TableInsertSchema -> ShowS
$cshow :: TableInsertSchema -> String
show :: TableInsertSchema -> String
$cshowList :: [TableInsertSchema] -> ShowS
showList :: [TableInsertSchema] -> ShowS
Show)

instance Semigroup TableInsertSchema where
  TableInsertSchema
l <> :: TableInsertSchema -> TableInsertSchema -> TableInsertSchema
<> TableInsertSchema
r =
    TableInsertSchema
      { _tisPrimaryKey :: Maybe (NonEmpty ColumnName)
_tisPrimaryKey = TableInsertSchema -> Maybe (NonEmpty ColumnName)
_tisPrimaryKey TableInsertSchema
l,
        _tisFields :: HashMap FieldName InsertFieldSchema
_tisFields = HashMap FieldName InsertFieldSchema
-> HashMap FieldName InsertFieldSchema
-> HashMap FieldName InsertFieldSchema
forall k v.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k v -> HashMap k v
HashMap.union (TableInsertSchema -> HashMap FieldName InsertFieldSchema
_tisFields TableInsertSchema
l) (TableInsertSchema -> HashMap FieldName InsertFieldSchema
_tisFields TableInsertSchema
r)
      }

recordTableInsertSchema ::
  ( Has TableInsertSchemas writerOutput,
    Monoid writerOutput,
    MonadError QErr m
  ) =>
  API.TableName ->
  TableInsertSchema ->
  CPS.WriterT writerOutput m ()
recordTableInsertSchema :: forall writerOutput (m :: * -> *).
(Has TableInsertSchemas writerOutput, Monoid writerOutput,
 MonadError QErr m) =>
TableName -> TableInsertSchema -> WriterT writerOutput m ()
recordTableInsertSchema TableName
tableName TableInsertSchema
tableInsertSchema =
  let newTableSchema :: TableInsertSchemas
newTableSchema = HashMap TableName TableInsertSchema -> TableInsertSchemas
TableInsertSchemas (HashMap TableName TableInsertSchema -> TableInsertSchemas)
-> HashMap TableName TableInsertSchema -> TableInsertSchemas
forall a b. (a -> b) -> a -> b
$ TableName
-> TableInsertSchema -> HashMap TableName TableInsertSchema
forall k v. Hashable k => k -> v -> HashMap k v
HashMap.singleton TableName
tableName TableInsertSchema
tableInsertSchema
   in writerOutput -> WriterT writerOutput m ()
forall w (m :: * -> *). (Monoid w, Monad m) => w -> WriterT w m ()
CPS.tell (writerOutput -> WriterT writerOutput m ())
-> writerOutput -> WriterT writerOutput m ()
forall a b. (a -> b) -> a -> b
$ (TableInsertSchemas -> TableInsertSchemas)
-> writerOutput -> writerOutput
forall a t. Has a t => (a -> a) -> t -> t
modifier (TableInsertSchemas -> TableInsertSchemas -> TableInsertSchemas
forall a b. a -> b -> a
const TableInsertSchemas
newTableSchema) writerOutput
forall a. Monoid a => a
mempty

--------------------------------------------------------------------------------

mkMutationPlan ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  m (Plan API.MutationRequest API.MutationResponse)
mkMutationPlan :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
-> m (Plan MutationRequest MutationResponse)
mkMutationPlan SessionVariables
sessionVariables MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
mutationDB = do
  MutationRequest
request <- SessionVariables
-> MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
-> m MutationRequest
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
-> m MutationRequest
translateMutationDB SessionVariables
sessionVariables MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
mutationDB
  Plan MutationRequest MutationResponse
-> m (Plan MutationRequest MutationResponse)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Plan MutationRequest MutationResponse
 -> m (Plan MutationRequest MutationResponse))
-> Plan MutationRequest MutationResponse
-> m (Plan MutationRequest MutationResponse)
forall a b. (a -> b) -> a -> b
$ MutationRequest
-> (forall (m :: * -> *).
    MonadError QErr m =>
    MutationResponse -> m Encoding)
-> Plan MutationRequest MutationResponse
forall request response.
request
-> (forall (m :: * -> *).
    MonadError QErr m =>
    response -> m Encoding)
-> Plan request response
Plan MutationRequest
request (MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
-> MutationResponse -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
MutationDB 'DataConnector Void v -> MutationResponse -> m Encoding
reshapeResponseToMutationGqlShape MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
mutationDB)

translateMutationDB ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  m API.MutationRequest
translateMutationDB :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> MutationDB 'DataConnector Void (UnpreparedValue 'DataConnector)
-> m MutationRequest
translateMutationDB SessionVariables
sessionVariables = \case
  MDBInsert AnnotatedInsert
  'DataConnector Void (UnpreparedValue 'DataConnector)
insert -> do
    (InsertMutationOperation
insertOperation, (TableRelationships
tableRelationships, TableInsertSchemas
tableInsertSchemas)) <- WriterT
  (TableRelationships, TableInsertSchemas) m InsertMutationOperation
-> m (InsertMutationOperation,
      (TableRelationships, TableInsertSchemas))
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
CPS.runWriterT (WriterT
   (TableRelationships, TableInsertSchemas) m InsertMutationOperation
 -> m (InsertMutationOperation,
       (TableRelationships, TableInsertSchemas)))
-> WriterT
     (TableRelationships, TableInsertSchemas) m InsertMutationOperation
-> m (InsertMutationOperation,
      (TableRelationships, TableInsertSchemas))
forall a b. (a -> b) -> a -> b
$ SessionVariables
-> AnnotatedInsert
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT
     (TableRelationships, TableInsertSchemas) m InsertMutationOperation
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnotatedInsert
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT
     (TableRelationships, TableInsertSchemas) m InsertMutationOperation
translateInsert SessionVariables
sessionVariables AnnotatedInsert
  'DataConnector Void (UnpreparedValue 'DataConnector)
insert
    let apiTableInsertSchema :: [TableInsertSchema]
apiTableInsertSchema =
          TableInsertSchemas -> HashMap TableName TableInsertSchema
unTableInsertSchemas TableInsertSchemas
tableInsertSchemas
            HashMap TableName TableInsertSchema
-> (HashMap TableName TableInsertSchema
    -> [(TableName, TableInsertSchema)])
-> [(TableName, TableInsertSchema)]
forall a b. a -> (a -> b) -> b
& HashMap TableName TableInsertSchema
-> [(TableName, TableInsertSchema)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList
            [(TableName, TableInsertSchema)]
-> ([(TableName, TableInsertSchema)] -> [TableInsertSchema])
-> [TableInsertSchema]
forall a b. a -> (a -> b) -> b
& ((TableName, TableInsertSchema) -> TableInsertSchema)
-> [(TableName, TableInsertSchema)] -> [TableInsertSchema]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(TableName
tableName, TableInsertSchema {Maybe (NonEmpty ColumnName)
HashMap FieldName InsertFieldSchema
_tisPrimaryKey :: TableInsertSchema -> Maybe (NonEmpty ColumnName)
_tisFields :: TableInsertSchema -> HashMap FieldName InsertFieldSchema
_tisPrimaryKey :: Maybe (NonEmpty ColumnName)
_tisFields :: HashMap FieldName InsertFieldSchema
..}) -> TableName
-> Maybe (NonEmpty ColumnName)
-> HashMap FieldName InsertFieldSchema
-> TableInsertSchema
API.TableInsertSchema TableName
tableName Maybe (NonEmpty ColumnName)
_tisPrimaryKey HashMap FieldName InsertFieldSchema
_tisFields)
    let apiTableRelationships :: Set TableRelationships
apiTableRelationships = [TableRelationships] -> Set TableRelationships
forall a. Ord a => [a] -> Set a
Set.fromList ([TableRelationships] -> Set TableRelationships)
-> [TableRelationships] -> Set TableRelationships
forall a b. (a -> b) -> a -> b
$ (TableName
 -> HashMap RelationshipName Relationship -> TableRelationships)
-> (TableName, HashMap RelationshipName Relationship)
-> TableRelationships
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry TableName
-> HashMap RelationshipName Relationship -> TableRelationships
API.TableRelationships ((TableName, HashMap RelationshipName Relationship)
 -> TableRelationships)
-> [(TableName, HashMap RelationshipName Relationship)]
-> [TableRelationships]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Either
   (FunctionName, HashMap RelationshipName Relationship)
   (TableName, HashMap RelationshipName Relationship)]
-> [(TableName, HashMap RelationshipName Relationship)]
forall a b. [Either a b] -> [b]
rights (((TableRelationshipsKey, HashMap RelationshipName Relationship)
 -> Either
      (FunctionName, HashMap RelationshipName Relationship)
      (TableName, HashMap RelationshipName Relationship))
-> [(TableRelationshipsKey, HashMap RelationshipName Relationship)]
-> [Either
      (FunctionName, HashMap RelationshipName Relationship)
      (TableName, HashMap RelationshipName Relationship)]
forall a b. (a -> b) -> [a] -> [b]
map (TableRelationshipsKey, HashMap RelationshipName Relationship)
-> Either
     (FunctionName, HashMap RelationshipName Relationship)
     (TableName, HashMap RelationshipName Relationship)
forall c.
(TableRelationshipsKey, c)
-> Either (FunctionName, c) (TableName, c)
eitherKey (HashMap
  TableRelationshipsKey (HashMap RelationshipName Relationship)
-> [(TableRelationshipsKey, HashMap RelationshipName Relationship)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList (TableRelationships
-> HashMap
     TableRelationshipsKey (HashMap RelationshipName Relationship)
unTableRelationships TableRelationships
tableRelationships)))
    MutationRequest -> m MutationRequest
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      (MutationRequest -> m MutationRequest)
-> MutationRequest -> m MutationRequest
forall a b. (a -> b) -> a -> b
$ API.MutationRequest
        { _mrTableRelationships :: Set TableRelationships
_mrTableRelationships = Set TableRelationships
apiTableRelationships,
          _mrInsertSchema :: Set TableInsertSchema
_mrInsertSchema = [TableInsertSchema] -> Set TableInsertSchema
forall a. Ord a => [a] -> Set a
Set.fromList [TableInsertSchema]
apiTableInsertSchema,
          _mrOperations :: [MutationOperation]
_mrOperations = [InsertMutationOperation -> MutationOperation
API.InsertOperation InsertMutationOperation
insertOperation]
        }
  MDBUpdate AnnotatedUpdateG
  'DataConnector Void (UnpreparedValue 'DataConnector)
update -> do
    ([UpdateMutationOperation]
updateOperations, TableRelationships
tableRelationships) <- WriterT TableRelationships m [UpdateMutationOperation]
-> m ([UpdateMutationOperation], TableRelationships)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
CPS.runWriterT (WriterT TableRelationships m [UpdateMutationOperation]
 -> m ([UpdateMutationOperation], TableRelationships))
-> WriterT TableRelationships m [UpdateMutationOperation]
-> m ([UpdateMutationOperation], TableRelationships)
forall a b. (a -> b) -> a -> b
$ SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m [UpdateMutationOperation]
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m [UpdateMutationOperation]
translateUpdate SessionVariables
sessionVariables AnnotatedUpdateG
  'DataConnector Void (UnpreparedValue 'DataConnector)
update
    let apiTableRelationships :: Set TableRelationships
apiTableRelationships =
          [TableRelationships] -> Set TableRelationships
forall a. Ord a => [a] -> Set a
Set.fromList
            ([TableRelationships] -> Set TableRelationships)
-> [TableRelationships] -> Set TableRelationships
forall a b. (a -> b) -> a -> b
$ (TableName
 -> HashMap RelationshipName Relationship -> TableRelationships)
-> (TableName, HashMap RelationshipName Relationship)
-> TableRelationships
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry TableName
-> HashMap RelationshipName Relationship -> TableRelationships
API.TableRelationships
            ((TableName, HashMap RelationshipName Relationship)
 -> TableRelationships)
-> [(TableName, HashMap RelationshipName Relationship)]
-> [TableRelationships]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Either
   (FunctionName, HashMap RelationshipName Relationship)
   (TableName, HashMap RelationshipName Relationship)]
-> [(TableName, HashMap RelationshipName Relationship)]
forall a b. [Either a b] -> [b]
rights (((TableRelationshipsKey, HashMap RelationshipName Relationship)
 -> Either
      (FunctionName, HashMap RelationshipName Relationship)
      (TableName, HashMap RelationshipName Relationship))
-> [(TableRelationshipsKey, HashMap RelationshipName Relationship)]
-> [Either
      (FunctionName, HashMap RelationshipName Relationship)
      (TableName, HashMap RelationshipName Relationship)]
forall a b. (a -> b) -> [a] -> [b]
map (TableRelationshipsKey, HashMap RelationshipName Relationship)
-> Either
     (FunctionName, HashMap RelationshipName Relationship)
     (TableName, HashMap RelationshipName Relationship)
forall c.
(TableRelationshipsKey, c)
-> Either (FunctionName, c) (TableName, c)
eitherKey (HashMap
  TableRelationshipsKey (HashMap RelationshipName Relationship)
-> [(TableRelationshipsKey, HashMap RelationshipName Relationship)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList (TableRelationships
-> HashMap
     TableRelationshipsKey (HashMap RelationshipName Relationship)
unTableRelationships TableRelationships
tableRelationships)))
    MutationRequest -> m MutationRequest
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      (MutationRequest -> m MutationRequest)
-> MutationRequest -> m MutationRequest
forall a b. (a -> b) -> a -> b
$ API.MutationRequest
        { _mrTableRelationships :: Set TableRelationships
_mrTableRelationships = Set TableRelationships
apiTableRelationships,
          _mrInsertSchema :: Set TableInsertSchema
_mrInsertSchema = Set TableInsertSchema
forall a. Monoid a => a
mempty,
          _mrOperations :: [MutationOperation]
_mrOperations = UpdateMutationOperation -> MutationOperation
API.UpdateOperation (UpdateMutationOperation -> MutationOperation)
-> [UpdateMutationOperation] -> [MutationOperation]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [UpdateMutationOperation]
updateOperations
        }
  MDBDelete AnnDelG 'DataConnector Void (UnpreparedValue 'DataConnector)
delete -> do
    (DeleteMutationOperation
deleteOperation, TableRelationships
tableRelationships) <- WriterT TableRelationships m DeleteMutationOperation
-> m (DeleteMutationOperation, TableRelationships)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
CPS.runWriterT (WriterT TableRelationships m DeleteMutationOperation
 -> m (DeleteMutationOperation, TableRelationships))
-> WriterT TableRelationships m DeleteMutationOperation
-> m (DeleteMutationOperation, TableRelationships)
forall a b. (a -> b) -> a -> b
$ SessionVariables
-> AnnDelG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m DeleteMutationOperation
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnDelG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m DeleteMutationOperation
translateDelete SessionVariables
sessionVariables AnnDelG 'DataConnector Void (UnpreparedValue 'DataConnector)
delete
    let apiTableRelationships :: Set TableRelationships
apiTableRelationships =
          [TableRelationships] -> Set TableRelationships
forall a. Ord a => [a] -> Set a
Set.fromList
            ([TableRelationships] -> Set TableRelationships)
-> [TableRelationships] -> Set TableRelationships
forall a b. (a -> b) -> a -> b
$ (TableName
 -> HashMap RelationshipName Relationship -> TableRelationships)
-> (TableName, HashMap RelationshipName Relationship)
-> TableRelationships
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry TableName
-> HashMap RelationshipName Relationship -> TableRelationships
API.TableRelationships
            ((TableName, HashMap RelationshipName Relationship)
 -> TableRelationships)
-> [(TableName, HashMap RelationshipName Relationship)]
-> [TableRelationships]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Either
   (FunctionName, HashMap RelationshipName Relationship)
   (TableName, HashMap RelationshipName Relationship)]
-> [(TableName, HashMap RelationshipName Relationship)]
forall a b. [Either a b] -> [b]
rights (((TableRelationshipsKey, HashMap RelationshipName Relationship)
 -> Either
      (FunctionName, HashMap RelationshipName Relationship)
      (TableName, HashMap RelationshipName Relationship))
-> [(TableRelationshipsKey, HashMap RelationshipName Relationship)]
-> [Either
      (FunctionName, HashMap RelationshipName Relationship)
      (TableName, HashMap RelationshipName Relationship)]
forall a b. (a -> b) -> [a] -> [b]
map (TableRelationshipsKey, HashMap RelationshipName Relationship)
-> Either
     (FunctionName, HashMap RelationshipName Relationship)
     (TableName, HashMap RelationshipName Relationship)
forall c.
(TableRelationshipsKey, c)
-> Either (FunctionName, c) (TableName, c)
eitherKey (HashMap
  TableRelationshipsKey (HashMap RelationshipName Relationship)
-> [(TableRelationshipsKey, HashMap RelationshipName Relationship)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList (TableRelationships
-> HashMap
     TableRelationshipsKey (HashMap RelationshipName Relationship)
unTableRelationships TableRelationships
tableRelationships)))
    MutationRequest -> m MutationRequest
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      (MutationRequest -> m MutationRequest)
-> MutationRequest -> m MutationRequest
forall a b. (a -> b) -> a -> b
$ API.MutationRequest
        { _mrTableRelationships :: Set TableRelationships
_mrTableRelationships = Set TableRelationships
apiTableRelationships,
          _mrInsertSchema :: Set TableInsertSchema
_mrInsertSchema = Set TableInsertSchema
forall a. Monoid a => a
mempty,
          _mrOperations :: [MutationOperation]
_mrOperations = [DeleteMutationOperation -> MutationOperation
API.DeleteOperation DeleteMutationOperation
deleteOperation]
        }
  MDBFunction JsonAggSelect
_returnsSet AnnSimpleSelectG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_select ->
    Code -> Text -> m MutationRequest
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"translateMutationDB: function mutations not implemented for the Data Connector backend."

eitherKey :: (TableRelationshipsKey, c) -> Either (API.FunctionName, c) (API.TableName, c)
eitherKey :: forall c.
(TableRelationshipsKey, c)
-> Either (FunctionName, c) (TableName, c)
eitherKey (FunctionNameKey FunctionName
f, c
x) = (FunctionName, c) -> Either (FunctionName, c) (TableName, c)
forall a b. a -> Either a b
Left (FunctionName
f, c
x)
eitherKey (TableNameKey TableName
t, c
x) = (TableName, c) -> Either (FunctionName, c) (TableName, c)
forall a b. b -> Either a b
Right (TableName
t, c
x)

translateInsert ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  AnnotatedInsert 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  CPS.WriterT (TableRelationships, TableInsertSchemas) m API.InsertMutationOperation
translateInsert :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnotatedInsert
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT
     (TableRelationships, TableInsertSchemas) m InsertMutationOperation
translateInsert SessionVariables
sessionVariables AnnotatedInsert {_aiData :: forall (b :: BackendType) r v.
AnnotatedInsert b r v -> MultiObjectInsert b v
_aiData = AnnotatedInsertData {[AnnotatedInsertRow
   'DataConnector (UnpreparedValue 'DataConnector)]
[ColumnInfo 'DataConnector]
Maybe (NESeq (Column 'DataConnector))
Maybe (ValidateInput ResolvedWebhook)
(AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector),
 Maybe (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)))
PreSetColsG 'DataConnector (UnpreparedValue 'DataConnector)
TableName 'DataConnector
ExtraTableMetadata 'DataConnector
BackendInsert 'DataConnector (UnpreparedValue 'DataConnector)
_aiInsertObject :: [AnnotatedInsertRow
   'DataConnector (UnpreparedValue 'DataConnector)]
_aiTableName :: TableName 'DataConnector
_aiCheckCondition :: (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector),
 Maybe (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)))
_aiTableColumns :: [ColumnInfo 'DataConnector]
_aiPrimaryKey :: Maybe (NESeq (Column 'DataConnector))
_aiExtraTableMetadata :: ExtraTableMetadata 'DataConnector
_aiPresetValues :: PreSetColsG 'DataConnector (UnpreparedValue 'DataConnector)
_aiBackendInsert :: BackendInsert 'DataConnector (UnpreparedValue 'DataConnector)
_aiValidateInput :: Maybe (ValidateInput ResolvedWebhook)
_aiInsertObject :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> f (AnnotatedInsertRow b v)
_aiTableName :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> TableName b
_aiCheckCondition :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v
-> (AnnBoolExp b v, Maybe (AnnBoolExp b v))
_aiTableColumns :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> [ColumnInfo b]
_aiPrimaryKey :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> Maybe (NESeq (Column b))
_aiExtraTableMetadata :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> ExtraTableMetadata b
_aiPresetValues :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> PreSetColsG b v
_aiBackendInsert :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> BackendInsert b v
_aiValidateInput :: forall (b :: BackendType) (f :: * -> *) v.
AnnotatedInsertData b f v -> Maybe (ValidateInput ResolvedWebhook)
..}, Bool
Maybe NamingCase
Text
MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_aiFieldName :: Text
_aiIsSingle :: Bool
_aiOutput :: MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_aiNamingConvention :: Maybe NamingCase
_aiFieldName :: forall (b :: BackendType) r v. AnnotatedInsert b r v -> Text
_aiIsSingle :: forall (b :: BackendType) r v. AnnotatedInsert b r v -> Bool
_aiOutput :: forall (b :: BackendType) r v.
AnnotatedInsert b r v -> MutationOutputG b r v
_aiNamingConvention :: forall (b :: BackendType) r v.
AnnotatedInsert b r v -> Maybe NamingCase
..} = do
  TableName
-> [ColumnInfo 'DataConnector]
-> Maybe (NESeq ColumnName)
-> ExtraTableMetadata
-> WriterT (TableRelationships, TableInsertSchemas) m ()
forall writerOutput (m :: * -> *).
(Has TableInsertSchemas writerOutput, Monoid writerOutput,
 MonadError QErr m) =>
TableName
-> [ColumnInfo 'DataConnector]
-> Maybe (NESeq ColumnName)
-> ExtraTableMetadata
-> WriterT writerOutput m ()
captureTableInsertSchema TableName
tableName [ColumnInfo 'DataConnector]
_aiTableColumns Maybe (NESeq (Column 'DataConnector))
Maybe (NESeq ColumnName)
_aiPrimaryKey ExtraTableMetadata 'DataConnector
ExtraTableMetadata
_aiExtraTableMetadata
  [RowObject]
rows <- m [RowObject]
-> WriterT (TableRelationships, TableInsertSchemas) m [RowObject]
forall (m :: * -> *) a.
Monad m =>
m a -> WriterT (TableRelationships, TableInsertSchemas) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [RowObject]
 -> WriterT (TableRelationships, TableInsertSchemas) m [RowObject])
-> m [RowObject]
-> WriterT (TableRelationships, TableInsertSchemas) m [RowObject]
forall a b. (a -> b) -> a -> b
$ (AnnotatedInsertRow 'DataConnector (UnpreparedValue 'DataConnector)
 -> m RowObject)
-> [AnnotatedInsertRow
      'DataConnector (UnpreparedValue 'DataConnector)]
-> m [RowObject]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (SessionVariables
-> TableName
-> [ColumnInfo 'DataConnector]
-> HashMap ColumnName (UnpreparedValue 'DataConnector)
-> AnnotatedInsertRow
     'DataConnector (UnpreparedValue 'DataConnector)
-> m RowObject
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> [ColumnInfo 'DataConnector]
-> HashMap ColumnName (UnpreparedValue 'DataConnector)
-> AnnotatedInsertRow
     'DataConnector (UnpreparedValue 'DataConnector)
-> m RowObject
translateInsertRow SessionVariables
sessionVariables TableName
tableName [ColumnInfo 'DataConnector]
_aiTableColumns PreSetColsG 'DataConnector (UnpreparedValue 'DataConnector)
HashMap ColumnName (UnpreparedValue 'DataConnector)
_aiPresetValues) [AnnotatedInsertRow
   'DataConnector (UnpreparedValue 'DataConnector)]
_aiInsertObject
  Maybe Expression
postInsertCheck <- SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT
     (TableRelationships, TableInsertSchemas) m (Maybe Expression)
forall writerOutput (m :: * -> *) r.
(Has TableRelationships writerOutput, Monoid writerOutput,
 MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (Maybe Expression)
translateBoolExpToExpression SessionVariables
sessionVariables (TableName -> TableRelationshipsKey
TableNameKey TableName
tableName) AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
insertCheckCondition
  HashMap FieldName Field
returningFields <- SessionVariables
-> TableName
-> MutationOutputG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT
     (TableRelationships, TableInsertSchemas)
     m
     (HashMap FieldName Field)
forall (m :: * -> *) writerOutput r.
(MonadError QErr m, Has TableRelationships writerOutput,
 Monoid writerOutput, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> MutationOutputG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateMutationOutputToReturningFields SessionVariables
sessionVariables TableName
tableName MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_aiOutput
  InsertMutationOperation
-> WriterT
     (TableRelationships, TableInsertSchemas) m InsertMutationOperation
forall a. a -> WriterT (TableRelationships, TableInsertSchemas) m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (InsertMutationOperation
 -> WriterT
      (TableRelationships, TableInsertSchemas) m InsertMutationOperation)
-> InsertMutationOperation
-> WriterT
     (TableRelationships, TableInsertSchemas) m InsertMutationOperation
forall a b. (a -> b) -> a -> b
$ API.InsertMutationOperation
      { _imoTable :: TableName
API._imoTable = TableName
tableName,
        _imoRows :: [RowObject]
API._imoRows = [RowObject]
rows,
        _imoPostInsertCheck :: Maybe Expression
API._imoPostInsertCheck = Maybe Expression
postInsertCheck,
        _imoReturningFields :: HashMap FieldName Field
API._imoReturningFields = (FieldName -> FieldName)
-> HashMap FieldName Field -> HashMap FieldName Field
forall k2 k1 v.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> HashMap k1 v -> HashMap k2 v
HashMap.mapKeys (Text -> FieldName
API.FieldName (Text -> FieldName)
-> (FieldName -> Text) -> FieldName -> FieldName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldName -> Text
getFieldNameTxt) HashMap FieldName Field
returningFields
      }
  where
    tableName :: TableName
tableName = TableName -> TableName
forall source target. From source target => source -> target
Witch.from TableName 'DataConnector
TableName
_aiTableName
    -- Update check condition must be used once upserts are supported
    (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
insertCheckCondition, Maybe (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector))
_updateCheckCondition) = (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector),
 Maybe (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)))
_aiCheckCondition

captureTableInsertSchema ::
  ( Has TableInsertSchemas writerOutput,
    Monoid writerOutput,
    MonadError QErr m
  ) =>
  API.TableName ->
  [ColumnInfo 'DataConnector] ->
  Maybe (NESeq ColumnName) ->
  ExtraTableMetadata ->
  CPS.WriterT writerOutput m ()
captureTableInsertSchema :: forall writerOutput (m :: * -> *).
(Has TableInsertSchemas writerOutput, Monoid writerOutput,
 MonadError QErr m) =>
TableName
-> [ColumnInfo 'DataConnector]
-> Maybe (NESeq ColumnName)
-> ExtraTableMetadata
-> WriterT writerOutput m ()
captureTableInsertSchema TableName
tableName [ColumnInfo 'DataConnector]
tableColumns Maybe (NESeq ColumnName)
primaryKey ExtraTableMetadata {HashMap ColumnName ExtraColumnMetadata
TableType
_etmTableType :: TableType
_etmExtraColumnMetadata :: HashMap ColumnName ExtraColumnMetadata
_etmTableType :: ExtraTableMetadata -> TableType
_etmExtraColumnMetadata :: ExtraTableMetadata -> HashMap ColumnName ExtraColumnMetadata
..} = do
  let fieldSchemas :: HashMap FieldName InsertFieldSchema
fieldSchemas =
        [ColumnInfo 'DataConnector]
tableColumns
          [ColumnInfo 'DataConnector]
-> ([ColumnInfo 'DataConnector]
    -> [(FieldName, InsertFieldSchema)])
-> [(FieldName, InsertFieldSchema)]
forall a b. a -> (a -> b) -> b
& (ColumnInfo 'DataConnector -> (FieldName, InsertFieldSchema))
-> [ColumnInfo 'DataConnector] -> [(FieldName, InsertFieldSchema)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
            ( \ColumnInfo {Bool
Int
Maybe Description
Name
Column 'DataConnector
ColumnType 'DataConnector
ColumnMutability
ciColumn :: Column 'DataConnector
ciName :: Name
ciPosition :: Int
ciType :: ColumnType 'DataConnector
ciIsNullable :: Bool
ciDescription :: Maybe Description
ciMutability :: ColumnMutability
ciColumn :: forall (b :: BackendType). ColumnInfo b -> Column b
ciName :: forall (b :: BackendType). ColumnInfo b -> Name
ciPosition :: forall (b :: BackendType). ColumnInfo b -> Int
ciType :: forall (b :: BackendType). ColumnInfo b -> ColumnType b
ciIsNullable :: forall (b :: BackendType). ColumnInfo b -> Bool
ciDescription :: forall (b :: BackendType). ColumnInfo b -> Maybe Description
ciMutability :: forall (b :: BackendType). ColumnInfo b -> ColumnMutability
..} ->
                let extraColumnMetadata :: Maybe ExtraColumnMetadata
extraColumnMetadata = ColumnName
-> HashMap ColumnName ExtraColumnMetadata
-> Maybe ExtraColumnMetadata
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Column 'DataConnector
ColumnName
ciColumn HashMap ColumnName ExtraColumnMetadata
_etmExtraColumnMetadata
                    scalarType :: ScalarType
scalarType = ColumnType 'DataConnector -> ScalarType
columnTypeToScalarType ColumnType 'DataConnector
ciType
                    valueGenerated :: Maybe ColumnValueGenerationStrategy
valueGenerated = Maybe ExtraColumnMetadata
extraColumnMetadata Maybe ExtraColumnMetadata
-> (ExtraColumnMetadata -> Maybe ColumnValueGenerationStrategy)
-> Maybe ColumnValueGenerationStrategy
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ExtraColumnMetadata -> Maybe ColumnValueGenerationStrategy
_ecmValueGenerated
                    fieldName :: FieldName
fieldName = Text -> FieldName
API.FieldName (Text -> FieldName) -> Text -> FieldName
forall a b. (a -> b) -> a -> b
$ Name -> Text
G.unName Name
ciName
                    columnInsertSchema :: InsertFieldSchema
columnInsertSchema = ColumnInsertSchema -> InsertFieldSchema
API.ColumnInsert (ColumnInsertSchema -> InsertFieldSchema)
-> ColumnInsertSchema -> InsertFieldSchema
forall a b. (a -> b) -> a -> b
$ ColumnName
-> ColumnType
-> Bool
-> Maybe ColumnValueGenerationStrategy
-> ColumnInsertSchema
API.ColumnInsertSchema (ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from Column 'DataConnector
ColumnName
ciColumn) (ScalarType -> ColumnType
API.ColumnTypeScalar (ScalarType -> ColumnType) -> ScalarType -> ColumnType
forall a b. (a -> b) -> a -> b
$ ScalarType -> ScalarType
forall source target. From source target => source -> target
Witch.from ScalarType
scalarType) Bool
ciIsNullable Maybe ColumnValueGenerationStrategy
valueGenerated
                 in (FieldName
fieldName, InsertFieldSchema
columnInsertSchema)
            )
          [(FieldName, InsertFieldSchema)]
-> ([(FieldName, InsertFieldSchema)]
    -> HashMap FieldName InsertFieldSchema)
-> HashMap FieldName InsertFieldSchema
forall a b. a -> (a -> b) -> b
& [(FieldName, InsertFieldSchema)]
-> HashMap FieldName InsertFieldSchema
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  let primaryKey' :: Maybe (NonEmpty ColumnName)
primaryKey' = (ColumnName -> ColumnName)
-> NonEmpty ColumnName -> NonEmpty ColumnName
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from (NonEmpty ColumnName -> NonEmpty ColumnName)
-> (NESeq ColumnName -> NonEmpty ColumnName)
-> NESeq ColumnName
-> NonEmpty ColumnName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NESeq ColumnName -> NonEmpty ColumnName
forall a. NESeq a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty (NESeq ColumnName -> NonEmpty ColumnName)
-> Maybe (NESeq ColumnName) -> Maybe (NonEmpty ColumnName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NESeq ColumnName)
primaryKey
  TableName -> TableInsertSchema -> WriterT writerOutput m ()
forall writerOutput (m :: * -> *).
(Has TableInsertSchemas writerOutput, Monoid writerOutput,
 MonadError QErr m) =>
TableName -> TableInsertSchema -> WriterT writerOutput m ()
recordTableInsertSchema TableName
tableName (TableInsertSchema -> WriterT writerOutput m ())
-> TableInsertSchema -> WriterT writerOutput m ()
forall a b. (a -> b) -> a -> b
$ Maybe (NonEmpty ColumnName)
-> HashMap FieldName InsertFieldSchema -> TableInsertSchema
TableInsertSchema Maybe (NonEmpty ColumnName)
primaryKey' HashMap FieldName InsertFieldSchema
fieldSchemas

translateInsertRow ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  API.TableName ->
  [ColumnInfo 'DataConnector] ->
  HashMap ColumnName (UnpreparedValue 'DataConnector) ->
  AnnotatedInsertRow 'DataConnector (UnpreparedValue 'DataConnector) ->
  m API.RowObject
translateInsertRow :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> [ColumnInfo 'DataConnector]
-> HashMap ColumnName (UnpreparedValue 'DataConnector)
-> AnnotatedInsertRow
     'DataConnector (UnpreparedValue 'DataConnector)
-> m RowObject
translateInsertRow SessionVariables
sessionVariables TableName
tableName [ColumnInfo 'DataConnector]
tableColumns HashMap ColumnName (UnpreparedValue 'DataConnector)
defaultColumnValues AnnotatedInsertRow 'DataConnector (UnpreparedValue 'DataConnector)
insertRow = do
  [(FieldName, Value)]
columnSchemasAndValues <- [(ColumnName, UnpreparedValue 'DataConnector)]
-> ((ColumnName, UnpreparedValue 'DataConnector)
    -> m (FieldName, Value))
-> m [(FieldName, Value)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (HashMap ColumnName (UnpreparedValue 'DataConnector)
-> [(ColumnName, UnpreparedValue 'DataConnector)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap ColumnName (UnpreparedValue 'DataConnector)
columnUnpreparedValues) (((ColumnName, UnpreparedValue 'DataConnector)
  -> m (FieldName, Value))
 -> m [(FieldName, Value)])
-> ((ColumnName, UnpreparedValue 'DataConnector)
    -> m (FieldName, Value))
-> m [(FieldName, Value)]
forall a b. (a -> b) -> a -> b
$ \(ColumnName
columnName, UnpreparedValue 'DataConnector
columnValue) -> do
    FieldName
fieldName <-
      case (ColumnInfo 'DataConnector -> Bool)
-> [ColumnInfo 'DataConnector] -> Maybe (ColumnInfo 'DataConnector)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\ColumnInfo {Bool
Int
Maybe Description
Name
Column 'DataConnector
ColumnType 'DataConnector
ColumnMutability
ciColumn :: forall (b :: BackendType). ColumnInfo b -> Column b
ciName :: forall (b :: BackendType). ColumnInfo b -> Name
ciPosition :: forall (b :: BackendType). ColumnInfo b -> Int
ciType :: forall (b :: BackendType). ColumnInfo b -> ColumnType b
ciIsNullable :: forall (b :: BackendType). ColumnInfo b -> Bool
ciDescription :: forall (b :: BackendType). ColumnInfo b -> Maybe Description
ciMutability :: forall (b :: BackendType). ColumnInfo b -> ColumnMutability
ciColumn :: Column 'DataConnector
ciName :: Name
ciPosition :: Int
ciType :: ColumnType 'DataConnector
ciIsNullable :: Bool
ciDescription :: Maybe Description
ciMutability :: ColumnMutability
..} -> Column 'DataConnector
ColumnName
ciColumn ColumnName -> ColumnName -> Bool
forall a. Eq a => a -> a -> Bool
== ColumnName
columnName) [ColumnInfo 'DataConnector]
tableColumns of
        Just ColumnInfo {Bool
Int
Maybe Description
Name
Column 'DataConnector
ColumnType 'DataConnector
ColumnMutability
ciColumn :: forall (b :: BackendType). ColumnInfo b -> Column b
ciName :: forall (b :: BackendType). ColumnInfo b -> Name
ciPosition :: forall (b :: BackendType). ColumnInfo b -> Int
ciType :: forall (b :: BackendType). ColumnInfo b -> ColumnType b
ciIsNullable :: forall (b :: BackendType). ColumnInfo b -> Bool
ciDescription :: forall (b :: BackendType). ColumnInfo b -> Maybe Description
ciMutability :: forall (b :: BackendType). ColumnInfo b -> ColumnMutability
ciColumn :: Column 'DataConnector
ciName :: Name
ciPosition :: Int
ciType :: ColumnType 'DataConnector
ciIsNullable :: Bool
ciDescription :: Maybe Description
ciMutability :: ColumnMutability
..} -> FieldName -> m FieldName
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FieldName -> m FieldName)
-> (Text -> FieldName) -> Text -> m FieldName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> FieldName
API.FieldName (Text -> m FieldName) -> Text -> m FieldName
forall a b. (a -> b) -> a -> b
$ Name -> Text
G.unName Name
ciName
        Maybe (ColumnInfo 'DataConnector)
Nothing -> Text -> m FieldName
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 (Text -> m FieldName) -> Text -> m FieldName
forall a b. (a -> b) -> a -> b
$ Text
"Can't find column " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ColumnName -> Text
forall a. ToTxt a => a -> Text
toTxt ColumnName
columnName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" in table schema for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TableName -> Text
API.tableNameToText TableName
tableName
    Literal
preparedLiteral <- SessionVariables -> UnpreparedValue 'DataConnector -> m Literal
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables -> UnpreparedValue 'DataConnector -> m Literal
prepareLiteral SessionVariables
sessionVariables UnpreparedValue 'DataConnector
columnValue

    Value
value <-
      case Literal
preparedLiteral of
        ValueLiteral ScalarType
_scalarType Value
value -> Value -> m Value
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
value
        ArrayLiteral ScalarType
_scalarType [Value]
_values -> Code -> Text -> m Value
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"translateInsertRow: Array literals are not supported as column insert values"

    (FieldName, Value) -> m (FieldName, Value)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FieldName
fieldName, Value
value)

  let rowObject :: RowObject
rowObject =
        [(FieldName, Value)]
columnSchemasAndValues
          [(FieldName, Value)]
-> ([(FieldName, Value)] -> [(FieldName, InsertFieldValue)])
-> [(FieldName, InsertFieldValue)]
forall a b. a -> (a -> b) -> b
& ((FieldName, Value) -> (FieldName, InsertFieldValue))
-> [(FieldName, Value)] -> [(FieldName, InsertFieldValue)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Value -> InsertFieldValue)
-> (FieldName, Value) -> (FieldName, InsertFieldValue)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second Value -> InsertFieldValue
API.mkColumnInsertFieldValue)
          [(FieldName, InsertFieldValue)]
-> ([(FieldName, InsertFieldValue)]
    -> HashMap FieldName InsertFieldValue)
-> HashMap FieldName InsertFieldValue
forall a b. a -> (a -> b) -> b
& [(FieldName, InsertFieldValue)]
-> HashMap FieldName InsertFieldValue
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
          HashMap FieldName InsertFieldValue
-> (HashMap FieldName InsertFieldValue -> RowObject) -> RowObject
forall a b. a -> (a -> b) -> b
& HashMap FieldName InsertFieldValue -> RowObject
API.RowObject

  RowObject -> m RowObject
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RowObject
rowObject
  where
    columnUnpreparedValues :: HashMap ColumnName (UnpreparedValue 'DataConnector)
    columnUnpreparedValues :: HashMap ColumnName (UnpreparedValue 'DataConnector)
columnUnpreparedValues = HashMap ColumnName (UnpreparedValue 'DataConnector)
-> HashMap ColumnName (UnpreparedValue 'DataConnector)
-> HashMap ColumnName (UnpreparedValue 'DataConnector)
forall k v.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k v -> HashMap k v
HashMap.union HashMap ColumnName (UnpreparedValue 'DataConnector)
rowColumnValues HashMap ColumnName (UnpreparedValue 'DataConnector)
defaultColumnValues

    rowColumnValues :: HashMap ColumnName (UnpreparedValue 'DataConnector)
    rowColumnValues :: HashMap ColumnName (UnpreparedValue 'DataConnector)
rowColumnValues =
      AnnotatedInsertRow 'DataConnector (UnpreparedValue 'DataConnector)
insertRow
        AnnotatedInsertRow 'DataConnector (UnpreparedValue 'DataConnector)
-> (AnnotatedInsertRow
      'DataConnector (UnpreparedValue 'DataConnector)
    -> [(ColumnName, UnpreparedValue 'DataConnector)])
-> [(ColumnName, UnpreparedValue 'DataConnector)]
forall a b. a -> (a -> b) -> b
& (AnnotatedInsertField
   'DataConnector (UnpreparedValue 'DataConnector)
 -> (ColumnName, UnpreparedValue 'DataConnector))
-> AnnotatedInsertRow
     'DataConnector (UnpreparedValue 'DataConnector)
-> [(ColumnName, UnpreparedValue 'DataConnector)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(AIColumn (Column 'DataConnector, UnpreparedValue 'DataConnector)
columnNameAndValue) -> (Column 'DataConnector, UnpreparedValue 'DataConnector)
(ColumnName, UnpreparedValue 'DataConnector)
columnNameAndValue)
        [(ColumnName, UnpreparedValue 'DataConnector)]
-> ([(ColumnName, UnpreparedValue 'DataConnector)]
    -> HashMap ColumnName (UnpreparedValue 'DataConnector))
-> HashMap ColumnName (UnpreparedValue 'DataConnector)
forall a b. a -> (a -> b) -> b
& [(ColumnName, UnpreparedValue 'DataConnector)]
-> HashMap ColumnName (UnpreparedValue 'DataConnector)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList

translateUpdate ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  AnnotatedUpdateG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  CPS.WriterT TableRelationships m [API.UpdateMutationOperation]
translateUpdate :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m [UpdateMutationOperation]
translateUpdate SessionVariables
sessionVariables annUpdate :: AnnotatedUpdateG
  'DataConnector Void (UnpreparedValue 'DataConnector)
annUpdate@AnnotatedUpdateG {[ColumnInfo 'DataConnector]
Maybe NamingCase
Maybe (ValidateInput ResolvedWebhook)
TableName 'DataConnector
UpdateVariant 'DataConnector (UnpreparedValue 'DataConnector)
AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_auTable :: TableName 'DataConnector
_auUpdatePermissions :: AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_auCheck :: AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_auUpdateVariant :: UpdateVariant 'DataConnector (UnpreparedValue 'DataConnector)
_auOutput :: MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_auAllCols :: [ColumnInfo 'DataConnector]
_auNamingConvention :: Maybe NamingCase
_auValidateInput :: Maybe (ValidateInput ResolvedWebhook)
_auTable :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> TableName b
_auUpdatePermissions :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> AnnBoolExp b v
_auCheck :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> AnnBoolExp b v
_auUpdateVariant :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> UpdateVariant b v
_auOutput :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> MutationOutputG b r v
_auAllCols :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> [ColumnInfo b]
_auNamingConvention :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> Maybe NamingCase
_auValidateInput :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> Maybe (ValidateInput ResolvedWebhook)
..} = do
  case UpdateVariant 'DataConnector (UnpreparedValue 'DataConnector)
_auUpdateVariant of
    SingleBatch UpdateBatch
  'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
batch -> (UpdateMutationOperation
-> [UpdateMutationOperation] -> [UpdateMutationOperation]
forall a. a -> [a] -> [a]
: []) (UpdateMutationOperation -> [UpdateMutationOperation])
-> WriterT TableRelationships m UpdateMutationOperation
-> WriterT TableRelationships m [UpdateMutationOperation]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> UpdateBatch
     'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m UpdateMutationOperation
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> UpdateBatch
     'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m UpdateMutationOperation
translateUpdateBatch SessionVariables
sessionVariables AnnotatedUpdateG
  'DataConnector Void (UnpreparedValue 'DataConnector)
annUpdate UpdateBatch
  'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
batch
    MultipleBatches [UpdateBatch
   'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)]
batches -> (UpdateBatch
   'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
 -> WriterT TableRelationships m UpdateMutationOperation)
-> [UpdateBatch
      'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)]
-> WriterT TableRelationships m [UpdateMutationOperation]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> UpdateBatch
     'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m UpdateMutationOperation
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> UpdateBatch
     'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m UpdateMutationOperation
translateUpdateBatch SessionVariables
sessionVariables AnnotatedUpdateG
  'DataConnector Void (UnpreparedValue 'DataConnector)
annUpdate) [UpdateBatch
   'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)]
batches

translateUpdateBatch ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  AnnotatedUpdateG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  UpdateBatch 'DataConnector UpdateOperator (UnpreparedValue 'DataConnector) ->
  CPS.WriterT TableRelationships m API.UpdateMutationOperation
translateUpdateBatch :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnotatedUpdateG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> UpdateBatch
     'DataConnector UpdateOperator (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m UpdateMutationOperation
translateUpdateBatch SessionVariables
sessionVariables AnnotatedUpdateG {[ColumnInfo 'DataConnector]
Maybe NamingCase
Maybe (ValidateInput ResolvedWebhook)
TableName 'DataConnector
UpdateVariant 'DataConnector (UnpreparedValue 'DataConnector)
AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_auTable :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> TableName b
_auUpdatePermissions :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> AnnBoolExp b v
_auCheck :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> AnnBoolExp b v
_auUpdateVariant :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> UpdateVariant b v
_auOutput :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> MutationOutputG b r v
_auAllCols :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> [ColumnInfo b]
_auNamingConvention :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> Maybe NamingCase
_auValidateInput :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> Maybe (ValidateInput ResolvedWebhook)
_auTable :: TableName 'DataConnector
_auUpdatePermissions :: AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_auCheck :: AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_auUpdateVariant :: UpdateVariant 'DataConnector (UnpreparedValue 'DataConnector)
_auOutput :: MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_auAllCols :: [ColumnInfo 'DataConnector]
_auNamingConvention :: Maybe NamingCase
_auValidateInput :: Maybe (ValidateInput ResolvedWebhook)
..} UpdateBatch {HashMap
  (Column 'DataConnector)
  (UpdateOperator (UnpreparedValue 'DataConnector))
AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_ubOperations :: HashMap
  (Column 'DataConnector)
  (UpdateOperator (UnpreparedValue 'DataConnector))
_ubWhere :: AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_ubOperations :: forall (b :: BackendType) (updateOperators :: * -> *) v.
UpdateBatch b updateOperators v
-> HashMap (Column b) (updateOperators v)
_ubWhere :: forall (b :: BackendType) (updateOperators :: * -> *) v.
UpdateBatch b updateOperators v -> AnnBoolExp b v
..} = do
  Set RowUpdate
updates <- m (Set RowUpdate) -> WriterT TableRelationships m (Set RowUpdate)
forall (m :: * -> *) a.
Monad m =>
m a -> WriterT TableRelationships m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Set RowUpdate) -> WriterT TableRelationships m (Set RowUpdate))
-> m (Set RowUpdate)
-> WriterT TableRelationships m (Set RowUpdate)
forall a b. (a -> b) -> a -> b
$ SessionVariables
-> HashMap
     ColumnName (UpdateOperator (UnpreparedValue 'DataConnector))
-> m (Set RowUpdate)
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> HashMap
     ColumnName (UpdateOperator (UnpreparedValue 'DataConnector))
-> m (Set RowUpdate)
translateUpdateOperations SessionVariables
sessionVariables HashMap
  (Column 'DataConnector)
  (UpdateOperator (UnpreparedValue 'DataConnector))
HashMap
  ColumnName (UpdateOperator (UnpreparedValue 'DataConnector))
_ubOperations
  Maybe Expression
whereExp <- SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m (Maybe Expression)
forall writerOutput (m :: * -> *) r.
(Has TableRelationships writerOutput, Monoid writerOutput,
 MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (Maybe Expression)
translateBoolExpToExpression SessionVariables
sessionVariables (TableName -> TableRelationshipsKey
TableNameKey TableName
tableName) ([AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)]
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
forall (backend :: BackendType) field.
[GBoolExp backend field] -> GBoolExp backend field
BoolAnd [AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_auUpdatePermissions, AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_ubWhere])
  Maybe Expression
postUpdateCheck <- SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m (Maybe Expression)
forall writerOutput (m :: * -> *) r.
(Has TableRelationships writerOutput, Monoid writerOutput,
 MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (Maybe Expression)
translateBoolExpToExpression SessionVariables
sessionVariables (TableName -> TableRelationshipsKey
TableNameKey TableName
tableName) AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
_auCheck
  HashMap FieldName Field
returningFields <- SessionVariables
-> TableName
-> MutationOutputG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m (HashMap FieldName Field)
forall (m :: * -> *) writerOutput r.
(MonadError QErr m, Has TableRelationships writerOutput,
 Monoid writerOutput, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> MutationOutputG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateMutationOutputToReturningFields SessionVariables
sessionVariables TableName
tableName MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_auOutput

  UpdateMutationOperation
-> WriterT TableRelationships m UpdateMutationOperation
forall a. a -> WriterT TableRelationships m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (UpdateMutationOperation
 -> WriterT TableRelationships m UpdateMutationOperation)
-> UpdateMutationOperation
-> WriterT TableRelationships m UpdateMutationOperation
forall a b. (a -> b) -> a -> b
$ API.UpdateMutationOperation
      { _umoTable :: TableName
API._umoTable = TableName
tableName,
        _umoWhere :: Maybe Expression
API._umoWhere = Maybe Expression
whereExp,
        _umoUpdates :: Set RowUpdate
API._umoUpdates = Set RowUpdate
updates,
        _umoPostUpdateCheck :: Maybe Expression
API._umoPostUpdateCheck = Maybe Expression
postUpdateCheck,
        _umoReturningFields :: HashMap FieldName Field
API._umoReturningFields = (FieldName -> FieldName)
-> HashMap FieldName Field -> HashMap FieldName Field
forall k2 k1 v.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> HashMap k1 v -> HashMap k2 v
HashMap.mapKeys (Text -> FieldName
API.FieldName (Text -> FieldName)
-> (FieldName -> Text) -> FieldName -> FieldName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldName -> Text
getFieldNameTxt) HashMap FieldName Field
returningFields
      }
  where
    TableName
tableName :: API.TableName = TableName -> TableName
forall source target. From source target => source -> target
Witch.from TableName 'DataConnector
TableName
_auTable

translateUpdateOperations ::
  forall m r.
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  HashMap ColumnName (UpdateOperator (UnpreparedValue 'DataConnector)) ->
  m (Set API.RowUpdate)
translateUpdateOperations :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> HashMap
     ColumnName (UpdateOperator (UnpreparedValue 'DataConnector))
-> m (Set RowUpdate)
translateUpdateOperations SessionVariables
sessionVariables HashMap
  ColumnName (UpdateOperator (UnpreparedValue 'DataConnector))
columnUpdates =
  ([RowUpdate] -> Set RowUpdate)
-> m [RowUpdate] -> m (Set RowUpdate)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [RowUpdate] -> Set RowUpdate
forall a. Ord a => [a] -> Set a
Set.fromList (m [RowUpdate] -> m (Set RowUpdate))
-> (((ColumnName, UpdateOperator (UnpreparedValue 'DataConnector))
     -> m RowUpdate)
    -> m [RowUpdate])
-> ((ColumnName, UpdateOperator (UnpreparedValue 'DataConnector))
    -> m RowUpdate)
-> m (Set RowUpdate)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(ColumnName, UpdateOperator (UnpreparedValue 'DataConnector))]
-> ((ColumnName, UpdateOperator (UnpreparedValue 'DataConnector))
    -> m RowUpdate)
-> m [RowUpdate]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (HashMap
  ColumnName (UpdateOperator (UnpreparedValue 'DataConnector))
-> [(ColumnName, UpdateOperator (UnpreparedValue 'DataConnector))]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap
  ColumnName (UpdateOperator (UnpreparedValue 'DataConnector))
columnUpdates) (((ColumnName, UpdateOperator (UnpreparedValue 'DataConnector))
  -> m RowUpdate)
 -> m (Set RowUpdate))
-> ((ColumnName, UpdateOperator (UnpreparedValue 'DataConnector))
    -> m RowUpdate)
-> m (Set RowUpdate)
forall a b. (a -> b) -> a -> b
$ \(ColumnName
columnName, UpdateOperator (UnpreparedValue 'DataConnector)
updateOperator) -> do
    let (RowColumnOperatorValue -> RowUpdate
mkRowUpdate, UnpreparedValue 'DataConnector
value) =
          case UpdateOperator (UnpreparedValue 'DataConnector)
updateOperator of
            UpdateSet UnpreparedValue 'DataConnector
value' -> (RowColumnOperatorValue -> RowUpdate
API.SetColumn, UnpreparedValue 'DataConnector
value')
            UpdateCustomOperator UpdateColumnOperatorName
operatorName UnpreparedValue 'DataConnector
value' -> (UpdateColumnOperatorName -> RowColumnOperatorValue -> RowUpdate
API.CustomUpdateColumnOperator UpdateColumnOperatorName
operatorName, UnpreparedValue 'DataConnector
value')
    (ScalarType
scalarType, Value
literalValue) <- UnpreparedValue 'DataConnector -> m (ScalarType, Value)
prepareAndExtractLiteralValue UnpreparedValue 'DataConnector
value
    let operatorValue :: RowColumnOperatorValue
operatorValue = ColumnName -> Value -> ScalarType -> RowColumnOperatorValue
API.RowColumnOperatorValue (ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from ColumnName
columnName) Value
literalValue (ScalarType -> ScalarType
forall source target. From source target => source -> target
Witch.from ScalarType
scalarType)
    RowUpdate -> m RowUpdate
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RowUpdate -> m RowUpdate) -> RowUpdate -> m RowUpdate
forall a b. (a -> b) -> a -> b
$ RowColumnOperatorValue -> RowUpdate
mkRowUpdate RowColumnOperatorValue
operatorValue
  where
    prepareAndExtractLiteralValue :: UnpreparedValue 'DataConnector -> m (ScalarType, J.Value)
    prepareAndExtractLiteralValue :: UnpreparedValue 'DataConnector -> m (ScalarType, Value)
prepareAndExtractLiteralValue UnpreparedValue 'DataConnector
unpreparedValue = do
      Literal
preparedLiteral <- SessionVariables -> UnpreparedValue 'DataConnector -> m Literal
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables -> UnpreparedValue 'DataConnector -> m Literal
prepareLiteral SessionVariables
sessionVariables UnpreparedValue 'DataConnector
unpreparedValue
      case Literal
preparedLiteral of
        ValueLiteral ScalarType
scalarType Value
value -> (ScalarType, Value) -> m (ScalarType, Value)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ScalarType
scalarType, Value
value)
        ArrayLiteral ScalarType
_scalarType [Value]
_values -> Code -> Text -> m (ScalarType, Value)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"translateUpdateOperations: Array literals are not supported as column update values"

translateDelete ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  SessionVariables ->
  AnnDelG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  CPS.WriterT TableRelationships m API.DeleteMutationOperation
translateDelete :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> AnnDelG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m DeleteMutationOperation
translateDelete SessionVariables
sessionVariables AnnDel {Bool
[ColumnInfo 'DataConnector]
Maybe NamingCase
Maybe (ValidateInput ResolvedWebhook)
(AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector),
 AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector))
TableName 'DataConnector
MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_adTable :: TableName 'DataConnector
_adWhere :: (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector),
 AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector))
_adOutput :: MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_adAllCols :: [ColumnInfo 'DataConnector]
_adNamingConvention :: Maybe NamingCase
_adValidateInput :: Maybe (ValidateInput ResolvedWebhook)
_adIsDeleteByPk :: Bool
_adTable :: forall (b :: BackendType) r v. AnnDelG b r v -> TableName b
_adWhere :: forall (b :: BackendType) r v.
AnnDelG b r v -> (AnnBoolExp b v, AnnBoolExp b v)
_adOutput :: forall (b :: BackendType) r v.
AnnDelG b r v -> MutationOutputG b r v
_adAllCols :: forall (b :: BackendType) r v. AnnDelG b r v -> [ColumnInfo b]
_adNamingConvention :: forall (b :: BackendType) r v. AnnDelG b r v -> Maybe NamingCase
_adValidateInput :: forall (b :: BackendType) r v.
AnnDelG b r v -> Maybe (ValidateInput ResolvedWebhook)
_adIsDeleteByPk :: forall (b :: BackendType) r v. AnnDelG b r v -> Bool
..} = do
  Maybe Expression
whereExp <- SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m (Maybe Expression)
forall writerOutput (m :: * -> *) r.
(Has TableRelationships writerOutput, Monoid writerOutput,
 MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableRelationshipsKey
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (Maybe Expression)
translateBoolExpToExpression SessionVariables
sessionVariables (TableName -> TableRelationshipsKey
TableNameKey TableName
tableName) ([AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)]
-> AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
forall (backend :: BackendType) field.
[GBoolExp backend field] -> GBoolExp backend field
BoolAnd [AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
permissionFilter, AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
whereClause])
  HashMap FieldName Field
returningFields <- SessionVariables
-> TableName
-> MutationOutputG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT TableRelationships m (HashMap FieldName Field)
forall (m :: * -> *) writerOutput r.
(MonadError QErr m, Has TableRelationships writerOutput,
 Monoid writerOutput, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> MutationOutputG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateMutationOutputToReturningFields SessionVariables
sessionVariables TableName
tableName MutationOutputG
  'DataConnector Void (UnpreparedValue 'DataConnector)
_adOutput
  DeleteMutationOperation
-> WriterT TableRelationships m DeleteMutationOperation
forall a. a -> WriterT TableRelationships m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (DeleteMutationOperation
 -> WriterT TableRelationships m DeleteMutationOperation)
-> DeleteMutationOperation
-> WriterT TableRelationships m DeleteMutationOperation
forall a b. (a -> b) -> a -> b
$ API.DeleteMutationOperation
      { _dmoTable :: TableName
API._dmoTable = TableName
tableName,
        _dmoWhere :: Maybe Expression
API._dmoWhere = Maybe Expression
whereExp,
        _dmoReturningFields :: HashMap FieldName Field
API._dmoReturningFields = (FieldName -> FieldName)
-> HashMap FieldName Field -> HashMap FieldName Field
forall k2 k1 v.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> HashMap k1 v -> HashMap k2 v
HashMap.mapKeys (Text -> FieldName
API.FieldName (Text -> FieldName)
-> (FieldName -> Text) -> FieldName -> FieldName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldName -> Text
getFieldNameTxt) HashMap FieldName Field
returningFields
      }
  where
    TableName
tableName :: API.TableName = TableName -> TableName
forall source target. From source target => source -> target
Witch.from TableName 'DataConnector
TableName
_adTable
    (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
permissionFilter, AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector)
whereClause) = (AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector),
 AnnBoolExp 'DataConnector (UnpreparedValue 'DataConnector))
_adWhere

translateMutationOutputToReturningFields ::
  ( MonadError QErr m,
    Has TableRelationships writerOutput,
    Monoid writerOutput,
    MonadReader r m,
    Has API.ScalarTypesCapabilities r
  ) =>
  SessionVariables ->
  API.TableName ->
  MutationOutputG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  CPS.WriterT writerOutput m (HashMap FieldName API.Field)
translateMutationOutputToReturningFields :: forall (m :: * -> *) writerOutput r.
(MonadError QErr m, Has TableRelationships writerOutput,
 Monoid writerOutput, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> MutationOutputG
     'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateMutationOutputToReturningFields SessionVariables
sessionVariables TableName
tableName = \case
  MOutSinglerowObject AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
annFields ->
    SessionVariables
-> FieldPrefix
-> TableRelationshipsKey
-> AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
forall writerOutput (m :: * -> *) r.
(Has TableRelationships writerOutput, Monoid writerOutput,
 MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> FieldPrefix
-> TableRelationshipsKey
-> AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateAnnFields SessionVariables
sessionVariables FieldPrefix
noPrefix (TableName -> TableRelationshipsKey
TableNameKey TableName
tableName) AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
annFields
  MOutMultirowFields MutFldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
mutFields ->
    [HashMap FieldName Field] -> HashMap FieldName Field
forall k v. (Eq k, Hashable k) => [HashMap k v] -> HashMap k v
HashMap.unions ([HashMap FieldName Field] -> HashMap FieldName Field)
-> WriterT writerOutput m [HashMap FieldName Field]
-> WriterT writerOutput m (HashMap FieldName Field)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((FieldName,
  MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector))
 -> WriterT writerOutput m (HashMap FieldName Field))
-> MutFldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m [HashMap FieldName Field]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ((FieldName
 -> MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector)
 -> WriterT writerOutput m (HashMap FieldName Field))
-> (FieldName,
    MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector))
-> WriterT writerOutput m (HashMap FieldName Field)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((FieldName
  -> MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector)
  -> WriterT writerOutput m (HashMap FieldName Field))
 -> (FieldName,
     MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector))
 -> WriterT writerOutput m (HashMap FieldName Field))
-> (FieldName
    -> MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector)
    -> WriterT writerOutput m (HashMap FieldName Field))
-> (FieldName,
    MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector))
-> WriterT writerOutput m (HashMap FieldName Field)
forall a b. (a -> b) -> a -> b
$ SessionVariables
-> TableName
-> FieldName
-> MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
forall (m :: * -> *) writerOutput r.
(MonadError QErr m, Has TableRelationships writerOutput,
 Monoid writerOutput, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> FieldName
-> MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateMutField SessionVariables
sessionVariables TableName
tableName) MutFldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
mutFields

translateMutField ::
  ( MonadError QErr m,
    Has TableRelationships writerOutput,
    Monoid writerOutput,
    MonadReader r m,
    Has API.ScalarTypesCapabilities r
  ) =>
  SessionVariables ->
  API.TableName ->
  FieldName ->
  MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector) ->
  CPS.WriterT writerOutput m (HashMap FieldName API.Field)
translateMutField :: forall (m :: * -> *) writerOutput r.
(MonadError QErr m, Has TableRelationships writerOutput,
 Monoid writerOutput, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> TableName
-> FieldName
-> MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateMutField SessionVariables
sessionVariables TableName
tableName FieldName
fieldName = \case
  MutFldG 'DataConnector Void (UnpreparedValue 'DataConnector)
MCount ->
    -- All mutation operations in a request return their affected rows count.
    -- The count can just be added to the response JSON during agent response reshaping
    HashMap FieldName Field
-> WriterT writerOutput m (HashMap FieldName Field)
forall a. a -> WriterT writerOutput m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashMap FieldName Field
forall a. Monoid a => a
mempty
  MExp Text
_text ->
    -- We ignore literal text fields (we don't send them to the data connector agent)
    -- and add them back to the response JSON when we reshape what the agent returns
    -- to us
    HashMap FieldName Field
-> WriterT writerOutput m (HashMap FieldName Field)
forall a. a -> WriterT writerOutput m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashMap FieldName Field
forall a. Monoid a => a
mempty
  MRet AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
annFields ->
    SessionVariables
-> FieldPrefix
-> TableRelationshipsKey
-> AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
forall writerOutput (m :: * -> *) r.
(Has TableRelationships writerOutput, Monoid writerOutput,
 MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
SessionVariables
-> FieldPrefix
-> TableRelationshipsKey
-> AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
-> WriterT writerOutput m (HashMap FieldName Field)
translateAnnFields SessionVariables
sessionVariables (FieldName -> FieldPrefix
prefixWith FieldName
fieldName) (TableName -> TableRelationshipsKey
TableNameKey TableName
tableName) AnnFieldsG 'DataConnector Void (UnpreparedValue 'DataConnector)
annFields

--------------------------------------------------------------------------------

reshapeResponseToMutationGqlShape ::
  (MonadError QErr m) =>
  MutationDB 'DataConnector Void v ->
  API.MutationResponse ->
  m J.Encoding
reshapeResponseToMutationGqlShape :: forall (m :: * -> *) v.
MonadError QErr m =>
MutationDB 'DataConnector Void v -> MutationResponse -> m Encoding
reshapeResponseToMutationGqlShape MutationDB 'DataConnector Void v
mutationDb MutationResponse
mutationResponse = do
  case MutationDB 'DataConnector Void v
mutationDb of
    MDBInsert AnnotatedInsert {Bool
Maybe NamingCase
Text
MutationOutputG 'DataConnector Void v
MultiObjectInsert 'DataConnector v
_aiData :: forall (b :: BackendType) r v.
AnnotatedInsert b r v -> MultiObjectInsert b v
_aiFieldName :: forall (b :: BackendType) r v. AnnotatedInsert b r v -> Text
_aiIsSingle :: forall (b :: BackendType) r v. AnnotatedInsert b r v -> Bool
_aiOutput :: forall (b :: BackendType) r v.
AnnotatedInsert b r v -> MutationOutputG b r v
_aiNamingConvention :: forall (b :: BackendType) r v.
AnnotatedInsert b r v -> Maybe NamingCase
_aiFieldName :: Text
_aiIsSingle :: Bool
_aiData :: MultiObjectInsert 'DataConnector v
_aiOutput :: MutationOutputG 'DataConnector Void v
_aiNamingConvention :: Maybe NamingCase
..} ->
      MutationOutputG 'DataConnector Void v
-> MutationResponse -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
MutationOutputG 'DataConnector Void v
-> MutationResponse -> m Encoding
reshapeOutputForSingleBatchOperation MutationOutputG 'DataConnector Void v
_aiOutput MutationResponse
mutationResponse
    MDBUpdate AnnotatedUpdateG {[ColumnInfo 'DataConnector]
Maybe NamingCase
Maybe (ValidateInput ResolvedWebhook)
TableName 'DataConnector
UpdateVariant 'DataConnector v
AnnBoolExp 'DataConnector v
MutationOutputG 'DataConnector Void v
_auTable :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> TableName b
_auUpdatePermissions :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> AnnBoolExp b v
_auCheck :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> AnnBoolExp b v
_auUpdateVariant :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> UpdateVariant b v
_auOutput :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> MutationOutputG b r v
_auAllCols :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> [ColumnInfo b]
_auNamingConvention :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> Maybe NamingCase
_auValidateInput :: forall (b :: BackendType) r v.
AnnotatedUpdateG b r v -> Maybe (ValidateInput ResolvedWebhook)
_auTable :: TableName 'DataConnector
_auUpdatePermissions :: AnnBoolExp 'DataConnector v
_auCheck :: AnnBoolExp 'DataConnector v
_auUpdateVariant :: UpdateVariant 'DataConnector v
_auOutput :: MutationOutputG 'DataConnector Void v
_auAllCols :: [ColumnInfo 'DataConnector]
_auNamingConvention :: Maybe NamingCase
_auValidateInput :: Maybe (ValidateInput ResolvedWebhook)
..} ->
      case UpdateVariant 'DataConnector v
_auUpdateVariant of
        SingleBatch UpdateBatch 'DataConnector UpdateOperator v
_batch ->
          MutationOutputG 'DataConnector Void v
-> MutationResponse -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
MutationOutputG 'DataConnector Void v
-> MutationResponse -> m Encoding
reshapeOutputForSingleBatchOperation MutationOutputG 'DataConnector Void v
_auOutput MutationResponse
mutationResponse
        MultipleBatches [UpdateBatch 'DataConnector UpdateOperator v]
batches ->
          let outputs :: [MutationOutputG 'DataConnector Void v]
outputs = Int
-> MutationOutputG 'DataConnector Void v
-> [MutationOutputG 'DataConnector Void v]
forall a. Int -> a -> [a]
replicate ([UpdateBatch 'DataConnector UpdateOperator v] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [UpdateBatch 'DataConnector UpdateOperator v]
batches) MutationOutputG 'DataConnector Void v
_auOutput
           in [MutationOutputG 'DataConnector Void v]
-> MutationResponse -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
[MutationOutputG 'DataConnector Void v]
-> MutationResponse -> m Encoding
reshapeOutputForMultipleBatchOperation [MutationOutputG 'DataConnector Void v]
outputs MutationResponse
mutationResponse
    MDBDelete AnnDel {Bool
[ColumnInfo 'DataConnector]
Maybe NamingCase
Maybe (ValidateInput ResolvedWebhook)
(AnnBoolExp 'DataConnector v, AnnBoolExp 'DataConnector v)
TableName 'DataConnector
MutationOutputG 'DataConnector Void v
_adTable :: forall (b :: BackendType) r v. AnnDelG b r v -> TableName b
_adWhere :: forall (b :: BackendType) r v.
AnnDelG b r v -> (AnnBoolExp b v, AnnBoolExp b v)
_adOutput :: forall (b :: BackendType) r v.
AnnDelG b r v -> MutationOutputG b r v
_adAllCols :: forall (b :: BackendType) r v. AnnDelG b r v -> [ColumnInfo b]
_adNamingConvention :: forall (b :: BackendType) r v. AnnDelG b r v -> Maybe NamingCase
_adValidateInput :: forall (b :: BackendType) r v.
AnnDelG b r v -> Maybe (ValidateInput ResolvedWebhook)
_adIsDeleteByPk :: forall (b :: BackendType) r v. AnnDelG b r v -> Bool
_adTable :: TableName 'DataConnector
_adWhere :: (AnnBoolExp 'DataConnector v, AnnBoolExp 'DataConnector v)
_adOutput :: MutationOutputG 'DataConnector Void v
_adAllCols :: [ColumnInfo 'DataConnector]
_adNamingConvention :: Maybe NamingCase
_adValidateInput :: Maybe (ValidateInput ResolvedWebhook)
_adIsDeleteByPk :: Bool
..} ->
      MutationOutputG 'DataConnector Void v
-> MutationResponse -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
MutationOutputG 'DataConnector Void v
-> MutationResponse -> m Encoding
reshapeOutputForSingleBatchOperation MutationOutputG 'DataConnector Void v
_adOutput MutationResponse
mutationResponse
    MDBFunction JsonAggSelect
_returnsSet AnnSimpleSelectG 'DataConnector Void v
_select ->
      Code -> Text -> m Encoding
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"reshapeResponseToMutationGqlShape: function mutations not implemented for the Data Connector backend."

reshapeOutputForSingleBatchOperation ::
  (MonadError QErr m) =>
  MutationOutputG 'DataConnector Void v ->
  API.MutationResponse ->
  m J.Encoding
reshapeOutputForSingleBatchOperation :: forall (m :: * -> *) v.
MonadError QErr m =>
MutationOutputG 'DataConnector Void v
-> MutationResponse -> m Encoding
reshapeOutputForSingleBatchOperation MutationOutputG 'DataConnector Void v
mutationOutput API.MutationResponse {[MutationOperationResults]
_mrOperationResults :: [MutationOperationResults]
_mrOperationResults :: MutationResponse -> [MutationOperationResults]
..} = do
  MutationOperationResults
mutationOperationResult <-
    [MutationOperationResults] -> Maybe MutationOperationResults
forall a. [a] -> Maybe a
listToMaybe [MutationOperationResults]
_mrOperationResults
      Maybe MutationOperationResults
-> m MutationOperationResults -> m MutationOperationResults
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Text -> m MutationOperationResults
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 Text
"Unable to find expected mutation operation results"
  MutationOutputG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
MutationOutputG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
reshapeMutationOutput MutationOutputG 'DataConnector Void v
mutationOutput MutationOperationResults
mutationOperationResult

reshapeOutputForMultipleBatchOperation ::
  (MonadError QErr m) =>
  [MutationOutputG 'DataConnector Void v] ->
  API.MutationResponse ->
  m J.Encoding
reshapeOutputForMultipleBatchOperation :: forall (m :: * -> *) v.
MonadError QErr m =>
[MutationOutputG 'DataConnector Void v]
-> MutationResponse -> m Encoding
reshapeOutputForMultipleBatchOperation [MutationOutputG 'DataConnector Void v]
mutationOutputs API.MutationResponse {[MutationOperationResults]
_mrOperationResults :: MutationResponse -> [MutationOperationResults]
_mrOperationResults :: [MutationOperationResults]
..} = do
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
operationResultCount Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
requiredResultCount)
    (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Text -> m ()
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 (Text
"Data Connector agent returned " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
operationResultCount Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" mutation operation results where at least " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
requiredResultCount Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" was expected")

  [Encoding]
reshapedResults <-
    [MutationOutputG 'DataConnector Void v]
-> [MutationOperationResults]
-> [(MutationOutputG 'DataConnector Void v,
     MutationOperationResults)]
forall a b. [a] -> [b] -> [(a, b)]
zip [MutationOutputG 'DataConnector Void v]
mutationOutputs [MutationOperationResults]
_mrOperationResults
      [(MutationOutputG 'DataConnector Void v, MutationOperationResults)]
-> ([(MutationOutputG 'DataConnector Void v,
      MutationOperationResults)]
    -> m [Encoding])
-> m [Encoding]
forall a b. a -> (a -> b) -> b
& ((MutationOutputG 'DataConnector Void v, MutationOperationResults)
 -> m Encoding)
-> [(MutationOutputG 'DataConnector Void v,
     MutationOperationResults)]
-> m [Encoding]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ((MutationOutputG 'DataConnector Void v
 -> MutationOperationResults -> m Encoding)
-> (MutationOutputG 'DataConnector Void v,
    MutationOperationResults)
-> m Encoding
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry MutationOutputG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
MutationOutputG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
reshapeMutationOutput)

  Encoding -> m Encoding
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Encoding -> m Encoding) -> Encoding -> m Encoding
forall a b. (a -> b) -> a -> b
$ (Encoding -> Encoding) -> [Encoding] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
JE.list Encoding -> Encoding
forall a. a -> a
id [Encoding]
reshapedResults
  where
    requiredResultCount :: Int
requiredResultCount = [MutationOutputG 'DataConnector Void v] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [MutationOutputG 'DataConnector Void v]
mutationOutputs
    operationResultCount :: Int
operationResultCount = [MutationOperationResults] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [MutationOperationResults]
_mrOperationResults

reshapeMutationOutput ::
  (MonadError QErr m) =>
  MutationOutputG 'DataConnector Void v ->
  API.MutationOperationResults ->
  m J.Encoding
reshapeMutationOutput :: forall (m :: * -> *) v.
MonadError QErr m =>
MutationOutputG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
reshapeMutationOutput MutationOutputG 'DataConnector Void v
mutationOutput MutationOperationResults
mutationOperationResults =
  case MutationOutputG 'DataConnector Void v
mutationOutput of
    MOutSinglerowObject AnnFieldsG 'DataConnector Void v
annFields -> Cardinality
-> FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> MutationOperationResults
-> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
Cardinality
-> FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> MutationOperationResults
-> m Encoding
reshapeReturningRows Cardinality
Single FieldPrefix
noPrefix AnnFieldsG 'DataConnector Void v
annFields MutationOperationResults
mutationOperationResults
    MOutMultirowFields MutFldsG 'DataConnector Void v
mutFields -> MutFldsG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
MutFldsG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
reshapeMutFields MutFldsG 'DataConnector Void v
mutFields MutationOperationResults
mutationOperationResults

reshapeReturningRows ::
  (MonadError QErr m) =>
  Cardinality ->
  FieldPrefix ->
  AnnFieldsG 'DataConnector Void v ->
  API.MutationOperationResults ->
  m J.Encoding
reshapeReturningRows :: forall (m :: * -> *) v.
MonadError QErr m =>
Cardinality
-> FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> MutationOperationResults
-> m Encoding
reshapeReturningRows Cardinality
cardinality FieldPrefix
fieldNamePrefix AnnFieldsG 'DataConnector Void v
annFields API.MutationOperationResults {Int
Maybe [HashMap FieldName FieldValue]
_morAffectedRows :: Int
_morReturning :: Maybe [HashMap FieldName FieldValue]
_morAffectedRows :: MutationOperationResults -> Int
_morReturning :: MutationOperationResults -> Maybe [HashMap FieldName FieldValue]
..} =
  case Cardinality
cardinality of
    Cardinality
Single ->
      case [HashMap FieldName FieldValue]
rows of
        [] -> Encoding -> m Encoding
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Encoding -> m Encoding) -> Encoding -> m Encoding
forall a b. (a -> b) -> a -> b
$ Value -> Encoding
forall a. ToJSON a => a -> Encoding
J.toEncoding Value
J.Null
        [HashMap FieldName FieldValue
singleRow] -> FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> HashMap FieldName FieldValue
-> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> HashMap FieldName FieldValue
-> m Encoding
reshapeAnnFields FieldPrefix
fieldNamePrefix AnnFieldsG 'DataConnector Void v
annFields HashMap FieldName FieldValue
singleRow
        [HashMap FieldName FieldValue]
_multipleRows ->
          Text -> m Encoding
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 Text
"Data Connector agent returned multiple rows in a mutation operation result when only one was expected"
    Cardinality
Many -> do
      [Encoding]
reshapedRows <- (HashMap FieldName FieldValue -> m Encoding)
-> [HashMap FieldName FieldValue] -> m [Encoding]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> HashMap FieldName FieldValue
-> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> HashMap FieldName FieldValue
-> m Encoding
reshapeAnnFields FieldPrefix
fieldNamePrefix AnnFieldsG 'DataConnector Void v
annFields) [HashMap FieldName FieldValue]
rows
      Encoding -> m Encoding
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Encoding -> m Encoding) -> Encoding -> m Encoding
forall a b. (a -> b) -> a -> b
$ (Encoding -> Encoding) -> [Encoding] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
JE.list Encoding -> Encoding
forall a. a -> a
id [Encoding]
reshapedRows
  where
    rows :: [HashMap FieldName FieldValue]
rows = [HashMap FieldName FieldValue]
-> Maybe [HashMap FieldName FieldValue]
-> [HashMap FieldName FieldValue]
forall a. a -> Maybe a -> a
fromMaybe [HashMap FieldName FieldValue]
forall a. Monoid a => a
mempty Maybe [HashMap FieldName FieldValue]
_morReturning

reshapeMutFields ::
  (MonadError QErr m) =>
  MutFldsG 'DataConnector Void v ->
  API.MutationOperationResults ->
  m J.Encoding
reshapeMutFields :: forall (m :: * -> *) v.
MonadError QErr m =>
MutFldsG 'DataConnector Void v
-> MutationOperationResults -> m Encoding
reshapeMutFields MutFldsG 'DataConnector Void v
mutFields mutationOperationResults :: MutationOperationResults
mutationOperationResults@API.MutationOperationResults {Int
Maybe [HashMap FieldName FieldValue]
_morAffectedRows :: MutationOperationResults -> Int
_morReturning :: MutationOperationResults -> Maybe [HashMap FieldName FieldValue]
_morAffectedRows :: Int
_morReturning :: Maybe [HashMap FieldName FieldValue]
..} = do
  [(Text, Encoding)]
reshapedMutFields <- MutFldsG 'DataConnector Void v
-> ((FieldName, MutFldG 'DataConnector Void v)
    -> m (Text, Encoding))
-> m [(Text, Encoding)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM MutFldsG 'DataConnector Void v
mutFields (((FieldName, MutFldG 'DataConnector Void v) -> m (Text, Encoding))
 -> m [(Text, Encoding)])
-> ((FieldName, MutFldG 'DataConnector Void v)
    -> m (Text, Encoding))
-> m [(Text, Encoding)]
forall a b. (a -> b) -> a -> b
$ \(fieldName :: FieldName
fieldName@(FieldName Text
fieldNameText), MutFldG 'DataConnector Void v
mutField) -> do
    Encoding
reshapedFieldValue <-
      case MutFldG 'DataConnector Void v
mutField of
        MutFldG 'DataConnector Void v
MCount -> Encoding -> m Encoding
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Encoding -> m Encoding) -> Encoding -> m Encoding
forall a b. (a -> b) -> a -> b
$ Int -> Encoding
JE.int Int
_morAffectedRows
        MExp Text
literalText -> Encoding -> m Encoding
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Encoding -> m Encoding) -> Encoding -> m Encoding
forall a b. (a -> b) -> a -> b
$ Text -> Encoding
forall a. Text -> Encoding' a
JE.text Text
literalText
        MRet AnnFieldsG 'DataConnector Void v
annFields -> Cardinality
-> FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> MutationOperationResults
-> m Encoding
forall (m :: * -> *) v.
MonadError QErr m =>
Cardinality
-> FieldPrefix
-> AnnFieldsG 'DataConnector Void v
-> MutationOperationResults
-> m Encoding
reshapeReturningRows Cardinality
Many (FieldName -> FieldPrefix
prefixWith FieldName
fieldName) AnnFieldsG 'DataConnector Void v
annFields MutationOperationResults
mutationOperationResults
    (Text, Encoding) -> m (Text, Encoding)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
fieldNameText, Encoding
reshapedFieldValue)

  Encoding -> m Encoding
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Encoding -> m Encoding) -> Encoding -> m Encoding
forall a b. (a -> b) -> a -> b
$ [(Text, Encoding)] -> Encoding
encodeAssocListAsObject [(Text, Encoding)]
reshapedMutFields