on master help sign in
implement Tutorial D delete with predicate
5f5dd31AgentM
RelationExpr.hs+5−6
@@ -198,12 +198,11 @@ evalContextExpr (Assign relVarName expr) = do
198evalContextExpr (Insert relVarName relExpr) = evalContextExpr $ Assign relVarName (Union relExpr (RelationVariable relVarName))
198evalContextExpr (Insert relVarName relExpr) = evalContextExpr $ Assign relVarName (Union relExpr (RelationVariable relVarName))
199
199
200--assign empty rel until restriction is implemented
200--assign empty rel until restriction is implemented
201−evalContextExpr (Delete relVarName) = do
201+evalContextExpr (Delete relVarName predicate) = do
202− relVarTable <- liftM relationVariables get
202+ updatedRel <- evalRelationalExpr (Restrict (NotPredicate predicate) (RelationVariable relVarName))
203− case M.lookup relVarName relVarTable of
203+ case updatedRel of
204− Nothing -> return $ Just (RelVarNotDefinedError relVarName)
204+ Left err -> return $ Just err
205− Just rel -> setRelVar relVarName (Relation (attributes rel) emptyTupleSet)
205+ Right rel -> setRelVar relVarName rel
206−
207
206
208--union of restricted+updated portion and the unrestricted+unupdated portion
207--union of restricted+updated portion and the unrestricted+unupdated portion
209evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = do
208evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = do
RelationTupleSet.hs−1
@@ -3,7 +3,6 @@ import RelationType
3import RelationTuple
3import RelationTuple
4import RelationalError
4import RelationalError
5import qualified Data.Map as M
5import qualified Data.Map as M
6−import qualified Data.Set as S
7import qualified Data.HashSet as HS
6import qualified Data.HashSet as HS
8import Control.Monad
7import Control.Monad
9
8
RelationType.hs+2−1
@@ -76,9 +76,10 @@ instance Eq InclusionDependency where
76--Database context expressions modify the database context while relational expressions do not
76--Database context expressions modify the database context while relational expressions do not
77data DatabaseExpr where
77data DatabaseExpr where
78 Define :: String -> Attributes -> DatabaseExpr
78 Define :: String -> Attributes -> DatabaseExpr
79+ Undefine :: String -> DatabaseExpr --forget existence of relvar X
79 Assign :: String -> RelationalExpr -> DatabaseExpr
80 Assign :: String -> RelationalExpr -> DatabaseExpr
80 Insert :: String -> RelationalExpr -> DatabaseExpr
81 Insert :: String -> RelationalExpr -> DatabaseExpr
81− Delete :: String -> DatabaseExpr -- needs restriction support
82+ Delete :: String -> RestrictionPredicateExpr -> DatabaseExpr
82 Update :: String -> M.Map String Atom -> RestrictionPredicateExpr -> DatabaseExpr -- needs restriction support
83 Update :: String -> M.Map String Atom -> RestrictionPredicateExpr -> DatabaseExpr -- needs restriction support
83 AddInclusionDependency :: InclusionDependency -> DatabaseExpr
84 AddInclusionDependency :: InclusionDependency -> DatabaseExpr
84 MultipleExpr :: [DatabaseExpr] -> DatabaseExpr
85 MultipleExpr :: [DatabaseExpr] -> DatabaseExpr
TutorialDInterpreter.hs+2−1
@@ -204,7 +204,8 @@ deleteP :: Parser DatabaseExpr
204deleteP = do
204deleteP = do
205 reservedOp "delete"
205 reservedOp "delete"
206 relVarName <- identifier
206 relVarName <- identifier
207+ predicate <- option TruePredicate (reservedOp "where" *> restrictionPredicateP)
207− return $ Delete relVarName
208+ return $ Delete relVarName predicate
208
209
209updateP :: Parser DatabaseExpr
210updateP :: Parser DatabaseExpr
210updateP = do
211updateP = do
TutorialDInterpreterTest.hs+2−1
@@ -30,7 +30,8 @@ main = do
30 ("x:=true where true and true", Right relationTrue),
30 ("x:=true where true and true", Right relationTrue),
31 ("x:=true=true", Right relationTrue),
31 ("x:=true=true", Right relationTrue),
32 ("x:=true=false", Right relationFalse),
32 ("x:=true=false", Right relationFalse),
33− ("x:=relation {b int, a char}; insert x relation{tuple{b int(5), a char(\"spam\")}}", mkRelation simpleCAttributes (HS.fromList [RelationTuple $ M.fromList [("a", StringAtom "spam"), ("b", IntAtom 5)]]))
33+ ("x:=relation {b int, a char}; insert x relation{tuple{b int(5), a char(\"spam\")}}", mkRelation simpleCAttributes (HS.fromList [RelationTuple $ M.fromList [("a", StringAtom "spam"), ("b", IntAtom 5)]])),
34+ ("x:=relation{tuple{b int(5),a char(\"spam\")},tuple{b int(6),a char(\"sam\")}}; delete x where b=6", mkRelation simpleCAttributes $ HS.fromList [RelationTuple $ M.fromList [("a", StringAtom "spam"), ("b", IntAtom 5)]])
34 ]
35 ]
35 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
36 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
36 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
37 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]