1{-# LANGUAGE GADTs,OverloadedStrings #-}
2module TutorialD.Interpreter where
3import TutorialD.Interpreter.Base
4import TutorialD.Interpreter.RODatabaseContextOperator
5import TutorialD.Interpreter.DatabaseExpr
6import TutorialD.Interpreter.TransactionGraphOperator
8import TutorialD.Interpreter.Import.CSV
9import TutorialD.Interpreter.Import.TutorialD
10import TutorialD.Interpreter.Import.Base
12import TutorialD.Interpreter.Export.CSV
13import TutorialD.Interpreter.Export.Base
16import ProjectM36.Relation.Show.Term
17import ProjectM36.TransactionGraph
18import qualified ProjectM36.Client as C
20import Text.Parsec.String
21import Control.Monad.State
22import System.Console.Haskeline
23import System.Directory (getHomeDirectory)
24import qualified Data.Text as T
25import Data.Maybe (fromMaybe)
28context ops are read-only operations which only operate on the database context (relvars and constraints)
29database ops are read-write operations which change the database context (such as relvar assignment)
30graph ops are read-write operations which change the transaction graph
32data ParsedOperation = RODatabaseContextOp RODatabaseContextOperator |
33 DatabaseExprOp DatabaseExpr |
34 GraphOp TransactionGraphOperator |
35 ROGraphOp ROTransactionGraphOperator |
36 ImportRelVarOp RelVarDataImportOperator |
37 ImportDBContextOp DatabaseContextDataImportOperator |
38 RelVarExportOp RelVarDataExportOperator
40interpreterParserP :: Parser ParsedOperation
41interpreterParserP = liftM RODatabaseContextOp (roDatabaseContextOperatorP <* eof) <|>
42 liftM GraphOp (transactionGraphOpP <* eof) <|>
43 liftM ROGraphOp (roTransactionGraphOpP <* eof) <|>
44 liftM DatabaseExprOp (databaseExprOpP <* eof) <|>
45 liftM ImportRelVarOp (importCSVP <* eof) <|>
46 liftM ImportDBContextOp (tutdImportP <* eof) <|>
47 liftM RelVarExportOp (exportCSVP <* eof)
49promptText :: Maybe HeadName -> StringType
50promptText mHeadName = "TutorialD (" `T.append` transInfo `T.append` "): "
52 transInfo = fromMaybe "<unknown>" mHeadName
54parseTutorialD :: String -> Either ParseError ParsedOperation
55parseTutorialD inputString = parse interpreterParserP "" inputString
57--execute the operation and display result
58evalTutorialD :: C.Connection -> ParsedOperation -> IO (TutorialDOperatorResult)
59evalTutorialD conn expr = case expr of
61 --this does not pass through the ProjectM36.Client library because the operations
62 --are specific to the interpreter, though some operations may be of general use in the future
63 (RODatabaseContextOp execOp) -> do
64 evalRODatabaseContextOp conn execOp
66 (DatabaseExprOp execOp) -> do
67 maybeErr <- C.executeDatabaseContextExpr conn execOp
70 Nothing -> return QuietSuccessResult
72 (GraphOp execOp) -> do
73 maybeErr <- C.executeGraphExpr conn execOp
76 Nothing -> return QuietSuccessResult
78 (ROGraphOp execOp) -> do
79 opResult <- evalROGraphOp conn execOp
82 Right rel -> return $ DisplayResult $ showRelation rel
84 (ImportRelVarOp execOp@(RelVarDataImportOperator relVarName _ _)) -> do
85 -- collect attributes from relvar name
86 -- is there a race condition here? The attributes of the relvar may have since changed, no?
87 eImportAttributes <- C.attributesForRelationalExpr conn (RelationVariable relVarName)
88 case eImportAttributes of
90 Right importAttributes -> do
91 exprErr <- evalRelVarDataImportOperator execOp importAttributes
94 Right dbexpr -> evalTutorialD conn (DatabaseExprOp dbexpr)
96 (ImportDBContextOp execOp) -> do
97 mDbexprs <- evalDatabaseContextDataImportOperator execOp
100 Right dbexprs -> evalTutorialD conn (DatabaseExprOp dbexprs)
102 (RelVarExportOp execOp@(RelVarDataExportOperator relExpr _ _)) -> do
103 --eval relexpr to relation and pass to export function
104 eRel <- C.executeRelationalExpr conn relExpr
108 exportResult <- evalRelVarDataExportOperator execOp rel
111 Nothing -> pure QuietSuccessResult
113 barf err = return $ DisplayErrorResult (T.pack (show err))
115data InterpreterConfig = LocalInterpreterConfig PersistenceStrategy |
116 RemoteInterpreterConfig C.NodeId C.DatabaseName
118reprLoop :: InterpreterConfig -> C.Connection -> IO ()
119reprLoop config conn = do
120 homeDirectory <- getHomeDirectory
121 let settings = defaultSettings {historyFile = Just (homeDirectory ++ "/.tutd_history")}
122 mHeadName <- C.headName conn
123 maybeLine <- runInputT settings $ getInputLine (T.unpack (promptText mHeadName))
127 case parseTutorialD line of
129 displayOpResult $ DisplayErrorResult (T.pack (show err))
131 evald <- evalTutorialD conn parsed
132 displayOpResult evald
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list