ClickHouse/src/Parsers/ParserAlterNamedCollectionQuery.cpp
Salvatore Mesoraca 1174689f8b
Refactoring
2023-11-09 17:25:20 +01:00

99 lines
2.7 KiB
C++

#include <Parsers/ASTIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ParserAlterNamedCollectionQuery.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTAlterNamedCollectionQuery.h>
#include <Parsers/ASTSetQuery.h>
namespace DB
{
bool ParserAlterNamedCollectionQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_alter("ALTER");
ParserKeyword s_collection("NAMED COLLECTION");
ParserKeyword s_if_exists("IF EXISTS");
ParserKeyword s_on("ON");
ParserKeyword s_delete("DELETE");
ParserIdentifier name_p;
ParserKeyword s_set("SET");
ParserKeyword s_overridable("OVERRIDABLE");
ParserKeyword s_not_overridable("NOT OVERRIDABLE");
ParserToken s_comma(TokenType::Comma);
String cluster_str;
bool if_exists = false;
ASTPtr collection_name;
std::vector<std::string> delete_keys;
if (!s_alter.ignore(pos, expected))
return false;
if (!s_collection.ignore(pos, expected))
return false;
if (s_if_exists.ignore(pos, expected))
if_exists = true;
if (!name_p.parse(pos, collection_name, expected))
return false;
if (s_on.ignore(pos, expected))
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
SettingsChanges changes;
std::unordered_map<String, bool> overridability;
if (s_set.ignore(pos, expected))
{
while (true)
{
if (!changes.empty() && !s_comma.ignore(pos))
break;
changes.push_back(SettingChange{});
if (!ParserSetQuery::parseNameValuePair(changes.back(), pos, expected))
return false;
if (s_not_overridable.ignore(pos, expected))
overridability.emplace(changes.back().name, false);
else if (s_overridable.ignore(pos, expected))
overridability.emplace(changes.back().name, true);
}
}
if (s_delete.ignore(pos, expected))
{
while (true)
{
if (!delete_keys.empty() && !s_comma.ignore(pos))
break;
ASTPtr key;
if (!name_p.parse(pos, key, expected))
return false;
delete_keys.push_back(getIdentifierName(key));
}
}
auto query = std::make_shared<ASTAlterNamedCollectionQuery>();
query->collection_name = getIdentifierName(collection_name);
query->if_exists = if_exists;
query->cluster = std::move(cluster_str);
query->changes = changes;
query->overridability = std::move(overridability);
query->delete_keys = delete_keys;
node = query;
return true;
}
}