@@ -2,13 +2,15 @@
2module ProjectM36.IsomorphicSchema where
2module ProjectM36.IsomorphicSchema where
3import ProjectM36.Base
3import ProjectM36.Base
4import ProjectM36.Error
4import ProjectM36.Error
5+import ProjectM36.RelationalExpression
5import Data.Maybe
6import Data.Maybe
6import Control.Monad
7import Control.Monad
8+import Control.Monad.State
7import GHC.Generics
9import GHC.Generics
8import Data.Binary
10import Data.Binary
9import qualified Data.Map as M
11import qualified Data.Map as M
10import qualified Data.Set as S
12import qualified Data.Set as S
11−import Debug.Trace
13+--import Debug.Trace
12-- isomorphic schemas offer bi-directional functors between two schemas
14-- isomorphic schemas offer bi-directional functors between two schemas
14--TODO: note that renaming a relvar should alter any stored isomorphisms as well
16--TODO: note that renaming a relvar should alter any stored isomorphisms as well
@@ -84,6 +86,16 @@ processDatabaseContextExprInSchema schema@(Schema morphs) dbExpr = do
84 validRelVarNames = isomorphsInRelVarNames morphs
86 validRelVarNames = isomorphsInRelVarNames morphs
85 foldM processDbExpr dbExpr morphs
87 foldM processDbExpr dbExpr morphs
89+-- re-evaluate- it's not possible to display an incdep that may be for a foreign key to a relvar which is not available in the subschema!
90+-- weird compromise: allow inclusion dependencies failures not in the subschema to be propagated- in the worst case, only the inclusion dependency's name is leaked.
92+-- | Convert inclusion dependencies for display in a specific schema.
93+applySchemaToInclusionDependencies :: Schema -> InclusionDependencies -> Either RelationalError InclusionDependencies
94+applySchemaToInclusionDependencies (Schema morphs) incDeps =
95+ let incDepMorph incDep = --check that the mentioned relvars are in fact in the current schema
96+ M.update incDepMorph incDeps
88-- | Morph a relational expression in one schema to another isomorphic schema.
100-- | Morph a relational expression in one schema to another isomorphic schema.
89relExprMorph :: SchemaIsomorph -> (RelationalExpr -> Either RelationalError RelationalExpr)
101relExprMorph :: SchemaIsomorph -> (RelationalExpr -> Either RelationalError RelationalExpr)
@@ -197,6 +209,22 @@ applyInclusionDependenciesSchemaIsoMorphs :: SchemaIsomorphs -> InclusionDepende
197--add inc deps for restriction with some automatic name
209--add inc deps for restriction with some automatic name
198applyInclusionDependenciesSchemaIsoMorphs morphs incDeps = undefined
210applyInclusionDependenciesSchemaIsoMorphs morphs incDeps = undefined
212+relationVariablesInSchema :: Schema -> DatabaseContext -> Either RelationalError RelationVariables
213+relationVariablesInSchema schema@(Schema morphs) context = if null morphs then --main schema
214+ pure (relationVariables context)
216+ foldM transform M.empty morphs
218+ transform newRvMap morph = do
219+ let rvNames = isomorphInRelVarNames morph
220+ rvAssocs <- mapM (\rv -> do
221+ expr' <- processRelationalExprInSchema schema (RelationVariable rv ())
222+ rel <- evalState (evalRelationalExpr expr') context
223+ pure (rv, rel)) rvNames
224+ pure (M.union newRvMap (M.fromList rvAssocs))
202data DatabaseContext =
230data DatabaseContext =