Пример #1
0
def repl() -> None:
    while True:
        try:
            user_input = input(prompt_with_color())
        except (KeyboardInterrupt, EOFError):
            sys.exit(1)
        if user_input == "exit":
            break
        lexer = Lexer(program=user_input)
        parser = Parser(lexer=lexer)
        commands: List[Optional[Union[CreateTable, Insert, Select]]] = []
        try:
            commands = list(parser.parse())
        except ValueError as e:
            print(e)
            continue
        if len(commands) == 0:
            print("no command given")
        for command in commands:
            if command is None:
                continue
            try:
                tables = get_tables()
                evaluator = Evaluator(tables=tables)
                result = evaluator.handle_command(command)
            except ValueError as e:
                print(e)
                continue
            if isinstance(result, str):
                print(result)
            else:
                print_selection(result)
Пример #2
0
 def test_eval_in_memory(self):
     user_input = "select * from family"
     lexer = Lexer(program=user_input)
     parser = Parser(lexer=lexer)
     commands: List[Optional[Union[CreateTable, Insert, Select]]] = []
     commands = list(parser.parse())
     table = Table.from_file("family", storage_type="in-memory")
     tables = {"family": table}
     evaluator = Evaluator(tables=tables)
     result = evaluator.handle_command(commands[0])
     assert len(result) > 10
     assert result[0] == ["vegard", 26]
Пример #3
0
def run_command(evaluator: Evaluator,
                sql_string: str) -> Union[str, List[List[Union[str, int]]]]:
    lexer = Lexer(program=sql_string)
    parser = Parser(lexer=lexer)
    commands = list(parser.parse())
    return evaluator.handle_command(commands[0])