{-# LANGUAGE Arrows #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Hasura.Backends.DataConnector.Adapter.Metadata (requestDatabaseSchema) where

import Control.Arrow.Extended
import Control.Monad.Trans.Control
import Data.Aeson qualified as J
import Data.Aeson.Key qualified as K
import Data.Aeson.KeyMap qualified as KM
import Data.Bifunctor (bimap)
import Data.Environment (Environment)
import Data.Has (Has (getter))
import Data.HashMap.Strict qualified as HashMap
import Data.HashMap.Strict.Extended qualified as HashMap
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.HashMap.Strict.NonEmpty qualified as NEHashMap
import Data.HashSet qualified as HashSet
import Data.List.NonEmpty qualified as NEList
import Data.Map.Strict qualified as Map
import Data.Semigroup.Foldable (Foldable1 (..))
import Data.Sequence qualified as Seq
import Data.Sequence.NonEmpty qualified as NESeq
import Data.Text.Extended (toTxt, (<<>), (<>>))
import Hasura.Backends.DataConnector.API qualified as API
import Hasura.Backends.DataConnector.API.V0 (FunctionInfo (_fiDescription, _fiName))
import Hasura.Backends.DataConnector.API.V0.Table qualified as DC (TableType (..))
import Hasura.Backends.DataConnector.Adapter.Backend (columnTypeToScalarType)
import Hasura.Backends.DataConnector.Adapter.ConfigTransform (transformSourceConfig, validateConnSourceConfig)
import Hasura.Backends.DataConnector.Adapter.Types qualified as DC
import Hasura.Backends.DataConnector.Agent.Client (AgentClientContext (..), runAgentClientT)
import Hasura.Backends.DataConnector.Agent.Client qualified as Client
import Hasura.Backends.Postgres.SQL.Types (PGDescription (..))
import Hasura.Base.Error (Code (..), QErr (..), decodeValue, runAesonParser, throw400, withPathK)
import Hasura.Function.Cache
  ( FunctionConfig (..),
    FunctionExposedAs (FEAMutation, FEAQuery),
    FunctionInfo (..),
    FunctionOverloads (FunctionOverloads),
    FunctionPermissionsMap,
    FunctionVolatility (FTSTABLE, FTVOLATILE),
    InputArgument (..),
    TrackableFunctionInfo (..),
    TrackableInfo (..),
    TrackableTableInfo (..),
    getFuncArgNameTxt,
  )
import Hasura.Function.Common
  ( getFunctionAggregateGQLName,
    getFunctionArgsGQLName,
    getFunctionGQLName,
  )
import Hasura.Incremental qualified as Inc
import Hasura.Incremental.Select qualified as Inc
import Hasura.Logging (Hasura, Logger)
import Hasura.LogicalModel.Cache
import Hasura.LogicalModel.Metadata (LogicalModelMetadata (..))
import Hasura.LogicalModel.Types
import Hasura.Prelude
import Hasura.RQL.DDL.Relationship (defaultBuildArrayRelationshipInfo, defaultBuildObjectRelationshipInfo)
import Hasura.RQL.IR.BoolExp (ComparisonNullability (..), OpExpG (..), PartialSQLExp (..), RootOrCurrent (..), RootOrCurrentColumn (..))
import Hasura.RQL.Types.Backend (FunctionReturnType (..), functionGraphQLName)
import Hasura.RQL.Types.BackendType (BackendSourceKind (..), BackendType (..))
import Hasura.RQL.Types.Column qualified as RQL.T.C
import Hasura.RQL.Types.Common (JsonAggSelect (JASMultipleRows, JASSingleObject), OID (..), SourceName, SystemDefined)
import Hasura.RQL.Types.EventTrigger (RecreateEventTriggers (RETDoNothing))
import Hasura.RQL.Types.Metadata (SourceMetadata (..))
import Hasura.RQL.Types.Metadata.Backend (BackendMetadata (..))
import Hasura.RQL.Types.Metadata.Object
import Hasura.RQL.Types.NamingCase (NamingCase)
import Hasura.RQL.Types.Relationships.Local (ArrRelDef, ObjRelDef, RelInfo ())
import Hasura.RQL.Types.SchemaCache (CacheRM, askSourceConfig, askSourceInfo)
import Hasura.RQL.Types.SchemaCache.Build
import Hasura.RQL.Types.SchemaCacheTypes (DependencyReason (DRTable), SchemaDependency (SchemaDependency), SchemaObjId (SOSourceObj), SourceObjId (SOITable))
import Hasura.RQL.Types.Source (DBObjectsIntrospection (..), SourceInfo (..))
import Hasura.RQL.Types.Source.Column (ColumnValueGenerationStrategy (..), SourceColumnInfo (..))
import Hasura.RQL.Types.Source.Table (SourceConstraint (..), SourceForeignKeys (..), SourceTableInfo (..), SourceTableType (..))
import Hasura.RQL.Types.SourceCustomization (applyFieldNameCaseCust)
import Hasura.SQL.AnyBackend (mkAnyBackend)
import Hasura.SQL.Types (CollectableType (..))
import Hasura.Server.Migrate.Version (SourceCatalogMigrationState (..))
import Hasura.Server.Utils qualified as HSU
import Hasura.Services.Network
import Hasura.Session (SessionVariable, mkSessionVariable)
import Hasura.Table.Cache (ForeignKey (_fkConstraint))
import Hasura.Table.Cache qualified as RQL.T.T
import Hasura.Tracing (ignoreTraceT)
import Language.GraphQL.Draft.Syntax qualified as G
import Language.GraphQL.Draft.Syntax qualified as GQL
import Network.HTTP.Client qualified as HTTP
import Witch qualified

instance BackendMetadata 'DataConnector where
  prepareCatalog :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'DataConnector
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
prepareCatalog SourceConfig 'DataConnector
_ = (RecreateEventTriggers, SourceCatalogMigrationState)
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RecreateEventTriggers
RETDoNothing, SourceCatalogMigrationState
SCMSNotSupported)
  type BackendInvalidationKeys 'DataConnector = HashMap DC.DataConnectorName Inc.InvalidationKey
  resolveBackendInfo :: forall (arr :: * -> * -> *) (m :: * -> *).
(ArrowChoice arr, ArrowCache m arr, ArrowDistribute arr,
 ArrowWriter (Seq CollectItem) arr, MonadIO m,
 MonadBaseControl IO m, ProvidesNetwork m) =>
