ClickHouse/dbms/programs/client/ConnectionParameters.h

91 lines
2.8 KiB
C++
Raw Normal View History

2018-08-25 13:55:18 +00:00
#pragma once
#include <iostream>
#include <Core/Types.h>
#include <Core/Protocol.h>
#include <Core/Defines.h>
#include <Common/Exception.h>
#include <IO/ConnectionTimeouts.h>
2019-02-02 00:25:12 +00:00
#include <common/setTerminalEcho.h>
2018-08-25 13:55:18 +00:00
#include <ext/scope_guard.h>
#include <Poco/Util/AbstractConfiguration.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
struct ConnectionParameters
{
String host;
2019-01-19 23:27:52 +00:00
UInt16 port{};
2018-08-25 13:55:18 +00:00
String default_database;
String user;
String password;
2019-01-19 23:27:52 +00:00
Protocol::Secure security = Protocol::Secure::Disable;
Protocol::Compression compression = Protocol::Compression::Enable;
2018-08-25 13:55:18 +00:00
ConnectionTimeouts timeouts;
ConnectionParameters() {}
ConnectionParameters(const Poco::Util::AbstractConfiguration & config)
{
bool is_secure = config.getBool("secure", false);
security = is_secure
? Protocol::Secure::Enable
: Protocol::Secure::Disable;
host = config.getString("host", "localhost");
port = config.getInt("port",
config.getInt(is_secure ? "tcp_port_secure" : "tcp_port",
is_secure ? DBMS_DEFAULT_SECURE_PORT : DBMS_DEFAULT_PORT));
default_database = config.getString("database", "");
/// changed the default value to "default" to fix the issue when the user in the prompt is blank
user = config.getString("user", "default");
bool password_prompt = false;
2018-08-25 13:55:18 +00:00
if (config.getBool("ask-password", false))
{
if (config.has("password"))
throw Exception("Specified both --password and --ask-password. Remove one of them", ErrorCodes::BAD_ARGUMENTS);
password_prompt = true;
2018-08-25 13:55:18 +00:00
}
else
{
password = config.getString("password", "");
/// if the value of --password is omitted, the password will be set implicitly to "\n"
2019-02-02 17:52:57 +00:00
if (password == "\n")
password_prompt = true;
2018-08-25 13:55:18 +00:00
}
if (password_prompt)
{
std::cout << "Password for user (" << user << "): ";
2019-02-02 14:35:51 +00:00
setTerminalEcho(false);
2018-08-25 13:55:18 +00:00
SCOPE_EXIT({
2019-02-02 00:25:12 +00:00
setTerminalEcho(true);
2018-08-25 13:55:18 +00:00
});
std::getline(std::cin, password);
std::cout << std::endl;
}
compression = config.getBool("compression", true)
? Protocol::Compression::Enable
: Protocol::Compression::Disable;
timeouts = ConnectionTimeouts(
Poco::Timespan(config.getInt("connect_timeout", DBMS_DEFAULT_CONNECT_TIMEOUT_SEC), 0),
2018-10-22 23:02:57 +00:00
Poco::Timespan(config.getInt("send_timeout", DBMS_DEFAULT_SEND_TIMEOUT_SEC), 0),
Poco::Timespan(config.getInt("receive_timeout", DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC), 0),
2018-10-22 23:02:57 +00:00
Poco::Timespan(config.getInt("tcp_keep_alive_timeout", 0), 0));
2018-08-25 13:55:18 +00:00
}
};
}