Avoid writing "HOST ANY" if the host is any by default.

This commit is contained in:
Vitaly Baranov 2020-04-05 22:18:30 +03:00
parent d0af31bbcf
commit b77e0a5b4e
4 changed files with 32 additions and 41 deletions

View File

@ -91,7 +91,7 @@ public:
/// Allows IP addresses or host names using LIKE pattern.
/// This pattern can contain % and _ wildcard characters.
/// For example, addLikePattern("@") will allow all addresses.
/// For example, addLikePattern("%") will allow all addresses.
void addLikePattern(const String & pattern);
void removeLikePattern(const String & like_pattern);
const std::vector<String> & getLikePatterns() const { return like_patterns; }
@ -298,7 +298,7 @@ inline void AllowedClientHosts::addLikePattern(const String & pattern)
{
if (boost::iequals(pattern, "localhost") || (pattern == "127.0.0.1") || (pattern == "::1"))
local_host = true;
else if ((pattern == "@") || (pattern == "0.0.0.0/0") || (pattern == "::/0"))
else if ((pattern == "%") || (pattern == "0.0.0.0/0") || (pattern == "::/0"))
any_host = true;
else if (boost::range::find(like_patterns, pattern) == name_regexps.end())
like_patterns.push_back(pattern);
@ -308,7 +308,7 @@ inline void AllowedClientHosts::removeLikePattern(const String & pattern)
{
if (boost::iequals(pattern, "localhost") || (pattern == "127.0.0.1") || (pattern == "::1"))
local_host = false;
else if ((pattern == "@") || (pattern == "0.0.0.0/0") || (pattern == "::/0"))
else if ((pattern == "%") || (pattern == "0.0.0.0/0") || (pattern == "::/0"))
any_host = false;
else
boost::range::remove_erase(like_patterns, pattern);

View File

@ -23,7 +23,7 @@ namespace ErrorCodes
namespace
{
bool parseRenameTo(IParserBase::Pos & pos, Expected & expected, String & new_name, String & new_host_pattern)
bool parseRenameTo(IParserBase::Pos & pos, Expected & expected, String & new_name, std::optional<String> & new_host_pattern)
{
return IParserBase::wrapParseImpl(pos, [&]
{
@ -286,12 +286,12 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
}
String name;
String host_pattern;
std::optional<String> host_pattern;
if (!parseUserName(pos, expected, name, host_pattern))
return false;
String new_name;
String new_host_pattern;
std::optional<String> new_host_pattern;
std::optional<Authentication> authentication;
std::optional<AllowedClientHosts> hosts;
std::optional<AllowedClientHosts> add_hosts;
@ -327,10 +327,10 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
if (!hosts)
{
if (!alter)
hosts.emplace().addLikePattern(host_pattern);
else if (alter && !new_name.empty())
hosts.emplace().addLikePattern(new_host_pattern);
if (!alter && host_pattern)
hosts.emplace().addLikePattern(*host_pattern);
else if (alter && new_host_pattern)
hosts.emplace().addLikePattern(*new_host_pattern);
}
auto query = std::make_shared<ASTCreateUserQuery>();

View File

@ -3,48 +3,39 @@
#include <Parsers/CommonParsers.h>
#include <boost/algorithm/string.hpp>
namespace DB
{
namespace
bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, std::optional<String> & host_like_pattern)
{
bool parseUserNameImpl(IParser::Pos & pos, Expected & expected, String & user_name, String * host_like_pattern)
String name;
if (!parseIdentifierOrStringLiteral(pos, expected, name))
return false;
boost::algorithm::trim(name);
std::optional<String> pattern;
if (ParserToken{TokenType::At}.ignore(pos, expected))
{
String name;
if (!parseIdentifierOrStringLiteral(pos, expected, name))
if (!parseIdentifierOrStringLiteral(pos, expected, pattern.emplace()))
return false;
boost::algorithm::trim(name);
String pattern = "%";
if (ParserToken{TokenType::At}.ignore(pos, expected))
{
if (!parseIdentifierOrStringLiteral(pos, expected, pattern))
return false;
boost::algorithm::trim(pattern);
}
if (pattern != "%")
name += '@' + pattern;
user_name = std::move(name);
if (host_like_pattern)
*host_like_pattern = std::move(pattern);
return true;
boost::algorithm::trim(*pattern);
}
if (pattern && (pattern != "%"))
name += '@' + *pattern;
user_name = std::move(name);
host_like_pattern = std::move(pattern);
return true;
}
bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name)
{
return parseUserNameImpl(pos, expected, user_name, nullptr);
}
bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, String & host_like_pattern)
{
return parseUserNameImpl(pos, expected, user_name, &host_like_pattern);
std::optional<String> unused_pattern;
return parseUserName(pos, expected, user_name, unused_pattern);
}

View File

@ -10,7 +10,7 @@ namespace DB
/// The `host` can be an ip address, ip subnet, or a host name.
/// The % and _ wildcard characters are permitted in `host`.
/// These have the same meaning as for pattern-matching operations performed with the LIKE operator.
bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, String & host_like_pattern);
bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name, std::optional<String> & host_like_pattern);
bool parseUserName(IParser::Pos & pos, Expected & expected, String & user_name);
/// Parses either a user name or the 'CURRENT_USER' keyword (or some of the aliases).