From 93267531bee8e94f8114f26667668487a042eef0 Mon Sep 17 00:00:00 2001 From: Marko Culinovic Date: Wed, 17 Oct 2018 16:07:16 +0200 Subject: [PATCH] Improve mg_client Summary: - Set query time to the milisecond precision. - Print 'Bye' when `EOF` is received in interactive mode. - In non-interactive mode output exit on first failed query and print the query along with the error message. - Don't quote stings when they are alone Reviewers: mferencevic Reviewed By: mferencevic Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D1670 --- tools/src/mg_client/main.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/tools/src/mg_client/main.cpp b/tools/src/mg_client/main.cpp index 902608916..293e7fc18 100644 --- a/tools/src/mg_client/main.cpp +++ b/tools/src/mg_client/main.cpp @@ -115,6 +115,22 @@ static const std::string kMultilinePrompt = " -> "; static void PrintHelp() { std::cout << kInteractiveUsage << std::endl; } +static void PrintValue(std::ostream &os, const std::string &value) { + os << value; +} + +static void PrintValue(std::ostream &os, + const communication::bolt::Value &value) { + switch (value.type()) { + case communication::bolt::Value::Type::String: + os << value.ValueString(); + return; + default: + os << value; + return; + } +} + static void EchoFailure(const std::string &failure_msg, const std::string &explanation) { if (isatty(STDIN_FILENO)) { @@ -367,7 +383,7 @@ static void PrintRowTabular(const std::vector &data, int total_width, int idx = i / column_width; if (idx < num_columns) { std::stringstream field; - field << data[idx]; // convert Value to string + PrintValue(field, data[idx]); // convert Value to string std::string field_str(field.str()); if (field_str.size() > column_width - 2 * margin - 1) { field_str.erase(column_width - 2 * margin - 1, std::string::npos); @@ -394,7 +410,7 @@ static uint64_t GetMaxColumnWidth(const std::vector &data, int margin = 1) { uint64_t column_width = 0; for (auto &elem : data) { std::stringstream field; - field << elem; + PrintValue(field, elem); column_width = std::max(column_width, field.str().size() + 2 * margin); } return column_width + 1; @@ -625,7 +641,10 @@ int main(int argc, char **argv) { int num_retries = 3; while (true) { auto query = GetQuery(); - if (!query) break; + if (!query) { + EchoInfo("Bye"); + break; + } if (query->empty()) continue; try { utils::Timer t; @@ -641,7 +660,7 @@ int main(int argc, char **argv) { } else { summary = std::to_string(ret.records.size()) + " rows in set"; } - std::cout << summary << " (" << fmt::format("{:.2f}", elapsed) + std::cout << summary << " (" << fmt::format("{:.3f}", elapsed) << " sec)" << std::endl; #ifdef HAS_READLINE auto history_ret = save_history(); @@ -649,7 +668,13 @@ int main(int argc, char **argv) { #endif } } catch (const communication::bolt::ClientQueryException &e) { + if (!isatty(STDIN_FILENO)) { + EchoFailure("Failed query", *query); + } EchoFailure("Client received exception", e.what()); + if (!isatty(STDIN_FILENO)) { + return 1; + } } catch (const communication::bolt::ClientFatalException &e) { EchoFailure("Client received exception", e.what()); EchoInfo("Trying to reconnect");