Repository navigation
Expand file tree
/
Copy pathClientEmbedded.cpp
More file actions
265 lines (215 loc) · 7.58 KB
/
Copy pathClientEmbedded.cpp
File metadata and controls
265 lines (215 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#if defined(OS_LINUX)
#include <Server/ClientEmbedded/ClientEmbedded.h>
#include <base/getFQDNOrHostName.h>
#include <Interpreters/Session.h>
#include <Interpreters/Context.h>
#include <Common/setThreadName.h>
#include <Common/Config/ConfigHelper.h>
#include <Common/Exception.h>
#include <Core/Settings.h>
#include <iomanip>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
namespace Setting
{
extern const SettingsNonZeroUInt64 max_insert_block_size;
}
ClientEmbedded::ClientEmbedded(
std::unique_ptr<Session> && session_,
int in_fd_,
int out_fd_,
int err_fd_,
std::istream & input_stream_,
std::ostream & output_stream_,
std::ostream & error_stream_)
: ClientBase(in_fd_, out_fd_, err_fd_, input_stream_, output_stream_, error_stream_), session(std::move(session_))
{
global_context = session->makeSessionContext();
configuration = ConfigHelper::createEmpty();
layered_configuration = new Poco::Util::LayeredConfiguration();
layered_configuration->addWriteable(configuration, 0);
}
void ClientEmbedded::printHelpMessage(const OptionsDescription & options_description)
{
output_stream << "Welcome to the ClickHouse embedded client!" << "\n";
output_stream << "This client runs on the server side inside the ClickHouse's main process." << "\n";
if (options_description.main_description.has_value())
output_stream << options_description.main_description.value() << "\n";
if (options_description.external_description.has_value())
output_stream << options_description.external_description.value() << "\n";
if (options_description.hosts_and_ports_description.has_value())
output_stream << options_description.hosts_and_ports_description.value() << "\n";
output_stream << "All settings are documented at https://clickhouse.com/docs/en/operations/settings/settings.\n\n";
output_stream << "See also: https://clickhouse.com/docs/en/integrations/sql-clients/cli\n";
}
void ClientEmbedded::processError(std::string_view) const
{
if (ignore_error)
return;
if (is_interactive)
{
String message;
if (server_exception)
{
message = getExceptionMessage(*server_exception, print_stack_trace, true);
}
else if (client_exception)
{
message = client_exception->message();
}
error_stream << fmt::format("Received exception\n{}\n\n", message);
}
else
{
if (server_exception)
server_exception->rethrow();
if (client_exception)
client_exception->rethrow();
}
}
void ClientEmbedded::cleanup()
{
try
{
connection.reset();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
void ClientEmbedded::connect()
{
if (!session)
{
throw Exception(ErrorCodes::LOGICAL_ERROR, "Error creating connection without session object");
}
connection_parameters = ConnectionParameters::createForEmbedded(session->sessionContext()->getUserName(), default_database);
connection = LocalConnection::createConnection(
connection_parameters, std::move(session), std_in.get(), need_render_progress, need_render_profile_events, server_display_name);
if (!default_database.empty())
{
connection->setDefaultDatabase(default_database);
}
}
Poco::Util::LayeredConfiguration & ClientEmbedded::getClientConfiguration()
{
chassert(layered_configuration);
return *layered_configuration;
}
bool ClientEmbedded::isEmbeeddedClient() const
{
return true;
}
int ClientEmbedded::run(const NameToNameMap & envVars, const String & first_query)
try
{
setThreadName("LocalServerPty");
output_stream << std::fixed << std::setprecision(3);
error_stream << std::fixed << std::setprecision(3);
/**
* To pass the environment variables through the SSH protocol you need to follow
* the format: ssh -o SetEnv="key1=value1 key2=value2"
* But the whole code is used to work with command line options, so we reconstruct them back.
*/
Arguments arguments;
arguments.reserve(envVars.size() * 2);
for (const auto & [key, value] : envVars)
{
arguments.emplace_back("--" + key);
arguments.emplace_back(value);
}
OptionsDescription options_description;
addCommonOptions(options_description);
addSettingsToProgramOptionsAndSubscribeToChanges(options_description);
po::variables_map options;
auto parser = po::command_line_parser(arguments)
.options(options_description.main_description.value())
.allow_unregistered();
auto parsed = parser.run();
po::store(parsed, options);
po::notify(options);
if (options.count("version") || options.count("V"))
{
showClientVersion();
cleanup();
return 0;
}
if (options.count("help"))
{
printHelpMessage(options_description);
cleanup();
return 0;
}
addOptionsToTheClientConfiguration(options);
/// Apply settings specified as command line arguments (read environment variables).
global_context = session->sessionContext();
global_context->setApplicationType(Context::ApplicationType::SERVER);
global_context->setSettings(*cmd_settings);
is_interactive = stdin_is_a_tty;
/// If a query is passed via SSH - just append it to the list of queries to execute:
/// ssh -i ~/.ssh/id_rsa default@localhost -p 9022 "SELECT 1"
if (!first_query.empty())
queries.push_back(first_query);
delayed_interactive = is_interactive && !queries.empty();
if (!is_interactive || delayed_interactive)
{
echo_queries = getClientConfiguration().getBool("echo", false);
ignore_error = getClientConfiguration().getBool("ignore-error", false);
}
load_suggestions = true;
wait_for_suggestions_to_load = true;
server_display_name = getFQDNOrHostName();
prompt = format("{} :) ", global_context->getConfigRef().getString("display_name", server_display_name));
query_processing_stage = QueryProcessingStage::Enum::Complete;
pager = getClientConfiguration().getString("pager", "");
enable_highlight = getClientConfiguration().getBool("highlight", true);
multiline = getClientConfiguration().has("multiline");
print_stack_trace = getClientConfiguration().getBool("stacktrace", false);
default_database = getClientConfiguration().getString("database", "");
setDefaultFormatsAndCompressionFromConfiguration();
initTTYBuffer(toProgressOption(getClientConfiguration().getString("progress", "default")),
toProgressOption(getClientConfiguration().getString("progress-table", "default")));
initKeystrokeInterceptor();
initClientContext(session->sessionContext());
/// Note, QueryScope will be initialized in the LocalConnection
if (is_interactive)
{
clearTerminal();
showClientVersion();
error_stream << std::endl;
}
connect();
if (is_interactive && !delayed_interactive)
{
runInteractive();
}
else
{
runNonInteractive();
if (delayed_interactive)
runInteractive();
}
cleanup();
return 0;
}
catch (const DB::Exception & e)
{
cleanup();
error_stream << getExceptionMessage(e, print_stack_trace, true) << std::endl;
auto code = DB::getCurrentExceptionCode();
return static_cast<UInt8>(code) ? code : 1;
}
catch (...)
{
cleanup();
error_stream << getCurrentExceptionMessage(false) << std::endl;
auto code = DB::getCurrentExceptionCode();
return static_cast<UInt8>(code) ? code : 1;
}
}
#endif