2020-02-04 22:37:04 +00:00
|
|
|
#include <Parsers/ASTCreateUserQuery.h>
|
2020-05-30 14:18:08 +00:00
|
|
|
#include <Parsers/ASTUserNameWithHost.h>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Parsers/ASTExtendedRoleSet.h>
|
2020-03-18 14:11:44 +00:00
|
|
|
#include <Parsers/ASTSettingsProfileElement.h>
|
2020-02-04 22:37:04 +00:00
|
|
|
#include <Common/quoteString.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-05-20 20:31:55 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void formatRenameTo(const String & new_name, const IAST::FormatSettings & settings)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " RENAME TO " << (settings.hilite ? IAST::hilite_none : "")
|
|
|
|
<< quoteString(new_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-20 20:31:55 +00:00
|
|
|
void formatAuthentication(const Authentication & authentication, bool show_password, const IAST::FormatSettings & settings)
|
2020-02-04 22:37:04 +00:00
|
|
|
{
|
2020-05-20 20:31:55 +00:00
|
|
|
auto authentication_type = authentication.getType();
|
|
|
|
if (authentication_type == Authentication::NO_PASSWORD)
|
2020-02-04 22:37:04 +00:00
|
|
|
{
|
2020-05-20 20:31:55 +00:00
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " NOT IDENTIFIED"
|
|
|
|
<< (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
return;
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
2020-05-20 20:31:55 +00:00
|
|
|
|
|
|
|
String authentication_type_name = Authentication::TypeInfo::get(authentication_type).name;
|
|
|
|
std::optional<String> password;
|
|
|
|
|
|
|
|
if (show_password)
|
|
|
|
{
|
|
|
|
switch (authentication_type)
|
|
|
|
{
|
|
|
|
case Authentication::PLAINTEXT_PASSWORD:
|
|
|
|
{
|
|
|
|
password = authentication.getPassword();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Authentication::SHA256_PASSWORD:
|
|
|
|
{
|
|
|
|
authentication_type_name = "sha256_hash";
|
|
|
|
password = authentication.getPasswordHashHex();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Authentication::DOUBLE_SHA1_PASSWORD:
|
|
|
|
{
|
|
|
|
authentication_type_name = "double_sha1_hash";
|
|
|
|
password = authentication.getPasswordHashHex();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Authentication::NO_PASSWORD: [[fallthrough]];
|
|
|
|
case Authentication::MAX_TYPE:
|
|
|
|
throw Exception("AST: Unexpected authentication type " + toString(authentication_type), ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " IDENTIFIED WITH " << authentication_type_name
|
|
|
|
<< (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
if (password)
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " BY " << quoteString(*password);
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void formatHosts(const char * prefix, const AllowedClientHosts & hosts, const IAST::FormatSettings & settings)
|
|
|
|
{
|
|
|
|
if (prefix)
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " " << prefix << " HOST "
|
|
|
|
<< (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
else
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " HOST " << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
|
|
|
|
if (hosts.empty())
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "NONE" << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hosts.containsAnyHost())
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "ANY" << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_comma = false;
|
|
|
|
if (hosts.containsLocalHost())
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "LOCAL" << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto & addresses = hosts.getAddresses();
|
|
|
|
const auto & subnets = hosts.getSubnets();
|
|
|
|
if (!addresses.empty() || !subnets.empty())
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "IP " << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
bool need_comma2 = false;
|
|
|
|
for (const auto & address : addresses)
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma2, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << quoteString(address.toString());
|
|
|
|
}
|
|
|
|
for (const auto & subnet : subnets)
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma2, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << quoteString(subnet.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto & names = hosts.getNames();
|
|
|
|
if (!names.empty())
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "NAME " << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
bool need_comma2 = false;
|
|
|
|
for (const auto & name : names)
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma2, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << quoteString(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto & name_regexps = hosts.getNameRegexps();
|
|
|
|
if (!name_regexps.empty())
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma, true))
|
|
|
|
settings.ostr << ", ";
|
2020-04-08 23:57:45 +00:00
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "REGEXP " << (settings.hilite ? IAST::hilite_none : "");
|
2020-02-04 22:37:04 +00:00
|
|
|
bool need_comma2 = false;
|
|
|
|
for (const auto & host_regexp : name_regexps)
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma2, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << quoteString(host_regexp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto & like_patterns = hosts.getLikePatterns();
|
|
|
|
if (!like_patterns.empty())
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "LIKE " << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
bool need_comma2 = false;
|
|
|
|
for (const auto & like_pattern : like_patterns)
|
|
|
|
{
|
|
|
|
if (std::exchange(need_comma2, true))
|
|
|
|
settings.ostr << ", ";
|
|
|
|
settings.ostr << quoteString(like_pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void formatDefaultRoles(const ASTExtendedRoleSet & default_roles, const IAST::FormatSettings & settings)
|
2020-02-17 02:59:56 +00:00
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " DEFAULT ROLE " << (settings.hilite ? IAST::hilite_none : "");
|
|
|
|
default_roles.format(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
void formatSettings(const ASTSettingsProfileElements & settings, const IAST::FormatSettings & format)
|
2020-02-04 22:37:04 +00:00
|
|
|
{
|
2020-03-18 14:11:44 +00:00
|
|
|
format.ostr << (format.hilite ? IAST::hilite_keyword : "") << " SETTINGS " << (format.hilite ? IAST::hilite_none : "");
|
|
|
|
settings.format(format);
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String ASTCreateUserQuery::getID(char) const
|
|
|
|
{
|
|
|
|
return "CreateUserQuery";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTPtr ASTCreateUserQuery::clone() const
|
|
|
|
{
|
|
|
|
return std::make_shared<ASTCreateUserQuery>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
void ASTCreateUserQuery::formatImpl(const FormatSettings & format, FormatState &, FormatStateStacked) const
|
2020-02-04 22:37:04 +00:00
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
if (attach)
|
|
|
|
{
|
2020-03-18 14:11:44 +00:00
|
|
|
format.ostr << (format.hilite ? hilite_keyword : "") << "ATTACH USER" << (format.hilite ? hilite_none : "");
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-18 14:11:44 +00:00
|
|
|
format.ostr << (format.hilite ? hilite_keyword : "") << (alter ? "ALTER USER" : "CREATE USER")
|
|
|
|
<< (format.hilite ? hilite_none : "");
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
2020-02-04 22:37:04 +00:00
|
|
|
|
|
|
|
if (if_exists)
|
2020-03-18 14:11:44 +00:00
|
|
|
format.ostr << (format.hilite ? hilite_keyword : "") << " IF EXISTS" << (format.hilite ? hilite_none : "");
|
2020-02-04 22:37:04 +00:00
|
|
|
else if (if_not_exists)
|
2020-03-18 14:11:44 +00:00
|
|
|
format.ostr << (format.hilite ? hilite_keyword : "") << " IF NOT EXISTS" << (format.hilite ? hilite_none : "");
|
2020-02-04 22:37:04 +00:00
|
|
|
else if (or_replace)
|
2020-03-18 14:11:44 +00:00
|
|
|
format.ostr << (format.hilite ? hilite_keyword : "") << " OR REPLACE" << (format.hilite ? hilite_none : "");
|
2020-02-04 22:37:04 +00:00
|
|
|
|
2020-05-30 14:18:08 +00:00
|
|
|
format.ostr << " ";
|
|
|
|
names->format(format);
|
2020-02-04 22:37:04 +00:00
|
|
|
|
2020-04-05 23:03:20 +00:00
|
|
|
formatOnCluster(format);
|
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
if (!new_name.empty())
|
2020-03-18 14:11:44 +00:00
|
|
|
formatRenameTo(new_name, format);
|
2020-02-04 22:37:04 +00:00
|
|
|
|
|
|
|
if (authentication)
|
2020-05-20 20:31:55 +00:00
|
|
|
formatAuthentication(*authentication, show_password, format);
|
2020-02-04 22:37:04 +00:00
|
|
|
|
|
|
|
if (hosts)
|
2020-03-18 14:11:44 +00:00
|
|
|
formatHosts(nullptr, *hosts, format);
|
2020-02-04 22:37:04 +00:00
|
|
|
if (add_hosts)
|
2020-03-18 14:11:44 +00:00
|
|
|
formatHosts("ADD", *add_hosts, format);
|
2020-02-04 22:37:04 +00:00
|
|
|
if (remove_hosts)
|
2020-04-05 18:44:28 +00:00
|
|
|
formatHosts("DROP", *remove_hosts, format);
|
2020-02-04 22:37:04 +00:00
|
|
|
|
2020-02-17 02:59:56 +00:00
|
|
|
if (default_roles)
|
2020-03-18 14:11:44 +00:00
|
|
|
formatDefaultRoles(*default_roles, format);
|
2020-02-17 02:59:56 +00:00
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
if (settings && (!settings->empty() || alter))
|
|
|
|
formatSettings(*settings, format);
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
}
|