on 62_python_dbapi help sign in
project-m36/examples/SimpleClient.hs
39 lines 1.8 kB history raw
1import ProjectM36.Client
2import ProjectM36.TupleSet
3import ProjectM36.Base
4import ProjectM36.Relation.Show.Term
6main :: IO ()
7main = do
8 -- 1. create a ConnectionInfo
9 let connInfo = RemoteProcessConnectionInfo "mytestdb" (createNodeId "127.0.0.1" defaultServerPort) emptyNotificationCallback
10 -- 2. conncted to the remote database
11 eConn <- connectProjectM36 connInfo
12 case eConn of
13 Left err -> putStrLn (show err)
14 Right conn -> do
15 --3. create a session on the "master" branch
16 eSessionId <- createSessionAtHead "master" conn
17 case eSessionId of
18 Left err -> putStrLn (show err)
19 Right sessionId -> do
20 --4. define a new relation variable with a DatabaseContext expression
21 let attrList = [Attribute "name" TextAtomType,
22 Attribute "age" IntAtomType]
23 attrs = attributesFromList attrList
24 mErr1 <- executeDatabaseContextExpr sessionId conn (Define "person" (map NakedAttributeExpr attrList))
25 putStrLn (show mErr1)
26 --5. add a tuple to the relation referenced by the relation variable
27 let (Right tupSet) = mkTupleSetFromList attrs [[TextAtom "Bob", IntAtom 45]]
28 mErr2 <- executeDatabaseContextExpr sessionId conn (Insert "person" (MakeStaticRelation attrs tupSet))
29 putStrLn (show mErr2)
31 --6. execute a relational algebra query
32 let restrictionPredicate = AttributeEqualityPredicate "name" (NakedAtomExpr (TextAtom "Steve"))
33 eRel <- executeRelationalExpr sessionId conn (Restrict restrictionPredicate (RelationVariable "person" ()))
34 case eRel of
35 Left err -> putStrLn (show err)
36 Right rel -> putStrLn (show $ showRelation rel)
38 --7. close the connection
39 close conn