Skip to content

Commit

Permalink
clickhouse client/local interactive: support \i file
Browse files Browse the repository at this point in the history
porting ClickHouse/ClickHouse#38813 (Jul 4, 2022)
  • Loading branch information
yokofly committed Oct 24, 2023
1 parent eab8cda commit 8ddffd1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Client/ClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,9 +1525,21 @@ MultiQueryProcessingStage ClientBase::analyzeMultiQueryText(

bool ClientBase::processQueryText(const String & text)
{
if (exit_strings.end() != exit_strings.find(trim(text, [](char c) { return isWhitespaceASCII(c) || c == ';'; })))
auto trimmed_input = trim(text, [](char c) { return isWhitespaceASCII(c) || c == ';'; });

if (exit_strings.end() != exit_strings.find(trimmed_input))
return false;

if (trimmed_input.starts_with("\\i"))
{
size_t skip_prefix_size = std::strlen("\\i");
auto file_name = trim(
trimmed_input.substr(skip_prefix_size, trimmed_input.size() - skip_prefix_size),
[](char c) { return isWhitespaceASCII(c); });

return processMultiQueryFromFile(file_name);
}

if (!is_multiquery)
{
assert(!query_fuzzer_runs);
Expand Down Expand Up @@ -1696,28 +1708,27 @@ void ClientBase::runInteractive()
std::cout << "Bye." << std::endl;
}

bool ClientBase::processMultiQueryFromFile(const String & file_name)
{
String queries_from_file;

ReadBufferFromFile in(file_name);
readStringUntilEOF(queries_from_file, in);

return executeMultiQuery(queries_from_file);
}

void ClientBase::runNonInteractive()
{
if (!queries_files.empty())
{
auto process_multi_query_from_file = [&](const String & file)
{
String queries_from_file;

ReadBufferFromFile in(file);
readStringUntilEOF(queries_from_file, in);

return executeMultiQuery(queries_from_file);
};

for (const auto & queries_file : queries_files)
{
for (const auto & interleave_file : interleave_queries_files)
if (!process_multi_query_from_file(interleave_file))
if (!processMultiQueryFromFile(interleave_file))
return;

if (!process_multi_query_from_file(queries_file))
if (!processMultiQueryFromFile(queries_file))
return;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Client/ClientBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class ClientBase : public Poco::Util::Application, public IHints<2, ClientBase>
SharedContextHolder shared_context;
ContextMutablePtr global_context;

bool processMultiQueryFromFile(const String & file_name);

bool is_interactive = false; /// Use either interactive line editing interface or batch mode.
bool is_multiquery = false;
bool delayed_interactive = false;
Expand Down

0 comments on commit 8ddffd1

Please sign in to comment.