on master help sign in
update dbc function documentation to include details on errors
commitc8abbcf3a86e596ec5744c76a2b8115e5824f2a9 authorAgentM <agentm@themactionfaction.com> dateSun 9 Apr 2017, 15:47 (10 years ago) parentf4794d3
1 file changed+8−8
docs/database_context_functions.markdown+8−8
docs/database_context_functions.markdown+8−8
@@ -13,10 +13,10 @@ In order to use this feature in Project:M36, be sure to build your own Project:M
1313The Haskell type of all database context functions is:
1414
1515```haskell
16−[Atom] -> DatabaseContext -> DatabaseContext
16+[Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext
1717```
1818
19−That is, the function takes an array of ```Atom```s which represent the function's arguments and a context and returns an updated context. Notice that this operation is idempotent ("pure" in Haskell parlance).
19+That is, the function takes an array of ```Atom```s which represent the function's arguments and a context and returns an updated context or an error. Notice that this operation is idempotent ("pure" in Haskell parlance).
2020
2121The database context references all data which can be committed; this includes relation variables, atom functions, notifications, database context functions, and other versionable data. Database contexts become immutable once committed.
2222
@@ -26,12 +26,14 @@ Database context functions are committed alongside the transaction. Thus, previo
2626
2727Remember that database context functions don't need to worry about locks or other synchronization. The database context is completely isolated from other ongoing transactions.
2828
29+Returning a ```DatabaseContextFunctionError``` from the function ensures that any changes made by the function are discarded.
30+
2931## TutorialD Demonstration
3032
3133Use ```adddatabasecontextfunction``` to compile and name a new database context function:
3234
3335```
34−adddatabasecontextfunction "addperson" Int -> Text -> DatabaseContext -> DatabaseContext """(\(age:name:_) ctx -> let newrel = MakeRelationFromExprs Nothing [TupleExpr (fromList [("name", NakedAtomExpr name),("age", NakedAtomExpr age)])] in if isRight (evalState (evalRelationalExpr (RelationVariable "person" ())) (mkRelationalExprState ctx)) then execState (evalDatabaseContextExpr (Insert "person" newrel)) ctx else execState (evalDatabaseContextExpr (Assign "person" newrel)) ctx) :: [Atom] -> DatabaseContext -> DatabaseContext"""
36+adddatabasecontextfunction "addperson" Int -> Text -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext """(\(age:name:_) ctx -> let newrel = MakeRelationFromExprs Nothing [TupleExpr (fromList [("name", NakedAtomExpr name),("age", NakedAtomExpr age)])] in if isRight (evalState (evalRelationalExpr (RelationVariable "person" ())) (mkRelationalExprState ctx)) then pure (execState (evalDatabaseContextExpr (Insert "person" newrel)) ctx) else pure (execState (evalDatabaseContextExpr (Assign "person" newrel)) ctx)) :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext"""
3537```
3638
3739This function is quite dense, so let's examine its components.
@@ -39,7 +41,7 @@ This function is quite dense, so let's examine its components.
3941The first components are:
4042
4143```
42−adddatabasecontextfunction "addperson" Int -> Text -> DatabaseContext -> DatabaseContext
44+adddatabasecontextfunction "addperson" Int -> Text -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext
4345```
4446
4547which defines a function "addperson" which takes three arguments (an Int value, a Text value, and a DatabaseContext) and returns a DatabaseContext. All database context functions take as a final argument a database context and must return a database context.
@@ -49,9 +51,9 @@ The meat of the function is obviously the Haskell, so let's lay it out:
4951```haskell
5052(\(age:name:_) ctx -> let newrel = MakeRelationFromExprs Nothing [TupleExpr (fromList [("name", NakedAtomExpr name),("age", NakedAtomExpr age)])] in
5153 if isRight (evalState (evalRelationalExpr (RelationVariable "person" ())) (mkRelationalExprState ctx)) then
52− execState (evalDatabaseContextExpr (Insert "person" newrel)) ctx
54+ pure (execState (evalDatabaseContextExpr (Insert "person" newrel)) ctx)
5355 else
54− execState (evalDatabaseContextExpr (Assign "person" newrel)) ctx) :: [Atom] -> DatabaseContext -> DatabaseContext
56+ pure (execState (evalDatabaseContextExpr (Assign "person" newrel)) ctx)) :: [Atom] -> DatabaseContext -> Either DatabaseContextFunctionError DatabaseContext
5557```
5658
5759Line 1 sets up a new relation created from the function's arguments. In this case, we create a relation to represent a new person in our database.
@@ -113,5 +115,3 @@ from the same module.
113115## Future Improvements
114116
115117There are likely a variety of convenience functions which would make sense in the context of the scripted function. If you have ideas for such functions, please open a GitHub issue.
116−
117−Error-handling is currently based on exceptions thrown from the functions. It would likely be smoother to be able to return errors from the functions instead.