Logger Hasura
-> arr
     (Dependency (Maybe (BackendInvalidationKeys 'DataConnector)),
      BackendConfig 'DataConnector)
     (BackendInfo 'DataConnector)
resolveBackendInfo = Logger Hasura
-> arr
     (Dependency (Maybe (HashMap DataConnectorName InvalidationKey)),
      Map DataConnectorName DataConnectorOptions)
     (HashMap DataConnectorName DataConnectorInfo)
Logger Hasura
-> arr
     (Dependency (Maybe (BackendInvalidationKeys 'DataConnector)),
      BackendConfig 'DataConnector)
     (BackendInfo 'DataConnector)
forall (arr :: * -> * -> *) (m :: * -> *).
(ArrowChoice arr, ArrowCache m arr, ArrowDistribute arr,
 ArrowWriter (Seq CollectItem) arr, MonadIO m,
 MonadBaseControl IO m, ProvidesNetwork m) =>
Logger Hasura
-> arr
     (Dependency (Maybe (HashMap DataConnectorName InvalidationKey)),
      Map DataConnectorName DataConnectorOptions)
     (HashMap DataConnectorName DataConnectorInfo)
resolveBackendInfo'
  resolveSourceConfig :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadResolveSource m) =>
SourceName
-> SourceConnConfiguration 'DataConnector
-> BackendSourceKind 'DataConnector
-> BackendInfo 'DataConnector
-> Environment
-> Manager
-> m (Either QErr (SourceConfig 'DataConnector))
resolveSourceConfig = SourceName
-> SourceConnConfiguration 'DataConnector
-> BackendSourceKind 'DataConnector
-> BackendInfo 'DataConnector
-> Environment
-> Manager
-> m (Either QErr (SourceConfig 'DataConnector))
SourceName
-> ConnSourceConfig
-> BackendSourceKind 'DataConnector
-> HashMap DataConnectorName DataConnectorInfo
-> Environment
-> Manager
-> m (Either QErr SourceConfig)
forall (m :: * -> *).
Monad m =>
SourceName
-> ConnSourceConfig
-> BackendSourceKind 'DataConnector
-> HashMap DataConnectorName DataConnectorInfo
-> Environment
-> Manager
-> m (Either QErr SourceConfig)
resolveSourceConfig'
  resolveDatabaseMetadata :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadResolveSource m) =>
Logger Hasura
-> SourceMetadata 'DataConnector
-> SourceConfig 'DataConnector
-> m (Either QErr (DBObjectsIntrospection 'DataConnector))
resolveDatabaseMetadata = Logger Hasura
-> SourceMetadata 'DataConnector
-> SourceConfig 'DataConnector
-> m (Either QErr (DBObjectsIntrospection 'DataConnector))
Logger Hasura
-> SourceMetadata 'DataConnector
-> SourceConfig
-> m (Either QErr (DBObjectsIntrospection 'DataConnector))
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
Logger Hasura
-> SourceMetadata 'DataConnector
-> SourceConfig
-> m (Either QErr (DBObjectsIntrospection 'DataConnector))
resolveDatabaseMetadata'
  parseBoolExpOperations :: forall (m :: * -> *) v.
MonadError QErr m =>
ValueParser 'DataConnector m v
-> FieldInfoMap (FieldInfo 'DataConnector)
-> FieldInfoMap (FieldInfo 'DataConnector)
-> ColumnReference 'DataConnector
-> Value
-> m [OpExpG 'DataConnector v]
parseBoolExpOperations = ValueParser 'DataConnector m v
-> FieldInfoMap (FieldInfo 'DataConnector)
-> FieldInfoMap (FieldInfo 'DataConnector)
-> ColumnReference 'DataConnector
-> Value
-> m [OpExpG 'DataConnector v]
forall (m :: * -> *) v.
MonadError QErr m =>
ValueParser 'DataConnector m v
-> FieldInfoMap (FieldInfo 'DataConnector)
-> FieldInfoMap (FieldInfo 'DataConnector)
-> ColumnReference 'DataConnector
-> Value
-> m [OpExpG 'DataConnector v]
parseBoolExpOperations'
  parseCollectableType :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has (ScalarTypeParsingContext 'DataConnector) r) =>
CollectableType (ColumnType 'DataConnector)
-> Value -> m (PartialSQLExp 'DataConnector)
parseCollectableType = CollectableType (ColumnType 'DataConnector)
-> Value -> m (PartialSQLExp 'DataConnector)
forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
CollectableType (ColumnType 'DataConnector)
-> Value -> m (PartialSQLExp 'DataConnector)
parseCollectableType'
  buildComputedFieldInfo :: forall (m :: * -> *).
MonadError QErr m =>
HashSet (TableName 'DataConnector)
-> TableName 'DataConnector
-> HashSet (Column 'DataConnector)
-> ComputedFieldName
-> ComputedFieldDefinition 'DataConnector
-> RawFunctionInfo 'DataConnector
-> Comment
-> m (ComputedFieldInfo 'DataConnector)
buildComputedFieldInfo = [Char]
-> HashSet TableName
-> TableName
-> HashSet ColumnName
-> ComputedFieldName
-> ()
-> FunctionInfo
-> Comment
-> m (ComputedFieldInfo 'DataConnector)
forall a. HasCallStack => [Char] -> a
error [Char]
"buildComputedFieldInfo: not implemented for the Data Connector backend."
  buildArrayRelationshipInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig 'DataConnector
-> SourceName
-> HashMap
     (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
-> TableName 'DataConnector
-> ArrRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
buildArrayRelationshipInfo = SourceConfig 'DataConnector
-> SourceName
-> HashMap
     (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
-> TableName 'DataConnector
-> ArrRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
SourceConfig
-> SourceName
-> HashMap TableName (HashSet (ForeignKey 'DataConnector))
-> TableName
-> ArrRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall (m :: * -> *).
MonadError QErr m =>
SourceConfig
-> SourceName
-> HashMap TableName (HashSet (ForeignKey 'DataConnector))
-> TableName
-> ArrRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
buildArrayRelationshipInfo'
  buildObjectRelationshipInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig 'DataConnector
-> SourceName
-> HashMap
     (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
-> TableName 'DataConnector
-> ObjRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
buildObjectRelationshipInfo = SourceConfig 'DataConnector
-> SourceName
-> HashMap
     (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
-> TableName 'DataConnector
-> ObjRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
SourceConfig
-> SourceName
-> HashMap TableName (HashSet (ForeignKey 'DataConnector))
-> TableName
-> ObjRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall (m :: * -> *).
MonadError QErr m =>
SourceConfig
-> SourceName
-> HashMap TableName (HashSet (ForeignKey 'DataConnector))
-> TableName
-> ObjRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
buildObjectRelationshipInfo'

  -- If/when we implement enums for Data Connector backend, we will also need to fix columnTypeToScalarType function
  -- in Hasura.Backends.DataConnector.Adapter.Backend. See note there for more information.
  fetchAndValidateEnumValues :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'DataConnector
-> TableName 'DataConnector
-> Maybe (PrimaryKey 'DataConnector (RawColumnInfo 'DataConnector))
-> [RawColumnInfo 'DataConnector]
-> m (Either QErr EnumValues)
fetchAndValidateEnumValues = [Char]
-> SourceConfig
-> TableName
-> Maybe (PrimaryKey 'DataConnector (RawColumnInfo 'DataConnector))
-> [RawColumnInfo 'DataConnector]
-> m (Either QErr EnumValues)
forall a. HasCallStack => [Char] -> a
error [Char]
"fetchAndValidateEnumValues: not implemented for the Data Connector backend."
  buildFunctionInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceName
-> FunctionName 'DataConnector
-> SystemDefined
-> FunctionConfig 'DataConnector
-> FunctionPermissionsMap
-> RawFunctionInfo 'DataConnector
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'DataConnector, SchemaDependency)
buildFunctionInfo = SourceName
-> FunctionName 'DataConnector
-> SystemDefined
-> FunctionConfig 'DataConnector
-> FunctionPermissionsMap
-> RawFunctionInfo 'DataConnector
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'DataConnector, SchemaDependency)
SourceName
-> FunctionName
-> SystemDefined
-> FunctionConfig 'DataConnector
-> FunctionPermissionsMap
-> FunctionInfo
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'DataConnector, SchemaDependency)
forall (m :: * -> *).
MonadError QErr m =>
SourceName
-> FunctionName
-> SystemDefined
-> FunctionConfig 'DataConnector
-> FunctionPermissionsMap
-> FunctionInfo
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'DataConnector, SchemaDependency)
buildFunctionInfo'
  updateColumnInEventTrigger :: TableName 'DataConnector
-> Column 'DataConnector
-> Column 'DataConnector
-> TableName 'DataConnector
-> EventTriggerConf 'DataConnector
-> EventTriggerConf 'DataConnector
updateColumnInEventTrigger = [Char]
-> TableName
-> ColumnName
-> ColumnName
-> TableName
-> EventTriggerConf 'DataConnector
-> EventTriggerConf 'DataConnector
forall a. HasCallStack => [Char] -> a
error [Char]
"updateColumnInEventTrigger: not implemented for the Data Connector backend."
  postDropSourceHook :: forall (m :: * -> *).
(MonadError QErr m, MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'DataConnector
-> TableEventTriggers 'DataConnector -> m ()
postDropSourceHook SourceConfig 'DataConnector
_sourceConfig TableEventTriggers 'DataConnector
_tableTriggerMap = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  buildComputedFieldBooleanExp :: forall (m :: * -> *) v.
(MonadError QErr m, TableCoreInfoRM 'DataConnector m) =>
BoolExpResolver 'DataConnector m v
-> BoolExpRHSParser 'DataConnector m v
-> FieldInfoMap (FieldInfo 'DataConnector)
-> FieldInfoMap (FieldInfo 'DataConnector)
-> ComputedFieldInfo 'DataConnector
-> Value
-> m (AnnComputedFieldBoolExp 'DataConnector v)
buildComputedFieldBooleanExp BoolExpResolver 'DataConnector m v
_ BoolExpRHSParser 'DataConnector m v
_ FieldInfoMap (FieldInfo 'DataConnector)
_ FieldInfoMap (FieldInfo 'DataConnector)
_ ComputedFieldInfo 'DataConnector
_ Value
_ =
    [Char] -> m (AnnComputedFieldBoolExp 'DataConnector v)
forall a. HasCallStack => [Char] -> a
error [Char]
"buildComputedFieldBooleanExp: not implemented for the Data Connector backend."
  listAllTables :: forall (m :: * -> *) r.
(CacheRM m, MonadBaseControl IO m, MetadataM m, MonadError QErr m,
 MonadIO m, MonadReader r m, Has (Logger Hasura) r,
 ProvidesNetwork m) =>
SourceName -> m [TableName 'DataConnector]
listAllTables = SourceName -> m [TableName 'DataConnector]
SourceName -> m [TableName]
forall (m :: * -> *) r.
(CacheRM m, Has (Logger Hasura) r, MonadIO m,
 MonadBaseControl IO m, MonadReader r m, MonadError QErr m,
 MetadataM m) =>
SourceName -> m [TableName]
listAllTables'
  listAllTrackables :: forall (m :: * -> *) r.
(CacheRM m, MonadBaseControl IO m, MetadataM m, MonadError QErr m,
 MonadIO m, MonadReader r m, Has (Logger Hasura) r,
 ProvidesNetwork m) =>
SourceName -> m (TrackableInfo 'DataConnector)
listAllTrackables = SourceName -> m (TrackableInfo 'DataConnector)
forall (m :: * -> *) r.
(CacheRM m, Has (Logger Hasura) r, MonadIO m,
 MonadBaseControl IO m, MonadReader r m, MonadError QErr m,
 MetadataM m) =>
SourceName -> m (TrackableInfo 'DataConnector)
listAllTrackables'
  getTableInfo :: forall (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m, MonadBaseControl IO m,
 MonadIO m) =>
SourceName
-> TableName 'DataConnector
-> m (Maybe (SourceTableInfo 'DataConnector))
getTableInfo = SourceName
-> TableName 'DataConnector
-> m (Maybe (SourceTableInfo 'DataConnector))
SourceName
-> TableName -> m (Maybe (SourceTableInfo 'DataConnector))
forall (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m) =>
SourceName
-> TableName -> m (Maybe (SourceTableInfo 'DataConnector))
getTableInfo'
  supportsBeingRemoteRelationshipTarget :: SourceConfig 'DataConnector -> Bool
supportsBeingRemoteRelationshipTarget = SourceConfig 'DataConnector -> Bool
SourceConfig -> Bool
supportsBeingRemoteRelationshipTarget'

arityJsonAggSelect :: API.FunctionArity -> JsonAggSelect
arityJsonAggSelect :: FunctionArity -> JsonAggSelect
arityJsonAggSelect = \case
  FunctionArity
API.FunctionArityOne -> JsonAggSelect
JASSingleObject
  FunctionArity
API.FunctionArityMany -> JsonAggSelect
JASMultipleRows

functionReturnTypeFromAPI ::
  (MonadError QErr m) =>
  DC.FunctionName ->
  (Maybe (FunctionReturnType 'DataConnector), API.FunctionReturnType) ->
  m DC.TableName
functionReturnTypeFromAPI :: forall (m :: * -> *).
MonadError QErr m =>
FunctionName
-> (Maybe (FunctionReturnType 'DataConnector), FunctionReturnType)
-> m TableName
functionReturnTypeFromAPI FunctionName
funcGivenName = \case
  (Just (DC.FunctionReturnsTable TableName
t), FunctionReturnType
_) -> TableName -> m TableName
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableName
t
  (Maybe (FunctionReturnType 'DataConnector)
_, API.FunctionReturnsTable TableName
t) -> TableName -> m TableName
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TableName -> TableName
forall target source. From source target => source -> target
Witch.into TableName
t)
  (Maybe (FunctionReturnType 'DataConnector), FunctionReturnType)
_ ->
    Code -> Text -> m TableName
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported
      (Text -> m TableName) -> Text -> m TableName
forall a b. (a -> b) -> a -> b
$ Text
"Function "
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FunctionName -> Text
forall a. ToTxt a => a -> Text
toTxt FunctionName
funcGivenName
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is missing a return type - This should be explicit in metadata, or inferred from agent"

buildFunctionInfo' ::
  (MonadError QErr m) =>
  SourceName ->
  DC.FunctionName ->
  SystemDefined ->
  FunctionConfig 'DataConnector ->
  FunctionPermissionsMap ->
  API.FunctionInfo ->
  Maybe Text ->
  NamingCase ->
  m
    ( Hasura.Function.Cache.FunctionInfo 'DataConnector,
      SchemaDependency
    )
buildFunctionInfo' :: forall (m :: * -> *).
MonadError QErr m =>
SourceName
-> FunctionName
-> SystemDefined
-> FunctionConfig 'DataConnector
-> FunctionPermissionsMap
-> FunctionInfo
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'DataConnector, SchemaDependency)
buildFunctionInfo'
  SourceName
sourceName
  FunctionName
funcName
  SystemDefined
sysDefined
  funcConfig :: FunctionConfig 'DataConnector
funcConfig@FunctionConfig {Maybe Name
Maybe (FunctionReturnType 'DataConnector)
Maybe FunctionExposedAs
Maybe FunctionArgName
FunctionCustomRootFields
_fcSessionArgument :: Maybe FunctionArgName
_fcExposedAs :: Maybe FunctionExposedAs
_fcCustomRootFields :: FunctionCustomRootFields
_fcCustomName :: Maybe Name
_fcResponse :: Maybe (FunctionReturnType 'DataConnector)
_fcSessionArgument :: forall (b :: BackendType).
FunctionConfig b -> Maybe FunctionArgName
_fcExposedAs :: forall (b :: BackendType).
FunctionConfig b -> Maybe FunctionExposedAs
_fcCustomRootFields :: forall (b :: BackendType).
FunctionConfig b -> FunctionCustomRootFields
_fcCustomName :: forall (b :: BackendType). FunctionConfig b -> Maybe Name
_fcResponse :: forall (b :: BackendType).
FunctionConfig b -> Maybe (FunctionReturnType b)
..}
  FunctionPermissionsMap
permissionMap
  (API.FunctionInfo FunctionName
infoName FunctionType
infoType FunctionReturnType
returnType FunctionArity
infoSet [FunctionArg]
infoArgs Maybe Text
infoDesc)
  Maybe Text
funcComment
  NamingCase
namingCase =
    do
      Name
funcGivenName <- forall (b :: BackendType).
Backend b =>
FunctionName b -> Either QErr Name
functionGraphQLName @'DataConnector FunctionName 'DataConnector
FunctionName
funcName Either QErr Name -> (QErr -> m Name) -> m Name
forall (m :: * -> *) e a.
Applicative m =>
Either e a -> (e -> m a) -> m a
`onLeft` QErr -> m Name
forall a. QErr -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
      let (FunctionVolatility
volitility, FunctionExposedAs
exposeAs) = case FunctionType
infoType of
            FunctionType
API.FRead -> (FunctionVolatility
FTSTABLE, FunctionExposedAs
FEAQuery)
            FunctionType
API.FWrite -> (FunctionVolatility
FTVOLATILE, FunctionExposedAs
FEAMutation)
          setNamingCase :: Name -> Name
setNamingCase = NamingCase -> Name -> Name
applyFieldNameCaseCust NamingCase
namingCase
      SchemaObjId
objid <-
        case (Maybe (FunctionReturnType 'DataConnector)
Maybe FunctionReturnType
_fcResponse, FunctionReturnType
returnType) of
          (Just (DC.FunctionReturnsTable TableName
t), FunctionReturnType
_) -> SchemaObjId -> m SchemaObjId
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SchemaObjId -> m SchemaObjId) -> SchemaObjId -> m SchemaObjId
forall a b. (a -> b) -> a -> b
$ SourceName -> AnyBackend SourceObjId -> SchemaObjId
SOSourceObj SourceName
sourceName (AnyBackend SourceObjId -> SchemaObjId)
-> AnyBackend SourceObjId -> SchemaObjId
forall a b. (a -> b) -> a -> b
$ SourceObjId 'DataConnector -> AnyBackend SourceObjId
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
mkAnyBackend (SourceObjId 'DataConnector -> AnyBackend SourceObjId)
-> SourceObjId 'DataConnector -> AnyBackend SourceObjId
forall a b. (a -> b) -> a -> b
$ forall (b :: BackendType). TableName b -> SourceObjId b
SOITable @'DataConnector TableName 'DataConnector
TableName
t
          (Maybe FunctionReturnType
_, API.FunctionReturnsTable TableName
t) -> SchemaObjId -> m SchemaObjId
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SchemaObjId -> m SchemaObjId) -> SchemaObjId -> m SchemaObjId
forall a b. (a -> b) -> a -> b
$ SourceName -> AnyBackend SourceObjId -> SchemaObjId
SOSourceObj SourceName
sourceName (AnyBackend SourceObjId -> SchemaObjId)
-> AnyBackend SourceObjId -> SchemaObjId
forall a b. (a -> b) -> a -> b
$ SourceObjId 'DataConnector -> AnyBackend SourceObjId
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
mkAnyBackend (SourceObjId 'DataConnector -> AnyBackend SourceObjId)
-> SourceObjId 'DataConnector -> AnyBackend SourceObjId
forall a b. (a -> b) -> a -> b
$ forall (b :: BackendType). TableName b -> SourceObjId b
SOITable @'DataConnector (TableName -> TableName
forall target source. From source target => source -> target
Witch.into TableName
t)
          (Maybe FunctionReturnType, FunctionReturnType)
_ ->
            Code -> Text -> m SchemaObjId
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported
              (Text -> m SchemaObjId) -> Text -> m SchemaObjId
forall a b. (a -> b) -> a -> b
$ Text
"Function "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FunctionName -> Text
forall a. Show a => a -> Text
tshow FunctionName
funcName
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is missing a return type - This should be explicit in metadata, or inferred from agent"

      Seq (InputArgument FunctionArg)
inputArguments <- do
        let argNames :: [Text]
argNames = (FunctionArg -> Text) -> [FunctionArg] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map FunctionArg -> Text
API._faInputArgName [FunctionArg]
infoArgs
            invalidArgs :: [Text]
invalidArgs = (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Maybe Name -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe Name -> Bool) -> (Text -> Maybe Name) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe Name
GQL.mkName) [Text]
argNames
        Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
invalidArgs) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
"Invalid argument names: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Text] -> Text
forall a. Show a => a -> Text
tshow [Text]
invalidArgs
        -- Modified version of makeInputArguments from PG:
        case Maybe FunctionArgName
_fcSessionArgument of
          Maybe FunctionArgName
Nothing -> Seq (InputArgument FunctionArg)
-> m (Seq (InputArgument FunctionArg))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Seq (InputArgument FunctionArg)
 -> m (Seq (InputArgument FunctionArg)))
-> Seq (InputArgument FunctionArg)
-> m (Seq (InputArgument FunctionArg))
forall a b. (a -> b) -> a -> b
$ [InputArgument FunctionArg] -> Seq (InputArgument FunctionArg)
forall a. [a] -> Seq a
Seq.fromList ([InputArgument FunctionArg] -> Seq (InputArgument FunctionArg))
-> [InputArgument FunctionArg] -> Seq (InputArgument FunctionArg)
forall a b. (a -> b) -> a -> b
$ (FunctionArg -> InputArgument FunctionArg)
-> [FunctionArg] -> [InputArgument FunctionArg]
forall a b. (a -> b) -> [a] -> [b]
map FunctionArg -> InputArgument FunctionArg
forall a. a -> InputArgument a
IAUserProvided [FunctionArg]
infoArgs
          Just FunctionArgName
sessionArgName -> do
            Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((FunctionArg -> Bool) -> [FunctionArg] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\FunctionArg
arg -> FunctionArgName -> Text
getFuncArgNameTxt FunctionArgName
sessionArgName Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== FunctionArg -> Text
API._faInputArgName FunctionArg
arg) [FunctionArg]
infoArgs)
              (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported
              (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
"Session argument not mappable: "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FunctionArgName -> Text
forall a. Show a => a -> Text
tshow FunctionArgName
sessionArgName
            Seq (InputArgument FunctionArg)
-> m (Seq (InputArgument FunctionArg))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
              (Seq (InputArgument FunctionArg)
 -> m (Seq (InputArgument FunctionArg)))
-> Seq (InputArgument FunctionArg)
-> m (Seq (InputArgument FunctionArg))
forall a b. (a -> b) -> a -> b
$ [InputArgument FunctionArg] -> Seq (InputArgument FunctionArg)
forall a. [a] -> Seq a
Seq.fromList
              ([InputArgument FunctionArg] -> Seq (InputArgument FunctionArg))
-> [InputArgument FunctionArg] -> Seq (InputArgument FunctionArg)
forall a b. (a -> b) -> a -> b
$ ((FunctionArg -> InputArgument FunctionArg)
 -> [FunctionArg] -> [InputArgument FunctionArg])
-> [FunctionArg]
-> (FunctionArg -> InputArgument FunctionArg)
-> [InputArgument FunctionArg]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (FunctionArg -> InputArgument FunctionArg)
-> [FunctionArg] -> [InputArgument FunctionArg]
forall a b. (a -> b) -> [a] -> [b]
map [FunctionArg]
infoArgs
              ((FunctionArg -> InputArgument FunctionArg)
 -> [InputArgument FunctionArg])
-> (FunctionArg -> InputArgument FunctionArg)
-> [InputArgument FunctionArg]
forall a b. (a -> b) -> a -> b
$ \FunctionArg
arg ->
                if FunctionArgName -> Text
getFuncArgNameTxt FunctionArgName
sessionArgName Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== FunctionArg -> Text
API._faInputArgName FunctionArg
arg
                  then FunctionArgName -> InputArgument FunctionArg
forall a. FunctionArgName -> InputArgument a
IASessionVariables FunctionArgName
sessionArgName
                  else FunctionArg -> InputArgument FunctionArg
forall a. a -> InputArgument a
IAUserProvided FunctionArg
arg

      TableName
functionReturnType <- FunctionName
-> (Maybe (FunctionReturnType 'DataConnector), FunctionReturnType)
-> m TableName
forall (m :: * -> *).
MonadError QErr m =>
FunctionName
-> (Maybe (FunctionReturnType 'DataConnector), FunctionReturnType)
-> m TableName
functionReturnTypeFromAPI FunctionName
funcName (Maybe (FunctionReturnType 'DataConnector)
_fcResponse, FunctionReturnType
returnType)

      let funcInfo :: FunctionInfo 'DataConnector
funcInfo =
            FunctionInfo
              { _fiSQLName :: FunctionName 'DataConnector
_fiSQLName = FunctionName -> FunctionName
forall target source. From source target => source -> target
Witch.into FunctionName
infoName, -- Converts to DC.FunctionName
                _fiGQLName :: Name
_fiGQLName = Name -> FunctionConfig 'DataConnector -> (Name -> Name) -> Name
forall (b :: BackendType).
Name -> FunctionConfig b -> (Name -> Name) -> Name
getFunctionGQLName Name
funcGivenName FunctionConfig 'DataConnector
funcConfig Name -> Name
setNamingCase,
                _fiGQLArgsName :: Name
_fiGQLArgsName = Name -> FunctionConfig 'DataConnector -> (Name -> Name) -> Name
forall (b :: BackendType).
Name -> FunctionConfig b -> (Name -> Name) -> Name
getFunctionArgsGQLName Name
funcGivenName FunctionConfig 'DataConnector
funcConfig Name -> Name
setNamingCase,
                _fiGQLAggregateName :: Name
_fiGQLAggregateName = Name -> FunctionConfig 'DataConnector -> (Name -> Name) -> Name
forall (b :: BackendType).
Name -> FunctionConfig b -> (Name -> Name) -> Name
getFunctionAggregateGQLName Name
funcGivenName FunctionConfig 'DataConnector
funcConfig Name -> Name
setNamingCase,
                _fiSystemDefined :: SystemDefined
_fiSystemDefined = SystemDefined
sysDefined,
                _fiVolatility :: FunctionVolatility
_fiVolatility = FunctionVolatility
volitility,
                _fiExposedAs :: FunctionExposedAs
_fiExposedAs = FunctionExposedAs
exposeAs,
                _fiInputArgs :: Seq (FunctionInputArgument 'DataConnector)
_fiInputArgs = Seq (InputArgument FunctionArg)
Seq (FunctionInputArgument 'DataConnector)
inputArguments,
                _fiReturnType :: TableName 'DataConnector
_fiReturnType = TableName 'DataConnector
TableName
functionReturnType,
                _fiDescription :: Maybe Text
_fiDescription = Maybe Text
infoDesc,
                _fiPermissions :: FunctionPermissionsMap
_fiPermissions = FunctionPermissionsMap
permissionMap,
                _fiJsonAggSelect :: JsonAggSelect
_fiJsonAggSelect = FunctionArity -> JsonAggSelect
arityJsonAggSelect FunctionArity
infoSet,
                _fiComment :: Maybe Text
_fiComment = Maybe Text
funcComment
              }
      (FunctionInfo 'DataConnector, SchemaDependency)
-> m (FunctionInfo 'DataConnector, SchemaDependency)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((FunctionInfo 'DataConnector, SchemaDependency)
 -> m (FunctionInfo 'DataConnector, SchemaDependency))
-> (FunctionInfo 'DataConnector, SchemaDependency)
-> m (FunctionInfo 'DataConnector, SchemaDependency)
forall a b. (a -> b) -> a -> b
$ (FunctionInfo 'DataConnector
funcInfo, SchemaObjId -> DependencyReason -> SchemaDependency
SchemaDependency SchemaObjId
objid DependencyReason
DRTable)

resolveBackendInfo' ::
  forall arr m.
  ( ArrowChoice arr,
    Inc.ArrowCache m arr,
    Inc.ArrowDistribute arr,
    ArrowWriter (Seq CollectItem) arr,
    MonadIO m,
    MonadBaseControl IO m,
    ProvidesNetwork m
  ) =>
  Logger Hasura ->
  (Inc.Dependency (Maybe (HashMap DC.DataConnectorName Inc.InvalidationKey)), Map.Map DC.DataConnectorName DC.DataConnectorOptions) `arr` HashMap DC.DataConnectorName DC.DataConnectorInfo
resolveBackendInfo' :: forall (arr :: * -> * -> *) (m :: * -> *).
(ArrowChoice arr, ArrowCache m arr, ArrowDistribute arr,
 ArrowWriter (Seq CollectItem) arr, MonadIO m,
 MonadBaseControl IO m, ProvidesNetwork m) =>
Logger Hasura
-> arr
     (Dependency (Maybe (HashMap DataConnectorName InvalidationKey)),
      Map DataConnectorName DataConnectorOptions)
     (HashMap DataConnectorName DataConnectorInfo)
resolveBackendInfo' Logger Hasura
logger = proc (Dependency (Maybe (HashMap DataConnectorName InvalidationKey))
invalidationKeys, Map DataConnectorName DataConnectorOptions
optionsMap) -> do
  HashMap DataConnectorName (Maybe DataConnectorInfo)
maybeDataConnectorCapabilities <-
    (|
      arr
  (a, (DataConnectorName, (DataConnectorOptions, ())))
  (Maybe DataConnectorInfo)
-> arr
     (a, (HashMap DataConnectorName DataConnectorOptions, ()))
     (HashMap DataConnectorName (Maybe DataConnectorInfo))
forall {a}.
arr
  (a, (DataConnectorName, (DataConnectorOptions, ())))
  (Maybe DataConnectorInfo)
-> arr
     (a, (HashMap DataConnectorName DataConnectorOptions, ()))
     (HashMap DataConnectorName (Maybe DataConnectorInfo))
forall k e a s b.
Hashable k =>
arr (e, (k, (a, s))) b -> arr (e, (HashMap k a, s)) (HashMap k b)
forall (arr :: * -> * -> *) k e a s b.
(ArrowDistribute arr, Hashable k) =>
arr (e, (k, (a, s))) b -> arr (e, (HashMap k a, s)) (HashMap k b)
Inc.keyed
        ( \DataConnectorName
dataConnectorName DataConnectorOptions
dataConnectorOptions -> do
            arr
  (Dependency (Maybe (HashMap DataConnectorName InvalidationKey)),
   DataConnectorName, DataConnectorOptions)
  (Maybe DataConnectorInfo)
getDataConnectorCapabilitiesIfNeeded -< (Dependency (Maybe (HashMap DataConnectorName InvalidationKey))
invalidationKeys, DataConnectorName
dataConnectorName, DataConnectorOptions
dataConnectorOptions)
        )
      |)
      (Map DataConnectorName DataConnectorOptions
-> HashMap DataConnectorName DataConnectorOptions
forall {v}. Map DataConnectorName v -> HashMap DataConnectorName v
toHashMap Map DataConnectorName DataConnectorOptions
optionsMap)
  arr
  (HashMap DataConnectorName DataConnectorInfo)
  (HashMap DataConnectorName DataConnectorInfo)
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< HashMap DataConnectorName (Maybe DataConnectorInfo)
-> HashMap DataConnectorName DataConnectorInfo
forall k v. HashMap k (Maybe v) -> HashMap k v
HashMap.catMaybes HashMap DataConnectorName (Maybe DataConnectorInfo)
maybeDataConnectorCapabilities
  where
    getDataConnectorCapabilitiesIfNeeded ::
      (Inc.Dependency (Maybe (HashMap DC.DataConnectorName Inc.InvalidationKey)), DC.DataConnectorName, DC.DataConnectorOptions) `arr` Maybe DC.DataConnectorInfo
    getDataConnectorCapabilitiesIfNeeded :: arr
  (Dependency (Maybe (HashMap DataConnectorName InvalidationKey)),
   DataConnectorName, DataConnectorOptions)
  (Maybe DataConnectorInfo)
getDataConnectorCapabilitiesIfNeeded = arr
  (Dependency (Maybe (HashMap DataConnectorName InvalidationKey)),
   DataConnectorName, DataConnectorOptions)
  (Maybe DataConnectorInfo)
-> arr
     (Dependency (Maybe (HashMap DataConnectorName InvalidationKey)),
      DataConnectorName, DataConnectorOptions)
     (Maybe DataConnectorInfo)
forall a b. (Given Accesses => Eq a) => arr a b -> arr a b
forall (m :: * -> *) (arr :: * -> * -> *) a b.
(ArrowCache m arr, Given Accesses => Eq a) =>
arr a b -> arr a b
Inc.cache proc (Dependency (Maybe (HashMap DataConnectorName InvalidationKey))
invalidationKeys, DataConnectorName
dataConnectorName, DataConnectorOptions
dataConnectorOptions) -> do
      let metadataObj :: MetadataObject
metadataObj = MetadataObjId -> Value -> MetadataObject
MetadataObject (DataConnectorName -> MetadataObjId
MODataConnectorAgent DataConnectorName
dataConnectorName) (Value -> MetadataObject) -> Value -> MetadataObject
forall a b. (a -> b) -> a -> b
$ DataConnectorName -> Value
forall a. ToJSON a => a -> Value
J.toJSON DataConnectorName
dataConnectorName
      Manager
httpMgr <- arr (m Manager) Manager
forall (m :: * -> *) (arr :: * -> * -> *) a.
ArrowKleisli m arr =>
arr (m a) a
bindA -< m Manager
forall (m :: * -> *). ProvidesNetwork m => m Manager
askHTTPManager
      arr
  (Dependency (Maybe (Maybe InvalidationKey)))
  (Maybe (Maybe InvalidationKey))
forall a. Eq a => arr (Dependency a) a
forall (m :: * -> *) (arr :: * -> * -> *) a.
(ArrowCache m arr, Eq a) =>
arr (Dependency a) a
Inc.dependOn -< Selector
  (HashMap DataConnectorName InvalidationKey) (Maybe InvalidationKey)
-> Dependency (Maybe (HashMap DataConnectorName InvalidationKey))
-> Dependency (Maybe (Maybe InvalidationKey))
forall a b.
Select a =>
Selector a b -> Dependency (Maybe a) -> Dependency (Maybe b)
Inc.selectMaybeD (DataConnectorName
-> ConstS
     DataConnectorName (Maybe InvalidationKey) (Maybe InvalidationKey)
forall {k} k1 (a :: k). k1 -> ConstS k1 a a
Inc.ConstS DataConnectorName
dataConnectorName) Dependency (Maybe (HashMap DataConnectorName InvalidationKey))
invalidationKeys
      Maybe (Maybe DataConnectorInfo)
maybeDcInfo <- (| ErrorA QErr arr (a, ()) (Maybe DataConnectorInfo)
-> arr (a, (MetadataObject, ())) (Maybe (Maybe DataConnectorInfo))
forall {a}.
ErrorA QErr arr (a, ()) (Maybe DataConnectorInfo)
-> arr (a, (MetadataObject, ())) (Maybe (Maybe DataConnectorInfo))
forall (arr :: * -> * -> *) e s a.
(ArrowChoice arr, ArrowWriter (Seq CollectItem) arr) =>
ErrorA QErr arr (e, s) a -> arr (e, (MetadataObject, s)) (Maybe a)
withRecordInconsistency (ErrorA
  QErr
  arr
  (ExceptT QErr m (Maybe DataConnectorInfo))
  (Maybe DataConnectorInfo)
forall (arr :: * -> * -> *) (m :: * -> *) e a.
(ArrowChoice arr, ArrowKleisli m arr, ArrowError e arr) =>
arr (ExceptT e m a) a
bindErrorA -< DataConnectorOptions
-> Manager -> ExceptT QErr m (Maybe DataConnectorInfo)
getDataConnectorCapabilities DataConnectorOptions
dataConnectorOptions Manager
httpMgr) |) MetadataObject
metadataObj
      arr (Maybe DataConnectorInfo) (Maybe DataConnectorInfo)
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< Maybe (Maybe DataConnectorInfo) -> Maybe DataConnectorInfo
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join Maybe (Maybe DataConnectorInfo)
maybeDcInfo

    getDataConnectorCapabilities ::
      DC.DataConnectorOptions ->
      HTTP.Manager ->
      ExceptT QErr m (Maybe DC.DataConnectorInfo)
    getDataConnectorCapabilities :: DataConnectorOptions
-> Manager -> ExceptT QErr m (Maybe DataConnectorInfo)
getDataConnectorCapabilities options :: DataConnectorOptions
options@DC.DataConnectorOptions {Maybe Text
BaseUrl
_dcoUri :: BaseUrl
_dcoDisplayName :: Maybe Text
_dcoUri :: DataConnectorOptions -> BaseUrl
_dcoDisplayName :: DataConnectorOptions -> Maybe Text
..} Manager
manager =
      ( TraceT (ExceptT QErr m) (Maybe DataConnectorInfo)
-> ExceptT QErr m (Maybe DataConnectorInfo)
forall (m :: * -> *) a. TraceT m a -> m a
ignoreTraceT
          (TraceT (ExceptT QErr m) (Maybe DataConnectorInfo)
 -> ExceptT QErr m (Maybe DataConnectorInfo))
-> (AgentClientT
      (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
    -> TraceT (ExceptT QErr m) (Maybe DataConnectorInfo))
-> AgentClientT (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
-> ExceptT QErr m (Maybe DataConnectorInfo)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AgentClientT (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
 -> AgentClientContext
 -> TraceT (ExceptT QErr m) (Maybe DataConnectorInfo))
-> AgentClientContext
-> AgentClientT (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
-> TraceT (ExceptT QErr m) (Maybe DataConnectorInfo)
forall a b c. (a -> b -> c) -> b -> a -> c
flip AgentClientT (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
-> AgentClientContext
-> TraceT (ExceptT QErr m) (Maybe DataConnectorInfo)
forall (m :: * -> *) a.
AgentClientT m a -> AgentClientContext -> m a
runAgentClientT (Logger Hasura
-> BaseUrl
-> Manager
-> Maybe Int
-> Maybe AgentLicenseKey
-> AgentClientContext
AgentClientContext Logger Hasura
logger BaseUrl
_dcoUri Manager
manager Maybe Int
forall a. Maybe a
Nothing Maybe AgentLicenseKey
forall a. Maybe a
Nothing)
          (AgentClientT (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
 -> ExceptT QErr m (Maybe DataConnectorInfo))
-> AgentClientT (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
-> ExceptT QErr m (Maybe DataConnectorInfo)
forall a b. (a -> b) -> a -> b
$ (DataConnectorInfo -> Maybe DataConnectorInfo
forall a. a -> Maybe a
Just (DataConnectorInfo -> Maybe DataConnectorInfo)
-> (CapabilitiesResponse -> DataConnectorInfo)
-> CapabilitiesResponse
-> Maybe DataConnectorInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataConnectorOptions -> CapabilitiesResponse -> DataConnectorInfo
mkDataConnectorInfo DataConnectorOptions
options)
          (CapabilitiesResponse -> Maybe DataConnectorInfo)
-> AgentClientT (TraceT (ExceptT QErr m)) CapabilitiesResponse
-> AgentClientT (TraceT (ExceptT QErr m)) (Maybe DataConnectorInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AgentClientT (TraceT (ExceptT QErr m)) CapabilitiesResponse
forall (m :: * -> *).
(MonadIO m, MonadTrace m, MonadError QErr m) =>
AgentClientT m CapabilitiesResponse
Client.capabilities
      )
        ExceptT QErr m (Maybe DataConnectorInfo)
-> (QErr -> ExceptT QErr m (Maybe DataConnectorInfo))
-> ExceptT QErr m (Maybe DataConnectorInfo)
forall a.
ExceptT QErr m a -> (QErr -> ExceptT QErr m a) -> ExceptT QErr m a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` QErr -> ExceptT QErr m (Maybe DataConnectorInfo)
forall a. QErr -> ExceptT QErr m (Maybe a)
ignoreConnectionErrors

    -- If we can't connect to a data connector agent to get its capabilities
    -- we don't throw an error, we just return Nothing, which means the agent is in a broken state
    -- but we don't fail the schema cache building process with metadata inconsistencies if the
    -- agent isn't in use. If the agent is in use, when we go to consume its 'DC.DataConnectorInfo'
    -- later, it will be missing and we'll throw an error then.
    ignoreConnectionErrors :: QErr -> ExceptT QErr m (Maybe a)
    ignoreConnectionErrors :: forall a. QErr -> ExceptT QErr m (Maybe a)
ignoreConnectionErrors err :: QErr
err@QErr {JSONPath
Maybe QErrExtra
Text
Code
Status
qePath :: JSONPath
qeStatus :: Status
qeError :: Text
qeCode :: Code
qeInternal :: Maybe QErrExtra
qePath :: QErr -> JSONPath
qeStatus :: QErr -> Status
qeError :: QErr -> Text
qeCode :: QErr -> Code
qeInternal :: QErr -> Maybe QErrExtra
..} =
      if Code
qeCode Code -> Code -> Bool
forall a. Eq a => a -> a -> Bool
== Code
ConnectionNotEstablished
        then Maybe a -> ExceptT QErr m (Maybe a)
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
        else QErr -> ExceptT QErr m (Maybe a)
forall a. QErr -> ExceptT QErr m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError QErr
err

    mkDataConnectorInfo :: DC.DataConnectorOptions -> API.CapabilitiesResponse -> DC.DataConnectorInfo
    mkDataConnectorInfo :: DataConnectorOptions -> CapabilitiesResponse -> DataConnectorInfo
mkDataConnectorInfo DataConnectorOptions
options API.CapabilitiesResponse {Maybe Text
ConfigSchemaResponse
Capabilities
_crCapabilities :: Capabilities
_crConfigSchemaResponse :: ConfigSchemaResponse
_crDisplayName :: Maybe Text
_crReleaseName :: Maybe Text
_crCapabilities :: CapabilitiesResponse -> Capabilities
_crConfigSchemaResponse :: CapabilitiesResponse -> ConfigSchemaResponse
_crDisplayName :: CapabilitiesResponse -> Maybe Text
_crReleaseName :: CapabilitiesResponse -> Maybe Text
..} =
      DataConnectorOptions
-> Capabilities
-> ConfigSchemaResponse
-> Maybe Text
-> Maybe Text
-> DataConnectorInfo
DC.DataConnectorInfo DataConnectorOptions
options Capabilities
_crCapabilities ConfigSchemaResponse
_crConfigSchemaResponse Maybe Text
_crDisplayName Maybe Text
_crReleaseName

    toHashMap :: Map DataConnectorName v -> HashMap DataConnectorName v
toHashMap = [(DataConnectorName, v)] -> HashMap DataConnectorName v
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(DataConnectorName, v)] -> HashMap DataConnectorName v)
-> (Map DataConnectorName v -> [(DataConnectorName, v)])
-> Map DataConnectorName v
-> HashMap DataConnectorName v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map DataConnectorName v -> [(DataConnectorName, v)]
forall k a. Map k a -> [(k, a)]
Map.toList

resolveSourceConfig' ::
  (Monad m) =>
  SourceName ->
  DC.ConnSourceConfig ->
  BackendSourceKind 'DataConnector ->
  HashMap DC.DataConnectorName DC.DataConnectorInfo ->
  Environment ->
  HTTP.Manager ->
  m (Either QErr DC.SourceConfig)
resolveSourceConfig' :: forall (m :: * -> *).
Monad m =>
SourceName
-> ConnSourceConfig
-> BackendSourceKind 'DataConnector
-> HashMap DataConnectorName DataConnectorInfo
-> Environment
-> Manager
-> m (Either QErr SourceConfig)
resolveSourceConfig'
  SourceName
sourceName
  csc :: ConnSourceConfig
csc@DC.ConnSourceConfig {Maybe Text
template :: Maybe Text
template :: ConnSourceConfig -> Maybe Text
template, Maybe SourceTimeout
timeout :: Maybe SourceTimeout
timeout :: ConnSourceConfig -> Maybe SourceTimeout
timeout, value :: ConnSourceConfig -> Config
value = Config
originalConfig}
  (DataConnectorKind DataConnectorName
dataConnectorName)
  HashMap DataConnectorName DataConnectorInfo
backendInfo
  Environment
env
  Manager
manager = ExceptT QErr m SourceConfig -> m (Either QErr SourceConfig)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT do
    DC.DataConnectorInfo {_dciOptions :: DataConnectorInfo -> DataConnectorOptions
_dciOptions = DC.DataConnectorOptions {BaseUrl
_dcoUri :: DataConnectorOptions -> BaseUrl
_dcoUri :: BaseUrl
_dcoUri}, Maybe Text
ConfigSchemaResponse
Capabilities
_dciCapabilities :: Capabilities
_dciConfigSchemaResponse :: ConfigSchemaResponse
_dciDisplayName :: Maybe Text
_dciReleaseName :: Maybe Text
_dciCapabilities :: DataConnectorInfo -> Capabilities
_dciConfigSchemaResponse :: DataConnectorInfo -> ConfigSchemaResponse
_dciDisplayName :: DataConnectorInfo -> Maybe Text
_dciReleaseName :: DataConnectorInfo -> Maybe Text
..} <- DataConnectorName
-> HashMap DataConnectorName DataConnectorInfo
-> ExceptT QErr m DataConnectorInfo
forall (m :: * -> *).
MonadError QErr m =>
DataConnectorName
-> HashMap DataConnectorName DataConnectorInfo
-> m DataConnectorInfo
getDataConnectorInfo DataConnectorName
dataConnectorName HashMap DataConnectorName DataConnectorInfo
backendInfo

    DataConnectorName
-> SourceName
-> ConfigSchemaResponse
-> ConnSourceConfig
-> Maybe SessionVariables
-> Environment
-> ExceptT QErr m ()
forall (m :: * -> *).
MonadError QErr m =>
DataConnectorName
-> SourceName
-> ConfigSchemaResponse
-> ConnSourceConfig
-> Maybe SessionVariables
-> Environment
-> m ()
validateConnSourceConfig DataConnectorName
dataConnectorName SourceName
sourceName ConfigSchemaResponse
_dciConfigSchemaResponse ConnSourceConfig
csc Maybe SessionVariables
forall a. Maybe a
Nothing Environment
env

    SourceConfig -> ExceptT QErr m SourceConfig
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      DC.SourceConfig
        { _scEndpoint :: BaseUrl
_scEndpoint = BaseUrl
_dcoUri,
          _scConfig :: Config
_scConfig = Config
originalConfig,
          _scTemplate :: Maybe Text
_scTemplate = Maybe Text
template,
          _scCapabilities :: Capabilities
_scCapabilities = Capabilities
_dciCapabilities,
          _scManager :: Manager
_scManager = Manager
manager,
          _scTimeoutMicroseconds :: Maybe Int
_scTimeoutMicroseconds = (SourceTimeout -> Int
DC.sourceTimeoutMicroseconds (SourceTimeout -> Int) -> Maybe SourceTimeout -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe SourceTimeout
timeout),
          _scDataConnectorName :: DataConnectorName
_scDataConnectorName = DataConnectorName
dataConnectorName,
          _scEnvironment :: Environment
_scEnvironment = Environment
env
        }

getDataConnectorInfo :: (MonadError QErr m) => DC.DataConnectorName -> HashMap DC.DataConnectorName DC.DataConnectorInfo -> m DC.DataConnectorInfo
getDataConnectorInfo :: forall (m :: * -> *).
MonadError QErr m =>
DataConnectorName
-> HashMap DataConnectorName DataConnectorInfo
-> m DataConnectorInfo
getDataConnectorInfo DataConnectorName
dataConnectorName HashMap DataConnectorName DataConnectorInfo
backendInfo =
  Maybe DataConnectorInfo
-> m DataConnectorInfo -> m DataConnectorInfo
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
onNothing (DataConnectorName
-> HashMap DataConnectorName DataConnectorInfo
-> Maybe DataConnectorInfo
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup DataConnectorName
dataConnectorName HashMap DataConnectorName DataConnectorInfo
backendInfo)
    (m DataConnectorInfo -> m DataConnectorInfo)
-> m DataConnectorInfo -> m DataConnectorInfo
forall a b. (a -> b) -> a -> b
$ Code -> Text -> m DataConnectorInfo
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
DataConnectorError (Text
"Data connector named " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> DataConnectorName -> Text
forall a. ToTxt a => a -> Text
toTxt DataConnectorName
dataConnectorName Text -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" was not found in the data connector backend info")

mkRawColumnType :: API.ColumnType -> RQL.T.C.RawColumnType 'DataConnector
mkRawColumnType :: ColumnType -> RawColumnType 'DataConnector
mkRawColumnType = \case
  API.ColumnTypeScalar ScalarType
scalarType -> ScalarType 'DataConnector -> RawColumnType 'DataConnector
forall (b :: BackendType). ScalarType b -> RawColumnType b
RQL.T.C.RawColumnTypeScalar (ScalarType 'DataConnector -> RawColumnType 'DataConnector)
-> ScalarType 'DataConnector -> RawColumnType 'DataConnector
forall a b. (a -> b) -> a -> b
$ ScalarType -> ScalarType
forall source target. From source target => source -> target
Witch.from ScalarType
scalarType
  API.ColumnTypeObject Name
name -> XNestedObjects 'DataConnector
-> Name -> RawColumnType 'DataConnector
forall (b :: BackendType).
XNestedObjects b -> Name -> RawColumnType b
RQL.T.C.RawColumnTypeObject () Name
name
  API.ColumnTypeArray ColumnType
columnType Bool
isNullable -> XNestedObjects 'DataConnector
-> RawColumnType 'DataConnector
-> Bool
-> RawColumnType 'DataConnector
forall (b :: BackendType).
XNestedObjects b -> RawColumnType b -> Bool -> RawColumnType b
RQL.T.C.RawColumnTypeArray () (ColumnType -> RawColumnType 'DataConnector
mkRawColumnType ColumnType
columnType) Bool
isNullable

resolveDatabaseMetadata' ::
  ( MonadIO m,
    MonadBaseControl IO m
  ) =>
  Logger Hasura ->
  SourceMetadata 'DataConnector ->
  DC.SourceConfig ->
  m (Either QErr (DBObjectsIntrospection 'DataConnector))
resolveDatabaseMetadata' :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
Logger Hasura
-> SourceMetadata 'DataConnector
-> SourceConfig
-> m (Either QErr (DBObjectsIntrospection 'DataConnector))
resolveDatabaseMetadata' Logger Hasura
logger SourceMetadata {SourceName
_smName :: SourceName
_smName :: forall (b :: BackendType). SourceMetadata b -> SourceName
_smName} SourceConfig
sourceConfig = ExceptT QErr m (DBObjectsIntrospection 'DataConnector)
-> m (Either QErr (DBObjectsIntrospection 'DataConnector))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT do
  API.SchemaResponse {[TableInfo]
[FunctionInfo]
Maybe (NonEmpty ObjectTypeDefinition)
_srTables :: [TableInfo]
_srFunctions :: [FunctionInfo]
_srObjectTypes :: Maybe (NonEmpty ObjectTypeDefinition)
_srTables :: SchemaResponse -> [TableInfo]
_srFunctions :: SchemaResponse -> [FunctionInfo]
_srObjectTypes :: SchemaResponse -> Maybe (NonEmpty ObjectTypeDefinition)
..} <- Logger Hasura
-> SourceName -> SourceConfig -> ExceptT QErr m SchemaResponse
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
Logger Hasura -> SourceName -> SourceConfig -> m SchemaResponse
requestDatabaseSchema Logger Hasura
logger SourceName
_smName SourceConfig
sourceConfig
  let logicalModels :: InsOrdHashMap
  LogicalModelName (LogicalModelMetadata 'DataConnector)
logicalModels =
        InsOrdHashMap
  LogicalModelName (LogicalModelMetadata 'DataConnector)
-> (NonEmpty ObjectTypeDefinition
    -> InsOrdHashMap
         LogicalModelName (LogicalModelMetadata 'DataConnector))
-> Maybe (NonEmpty ObjectTypeDefinition)
-> InsOrdHashMap
     LogicalModelName (LogicalModelMetadata 'DataConnector)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe InsOrdHashMap
  LogicalModelName (LogicalModelMetadata 'DataConnector)
forall a. Monoid a => a
mempty ([(LogicalModelName, LogicalModelMetadata 'DataConnector)]
-> InsOrdHashMap
     LogicalModelName (LogicalModelMetadata 'DataConnector)
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
InsOrdHashMap.fromList ([(LogicalModelName, LogicalModelMetadata 'DataConnector)]
 -> InsOrdHashMap
      LogicalModelName (LogicalModelMetadata 'DataConnector))
-> (NonEmpty ObjectTypeDefinition
    -> [(LogicalModelName, LogicalModelMetadata 'DataConnector)])
-> NonEmpty ObjectTypeDefinition
-> InsOrdHashMap
     LogicalModelName (LogicalModelMetadata 'DataConnector)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ObjectTypeDefinition
 -> (LogicalModelName, LogicalModelMetadata 'DataConnector))
-> [ObjectTypeDefinition]
-> [(LogicalModelName, LogicalModelMetadata 'DataConnector)]
forall a b. (a -> b) -> [a] -> [b]
map ObjectTypeDefinition
-> (LogicalModelName, LogicalModelMetadata 'DataConnector)
toLogicalModelMetadata ([ObjectTypeDefinition]
 -> [(LogicalModelName, LogicalModelMetadata 'DataConnector)])
-> (NonEmpty ObjectTypeDefinition -> [ObjectTypeDefinition])
-> NonEmpty ObjectTypeDefinition
-> [(LogicalModelName, LogicalModelMetadata 'DataConnector)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty ObjectTypeDefinition -> [ObjectTypeDefinition]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList) Maybe (NonEmpty ObjectTypeDefinition)
_srObjectTypes
      tables :: HashMap TableName (DBTableMetadata 'DataConnector)
tables = [(TableName, DBTableMetadata 'DataConnector)]
-> HashMap TableName (DBTableMetadata 'DataConnector)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(TableName, DBTableMetadata 'DataConnector)]
 -> HashMap TableName (DBTableMetadata 'DataConnector))
-> [(TableName, DBTableMetadata 'DataConnector)]
-> HashMap TableName (DBTableMetadata 'DataConnector)
forall a b. (a -> b) -> a -> b
$ do
        API.TableInfo {Bool
[ColumnInfo]
Maybe (NonEmpty ColumnName)
Maybe Text
ForeignKeys
TableType
TableName
_tiName :: TableName
_tiType :: TableType
_tiColumns :: [ColumnInfo]
_tiPrimaryKey :: Maybe (NonEmpty ColumnName)
_tiForeignKeys :: ForeignKeys
_tiDescription :: Maybe Text
_tiInsertable :: Bool
_tiUpdatable :: Bool
_tiDeletable :: Bool
_tiName :: TableInfo -> TableName
_tiType :: TableInfo -> TableType
_tiColumns :: TableInfo -> [ColumnInfo]
_tiPrimaryKey :: TableInfo -> Maybe (NonEmpty ColumnName)
_tiForeignKeys :: TableInfo -> ForeignKeys
_tiDescription :: TableInfo -> Maybe Text
_tiInsertable :: TableInfo -> Bool
_tiUpdatable :: TableInfo -> Bool
_tiDeletable :: TableInfo -> Bool
..} <- [TableInfo]
_srTables
        let primaryKeyColumns :: Maybe (NESeq ColumnName)
primaryKeyColumns = (ColumnName -> ColumnName) -> NESeq ColumnName -> NESeq ColumnName
forall a b. (a -> b) -> NESeq a -> NESeq 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 (NESeq ColumnName -> NESeq ColumnName)
-> (NonEmpty ColumnName -> NESeq ColumnName)
-> NonEmpty ColumnName
-> NESeq ColumnName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty ColumnName -> NESeq ColumnName
forall a. NonEmpty a -> NESeq a
NESeq.fromList (NonEmpty ColumnName -> NESeq ColumnName)
-> Maybe (NonEmpty ColumnName) -> Maybe (NESeq ColumnName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NonEmpty ColumnName)
_tiPrimaryKey
        let meta :: DBTableMetadata 'DataConnector
meta =
              RQL.T.T.DBTableMetadata
                { _ptmiOid :: OID
_ptmiOid = Int -> OID
OID Int
0, -- TODO: This is wrong and needs to be fixed. It is used for diffing tables and seeing what's new/deleted/altered, so reusing 0 for all tables is problematic.
                  _ptmiColumns :: [RawColumnInfo 'DataConnector]
_ptmiColumns = do
                    API.ColumnInfo {Bool
Maybe Text
Maybe ColumnValueGenerationStrategy
ColumnType
ColumnName
_ciName :: ColumnName
_ciType :: ColumnType
_ciNullable :: Bool
_ciDescription :: Maybe Text
_ciInsertable :: Bool
_ciUpdatable :: Bool
_ciValueGenerated :: Maybe ColumnValueGenerationStrategy
_ciName :: ColumnInfo -> ColumnName
_ciType :: ColumnInfo -> ColumnType
_ciNullable :: ColumnInfo -> Bool
_ciDescription :: ColumnInfo -> Maybe Text
_ciInsertable :: ColumnInfo -> Bool
_ciUpdatable :: ColumnInfo -> Bool
_ciValueGenerated :: ColumnInfo -> Maybe ColumnValueGenerationStrategy
..} <- [ColumnInfo]
_tiColumns
                    RawColumnInfo 'DataConnector -> [RawColumnInfo 'DataConnector]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
                      (RawColumnInfo 'DataConnector -> [RawColumnInfo 'DataConnector])
-> RawColumnInfo 'DataConnector -> [RawColumnInfo 'DataConnector]
forall a b. (a -> b) -> a -> b
$ RQL.T.C.RawColumnInfo
                        { rciName :: Column 'DataConnector
rciName = ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from ColumnName
_ciName,
                          rciPosition :: Int
rciPosition = Int
1, -- TODO: This is very wrong and needs to be fixed. It is used for diffing tables and seeing what's new/deleted/altered, so reusing 1 for all columns is problematic.
                          rciType :: RawColumnType 'DataConnector
rciType = ColumnType -> RawColumnType 'DataConnector
mkRawColumnType ColumnType
_ciType,
                          rciIsNullable :: Bool
rciIsNullable = Bool
_ciNullable,
                          rciDescription :: Maybe Description
rciDescription = (Text -> Description) -> Maybe Text -> Maybe Description
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Description
GQL.Description Maybe Text
_ciDescription,
                          rciMutability :: ColumnMutability
rciMutability = Bool -> Bool -> ColumnMutability
RQL.T.C.ColumnMutability Bool
_ciInsertable Bool
_ciUpdatable
                        },
                  _ptmiPrimaryKey :: Maybe (PrimaryKey 'DataConnector (Column 'DataConnector))
_ptmiPrimaryKey = Constraint 'DataConnector
-> NESeq ColumnName -> PrimaryKey 'DataConnector ColumnName
forall (b :: BackendType) a.
Constraint b -> NESeq a -> PrimaryKey b a
RQL.T.T.PrimaryKey (ConstraintName 'DataConnector -> OID -> Constraint 'DataConnector
forall (b :: BackendType). ConstraintName b -> OID -> Constraint b
RQL.T.T.Constraint (Text -> ConstraintName
DC.ConstraintName Text
"") (Int -> OID
OID Int
0)) (NESeq ColumnName -> PrimaryKey 'DataConnector ColumnName)
-> Maybe (NESeq ColumnName)
-> Maybe (PrimaryKey 'DataConnector ColumnName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NESeq ColumnName)
primaryKeyColumns,
                  _ptmiUniqueConstraints :: HashSet (UniqueConstraint 'DataConnector)
_ptmiUniqueConstraints = HashSet (UniqueConstraint 'DataConnector)
forall a. Monoid a => a
mempty,
                  _ptmiForeignKeys :: HashSet (ForeignKeyMetadata 'DataConnector)
_ptmiForeignKeys = ForeignKeys -> HashSet (ForeignKeyMetadata 'DataConnector)
buildForeignKeySet ForeignKeys
_tiForeignKeys,
                  _ptmiViewInfo :: Maybe ViewInfo
_ptmiViewInfo =
                    ( if TableType
_tiType TableType -> TableType -> Bool
forall a. Eq a => a -> a -> Bool
== TableType
API.Table Bool -> Bool -> Bool
&& Bool
_tiInsertable Bool -> Bool -> Bool
&& Bool
_tiUpdatable Bool -> Bool -> Bool
&& Bool
_tiDeletable
                        then Maybe ViewInfo
forall a. Maybe a
Nothing
                        else ViewInfo -> Maybe ViewInfo
forall a. a -> Maybe a
Just (ViewInfo -> Maybe ViewInfo) -> ViewInfo -> Maybe ViewInfo
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> Bool -> ViewInfo
RQL.T.T.ViewInfo Bool
_tiInsertable Bool
_tiUpdatable Bool
_tiDeletable
                    ),
                  _ptmiDescription :: Maybe PGDescription
_ptmiDescription = (Text -> PGDescription) -> Maybe Text -> Maybe PGDescription
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> PGDescription
PGDescription Maybe Text
_tiDescription,
                  _ptmiExtraTableMetadata :: ExtraTableMetadata 'DataConnector
_ptmiExtraTableMetadata =
                    DC.ExtraTableMetadata
                      { _etmTableType :: TableType
_etmTableType = TableType
_tiType,
                        _etmExtraColumnMetadata :: HashMap ColumnName ExtraColumnMetadata
_etmExtraColumnMetadata =
                          [ColumnInfo]
_tiColumns
                            [ColumnInfo]
-> ([ColumnInfo] -> [(ColumnName, ExtraColumnMetadata)])
-> [(ColumnName, ExtraColumnMetadata)]
forall a b. a -> (a -> b) -> b
& (ColumnInfo -> (ColumnName, ExtraColumnMetadata))
-> [ColumnInfo] -> [(ColumnName, ExtraColumnMetadata)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\API.ColumnInfo {Bool
Maybe Text
Maybe ColumnValueGenerationStrategy
ColumnType
ColumnName
_ciName :: ColumnInfo -> ColumnName
_ciType :: ColumnInfo -> ColumnType
_ciNullable :: ColumnInfo -> Bool
_ciDescription :: ColumnInfo -> Maybe Text
_ciInsertable :: ColumnInfo -> Bool
_ciUpdatable :: ColumnInfo -> Bool
_ciValueGenerated :: ColumnInfo -> Maybe ColumnValueGenerationStrategy
_ciName :: ColumnName
_ciType :: ColumnType
_ciNullable :: Bool
_ciDescription :: Maybe Text
_ciInsertable :: Bool
_ciUpdatable :: Bool
_ciValueGenerated :: Maybe ColumnValueGenerationStrategy
..} -> (ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from ColumnName
_ciName, Maybe ColumnValueGenerationStrategy -> ExtraColumnMetadata
DC.ExtraColumnMetadata Maybe ColumnValueGenerationStrategy
_ciValueGenerated))
                            [(ColumnName, ExtraColumnMetadata)]
-> ([(ColumnName, ExtraColumnMetadata)]
    -> HashMap ColumnName ExtraColumnMetadata)
-> HashMap ColumnName ExtraColumnMetadata
forall a b. a -> (a -> b) -> b
& [(ColumnName, ExtraColumnMetadata)]
-> HashMap ColumnName ExtraColumnMetadata
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
                      }
                }
        (TableName, DBTableMetadata 'DataConnector)
-> [(TableName, DBTableMetadata 'DataConnector)]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TableName -> TableName
forall target source. From source target => source -> target
Witch.into TableName
_tiName, DBTableMetadata 'DataConnector
meta)
      functions :: HashMap FunctionName (FunctionOverloads 'DataConnector)
functions =
        let sorted :: [FunctionInfo]
sorted = (FunctionInfo -> FunctionName) -> [FunctionInfo] -> [FunctionInfo]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn FunctionInfo -> FunctionName
_fiName [FunctionInfo]
_srFunctions
            grouped :: [NonEmpty FunctionInfo]
grouped = (FunctionInfo -> FunctionInfo -> Bool)
-> [FunctionInfo] -> [NonEmpty FunctionInfo]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
NEList.groupBy (FunctionName -> FunctionName -> Bool
forall a. Eq a => a -> a -> Bool
(==) (FunctionName -> FunctionName -> Bool)
-> (FunctionInfo -> FunctionName)
-> FunctionInfo
-> FunctionInfo
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` FunctionInfo -> FunctionName
_fiName) [FunctionInfo]
sorted
         in [(FunctionName, FunctionOverloads 'DataConnector)]
-> HashMap FunctionName (FunctionOverloads 'DataConnector)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList do
              infos :: NonEmpty FunctionInfo
infos@(API.FunctionInfo {[FunctionArg]
Maybe Text
FunctionArity
FunctionReturnType
FunctionType
FunctionName
_fiDescription :: FunctionInfo -> Maybe Text
_fiName :: FunctionInfo -> FunctionName
_fiName :: FunctionName
_fiFunctionType :: FunctionType
_fiReturns :: FunctionReturnType
_fiResponseCardinality :: FunctionArity
_fiInputArgs :: [FunctionArg]
_fiDescription :: Maybe Text
_fiFunctionType :: FunctionInfo -> FunctionType
_fiReturns :: FunctionInfo -> FunctionReturnType
_fiResponseCardinality :: FunctionInfo -> FunctionArity
_fiInputArgs :: FunctionInfo -> [FunctionArg]
..} NEList.:| [FunctionInfo]
_) <- [NonEmpty FunctionInfo]
grouped
              (FunctionName, FunctionOverloads 'DataConnector)
-> [(FunctionName, FunctionOverloads 'DataConnector)]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FunctionName -> FunctionName
forall target source. From source target => source -> target
Witch.into FunctionName
_fiName, NonEmpty (RawFunctionInfo 'DataConnector)
-> FunctionOverloads 'DataConnector
forall (b :: BackendType).
NonEmpty (RawFunctionInfo b) -> FunctionOverloads b
FunctionOverloads NonEmpty FunctionInfo
NonEmpty (RawFunctionInfo 'DataConnector)
infos)
   in DBObjectsIntrospection 'DataConnector
-> ExceptT QErr m (DBObjectsIntrospection 'DataConnector)
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        DBObjectsIntrospection
          { _rsTables :: DBTablesMetadata 'DataConnector
_rsTables = DBTablesMetadata 'DataConnector
HashMap TableName (DBTableMetadata 'DataConnector)
tables,
            _rsFunctions :: DBFunctionsMetadata 'DataConnector
_rsFunctions = DBFunctionsMetadata 'DataConnector
HashMap FunctionName (FunctionOverloads 'DataConnector)
functions,
            _rsScalars :: ScalarMap 'DataConnector
_rsScalars = ScalarMap 'DataConnector
forall a. Monoid a => a
mempty,
            _rsLogicalModels :: InsOrdHashMap
  LogicalModelName (LogicalModelMetadata 'DataConnector)
_rsLogicalModels = InsOrdHashMap
  LogicalModelName (LogicalModelMetadata 'DataConnector)
logicalModels
          }

requestDatabaseSchema ::
  (MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
  Logger Hasura ->
  SourceName ->
  DC.SourceConfig ->
  m API.SchemaResponse
requestDatabaseSchema :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
Logger Hasura -> SourceName -> SourceConfig -> m SchemaResponse
requestDatabaseSchema Logger Hasura
logger SourceName
sourceName SourceConfig
sourceConfig = do
  SourceConfig
transformedSourceConfig <- SourceConfig -> Maybe SessionVariables -> m SourceConfig
forall (m :: * -> *).
MonadError QErr m =>
SourceConfig -> Maybe SessionVariables -> m SourceConfig
transformSourceConfig SourceConfig
sourceConfig Maybe SessionVariables
forall a. Maybe a
Nothing
  TraceT m SchemaResponse -> m SchemaResponse
forall (m :: * -> *) a. TraceT m a -> m a
ignoreTraceT
    (TraceT m SchemaResponse -> m SchemaResponse)
-> (AgentClientT (TraceT m) SchemaResponse
    -> TraceT m SchemaResponse)
-> AgentClientT (TraceT m) SchemaResponse
-> m SchemaResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AgentClientT (TraceT m) SchemaResponse
 -> AgentClientContext -> TraceT m SchemaResponse)
-> AgentClientContext
-> AgentClientT (TraceT m) SchemaResponse
-> TraceT m SchemaResponse
forall a b c. (a -> b -> c) -> b -> a -> c
flip AgentClientT (TraceT m) SchemaResponse
-> AgentClientContext -> TraceT m SchemaResponse
forall (m :: * -> *) a.
AgentClientT m a -> AgentClientContext -> m a
runAgentClientT (Logger Hasura
-> BaseUrl
-> Manager
-> Maybe Int
-> Maybe AgentLicenseKey
-> AgentClientContext
AgentClientContext Logger Hasura
logger (SourceConfig -> BaseUrl
DC._scEndpoint SourceConfig
transformedSourceConfig) (SourceConfig -> Manager
DC._scManager SourceConfig
transformedSourceConfig) (SourceConfig -> Maybe Int
DC._scTimeoutMicroseconds SourceConfig
transformedSourceConfig) Maybe AgentLicenseKey
forall a. Maybe a
Nothing)
    (AgentClientT (TraceT m) SchemaResponse -> m SchemaResponse)
-> AgentClientT (TraceT m) SchemaResponse -> m SchemaResponse
forall a b. (a -> b) -> a -> b
$ SourceName -> Config -> AgentClientT (TraceT m) SchemaResponse
forall (m :: * -> *).
(MonadIO m, MonadTrace m, MonadError QErr m) =>
SourceName -> Config -> AgentClientT m SchemaResponse
Client.schema SourceName
sourceName (SourceConfig -> Config
DC._scConfig SourceConfig
transformedSourceConfig)

getFieldType :: Bool -> API.ColumnType -> LogicalModelType 'DataConnector
getFieldType :: Bool -> ColumnType -> LogicalModelType 'DataConnector
getFieldType Bool
isNullable = \case
  API.ColumnTypeScalar ScalarType
scalarType -> LogicalModelTypeScalar 'DataConnector
-> LogicalModelType 'DataConnector
forall (b :: BackendType).
LogicalModelTypeScalar b -> LogicalModelType b
LogicalModelTypeScalar (LogicalModelTypeScalar 'DataConnector
 -> LogicalModelType 'DataConnector)
-> LogicalModelTypeScalar 'DataConnector
-> LogicalModelType 'DataConnector
forall a b. (a -> b) -> a -> b
$ ScalarType 'DataConnector
-> Bool -> LogicalModelTypeScalar 'DataConnector
forall (b :: BackendType).
ScalarType b -> Bool -> LogicalModelTypeScalar b
LogicalModelTypeScalarC (ScalarType -> ScalarType
forall source target. From source target => source -> target
Witch.from ScalarType
scalarType) Bool
isNullable
  API.ColumnTypeObject Name
objectTypeName -> LogicalModelTypeReference -> LogicalModelType 'DataConnector
forall (b :: BackendType).
LogicalModelTypeReference -> LogicalModelType b
LogicalModelTypeReference (LogicalModelTypeReference -> LogicalModelType 'DataConnector)
-> LogicalModelTypeReference -> LogicalModelType 'DataConnector
forall a b. (a -> b) -> a -> b
$ LogicalModelName -> Bool -> LogicalModelTypeReference
LogicalModelTypeReferenceC (Name -> LogicalModelName
LogicalModelName Name
objectTypeName) Bool
isNullable
  API.ColumnTypeArray ColumnType
columnType Bool
isNullable' -> LogicalModelTypeArray 'DataConnector
-> LogicalModelType 'DataConnector
forall (b :: BackendType).
LogicalModelTypeArray b -> LogicalModelType b
LogicalModelTypeArray (LogicalModelTypeArray 'DataConnector
 -> LogicalModelType 'DataConnector)
-> LogicalModelTypeArray 'DataConnector
-> LogicalModelType 'DataConnector
forall a b. (a -> b) -> a -> b
$ LogicalModelType 'DataConnector
-> Bool -> LogicalModelTypeArray 'DataConnector
forall (b :: BackendType).
LogicalModelType b -> Bool -> LogicalModelTypeArray b
LogicalModelTypeArrayC (Bool -> ColumnType -> LogicalModelType 'DataConnector
getFieldType Bool
isNullable' ColumnType
columnType) Bool
isNullable

toLogicalModelMetadata :: API.ObjectTypeDefinition -> (LogicalModelName, LogicalModelMetadata 'DataConnector)
toLogicalModelMetadata :: ObjectTypeDefinition
-> (LogicalModelName, LogicalModelMetadata 'DataConnector)
toLogicalModelMetadata API.ObjectTypeDefinition {Maybe Text
NonEmpty ColumnInfo
Name
_otdName :: Name
_otdDescription :: Maybe Text
_otdColumns :: NonEmpty ColumnInfo
_otdName :: ObjectTypeDefinition -> Name
_otdDescription :: ObjectTypeDefinition -> Maybe Text
_otdColumns :: ObjectTypeDefinition -> NonEmpty ColumnInfo
..} =
  ( LogicalModelName
logicalModelName,
    LogicalModelMetadata
      { _lmmName :: LogicalModelName
_lmmName = LogicalModelName
logicalModelName,
        _lmmFields :: InsOrdHashMap
  (Column 'DataConnector) (LogicalModelField 'DataConnector)
_lmmFields = [(Column 'DataConnector, LogicalModelField 'DataConnector)]
-> InsOrdHashMap
     (Column 'DataConnector) (LogicalModelField 'DataConnector)
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
InsOrdHashMap.fromList ([(Column 'DataConnector, LogicalModelField 'DataConnector)]
 -> InsOrdHashMap
      (Column 'DataConnector) (LogicalModelField 'DataConnector))
-> [(Column 'DataConnector, LogicalModelField 'DataConnector)]
-> InsOrdHashMap
     (Column 'DataConnector) (LogicalModelField 'DataConnector)
forall a b. (a -> b) -> a -> b
$ NonEmpty (Column 'DataConnector, LogicalModelField 'DataConnector)
-> [(Column 'DataConnector, LogicalModelField 'DataConnector)]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (NonEmpty (Column 'DataConnector, LogicalModelField 'DataConnector)
 -> [(Column 'DataConnector, LogicalModelField 'DataConnector)])
-> NonEmpty
     (Column 'DataConnector, LogicalModelField 'DataConnector)
-> [(Column 'DataConnector, LogicalModelField 'DataConnector)]
forall a b. (a -> b) -> a -> b
$ ColumnInfo -> (ColumnName, LogicalModelField 'DataConnector)
toTableObjectFieldDefinition (ColumnInfo -> (ColumnName, LogicalModelField 'DataConnector))
-> NonEmpty ColumnInfo
-> NonEmpty (ColumnName, LogicalModelField 'DataConnector)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty ColumnInfo
_otdColumns,
        _lmmDescription :: Maybe Text
_lmmDescription = Maybe Text
_otdDescription,
        _lmmSelectPermissions :: InsOrdHashMap RoleName (SelPermDef 'DataConnector)
_lmmSelectPermissions = InsOrdHashMap RoleName (SelPermDef 'DataConnector)
forall a. Monoid a => a
mempty
      }
  )
  where
    logicalModelName :: LogicalModelName
logicalModelName = Name -> LogicalModelName
LogicalModelName Name
_otdName
    toTableObjectFieldDefinition :: ColumnInfo -> (ColumnName, LogicalModelField 'DataConnector)
toTableObjectFieldDefinition API.ColumnInfo {Bool
Maybe Text
Maybe ColumnValueGenerationStrategy
ColumnType
ColumnName
_ciName :: ColumnInfo -> ColumnName
_ciType :: ColumnInfo -> ColumnType
_ciNullable :: ColumnInfo -> Bool
_ciDescription :: ColumnInfo -> Maybe Text
_ciInsertable :: ColumnInfo -> Bool
_ciUpdatable :: ColumnInfo -> Bool
_ciValueGenerated :: ColumnInfo -> Maybe ColumnValueGenerationStrategy
_ciName :: ColumnName
_ciType :: ColumnType
_ciNullable :: Bool
_ciDescription :: Maybe Text
_ciInsertable :: Bool
_ciUpdatable :: Bool
_ciValueGenerated :: Maybe ColumnValueGenerationStrategy
..} =
      let fieldType :: LogicalModelType 'DataConnector
fieldType = Bool -> ColumnType -> LogicalModelType 'DataConnector
getFieldType Bool
_ciNullable ColumnType
_ciType
          columnName :: ColumnName
columnName = ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from ColumnName
_ciName
       in ( ColumnName
columnName,
            LogicalModelField
              { lmfName :: Column 'DataConnector
lmfName = Column 'DataConnector
ColumnName
columnName,
                lmfType :: LogicalModelType 'DataConnector
lmfType = LogicalModelType 'DataConnector
fieldType,
                lmfDescription :: Maybe Text
lmfDescription = Maybe Text
_ciDescription
              }
          )

-- | Construct a 'HashSet' 'RQL.T.T.ForeignKeyMetadata'
-- 'DataConnector' to build the foreign key constraints in the table
-- metadata.
buildForeignKeySet :: API.ForeignKeys -> HashSet (RQL.T.T.ForeignKeyMetadata 'DataConnector)
buildForeignKeySet :: ForeignKeys -> HashSet (ForeignKeyMetadata 'DataConnector)
buildForeignKeySet (API.ForeignKeys HashMap ConstraintName Constraint
constraints) =
  [ForeignKeyMetadata 'DataConnector]
-> HashSet (ForeignKeyMetadata 'DataConnector)
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList
    ([ForeignKeyMetadata 'DataConnector]
 -> HashSet (ForeignKeyMetadata 'DataConnector))
-> [ForeignKeyMetadata 'DataConnector]
-> HashSet (ForeignKeyMetadata 'DataConnector)
forall a b. (a -> b) -> a -> b
$ HashMap ConstraintName Constraint
constraints
    HashMap ConstraintName Constraint
-> (HashMap ConstraintName Constraint
    -> [ForeignKeyMetadata 'DataConnector])
-> [ForeignKeyMetadata 'DataConnector]
forall a b. a -> (a -> b) -> b
& forall m k v. Monoid m => (k -> v -> m) -> HashMap k v -> m
HashMap.foldMapWithKey @[RQL.T.T.ForeignKeyMetadata 'DataConnector]
      \ConstraintName
constraintName API.Constraint {HashMap ColumnName ColumnName
TableName
_cForeignTable :: TableName
_cColumnMapping :: HashMap ColumnName ColumnName
_cForeignTable :: Constraint -> TableName
_cColumnMapping :: Constraint -> HashMap ColumnName ColumnName
..} -> Maybe (ForeignKeyMetadata 'DataConnector)
-> [ForeignKeyMetadata 'DataConnector]
forall a. Maybe a -> [a]
maybeToList do
        let columnMapAssocList :: [(ColumnName, ColumnName)]
columnMapAssocList = (ColumnName
 -> ColumnName
 -> [(ColumnName, ColumnName)]
 -> [(ColumnName, ColumnName)])
-> [(ColumnName, ColumnName)]
-> HashMap ColumnName ColumnName
-> [(ColumnName, ColumnName)]
forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
HashMap.foldrWithKey' (\(API.ColumnName Text
k) (API.ColumnName Text
v) [(ColumnName, ColumnName)]
acc -> (Text -> ColumnName
DC.ColumnName Text
k, Text -> ColumnName
DC.ColumnName Text
v) (ColumnName, ColumnName)
-> [(ColumnName, ColumnName)] -> [(ColumnName, ColumnName)]
forall a. a -> [a] -> [a]
: [(ColumnName, ColumnName)]
acc) [] HashMap ColumnName ColumnName
_cColumnMapping
        NEHashMap ColumnName ColumnName
columnMapping <- [(ColumnName, ColumnName)]
-> Maybe (NEHashMap ColumnName ColumnName)
forall k v. Hashable k => [(k, v)] -> Maybe (NEHashMap k v)
NEHashMap.fromList [(ColumnName, ColumnName)]
columnMapAssocList
        let foreignKey :: ForeignKey 'DataConnector
foreignKey =
              RQL.T.T.ForeignKey
                { _fkConstraint :: Constraint 'DataConnector
_fkConstraint = ConstraintName 'DataConnector -> OID -> Constraint 'DataConnector
forall (b :: BackendType). ConstraintName b -> OID -> Constraint b
RQL.T.T.Constraint (ConstraintName -> ConstraintName
forall source target. From source target => source -> target
Witch.from ConstraintName
constraintName) (Int -> OID
OID Int
1),
                  _fkForeignTable :: TableName 'DataConnector
_fkForeignTable = TableName -> TableName
forall source target. From source target => source -> target
Witch.from TableName
_cForeignTable,
                  _fkColumnMapping :: NEHashMap (Column 'DataConnector) (Column 'DataConnector)
_fkColumnMapping = NEHashMap (Column 'DataConnector) (Column 'DataConnector)
NEHashMap ColumnName ColumnName
columnMapping
                }
        ForeignKeyMetadata 'DataConnector
-> Maybe (ForeignKeyMetadata 'DataConnector)
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ForeignKeyMetadata 'DataConnector
 -> Maybe (ForeignKeyMetadata 'DataConnector))
-> ForeignKeyMetadata 'DataConnector
-> Maybe (ForeignKeyMetadata 'DataConnector)
forall a b. (a -> b) -> a -> b
$ ForeignKey 'DataConnector -> ForeignKeyMetadata 'DataConnector
forall (b :: BackendType). ForeignKey b -> ForeignKeyMetadata b
RQL.T.T.ForeignKeyMetadata ForeignKey 'DataConnector
foreignKey

-- | This is needed to get permissions to work
parseBoolExpOperations' ::
  forall m v.
  (MonadError QErr m) =>
  RQL.T.C.ValueParser 'DataConnector m v ->
  RQL.T.T.FieldInfoMap (RQL.T.T.FieldInfo 'DataConnector) ->
  RQL.T.T.FieldInfoMap (RQL.T.T.FieldInfo 'DataConnector) ->
  RQL.T.C.ColumnReference 'DataConnector ->
  J.Value ->
  m [OpExpG 'DataConnector v]
parseBoolExpOperations' :: forall (m :: * -> *) v.
MonadError QErr m =>
ValueParser 'DataConnector m v
-> FieldInfoMap (FieldInfo 'DataConnector)
-> FieldInfoMap (FieldInfo 'DataConnector)
-> ColumnReference 'DataConnector
-> Value
-> m [OpExpG 'DataConnector v]
parseBoolExpOperations' ValueParser 'DataConnector m v
rhsParser FieldInfoMap (FieldInfo 'DataConnector)
rootFieldInfoMap FieldInfoMap (FieldInfo 'DataConnector)
fieldInfoMap ColumnReference 'DataConnector
columnRef Value
value =
  Text -> m [OpExpG 'DataConnector v] -> m [OpExpG 'DataConnector v]
forall (m :: * -> *) a. QErrM m => Text -> m a -> m a
withPathK (ColumnReference 'DataConnector -> Text
forall a. ToTxt a => a -> Text
toTxt ColumnReference 'DataConnector
columnRef) (m [OpExpG 'DataConnector v] -> m [OpExpG 'DataConnector v])
-> m [OpExpG 'DataConnector v] -> m [OpExpG 'DataConnector v]
forall a b. (a -> b) -> a -> b
$ Value -> m [OpExpG 'DataConnector v]
parseOperations Value
value
  where
    columnType :: RQL.T.C.ColumnType 'DataConnector
    columnType :: ColumnType 'DataConnector
columnType = ColumnReference 'DataConnector -> ColumnType 'DataConnector
forall (backend :: BackendType).
ColumnReference backend -> ColumnType backend
RQL.T.C.columnReferenceType ColumnReference 'DataConnector
columnRef

    parseWithTy :: ColumnType 'DataConnector -> Value -> m v
parseWithTy ColumnType 'DataConnector
ty = ValueParser 'DataConnector m v
rhsParser (ColumnType 'DataConnector
-> CollectableType (ColumnType 'DataConnector)
forall a. a -> CollectableType a
CollectableTypeScalar ColumnType 'DataConnector
ty)

    parseOperations :: J.Value -> m [OpExpG 'DataConnector v]
    parseOperations :: Value -> m [OpExpG 'DataConnector v]
parseOperations = \case
      J.Object Object
o -> ((Key, Value) -> m (OpExpG 'DataConnector v))
-> [(Key, Value)] -> m [OpExpG 'DataConnector v]
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 ((Text, Value) -> m (OpExpG 'DataConnector v)
parseOperation ((Text, Value) -> m (OpExpG 'DataConnector v))
-> ((Key, Value) -> (Text, Value))
-> (Key, Value)
-> m (OpExpG 'DataConnector v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Key -> Text) -> (Key, Value) -> (Text, Value)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Key -> Text
K.toText) ([(Key, Value)] -> m [OpExpG 'DataConnector v])
-> [(Key, Value)] -> m [OpExpG 'DataConnector v]
forall a b. (a -> b) -> a -> b
$ Object -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KM.toList Object
o
      Value
v -> OpExpG 'DataConnector v -> [OpExpG 'DataConnector v]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OpExpG 'DataConnector v -> [OpExpG 'DataConnector v])
-> (v -> OpExpG 'DataConnector v) -> v -> [OpExpG 'DataConnector v]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ComparisonNullability -> v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
ComparisonNullability -> field -> OpExpG backend field
AEQ ComparisonNullability
NullableComparison (v -> [OpExpG 'DataConnector v])
-> m v -> m [OpExpG 'DataConnector v]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ColumnType 'DataConnector -> Value -> m v
parseWithTy ColumnType 'DataConnector
columnType Value
v

    parseOperation :: (Text, J.Value) -> m (OpExpG 'DataConnector v)
    parseOperation :: (Text, Value) -> m (OpExpG 'DataConnector v)
parseOperation (Text
opStr, Value
val) = Text -> m (OpExpG 'DataConnector v) -> m (OpExpG 'DataConnector v)
forall (m :: * -> *) a. QErrM m => Text -> m a -> m a
withPathK Text
opStr
      (m (OpExpG 'DataConnector v) -> m (OpExpG 'DataConnector v))
-> m (OpExpG 'DataConnector v) -> m (OpExpG 'DataConnector v)
forall a b. (a -> b) -> a -> b
$ case Text
opStr of
        Text
"_eq" -> m (OpExpG 'DataConnector v)
parseEq
        Text
"$eq" -> m (OpExpG 'DataConnector v)
parseEq
        Text
"_neq" -> m (OpExpG 'DataConnector v)
parseNeq
        Text
"$neq" -> m (OpExpG 'DataConnector v)
parseNeq
        Text
"_gt" -> m (OpExpG 'DataConnector v)
parseGt
        Text
"$gt" -> m (OpExpG 'DataConnector v)
parseGt
        Text
"_lt" -> m (OpExpG 'DataConnector v)
parseLt
        Text
"$lt" -> m (OpExpG 'DataConnector v)
parseLt
        Text
"_gte" -> m (OpExpG 'DataConnector v)
parseGte
        Text
"$gte" -> m (OpExpG 'DataConnector v)
parseGte
        Text
"_lte" -> m (OpExpG 'DataConnector v)
parseLte
        Text
"$lte" -> m (OpExpG 'DataConnector v)
parseLte
        Text
"_in" -> m (OpExpG 'DataConnector v)
parseIn
        Text
"$in" -> m (OpExpG 'DataConnector v)
parseIn
        Text
"_nin" -> m (OpExpG 'DataConnector v)
parseNin
        Text
"$nin" -> m (OpExpG 'DataConnector v)
parseNin
        Text
"_is_null" -> m (OpExpG 'DataConnector v)
parseIsNull
        Text
"$is_null" -> m (OpExpG 'DataConnector v)
parseIsNull
        Text
"_ceq" -> m (OpExpG 'DataConnector v)
parseCeq
        Text
"$ceq" -> m (OpExpG 'DataConnector v)
parseCeq
        Text
"_cneq" -> m (OpExpG 'DataConnector v)
parseCne
        Text
"$cneq" -> m (OpExpG 'DataConnector v)
parseCne
        Text
"_cgt" -> m (OpExpG 'DataConnector v)
parseCgt
        Text
"$cgt" -> m (OpExpG 'DataConnector v)
parseCgt
        Text
"_clt" -> m (OpExpG 'DataConnector v)
parseClt
        Text
"$clt" -> m (OpExpG 'DataConnector v)
parseClt
        Text
"_cgte" -> m (OpExpG 'DataConnector v)
parseCgte
        Text
"$cgte" -> m (OpExpG 'DataConnector v)
parseCgte
        Text
"_clte" -> m (OpExpG 'DataConnector v)
parseClte
        Text
"$clte" -> m (OpExpG 'DataConnector v)
parseClte
        -- "_like"          -> parseLike
        -- "$like"          -> parseLike
        --
        -- "_nlike"         -> parseNlike
        -- "$nlike"         -> parseNlike
        --
        -- "_cast" -> parseCast
        -- "$cast" -> parseCast

        Text
x -> Code -> Text -> m (OpExpG 'DataConnector v)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
UnexpectedPayload (Text -> m (OpExpG 'DataConnector v))
-> Text -> m (OpExpG 'DataConnector v)
forall a b. (a -> b) -> a -> b
$ Text
"Unknown operator: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x
      where
        parseOne :: m v
parseOne = ColumnType 'DataConnector -> Value -> m v
parseWithTy ColumnType 'DataConnector
columnType Value
val
        parseManyWithType :: ColumnType 'DataConnector -> m v
parseManyWithType ColumnType 'DataConnector
ty = ValueParser 'DataConnector m v
rhsParser (ColumnType 'DataConnector
-> CollectableType (ColumnType 'DataConnector)
forall a. a -> CollectableType a
CollectableTypeArray ColumnType 'DataConnector
ty) Value
val

        parseEq :: m (OpExpG 'DataConnector v)
parseEq = ComparisonNullability -> v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
ComparisonNullability -> field -> OpExpG backend field
AEQ ComparisonNullability
NullableComparison (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m v
parseOne
        parseNeq :: m (OpExpG 'DataConnector v)
parseNeq = ComparisonNullability -> v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
ComparisonNullability -> field -> OpExpG backend field
ANE ComparisonNullability
NullableComparison (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m v
parseOne
        parseIn :: m (OpExpG 'DataConnector v)
parseIn = v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
field -> OpExpG backend field
AIN (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ColumnType 'DataConnector -> m v
parseManyWithType ColumnType 'DataConnector
columnType
        parseNin :: m (OpExpG 'DataConnector v)
parseNin = v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
field -> OpExpG backend field
ANIN (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ColumnType 'DataConnector -> m v
parseManyWithType ColumnType 'DataConnector
columnType
        parseGt :: m (OpExpG 'DataConnector v)
parseGt = v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
field -> OpExpG backend field
AGT (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m v
parseOne
        parseLt :: m (OpExpG 'DataConnector v)
parseLt = v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
field -> OpExpG backend field
ALT (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m v
parseOne
        parseGte :: m (OpExpG 'DataConnector v)
parseGte = v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
field -> OpExpG backend field
AGTE (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m v
parseOne
        parseLte :: m (OpExpG 'DataConnector v)
parseLte = v -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
field -> OpExpG backend field
ALTE (v -> OpExpG 'DataConnector v)
-> m v -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m v
parseOne
        parseIsNull :: m (OpExpG 'DataConnector v)
parseIsNull = OpExpG 'DataConnector v
-> OpExpG 'DataConnector v -> Bool -> OpExpG 'DataConnector v
forall a. a -> a -> Bool -> a
bool OpExpG 'DataConnector v
forall (backend :: BackendType) field. OpExpG backend field
ANISNOTNULL OpExpG 'DataConnector v
forall (backend :: BackendType) field. OpExpG backend field
ANISNULL (Bool -> OpExpG 'DataConnector v)
-> m Bool -> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m Bool
forall a (m :: * -> *). (FromJSON a, QErrM m) => Value -> m a
decodeValue Value
val
        parseCeq :: m (OpExpG 'DataConnector v)
parseCeq = RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
RootOrCurrentColumn backend -> OpExpG backend field
CEQ (RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v)
-> m (RootOrCurrentColumn 'DataConnector)
-> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m (RootOrCurrentColumn 'DataConnector)
decodeAndValidateRhsCol Value
val
        parseCne :: m (OpExpG 'DataConnector v)
parseCne = RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
RootOrCurrentColumn backend -> OpExpG backend field
CNE (RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v)
-> m (RootOrCurrentColumn 'DataConnector)
-> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m (RootOrCurrentColumn 'DataConnector)
decodeAndValidateRhsCol Value
val
        parseCgt :: m (OpExpG 'DataConnector v)
parseCgt = RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
RootOrCurrentColumn backend -> OpExpG backend field
CGT (RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v)
-> m (RootOrCurrentColumn 'DataConnector)
-> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m (RootOrCurrentColumn 'DataConnector)
decodeAndValidateRhsCol Value
val
        parseClt :: m (OpExpG 'DataConnector v)
parseClt = RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
RootOrCurrentColumn backend -> OpExpG backend field
CLT (RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v)
-> m (RootOrCurrentColumn 'DataConnector)
-> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m (RootOrCurrentColumn 'DataConnector)
decodeAndValidateRhsCol Value
val
        parseCgte :: m (OpExpG 'DataConnector v)
parseCgte = RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
RootOrCurrentColumn backend -> OpExpG backend field
CGTE (RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v)
-> m (RootOrCurrentColumn 'DataConnector)
-> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m (RootOrCurrentColumn 'DataConnector)
decodeAndValidateRhsCol Value
val
        parseClte :: m (OpExpG 'DataConnector v)
parseClte = RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v
forall (backend :: BackendType) field.
RootOrCurrentColumn backend -> OpExpG backend field
CLTE (RootOrCurrentColumn 'DataConnector -> OpExpG 'DataConnector v)
-> m (RootOrCurrentColumn 'DataConnector)
-> m (OpExpG 'DataConnector v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m (RootOrCurrentColumn 'DataConnector)
decodeAndValidateRhsCol Value
val

        decodeAndValidateRhsCol :: J.Value -> m (RootOrCurrentColumn 'DataConnector)
        decodeAndValidateRhsCol :: Value -> m (RootOrCurrentColumn 'DataConnector)
decodeAndValidateRhsCol Value
v = case Value
v of
          J.String Text
_ -> RootOrCurrent
-> FieldInfoMap (FieldInfo 'DataConnector)
-> Value
-> m (RootOrCurrentColumn 'DataConnector)
go RootOrCurrent
IsCurrent FieldInfoMap (FieldInfo 'DataConnector)
fieldInfoMap Value
v
          J.Array Array
path -> case Array -> [Value]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Array
path of
            [] -> Code -> Text -> m (RootOrCurrentColumn 'DataConnector)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
Unexpected Text
"path cannot be empty"
            [Value
col] -> RootOrCurrent
-> FieldInfoMap (FieldInfo 'DataConnector)
-> Value
-> m (RootOrCurrentColumn 'DataConnector)
go RootOrCurrent
IsCurrent FieldInfoMap (FieldInfo 'DataConnector)
fieldInfoMap Value
col
            [J.String Text
"$", Value
col] -> RootOrCurrent
-> FieldInfoMap (FieldInfo 'DataConnector)
-> Value
-> m (RootOrCurrentColumn 'DataConnector)
go RootOrCurrent
IsRoot FieldInfoMap (FieldInfo 'DataConnector)
rootFieldInfoMap Value
col
            [Value]
_ -> Code -> Text -> m (RootOrCurrentColumn 'DataConnector)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"Relationship references are not supported in column comparison RHS"
          Value
_ -> Code -> Text -> m (RootOrCurrentColumn 'DataConnector)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
Unexpected Text
"a boolean expression JSON must be either a string or an array"
          where
            go :: RootOrCurrent
-> FieldInfoMap (FieldInfo 'DataConnector)
-> Value
-> m (RootOrCurrentColumn 'DataConnector)
go RootOrCurrent
rootInfo FieldInfoMap (FieldInfo 'DataConnector)
fieldInfoMap' Value
columnValue = do
              ColumnName
colName <- Value -> m ColumnName
forall a (m :: * -> *). (FromJSON a, QErrM m) => Value -> m a
decodeValue Value
columnValue
              ColumnName
colInfo <- FieldInfoMap (FieldInfo 'DataConnector)
-> ColumnName -> m ColumnName
validateRhsColumn FieldInfoMap (FieldInfo 'DataConnector)
fieldInfoMap' ColumnName
colName
              RootOrCurrentColumn 'DataConnector
-> m (RootOrCurrentColumn 'DataConnector)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RootOrCurrentColumn 'DataConnector
 -> m (RootOrCurrentColumn 'DataConnector))
-> RootOrCurrentColumn 'DataConnector
-> m (RootOrCurrentColumn 'DataConnector)
forall a b. (a -> b) -> a -> b
$ RootOrCurrent
-> Column 'DataConnector -> RootOrCurrentColumn 'DataConnector
forall (b :: BackendType).
RootOrCurrent -> Column b -> RootOrCurrentColumn b
RootOrCurrentColumn RootOrCurrent
rootInfo Column 'DataConnector
ColumnName
colInfo

        validateRhsColumn :: RQL.T.T.FieldInfoMap (RQL.T.T.FieldInfo 'DataConnector) -> DC.ColumnName -> m DC.ColumnName
        validateRhsColumn :: FieldInfoMap (FieldInfo 'DataConnector)
-> ColumnName -> m ColumnName
validateRhsColumn FieldInfoMap (FieldInfo 'DataConnector)
fieldInfoMap' ColumnName
rhsCol = do
          ColumnType 'DataConnector
rhsType <- FieldInfoMap (FieldInfo 'DataConnector)
-> Column 'DataConnector -> Text -> m (ColumnType 'DataConnector)
forall (m :: * -> *) (backend :: BackendType).
(MonadError QErr m, Backend backend) =>
FieldInfoMap (FieldInfo backend)
-> Column backend -> Text -> m (ColumnType backend)
RQL.T.T.askColumnType FieldInfoMap (FieldInfo 'DataConnector)
fieldInfoMap' Column 'DataConnector
ColumnName
rhsCol Text
"column operators can only compare table columns"
          Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ColumnType 'DataConnector
columnType ColumnType 'DataConnector -> ColumnType 'DataConnector -> Bool
forall a. Eq a => a -> a -> Bool
/= ColumnType 'DataConnector
rhsType)
            (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
UnexpectedPayload
            (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
"incompatible column types: "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ColumnReference 'DataConnector
columnRef
            ColumnReference 'DataConnector -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" has type "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ColumnType 'DataConnector
columnType
            ColumnType 'DataConnector -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
", but "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ColumnName
rhsCol
            ColumnName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" has type "
            Text -> ColumnType 'DataConnector -> Text
forall t. ToTxt t => Text -> t -> Text
<>> ColumnType 'DataConnector
rhsType
          ColumnName -> m ColumnName
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ColumnName
rhsCol

parseCollectableType' ::
  (MonadError QErr m, MonadReader r m, Has API.ScalarTypesCapabilities r) =>
  CollectableType (RQL.T.C.ColumnType 'DataConnector) ->
  J.Value ->
  m (PartialSQLExp 'DataConnector)
parseCollectableType' :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has ScalarTypesCapabilities r) =>
CollectableType (ColumnType 'DataConnector)
-> Value -> m (PartialSQLExp 'DataConnector)
parseCollectableType' CollectableType (ColumnType 'DataConnector)
collectableType = \case
  J.String Text
t
    | Text -> Bool
HSU.isSessionVariable Text
t -> PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector))
-> PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector)
forall a b. (a -> b) -> a -> b
$ CollectableType (ColumnType 'DataConnector)
-> SessionVariable -> PartialSQLExp 'DataConnector
mkTypedSessionVar CollectableType (ColumnType 'DataConnector)
collectableType (SessionVariable -> PartialSQLExp 'DataConnector)
-> SessionVariable -> PartialSQLExp 'DataConnector
forall a b. (a -> b) -> a -> b
$ Text -> SessionVariable
mkSessionVariable Text
t
    | Text -> Bool
HSU.isReqUserId Text
t -> PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector))
-> PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector)
forall a b. (a -> b) -> a -> b
$ CollectableType (ColumnType 'DataConnector)
-> SessionVariable -> PartialSQLExp 'DataConnector
mkTypedSessionVar CollectableType (ColumnType 'DataConnector)
collectableType SessionVariable
forall a. IsString a => a
HSU.userIdHeader
  Value
val -> case CollectableType (ColumnType 'DataConnector)
collectableType of
    CollectableTypeScalar ColumnType 'DataConnector
columnType ->
      SQLExpression 'DataConnector -> PartialSQLExp 'DataConnector
Literal -> PartialSQLExp 'DataConnector
forall (backend :: BackendType).
SQLExpression backend -> PartialSQLExp backend
PSESQLExp (Literal -> PartialSQLExp 'DataConnector)
-> (Value -> Literal) -> Value -> PartialSQLExp 'DataConnector
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScalarType -> Value -> Literal
DC.ValueLiteral (ColumnType 'DataConnector -> ScalarType
columnTypeToScalarType ColumnType 'DataConnector
columnType) (Value -> PartialSQLExp 'DataConnector)
-> m Value -> m (PartialSQLExp 'DataConnector)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ColumnType 'DataConnector
-> Value -> m (ScalarValue 'DataConnector)
forall (m :: * -> *) (b :: BackendType) r.
(MonadError QErr m, Backend b, MonadReader r m,
 Has (ScalarTypeParsingContext b) r) =>
ColumnType b -> Value -> m (ScalarValue b)
RQL.T.C.parseScalarValueColumnType ColumnType 'DataConnector
columnType Value
val
    CollectableTypeArray ColumnType 'DataConnector
columnType -> do
      [Value]
vals <- (Value -> Parser [Value]) -> Value -> m [Value]
forall (m :: * -> *) v a. QErrM m => (v -> Parser a) -> v -> m a
runAesonParser Value -> Parser [Value]
forall a. FromJSON a => Value -> Parser a
J.parseJSON Value
val
      [Value]
scalarValues <- ColumnType 'DataConnector
-> [Value] -> m [ScalarValue 'DataConnector]
forall (m :: * -> *) (b :: BackendType) r.
(MonadError QErr m, Backend b, MonadReader r m,
 Has (ScalarTypeParsingContext b) r) =>
ColumnType b -> [Value] -> m [ScalarValue b]
RQL.T.C.parseScalarValuesColumnType ColumnType 'DataConnector
columnType [Value]
vals
      PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialSQLExp 'DataConnector -> m (PartialSQLExp 'DataConnector))
-> (Literal -> PartialSQLExp 'DataConnector)
-> Literal
-> m (PartialSQLExp 'DataConnector)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SQLExpression 'DataConnector -> PartialSQLExp 'DataConnector
Literal -> PartialSQLExp 'DataConnector
forall (backend :: BackendType).
SQLExpression backend -> PartialSQLExp backend
PSESQLExp (Literal -> m (PartialSQLExp 'DataConnector))
-> Literal -> m (PartialSQLExp 'DataConnector)
forall a b. (a -> b) -> a -> b
$ ScalarType -> [Value] -> Literal
DC.ArrayLiteral (ColumnType 'DataConnector -> ScalarType
columnTypeToScalarType ColumnType 'DataConnector
columnType) [Value]
scalarValues

mkTypedSessionVar ::
  CollectableType (RQL.T.C.ColumnType 'DataConnector) ->
  SessionVariable ->
  PartialSQLExp 'DataConnector
mkTypedSessionVar :: CollectableType (ColumnType 'DataConnector)
-> SessionVariable -> PartialSQLExp 'DataConnector
mkTypedSessionVar CollectableType (ColumnType 'DataConnector)
columnType =
  SessionVarType 'DataConnector
-> SessionVariable -> PartialSQLExp 'DataConnector
forall (backend :: BackendType).
SessionVarType backend -> SessionVariable -> PartialSQLExp backend
PSESessVar (ColumnType 'DataConnector -> ScalarType
columnTypeToScalarType (ColumnType 'DataConnector -> ScalarType)
-> CollectableType (ColumnType 'DataConnector)
-> CollectableType ScalarType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CollectableType (ColumnType 'DataConnector)
columnType)

buildObjectRelationshipInfo' ::
  (MonadError QErr m) =>
  DC.SourceConfig ->
  SourceName ->
  HashMap DC.TableName (HashSet (ForeignKey 'DataConnector)) ->
  DC.TableName ->
  ObjRelDef 'DataConnector ->
  m (RelInfo 'DataConnector, Seq SchemaDependency)
buildObjectRelationshipInfo' :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig
-> SourceName
-> HashMap TableName (HashSet (ForeignKey 'DataConnector))
-> TableName
-> ObjRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
buildObjectRelationshipInfo' SourceConfig
sourceConfig SourceName
sourceName HashMap TableName (HashSet (ForeignKey 'DataConnector))
fks TableName
tableName ObjRelDef 'DataConnector
objRel = do
  SourceName
-> SourceConfig
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall (m :: * -> *) a.
MonadError QErr m =>
SourceName -> SourceConfig -> m a -> m a
ifSupportsLocalRelationships SourceName
sourceName SourceConfig
sourceConfig
    (m (RelInfo 'DataConnector, Seq SchemaDependency)
 -> m (RelInfo 'DataConnector, Seq SchemaDependency))
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall a b. (a -> b) -> a -> b
$ SourceName
-> HashMap
     (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
-> TableName 'DataConnector
-> ObjRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall (b :: BackendType) (m :: * -> *).
(QErrM m, Backend b) =>
SourceName
-> HashMap (TableName b) (HashSet (ForeignKey b))
-> TableName b
-> ObjRelDef b
-> m (RelInfo b, Seq SchemaDependency)
defaultBuildObjectRelationshipInfo SourceName
sourceName HashMap
  (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
HashMap TableName (HashSet (ForeignKey 'DataConnector))
fks TableName 'DataConnector
TableName
tableName ObjRelDef 'DataConnector
objRel

buildArrayRelationshipInfo' ::
  (MonadError QErr m) =>
  DC.SourceConfig ->
  SourceName ->
  HashMap DC.TableName (HashSet (ForeignKey 'DataConnector)) ->
  DC.TableName ->
  ArrRelDef 'DataConnector ->
  m (RelInfo 'DataConnector, Seq SchemaDependency)
buildArrayRelationshipInfo' :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig
-> SourceName
-> HashMap TableName (HashSet (ForeignKey 'DataConnector))
-> TableName
-> ArrRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
buildArrayRelationshipInfo' SourceConfig
sourceConfig SourceName
sourceName HashMap TableName (HashSet (ForeignKey 'DataConnector))
fks TableName
tableName ArrRelDef 'DataConnector
arrRel =
  SourceName
-> SourceConfig
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall (m :: * -> *) a.
MonadError QErr m =>
SourceName -> SourceConfig -> m a -> m a
ifSupportsLocalRelationships SourceName
sourceName SourceConfig
sourceConfig
    (m (RelInfo 'DataConnector, Seq SchemaDependency)
 -> m (RelInfo 'DataConnector, Seq SchemaDependency))
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall a b. (a -> b) -> a -> b
$ SourceName
-> HashMap
     (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
-> TableName 'DataConnector
-> ArrRelDef 'DataConnector
-> m (RelInfo 'DataConnector, Seq SchemaDependency)
forall (b :: BackendType) (m :: * -> *).
(QErrM m, Backend b) =>
SourceName
-> HashMap (TableName b) (HashSet (ForeignKey b))
-> TableName b
-> ArrRelDef b
-> m (RelInfo b, Seq SchemaDependency)
defaultBuildArrayRelationshipInfo SourceName
sourceName HashMap
  (TableName 'DataConnector) (HashSet (ForeignKey 'DataConnector))
HashMap TableName (HashSet (ForeignKey 'DataConnector))
fks TableName 'DataConnector
TableName
tableName ArrRelDef 'DataConnector
arrRel

ifSupportsLocalRelationships :: (MonadError QErr m) => SourceName -> DC.SourceConfig -> m a -> m a
ifSupportsLocalRelationships :: forall (m :: * -> *) a.
MonadError QErr m =>
SourceName -> SourceConfig -> m a -> m a
ifSupportsLocalRelationships SourceName
sourceName DC.SourceConfig {Maybe Int
Maybe Text
Config
Capabilities
Environment
Manager
BaseUrl
DataConnectorName
_scEndpoint :: SourceConfig -> BaseUrl
_scConfig :: SourceConfig -> Config
_scTemplate :: SourceConfig -> Maybe Text
_scCapabilities :: SourceConfig -> Capabilities
_scManager :: SourceConfig -> Manager
_scTimeoutMicroseconds :: SourceConfig -> Maybe Int
_scDataConnectorName :: SourceConfig -> DataConnectorName
_scEnvironment :: SourceConfig -> Environment
_scEndpoint :: BaseUrl
_scConfig :: Config
_scTemplate :: Maybe Text
_scCapabilities :: Capabilities
_scManager :: Manager
_scTimeoutMicroseconds :: Maybe Int
_scDataConnectorName :: DataConnectorName
_scEnvironment :: Environment
..} m a
action = do
  let supportsRelationships :: Bool
supportsRelationships = Maybe RelationshipCapabilities -> Bool
forall a. Maybe a -> Bool
isJust (Maybe RelationshipCapabilities -> Bool)
-> Maybe RelationshipCapabilities -> Bool
forall a b. (a -> b) -> a -> b
$ Capabilities -> Maybe RelationshipCapabilities
API._cRelationships Capabilities
_scCapabilities
  let supportsRemoteRelationships :: Bool
supportsRemoteRelationships = Maybe ForeachCapabilities -> Bool
forall a. Maybe a -> Bool
isJust (Maybe ForeachCapabilities -> Bool)
-> Maybe ForeachCapabilities -> Bool
forall a b. (a -> b) -> a -> b
$ QueryCapabilities -> Maybe ForeachCapabilities
API._qcForeach (QueryCapabilities -> Maybe ForeachCapabilities)
-> Maybe QueryCapabilities -> Maybe ForeachCapabilities
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Capabilities -> Maybe QueryCapabilities
API._cQueries Capabilities
_scCapabilities
  if Bool
supportsRelationships
    then m a
action
    else
      let suggestion :: Text
suggestion =
            if Bool
supportsRemoteRelationships
              then Text
" Instead consider using remote relationships to join between tables on the same source."
              else Text
""
       in Code -> Text -> m a
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported (Text -> m a) -> Text -> m a
forall a b. (a -> b) -> a -> b
$ Text
"Local object and array relationships are not supported for '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Text
forall a. ToTxt a => a -> Text
toTxt SourceName
sourceName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
suggestion

supportsBeingRemoteRelationshipTarget' :: DC.SourceConfig -> Bool
supportsBeingRemoteRelationshipTarget' :: SourceConfig -> Bool
supportsBeingRemoteRelationshipTarget' DC.SourceConfig {Maybe Int
Maybe Text
Config
Capabilities
Environment
Manager
BaseUrl
DataConnectorName
_scEndpoint :: SourceConfig -> BaseUrl
_scConfig :: SourceConfig -> Config
_scTemplate :: SourceConfig -> Maybe Text
_scCapabilities :: SourceConfig -> Capabilities
_scManager :: SourceConfig -> Manager
_scTimeoutMicroseconds :: SourceConfig -> Maybe Int
_scDataConnectorName :: SourceConfig -> DataConnectorName
_scEnvironment :: SourceConfig -> Environment
_scEndpoint :: BaseUrl
_scConfig :: Config
_scTemplate :: Maybe Text
_scCapabilities :: Capabilities
_scManager :: Manager
_scTimeoutMicroseconds :: Maybe Int
_scDataConnectorName :: DataConnectorName
_scEnvironment :: Environment
..} =
  Maybe ForeachCapabilities -> Bool
forall a. Maybe a -> Bool
isJust (Maybe ForeachCapabilities -> Bool)
-> Maybe ForeachCapabilities -> Bool
forall a b. (a -> b) -> a -> b
$ QueryCapabilities -> Maybe ForeachCapabilities
API._qcForeach (QueryCapabilities -> Maybe ForeachCapabilities)
-> Maybe QueryCapabilities -> Maybe ForeachCapabilities
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Capabilities -> Maybe QueryCapabilities
API._cQueries Capabilities
_scCapabilities

listAllTables' :: (CacheRM m, Has (Logger Hasura) r, MonadIO m, MonadBaseControl IO m, MonadReader r m, MonadError QErr m, MetadataM m) => SourceName -> m [DC.TableName]
listAllTables' :: forall (m :: * -> *) r.
(CacheRM m, Has (Logger Hasura) r, MonadIO m,
 MonadBaseControl IO m, MonadReader r m, MonadError QErr m,
 MetadataM m) =>
SourceName -> m [TableName]
listAllTables' SourceName
sourceName = do
  (Logger Hasura
logger :: Logger Hasura) <- (r -> Logger Hasura) -> m (Logger Hasura)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks r -> Logger Hasura
forall a t. Has a t => t -> a
getter
  SourceConfig
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @'DataConnector SourceName
sourceName
  SchemaResponse
schemaResponse <- Logger Hasura -> SourceName -> SourceConfig -> m SchemaResponse
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
Logger Hasura -> SourceName -> SourceConfig -> m SchemaResponse
requestDatabaseSchema Logger Hasura
logger SourceName
sourceName SourceConfig
sourceConfig
  [TableName] -> m [TableName]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TableName] -> m [TableName]) -> [TableName] -> m [TableName]
forall a b. (a -> b) -> a -> b
$ (TableInfo -> TableName) -> [TableInfo] -> [TableName]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TableName -> TableName
forall source target. From source target => source -> target
Witch.from (TableName -> TableName)
-> (TableInfo -> TableName) -> TableInfo -> TableName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableInfo -> TableName
API._tiName) ([TableInfo] -> [TableName]) -> [TableInfo] -> [TableName]
forall a b. (a -> b) -> a -> b
$ SchemaResponse -> [TableInfo]
API._srTables SchemaResponse
schemaResponse

listAllTrackables' :: (CacheRM m, Has (Logger Hasura) r, MonadIO m, MonadBaseControl IO m, MonadReader r m, MonadError QErr m, MetadataM m) => SourceName -> m (TrackableInfo 'DataConnector)
listAllTrackables' :: forall (m :: * -> *) r.
(CacheRM m, Has (Logger Hasura) r, MonadIO m,
 MonadBaseControl IO m, MonadReader r m, MonadError QErr m,
 MetadataM m) =>
SourceName -> m (TrackableInfo 'DataConnector)
listAllTrackables' SourceName
sourceName = do
  (Logger Hasura
logger :: Logger Hasura) <- (r -> Logger Hasura) -> m (Logger Hasura)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks r -> Logger Hasura
forall a t. Has a t => t -> a
getter
  SourceConfig
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @'DataConnector SourceName
sourceName
  SchemaResponse
schemaResponse <- Logger Hasura -> SourceName -> SourceConfig -> m SchemaResponse
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
Logger Hasura -> SourceName -> SourceConfig -> m SchemaResponse
requestDatabaseSchema Logger Hasura
logger SourceName
sourceName SourceConfig
sourceConfig
  let functions :: [TrackableFunctionInfo 'DataConnector]
functions = (FunctionInfo -> TrackableFunctionInfo 'DataConnector)
-> [FunctionInfo] -> [TrackableFunctionInfo 'DataConnector]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\FunctionInfo
fi -> FunctionName 'DataConnector
-> FunctionVolatility -> TrackableFunctionInfo 'DataConnector
forall (b :: BackendType).
FunctionName b -> FunctionVolatility -> TrackableFunctionInfo b
TrackableFunctionInfo (FunctionName -> FunctionName
forall target source. From source target => source -> target
Witch.into (FunctionInfo -> FunctionName
API._fiName FunctionInfo
fi)) (FunctionType -> FunctionVolatility
getVolatility (FunctionInfo -> FunctionType
API._fiFunctionType FunctionInfo
fi))) ([FunctionInfo] -> [TrackableFunctionInfo 'DataConnector])
-> [FunctionInfo] -> [TrackableFunctionInfo 'DataConnector]
forall a b. (a -> b) -> a -> b
$ SchemaResponse -> [FunctionInfo]
API._srFunctions SchemaResponse
schemaResponse
  let tables :: [TrackableTableInfo 'DataConnector]
tables = (TableInfo -> TrackableTableInfo 'DataConnector)
-> [TableInfo] -> [TrackableTableInfo 'DataConnector]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TableName 'DataConnector -> TrackableTableInfo 'DataConnector
TableName -> TrackableTableInfo 'DataConnector
forall (b :: BackendType). TableName b -> TrackableTableInfo b
TrackableTableInfo (TableName -> TrackableTableInfo 'DataConnector)
-> (TableInfo -> TableName)
-> TableInfo
-> TrackableTableInfo 'DataConnector
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableName -> TableName
forall target source. From source target => source -> target
Witch.into (TableName -> TableName)
-> (TableInfo -> TableName) -> TableInfo -> TableName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableInfo -> TableName
API._tiName) ([TableInfo] -> [TrackableTableInfo 'DataConnector])
-> [TableInfo] -> [TrackableTableInfo 'DataConnector]
forall a b. (a -> b) -> a -> b
$ SchemaResponse -> [TableInfo]
API._srTables SchemaResponse
schemaResponse
  TrackableInfo 'DataConnector -> m (TrackableInfo 'DataConnector)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    TrackableInfo
      { trackableTables :: [TrackableTableInfo 'DataConnector]
trackableTables = [TrackableTableInfo 'DataConnector]
tables,
        trackableFunctions :: [TrackableFunctionInfo 'DataConnector]
trackableFunctions = [TrackableFunctionInfo 'DataConnector]
functions
      }

getVolatility :: API.FunctionType -> FunctionVolatility
getVolatility :: FunctionType -> FunctionVolatility
getVolatility FunctionType
API.FRead = FunctionVolatility
FTSTABLE
getVolatility FunctionType
API.FWrite = FunctionVolatility
FTVOLATILE

getTableInfo' :: (CacheRM m, MetadataM m, MonadError QErr m) => SourceName -> DC.TableName -> m (Maybe (SourceTableInfo 'DataConnector))
getTableInfo' :: forall (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m) =>
SourceName
-> TableName -> m (Maybe (SourceTableInfo 'DataConnector))
getTableInfo' SourceName
sourceName TableName
tableName = do
  SourceInfo {DBObjectsIntrospection 'DataConnector
_siDbObjectsIntrospection :: DBObjectsIntrospection 'DataConnector
_siDbObjectsIntrospection :: forall (b :: BackendType). SourceInfo b -> DBObjectsIntrospection b
_siDbObjectsIntrospection, TableCache 'DataConnector
_siTables :: TableCache 'DataConnector
_siTables :: forall (b :: BackendType). SourceInfo b -> TableCache b
_siTables, HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
_siLogicalModels :: HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
_siLogicalModels :: forall (b :: BackendType). SourceInfo b -> LogicalModelCache b
_siLogicalModels} <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m, Backend b) =>
SourceName -> m (SourceInfo b)
askSourceInfo @'DataConnector SourceName
sourceName

  let tables :: HashMap DC.TableName (RQL.T.T.DBTableMetadata 'DataConnector)
      tables :: HashMap TableName (DBTableMetadata 'DataConnector)
tables = DBObjectsIntrospection 'DataConnector
-> DBTablesMetadata 'DataConnector
forall (b :: BackendType).
DBObjectsIntrospection b -> DBTablesMetadata b
_rsTables DBObjectsIntrospection 'DataConnector
_siDbObjectsIntrospection

  Maybe (SourceTableInfo 'DataConnector)
-> m (Maybe (SourceTableInfo 'DataConnector))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (SourceTableInfo 'DataConnector)
 -> m (Maybe (SourceTableInfo 'DataConnector)))
-> Maybe (SourceTableInfo 'DataConnector)
-> m (Maybe (SourceTableInfo 'DataConnector))
forall a b. (a -> b) -> a -> b
$ TableName
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> DBTableMetadata 'DataConnector
-> TableInfo 'DataConnector
-> SourceTableInfo 'DataConnector
convertTableMetadataToTableInfo TableName
tableName HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
_siLogicalModels (DBTableMetadata 'DataConnector
 -> TableInfo 'DataConnector -> SourceTableInfo 'DataConnector)
-> Maybe (DBTableMetadata 'DataConnector)
-> Maybe
     (TableInfo 'DataConnector -> SourceTableInfo 'DataConnector)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TableName
-> HashMap TableName (DBTableMetadata 'DataConnector)
-> Maybe (DBTableMetadata 'DataConnector)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup TableName
tableName HashMap TableName (DBTableMetadata 'DataConnector)
tables Maybe (TableInfo 'DataConnector -> SourceTableInfo 'DataConnector)
-> Maybe (TableInfo 'DataConnector)
-> Maybe (SourceTableInfo 'DataConnector)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TableName
-> HashMap TableName (TableInfo 'DataConnector)
-> Maybe (TableInfo 'DataConnector)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup TableName
tableName TableCache 'DataConnector
HashMap TableName (TableInfo 'DataConnector)
_siTables

convertTableMetadataToTableInfo :: DC.TableName -> LogicalModelCache 'DataConnector -> RQL.T.T.DBTableMetadata 'DataConnector -> RQL.T.T.TableInfo 'DataConnector -> SourceTableInfo 'DataConnector
convertTableMetadataToTableInfo :: TableName
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> DBTableMetadata 'DataConnector
-> TableInfo 'DataConnector
-> SourceTableInfo 'DataConnector
convertTableMetadataToTableInfo TableName
tableName HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
logicalModelCache RQL.T.T.DBTableMetadata {[RawColumnInfo 'DataConnector]
Maybe PGDescription
Maybe ViewInfo
Maybe (PrimaryKey 'DataConnector (Column 'DataConnector))
HashSet (UniqueConstraint 'DataConnector)
HashSet (ForeignKeyMetadata 'DataConnector)
OID
ExtraTableMetadata 'DataConnector
_ptmiOid :: forall (b :: BackendType). DBTableMetadata b -> OID
_ptmiColumns :: forall (b :: BackendType). DBTableMetadata b -> [RawColumnInfo b]
_ptmiPrimaryKey :: forall (b :: BackendType).
DBTableMetadata b -> Maybe (PrimaryKey b (Column b))
_ptmiUniqueConstraints :: forall (b :: BackendType).
DBTableMetadata b -> HashSet (UniqueConstraint b)
_ptmiForeignKeys :: forall (b :: BackendType).
DBTableMetadata b -> HashSet (ForeignKeyMetadata b)
_ptmiViewInfo :: forall (b :: BackendType). DBTableMetadata b -> Maybe ViewInfo
_ptmiDescription :: forall (b :: BackendType). DBTableMetadata b -> Maybe PGDescription
_ptmiExtraTableMetadata :: forall (b :: BackendType).
DBTableMetadata b -> ExtraTableMetadata b
_ptmiOid :: OID
_ptmiColumns :: [RawColumnInfo 'DataConnector]
_ptmiPrimaryKey :: Maybe (PrimaryKey 'DataConnector (Column 'DataConnector))
_ptmiUniqueConstraints :: HashSet (UniqueConstraint 'DataConnector)
_ptmiForeignKeys :: HashSet (ForeignKeyMetadata 'DataConnector)
_ptmiViewInfo :: Maybe ViewInfo
_ptmiDescription :: Maybe PGDescription
_ptmiExtraTableMetadata :: ExtraTableMetadata 'DataConnector
..} RQL.T.T.TableInfo {RolePermInfoMap 'DataConnector
EventTriggerInfoMap 'DataConnector
RolePermInfo 'DataConnector
TableCoreInfo 'DataConnector
_tiCoreInfo :: TableCoreInfo 'DataConnector
_tiRolePermInfoMap :: RolePermInfoMap 'DataConnector
_tiEventTriggerInfoMap :: EventTriggerInfoMap 'DataConnector
_tiAdminRolePermInfo :: RolePermInfo 'DataConnector
_tiCoreInfo :: forall (b :: BackendType). TableInfo b -> TableCoreInfo b
_tiRolePermInfoMap :: forall (b :: BackendType). TableInfo b -> RolePermInfoMap b
_tiEventTriggerInfoMap :: forall (b :: BackendType). TableInfo b -> EventTriggerInfoMap b
_tiAdminRolePermInfo :: forall (b :: BackendType). TableInfo b -> RolePermInfo b
..} =
  SourceTableInfo
    { _stiName :: TableName 'DataConnector
_stiName = TableName -> TableName
forall source target. From source target => source -> target
Witch.from TableName
tableName,
      _stiType :: SourceTableType
_stiType = case ExtraTableMetadata -> TableType
DC._etmTableType ExtraTableMetadata 'DataConnector
ExtraTableMetadata
_ptmiExtraTableMetadata of
        TableType
DC.Table -> SourceTableType
Table
        TableType
DC.View -> SourceTableType
View,
      _stiColumns :: [SourceColumnInfo 'DataConnector]
_stiColumns = RawColumnInfo 'DataConnector -> SourceColumnInfo 'DataConnector
convertColumn (RawColumnInfo 'DataConnector -> SourceColumnInfo 'DataConnector)
-> [RawColumnInfo 'DataConnector]
-> [SourceColumnInfo 'DataConnector]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TableCoreInfo 'DataConnector -> [RawColumnInfo 'DataConnector]
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> [RawColumnInfo b]
RQL.T.T._tciRawColumns TableCoreInfo 'DataConnector
_tiCoreInfo,
      _stiLogicalModels :: [LogicalModelMetadata 'DataConnector]
_stiLogicalModels = (LogicalModelInfo 'DataConnector
 -> LogicalModelMetadata 'DataConnector)
-> [LogicalModelInfo 'DataConnector]
-> [LogicalModelMetadata 'DataConnector]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LogicalModelInfo 'DataConnector
-> LogicalModelMetadata 'DataConnector
logicalModelInfoToMetadata ([LogicalModelInfo 'DataConnector]
 -> [LogicalModelMetadata 'DataConnector])
-> (HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
    -> [LogicalModelInfo 'DataConnector])
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> [LogicalModelMetadata 'DataConnector]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> [LogicalModelInfo 'DataConnector]
forall k v. HashMap k v -> [v]
HashMap.elems (HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
 -> [LogicalModelMetadata 'DataConnector])
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> [LogicalModelMetadata 'DataConnector]
forall a b. (a -> b) -> a -> b
$ (HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
 -> RawColumnType 'DataConnector
 -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector))
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> [RawColumnType 'DataConnector]
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> RawColumnType 'DataConnector
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModels HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
forall a. Monoid a => a
mempty ([RawColumnType 'DataConnector]
 -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector))
-> [RawColumnType 'DataConnector]
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
forall a b. (a -> b) -> a -> b
$ RawColumnInfo 'DataConnector -> RawColumnType 'DataConnector
forall (b :: BackendType). RawColumnInfo b -> RawColumnType b
RQL.T.C.rciType (RawColumnInfo 'DataConnector -> RawColumnType 'DataConnector)
-> [RawColumnInfo 'DataConnector] -> [RawColumnType 'DataConnector]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TableCoreInfo 'DataConnector -> [RawColumnInfo 'DataConnector]
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> [RawColumnInfo b]
RQL.T.T._tciRawColumns TableCoreInfo 'DataConnector
_tiCoreInfo,
      _stiPrimaryKey :: Maybe (NonEmpty (Column 'DataConnector))
_stiPrimaryKey = (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)
-> (PrimaryKey 'DataConnector ColumnName -> NonEmpty ColumnName)
-> PrimaryKey 'DataConnector 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)
-> (PrimaryKey 'DataConnector ColumnName -> NESeq ColumnName)
-> PrimaryKey 'DataConnector ColumnName
-> NonEmpty ColumnName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrimaryKey 'DataConnector ColumnName -> NESeq ColumnName
forall (b :: BackendType) a. PrimaryKey b a -> NESeq a
RQL.T.T._pkColumns (PrimaryKey 'DataConnector ColumnName -> NonEmpty ColumnName)
-> Maybe (PrimaryKey 'DataConnector ColumnName)
-> Maybe (NonEmpty ColumnName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (PrimaryKey 'DataConnector (Column 'DataConnector))
Maybe (PrimaryKey 'DataConnector ColumnName)
_ptmiPrimaryKey,
      _stiForeignKeys :: SourceForeignKeys 'DataConnector
_stiForeignKeys = HashSet (ForeignKeyMetadata 'DataConnector)
-> SourceForeignKeys 'DataConnector
convertForeignKeys HashSet (ForeignKeyMetadata 'DataConnector)
_ptmiForeignKeys,
      _stiDescription :: Maybe Text
_stiDescription = PGDescription -> Text
getPGDescription (PGDescription -> Text) -> Maybe PGDescription -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe PGDescription
_ptmiDescription,
      _stiInsertable :: Bool
_stiInsertable = (ViewInfo -> Bool) -> Maybe ViewInfo -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ViewInfo -> Bool
RQL.T.T.viIsInsertable Maybe ViewInfo
_ptmiViewInfo,
      _stiUpdatable :: Bool
_stiUpdatable = (ViewInfo -> Bool) -> Maybe ViewInfo -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ViewInfo -> Bool
RQL.T.T.viIsUpdatable Maybe ViewInfo
_ptmiViewInfo,
      _stiDeletable :: Bool
_stiDeletable = (ViewInfo -> Bool) -> Maybe ViewInfo -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ViewInfo -> Bool
RQL.T.T.viIsDeletable Maybe ViewInfo
_ptmiViewInfo
    }
  where
    convertColumn :: RQL.T.C.RawColumnInfo 'DataConnector -> SourceColumnInfo 'DataConnector
    convertColumn :: RawColumnInfo 'DataConnector -> SourceColumnInfo 'DataConnector
convertColumn RQL.T.C.RawColumnInfo {Bool
Int
Maybe Description
Column 'DataConnector
ColumnMutability
RawColumnType 'DataConnector
rciName :: forall (b :: BackendType). RawColumnInfo b -> Column b
rciPosition :: forall (b :: BackendType). RawColumnInfo b -> Int
rciType :: forall (b :: BackendType). RawColumnInfo b -> RawColumnType b
rciIsNullable :: forall (b :: BackendType). RawColumnInfo b -> Bool
rciDescription :: forall (b :: BackendType). RawColumnInfo b -> Maybe Description
rciMutability :: forall (b :: BackendType). RawColumnInfo b -> ColumnMutability
rciName :: Column 'DataConnector
rciPosition :: Int
rciType :: RawColumnType 'DataConnector
rciIsNullable :: Bool
rciDescription :: Maybe Description
rciMutability :: ColumnMutability
..} =
      SourceColumnInfo
        { _sciName :: Column 'DataConnector
_sciName = ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from Column 'DataConnector
ColumnName
rciName,
          _sciType :: RawColumnType 'DataConnector
_sciType = RawColumnType 'DataConnector -> RawColumnType 'DataConnector
forall source target. From source target => source -> target
Witch.from RawColumnType 'DataConnector
rciType,
          _sciNullable :: Bool
_sciNullable = Bool
rciIsNullable,
          _sciDescription :: Maybe Text
_sciDescription = Description -> Text
G.unDescription (Description -> Text) -> Maybe Description -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Description
rciDescription,
          _sciInsertable :: Bool
_sciInsertable = ColumnMutability -> Bool
RQL.T.C._cmIsInsertable ColumnMutability
rciMutability,
          _sciUpdatable :: Bool
_sciUpdatable = ColumnMutability -> Bool
RQL.T.C._cmIsUpdatable ColumnMutability
rciMutability,
          _sciValueGenerated :: Maybe ColumnValueGenerationStrategy
_sciValueGenerated =
            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
DC._ecmValueGenerated
              Maybe ColumnValueGenerationStrategy
-> (ColumnValueGenerationStrategy -> ColumnValueGenerationStrategy)
-> Maybe ColumnValueGenerationStrategy
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
                ColumnValueGenerationStrategy
API.AutoIncrement -> ColumnValueGenerationStrategy
AutoIncrement
                ColumnValueGenerationStrategy
API.UniqueIdentifier -> ColumnValueGenerationStrategy
UniqueIdentifier
                ColumnValueGenerationStrategy
API.DefaultValue -> ColumnValueGenerationStrategy
DefaultValue
        }
      where
        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
rciName (HashMap ColumnName ExtraColumnMetadata
 -> Maybe ExtraColumnMetadata)
-> (ExtraTableMetadata 'DataConnector
    -> HashMap ColumnName ExtraColumnMetadata)
-> ExtraTableMetadata 'DataConnector
-> Maybe ExtraColumnMetadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExtraTableMetadata 'DataConnector
-> HashMap ColumnName ExtraColumnMetadata
ExtraTableMetadata -> HashMap ColumnName ExtraColumnMetadata
DC._etmExtraColumnMetadata (ExtraTableMetadata 'DataConnector -> Maybe ExtraColumnMetadata)
-> ExtraTableMetadata 'DataConnector -> Maybe ExtraColumnMetadata
forall a b. (a -> b) -> a -> b
$ ExtraTableMetadata 'DataConnector
_ptmiExtraTableMetadata

    collectLogicalModels :: LogicalModelCache 'DataConnector -> RQL.T.C.RawColumnType 'DataConnector -> LogicalModelCache 'DataConnector
    collectLogicalModels :: HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> RawColumnType 'DataConnector
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModels HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels = \case
      RQL.T.C.RawColumnTypeScalar ScalarType 'DataConnector
_ -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels
      RQL.T.C.RawColumnTypeObject XNestedObjects 'DataConnector
_ Name
name -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> LogicalModelName
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModelName HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels (Name -> LogicalModelName
LogicalModelName Name
name)
      RQL.T.C.RawColumnTypeArray XNestedObjects 'DataConnector
_ RawColumnType 'DataConnector
rawColumnType Bool
_ -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> RawColumnType 'DataConnector
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModels HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels RawColumnType 'DataConnector
rawColumnType

    collectLogicalModelName :: LogicalModelCache 'DataConnector -> LogicalModelName -> LogicalModelCache 'DataConnector
    collectLogicalModelName :: HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> LogicalModelName
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModelName HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels LogicalModelName
logicalModelName
      | LogicalModelName
logicalModelName LogicalModelName
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> Bool
forall k a. (Eq k, Hashable k) => k -> HashMap k a -> Bool
`HashMap.member` HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels = HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels
      | Bool
otherwise =
          case LogicalModelName
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> Maybe (LogicalModelInfo 'DataConnector)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup LogicalModelName
logicalModelName HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
logicalModelCache of
            Maybe (LogicalModelInfo 'DataConnector)
Nothing -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels
            Just LogicalModelInfo 'DataConnector
logicalModelInfo ->
              let seenLogicalModels' :: HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels' = LogicalModelName
-> LogicalModelInfo 'DataConnector
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert LogicalModelName
logicalModelName LogicalModelInfo 'DataConnector
logicalModelInfo HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels
               in (HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
 -> LogicalModelType 'DataConnector
 -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector))
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> [LogicalModelType 'DataConnector]
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> LogicalModelType 'DataConnector
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModelType HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels' ((LogicalModelField 'DataConnector
 -> LogicalModelType 'DataConnector)
-> [LogicalModelField 'DataConnector]
-> [LogicalModelType 'DataConnector]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LogicalModelField 'DataConnector -> LogicalModelType 'DataConnector
forall (b :: BackendType).
LogicalModelField b -> LogicalModelType b
lmfType ([LogicalModelField 'DataConnector]
 -> [LogicalModelType 'DataConnector])
-> [LogicalModelField 'DataConnector]
-> [LogicalModelType 'DataConnector]
forall a b. (a -> b) -> a -> b
$ InsOrdHashMap ColumnName (LogicalModelField 'DataConnector)
-> [LogicalModelField 'DataConnector]
forall k v. InsOrdHashMap k v -> [v]
InsOrdHashMap.elems (InsOrdHashMap ColumnName (LogicalModelField 'DataConnector)
 -> [LogicalModelField 'DataConnector])
-> InsOrdHashMap ColumnName (LogicalModelField 'DataConnector)
-> [LogicalModelField 'DataConnector]
forall a b. (a -> b) -> a -> b
$ LogicalModelInfo 'DataConnector
-> InsOrdHashMap
     (Column 'DataConnector) (LogicalModelField 'DataConnector)
forall (b :: BackendType).
LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
_lmiFields LogicalModelInfo 'DataConnector
logicalModelInfo)

    collectLogicalModelType :: LogicalModelCache 'DataConnector -> LogicalModelType 'DataConnector -> LogicalModelCache 'DataConnector
    collectLogicalModelType :: HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> LogicalModelType 'DataConnector
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModelType HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels = \case
      LogicalModelTypeScalar LogicalModelTypeScalar 'DataConnector
_ -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels
      LogicalModelTypeArray LogicalModelTypeArrayC {Bool
LogicalModelType 'DataConnector
lmtaArray :: LogicalModelType 'DataConnector
lmtaNullable :: Bool
lmtaArray :: forall (b :: BackendType).
LogicalModelTypeArray b -> LogicalModelType b
lmtaNullable :: forall (b :: BackendType). LogicalModelTypeArray b -> Bool
..} -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> LogicalModelType 'DataConnector
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModelType HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels LogicalModelType 'DataConnector
lmtaArray
      LogicalModelTypeReference LogicalModelTypeReferenceC {Bool
LogicalModelName
lmtrReference :: LogicalModelName
lmtrNullable :: Bool
lmtrReference :: LogicalModelTypeReference -> LogicalModelName
lmtrNullable :: LogicalModelTypeReference -> Bool
..} -> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
-> LogicalModelName
-> HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
collectLogicalModelName HashMap LogicalModelName (LogicalModelInfo 'DataConnector)
seenLogicalModels LogicalModelName
lmtrReference

    logicalModelInfoToMetadata :: LogicalModelInfo 'DataConnector -> LogicalModelMetadata 'DataConnector
    logicalModelInfoToMetadata :: LogicalModelInfo 'DataConnector
-> LogicalModelMetadata 'DataConnector
logicalModelInfoToMetadata LogicalModelInfo {Maybe Text
RolePermInfoMap 'DataConnector
InsOrdHashMap
  (Column 'DataConnector) (LogicalModelField 'DataConnector)
LogicalModelName
_lmiFields :: forall (b :: BackendType).
LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
_lmiName :: LogicalModelName
_lmiFields :: InsOrdHashMap
  (Column 'DataConnector) (LogicalModelField 'DataConnector)
_lmiDescription :: Maybe Text
_lmiPermissions :: RolePermInfoMap 'DataConnector
_lmiName :: forall (b :: BackendType). LogicalModelInfo b -> LogicalModelName
_lmiDescription :: forall (b :: BackendType). LogicalModelInfo b -> Maybe Text
_lmiPermissions :: forall (b :: BackendType). LogicalModelInfo b -> RolePermInfoMap b
..} =
      LogicalModelMetadata
        { _lmmName :: LogicalModelName
_lmmName = LogicalModelName
_lmiName,
          _lmmFields :: InsOrdHashMap
  (Column 'DataConnector) (LogicalModelField 'DataConnector)
_lmmFields = InsOrdHashMap
  (Column 'DataConnector) (LogicalModelField 'DataConnector)
_lmiFields,
          _lmmDescription :: Maybe Text
_lmmDescription = Maybe Text
_lmiDescription,
          _lmmSelectPermissions :: InsOrdHashMap RoleName (SelPermDef 'DataConnector)
_lmmSelectPermissions = InsOrdHashMap RoleName (SelPermDef 'DataConnector)
forall a. Monoid a => a
mempty
        }

    convertForeignKeys :: HashSet (RQL.T.T.ForeignKeyMetadata 'DataConnector) -> SourceForeignKeys 'DataConnector
    convertForeignKeys :: HashSet (ForeignKeyMetadata 'DataConnector)
-> SourceForeignKeys 'DataConnector
convertForeignKeys HashSet (ForeignKeyMetadata 'DataConnector)
foreignKeys =
      HashSet (ForeignKeyMetadata 'DataConnector)
foreignKeys
        HashSet (ForeignKeyMetadata 'DataConnector)
-> (HashSet (ForeignKeyMetadata 'DataConnector)
    -> [ForeignKeyMetadata 'DataConnector])
-> [ForeignKeyMetadata 'DataConnector]
forall a b. a -> (a -> b) -> b
& HashSet (ForeignKeyMetadata 'DataConnector)
-> [ForeignKeyMetadata 'DataConnector]
forall a. HashSet a -> [a]
HashSet.toList
        [ForeignKeyMetadata 'DataConnector]
-> ([ForeignKeyMetadata 'DataConnector]
    -> [(ConstraintName, SourceConstraint 'DataConnector)])
-> [(ConstraintName, SourceConstraint 'DataConnector)]
forall a b. a -> (a -> b) -> b
& (ForeignKeyMetadata 'DataConnector
 -> (ConstraintName, SourceConstraint 'DataConnector))
-> [ForeignKeyMetadata 'DataConnector]
-> [(ConstraintName, SourceConstraint 'DataConnector)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
          ( \(RQL.T.T.ForeignKeyMetadata RQL.T.T.ForeignKey {NEHashMap (Column 'DataConnector) (Column 'DataConnector)
TableName 'DataConnector
Constraint 'DataConnector
_fkConstraint :: forall (b :: BackendType). ForeignKey b -> Constraint b
_fkForeignTable :: forall (b :: BackendType). ForeignKey b -> TableName b
_fkColumnMapping :: forall (b :: BackendType).
ForeignKey b -> NEHashMap (Column b) (Column b)
_fkConstraint :: Constraint 'DataConnector
_fkForeignTable :: TableName 'DataConnector
_fkColumnMapping :: NEHashMap (Column 'DataConnector) (Column 'DataConnector)
..}) ->
              let constraintName :: ConstraintName 'DataConnector
constraintName = Constraint 'DataConnector -> ConstraintName 'DataConnector
forall (b :: BackendType). Constraint b -> ConstraintName b
RQL.T.T._cName Constraint 'DataConnector
_fkConstraint
                  constraint :: SourceConstraint 'DataConnector
constraint =
                    SourceConstraint
                      { _scForeignTable :: TableName 'DataConnector
_scForeignTable = TableName -> TableName
forall source target. From source target => source -> target
Witch.from TableName 'DataConnector
TableName
_fkForeignTable,
                        _scColumnMapping :: HashMap (Column 'DataConnector) (Column 'DataConnector)
_scColumnMapping = [(Column 'DataConnector, Column 'DataConnector)]
-> HashMap (Column 'DataConnector) (Column 'DataConnector)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(Column 'DataConnector, Column 'DataConnector)]
 -> HashMap (Column 'DataConnector) (Column 'DataConnector))
-> [(Column 'DataConnector, Column 'DataConnector)]
-> HashMap (Column 'DataConnector) (Column 'DataConnector)
forall a b. (a -> b) -> a -> b
$ (ColumnName -> ColumnName)
-> (ColumnName -> ColumnName)
-> (ColumnName, ColumnName)
-> (ColumnName, ColumnName)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from ColumnName -> ColumnName
forall source target. From source target => source -> target
Witch.from ((ColumnName, ColumnName) -> (ColumnName, ColumnName))
-> [(ColumnName, ColumnName)] -> [(ColumnName, ColumnName)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NEHashMap ColumnName ColumnName -> [(ColumnName, ColumnName)]
forall k v. NEHashMap k v -> [(k, v)]
NEHashMap.toList NEHashMap (Column 'DataConnector) (Column 'DataConnector)
NEHashMap ColumnName ColumnName
_fkColumnMapping
                      }
               in (ConstraintName 'DataConnector
ConstraintName
constraintName, SourceConstraint 'DataConnector
constraint)
          )
        [(ConstraintName, SourceConstraint 'DataConnector)]
-> ([(ConstraintName, SourceConstraint 'DataConnector)]
    -> HashMap ConstraintName (SourceConstraint 'DataConnector))
-> HashMap ConstraintName (SourceConstraint 'DataConnector)
forall a b. a -> (a -> b) -> b
& [(ConstraintName, SourceConstraint 'DataConnector)]
-> HashMap ConstraintName (SourceConstraint 'DataConnector)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
        HashMap ConstraintName (SourceConstraint 'DataConnector)
-> (HashMap ConstraintName (SourceConstraint 'DataConnector)
    -> SourceForeignKeys 'DataConnector)
-> SourceForeignKeys 'DataConnector
forall a b. a -> (a -> b) -> b
& HashMap
  (ConstraintName 'DataConnector) (SourceConstraint 'DataConnector)
-> SourceForeignKeys 'DataConnector
HashMap ConstraintName (SourceConstraint 'DataConnector)
-> SourceForeignKeys 'DataConnector
forall (b :: BackendType).
HashMap (ConstraintName b) (SourceConstraint b)
-> SourceForeignKeys b
SourceForeignKeys