on master help sign in
implement Tutorial D delete with predicate
commit5f5dd313fd9d56c9c72e4d9b1762521e47b00936 authorAgentM <agentm@themactionfaction.com> dateWed 28 Jan 2015, 22:22 (12 years ago) parent7f32362
5 files changed+11−10
RelationExpr.hs+5−6 RelationTupleSet.hs−1 RelationType.hs+2−1 TutorialDInterpreter.hs+2−1 TutorialDInterpreterTest.hs+2−1
RelationExpr.hs+5−6
@@ -198,12 +198,11 @@ evalContextExpr (Assign relVarName expr) = do
198198evalContextExpr (Insert relVarName relExpr) = evalContextExpr $ Assign relVarName (Union relExpr (RelationVariable relVarName))
199199
200200--assign empty rel until restriction is implemented
201−evalContextExpr (Delete relVarName) = do
202− relVarTable <- liftM relationVariables get
203− case M.lookup relVarName relVarTable of
204− Nothing -> return $ Just (RelVarNotDefinedError relVarName)
205− Just rel -> setRelVar relVarName (Relation (attributes rel) emptyTupleSet)
206−
201+evalContextExpr (Delete relVarName predicate) = do
202+ updatedRel <- evalRelationalExpr (Restrict (NotPredicate predicate) (RelationVariable relVarName))
203+ case updatedRel of
204+ Left err -> return $ Just err
205+ Right rel -> setRelVar relVarName rel
207206
208207--union of restricted+updated portion and the unrestricted+unupdated portion
209208evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = do
RelationTupleSet.hs−1
@@ -3,7 +3,6 @@ import RelationType
33import RelationTuple
44import RelationalError
55import qualified Data.Map as M
6−import qualified Data.Set as S
76import qualified Data.HashSet as HS
87import Control.Monad
98
RelationType.hs+2−1
@@ -76,9 +76,10 @@ instance Eq InclusionDependency where
7676--Database context expressions modify the database context while relational expressions do not
7777data DatabaseExpr where
7878 Define :: String -> Attributes -> DatabaseExpr
79+ Undefine :: String -> DatabaseExpr --forget existence of relvar X
7980 Assign :: String -> RelationalExpr -> DatabaseExpr
8081 Insert :: String -> RelationalExpr -> DatabaseExpr
81− Delete :: String -> DatabaseExpr -- needs restriction support
82+ Delete :: String -> RestrictionPredicateExpr -> DatabaseExpr
8283 Update :: String -> M.Map String Atom -> RestrictionPredicateExpr -> DatabaseExpr -- needs restriction support
8384 AddInclusionDependency :: InclusionDependency -> DatabaseExpr
8485 MultipleExpr :: [DatabaseExpr] -> DatabaseExpr
TutorialDInterpreter.hs+2−1
@@ -204,7 +204,8 @@ deleteP :: Parser DatabaseExpr
204204deleteP = do
205205 reservedOp "delete"
206206 relVarName <- identifier
207− return $ Delete relVarName
207+ predicate <- option TruePredicate (reservedOp "where" *> restrictionPredicateP)
208+ return $ Delete relVarName predicate
208209
209210updateP :: Parser DatabaseExpr
210211updateP = do
TutorialDInterpreterTest.hs+2−1
@@ -30,7 +30,8 @@ main = do
3030 ("x:=true where true and true", Right relationTrue),
3131 ("x:=true=true", Right relationTrue),
3232 ("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)]])
3435 ]
3536 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
3637 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]