2020-03-18 14:11:44 +00:00
|
|
|
#include <Parsers/ParserCreateSettingsProfileQuery.h>
|
|
|
|
#include <Parsers/ASTCreateSettingsProfileQuery.h>
|
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTSettingsProfileElement.h>
|
|
|
|
#include <Parsers/ParserSettingsProfileElement.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Parsers/ParserRolesOrUsersSet.h>
|
|
|
|
#include <Parsers/ASTRolesOrUsersSet.h>
|
2020-03-18 14:11:44 +00:00
|
|
|
#include <Parsers/parseIdentifierOrStringLiteral.h>
|
2020-06-05 21:31:37 +00:00
|
|
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
2020-03-18 14:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool parseRenameTo(IParserBase::Pos & pos, Expected & expected, String & new_name)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"RENAME TO"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return parseIdentifierOrStringLiteral(pos, expected, new_name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-05 21:31:37 +00:00
|
|
|
bool parseSettings(IParserBase::Pos & pos, Expected & expected, bool id_mode, std::vector<std::shared_ptr<ASTSettingsProfileElement>> & settings)
|
2020-03-18 14:11:44 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"SETTINGS"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTPtr new_settings_ast;
|
2020-05-30 20:10:45 +00:00
|
|
|
ParserSettingsProfileElements elements_p;
|
|
|
|
elements_p.useInheritKeyword(true).useIDMode(id_mode);
|
|
|
|
if (!elements_p.parse(pos, new_settings_ast, expected))
|
2020-03-18 14:11:44 +00:00
|
|
|
return false;
|
|
|
|
|
2020-06-05 21:31:37 +00:00
|
|
|
settings = std::move(new_settings_ast->as<const ASTSettingsProfileElements &>().elements);
|
2020-03-18 14:11:44 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
bool parseToRoles(IParserBase::Pos & pos, Expected & expected, bool id_mode, std::shared_ptr<ASTRolesOrUsersSet> & roles)
|
2020-03-18 14:11:44 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
ASTPtr ast;
|
2020-06-05 21:31:37 +00:00
|
|
|
if (!ParserKeyword{"TO"}.ignore(pos, expected))
|
2020-03-18 14:11:44 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
ParserRolesOrUsersSet roles_p;
|
|
|
|
roles_p.allowAll().allowRoleNames().allowUserNames().allowCurrentUser().useIDMode(id_mode);
|
|
|
|
if (!roles_p.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
roles = std::static_pointer_cast<ASTRolesOrUsersSet>(ast);
|
2020-03-18 14:11:44 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2020-04-08 23:01:41 +00:00
|
|
|
|
|
|
|
bool parseOnCluster(IParserBase::Pos & pos, Expected & expected, String & cluster)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
return ParserKeyword{"ON"}.ignore(pos, expected) && ASTQueryWithOnCluster::parse(pos, cluster, expected);
|
|
|
|
});
|
|
|
|
}
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserCreateSettingsProfileQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
bool alter = false;
|
|
|
|
if (attach_mode)
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"ATTACH SETTINGS PROFILE"}.ignore(pos, expected) && !ParserKeyword{"ATTACH PROFILE"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ParserKeyword{"ALTER SETTINGS PROFILE"}.ignore(pos, expected) || ParserKeyword{"ALTER PROFILE"}.ignore(pos, expected))
|
|
|
|
alter = true;
|
|
|
|
else if (!ParserKeyword{"CREATE SETTINGS PROFILE"}.ignore(pos, expected) && !ParserKeyword{"CREATE PROFILE"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool if_exists = false;
|
|
|
|
bool if_not_exists = false;
|
|
|
|
bool or_replace = false;
|
|
|
|
if (alter)
|
|
|
|
{
|
|
|
|
if (ParserKeyword{"IF EXISTS"}.ignore(pos, expected))
|
|
|
|
if_exists = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ParserKeyword{"IF NOT EXISTS"}.ignore(pos, expected))
|
|
|
|
if_not_exists = true;
|
|
|
|
else if (ParserKeyword{"OR REPLACE"}.ignore(pos, expected))
|
|
|
|
or_replace = true;
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:18:08 +00:00
|
|
|
Strings names;
|
|
|
|
if (!parseIdentifiersOrStringLiterals(pos, expected, names))
|
2020-03-18 14:11:44 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
String new_name;
|
|
|
|
std::shared_ptr<ASTSettingsProfileElements> settings;
|
2020-04-08 23:01:41 +00:00
|
|
|
String cluster;
|
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2020-05-30 14:18:08 +00:00
|
|
|
if (alter && new_name.empty() && (names.size() == 1) && parseRenameTo(pos, expected, new_name))
|
2020-03-18 14:11:44 +00:00
|
|
|
continue;
|
|
|
|
|
2020-06-05 21:31:37 +00:00
|
|
|
std::vector<std::shared_ptr<ASTSettingsProfileElement>> new_settings;
|
|
|
|
if (parseSettings(pos, expected, attach_mode, new_settings))
|
|
|
|
{
|
|
|
|
if (!settings)
|
|
|
|
settings = std::make_shared<ASTSettingsProfileElements>();
|
|
|
|
boost::range::push_back(settings->elements, std::move(new_settings));
|
2020-03-18 14:11:44 +00:00
|
|
|
continue;
|
2020-06-05 21:31:37 +00:00
|
|
|
}
|
2020-03-18 14:11:44 +00:00
|
|
|
|
2020-04-08 23:01:41 +00:00
|
|
|
if (cluster.empty() && parseOnCluster(pos, expected, cluster))
|
|
|
|
continue;
|
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
std::shared_ptr<ASTRolesOrUsersSet> to_roles;
|
2020-03-18 14:11:44 +00:00
|
|
|
parseToRoles(pos, expected, attach_mode, to_roles);
|
|
|
|
|
2020-04-08 23:01:41 +00:00
|
|
|
if (cluster.empty())
|
|
|
|
parseOnCluster(pos, expected, cluster);
|
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
auto query = std::make_shared<ASTCreateSettingsProfileQuery>();
|
|
|
|
node = query;
|
|
|
|
|
|
|
|
query->alter = alter;
|
|
|
|
query->attach = attach_mode;
|
|
|
|
query->if_exists = if_exists;
|
|
|
|
query->if_not_exists = if_not_exists;
|
|
|
|
query->or_replace = or_replace;
|
2020-04-05 23:03:20 +00:00
|
|
|
query->cluster = std::move(cluster);
|
2020-05-30 14:18:08 +00:00
|
|
|
query->names = std::move(names);
|
2020-03-18 14:11:44 +00:00
|
|
|
query->new_name = std::move(new_name);
|
|
|
|
query->settings = std::move(settings);
|
|
|
|
query->to_roles = std::move(to_roles);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|