2018-03-08 17:36:17 +08:00
|
|
|
#include "repl.hpp"
|
2017-03-23 21:45:51 +08:00
|
|
|
|
2017-03-24 16:49:56 +08:00
|
|
|
#include <algorithm>
|
2017-03-23 21:45:51 +08:00
|
|
|
#include <iostream>
|
2017-03-24 16:49:56 +08:00
|
|
|
#include <iterator>
|
2017-03-23 21:45:51 +08:00
|
|
|
#include <sstream>
|
2017-04-28 18:06:18 +08:00
|
|
|
#include <vector>
|
2017-03-23 21:45:51 +08:00
|
|
|
|
2017-11-23 21:26:53 +08:00
|
|
|
#include "communication/result_stream_faker.hpp"
|
2017-03-23 21:45:51 +08:00
|
|
|
#include "query/exceptions.hpp"
|
|
|
|
#include "query/interpreter.hpp"
|
2017-04-10 18:22:48 +08:00
|
|
|
#include "query/typed_value.hpp"
|
|
|
|
#include "utils/algorithm.hpp"
|
2017-04-19 21:52:20 +08:00
|
|
|
#include "utils/exceptions.hpp"
|
2017-03-23 21:45:51 +08:00
|
|
|
|
2017-03-24 16:50:52 +08:00
|
|
|
#ifdef HAS_READLINE
|
|
|
|
|
2017-03-23 21:45:51 +08:00
|
|
|
#include "readline/history.h"
|
|
|
|
#include "readline/readline.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that reads a line from the
|
|
|
|
* standard input using the 'readline' lib.
|
|
|
|
* Adds support for history and reverse-search.
|
|
|
|
*
|
|
|
|
* @param prompt The prompt to display.
|
|
|
|
* @return A single command the user entered.
|
|
|
|
* Possibly empty.
|
|
|
|
*/
|
|
|
|
std::string ReadLine(const char *prompt) {
|
2017-03-24 16:49:56 +08:00
|
|
|
char *line = readline(prompt);
|
|
|
|
if (!line) return "";
|
|
|
|
|
|
|
|
if (*line) add_history(line);
|
|
|
|
std::string r_val(line);
|
|
|
|
free(line);
|
2017-03-23 21:45:51 +08:00
|
|
|
return r_val;
|
|
|
|
}
|
|
|
|
|
2017-03-24 16:50:52 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
std::string ReadLine(const char *prompt) {
|
|
|
|
std::cout << prompt;
|
|
|
|
std::string line;
|
|
|
|
std::getline(std::cin, line);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAS_READLINE
|
|
|
|
|
2018-01-12 22:17:04 +08:00
|
|
|
void query::Repl(database::GraphDb &db) {
|
2018-03-13 17:35:14 +08:00
|
|
|
query::Interpreter interpeter{db};
|
2018-03-08 17:36:17 +08:00
|
|
|
|
2017-03-24 16:49:56 +08:00
|
|
|
std::cout
|
|
|
|
<< "Welcome to *Awesome* Memgraph Read Evaluate Print Loop (AM-REPL)"
|
|
|
|
<< std::endl;
|
2017-03-23 21:45:51 +08:00
|
|
|
while (true) {
|
|
|
|
std::string command = ReadLine(">");
|
|
|
|
if (command.size() == 0) continue;
|
|
|
|
|
|
|
|
// special commands
|
|
|
|
if (command == "quit") break;
|
|
|
|
|
|
|
|
// regular cypher queries
|
|
|
|
try {
|
2018-01-12 22:17:04 +08:00
|
|
|
database::GraphDbAccessor dba(db);
|
2017-03-23 21:45:51 +08:00
|
|
|
ResultStreamFaker results;
|
2017-12-22 20:39:31 +08:00
|
|
|
interpeter(command, dba, {}, false).PullAll(results);
|
2017-11-23 21:26:53 +08:00
|
|
|
std::cout << results;
|
2017-10-30 17:43:25 +08:00
|
|
|
dba.Commit();
|
2017-03-23 21:45:51 +08:00
|
|
|
} catch (const query::SyntaxException &e) {
|
|
|
|
std::cout << "SYNTAX EXCEPTION: " << e.what() << std::endl;
|
2017-07-28 22:32:52 +08:00
|
|
|
} catch (const query::LexingException &e) {
|
|
|
|
std::cout << "LEXING EXCEPTION: " << e.what() << std::endl;
|
2017-03-23 21:45:51 +08:00
|
|
|
} catch (const query::SemanticException &e) {
|
|
|
|
std::cout << "SEMANTIC EXCEPTION: " << e.what() << std::endl;
|
2017-04-10 15:11:14 +08:00
|
|
|
} catch (const query::QueryRuntimeException &e) {
|
|
|
|
std::cout << "RUNTIME EXCEPTION: " << e.what() << std::endl;
|
2017-04-12 20:38:22 +08:00
|
|
|
} catch (const query::TypedValueException &e) {
|
|
|
|
std::cout << "TYPED VALUE EXCEPTION: " << e.what() << std::endl;
|
2017-07-14 19:58:25 +08:00
|
|
|
} catch (const query::HintedAbortError &e) {
|
|
|
|
std::cout << "HINTED ABORT ERROR: " << e.what() << std::endl;
|
2017-04-19 21:52:20 +08:00
|
|
|
} catch (const utils::NotYetImplemented &e) {
|
2017-04-19 19:14:43 +08:00
|
|
|
std::cout << e.what() << std::endl;
|
2017-03-23 21:45:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|