ClickHouse/src/Parsers/ParserSetQuery.cpp

82 lines
1.8 KiB
C++
Raw Normal View History

#include <Parsers/ASTIdentifier_fwd.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTSetQuery.h>
2012-08-02 19:03:32 +00:00
#include <Parsers/CommonParsers.h>
#include <Parsers/ParserSetQuery.h>
2012-08-02 19:03:32 +00:00
#include <Common/typeid_cast.h>
#include <Common/SettingsChanges.h>
2016-11-20 12:43:20 +00:00
2012-08-02 19:03:32 +00:00
namespace DB
{
2017-04-02 17:37:49 +00:00
/// Parse `name = value`.
bool ParserSetQuery::parseNameValuePair(SettingChange & change, IParser::Pos & pos, Expected & expected)
2012-08-02 19:03:32 +00:00
{
ParserCompoundIdentifier name_p;
ParserLiteral value_p;
ParserToken s_eq(TokenType::Equals);
ASTPtr name;
ASTPtr value;
2012-08-02 19:03:32 +00:00
if (!name_p.parse(pos, name, expected))
return false;
2012-08-02 19:03:32 +00:00
if (!s_eq.ignore(pos, expected))
return false;
2012-08-02 19:03:32 +00:00
if (ParserKeyword("TRUE").ignore(pos, expected))
value = std::make_shared<ASTLiteral>(Field(UInt64(1)));
else if (ParserKeyword("FALSE").ignore(pos, expected))
value = std::make_shared<ASTLiteral>(Field(UInt64(0)));
else if (!value_p.parse(pos, value, expected))
return false;
2012-08-02 19:03:32 +00:00
2019-08-08 20:02:30 +00:00
tryGetIdentifierNameInto(name, change.name);
change.value = value->as<ASTLiteral &>().value;
2012-08-02 19:03:32 +00:00
return true;
2012-08-02 19:03:32 +00:00
}
bool ParserSetQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
2012-08-02 19:03:32 +00:00
{
ParserToken s_comma(TokenType::Comma);
if (!parse_only_internals)
{
ParserKeyword s_set("SET");
2012-08-02 19:03:32 +00:00
if (!s_set.ignore(pos, expected))
return false;
}
2012-08-02 19:03:32 +00:00
SettingsChanges changes;
2012-08-02 19:03:32 +00:00
while (true)
{
if (!changes.empty() && !s_comma.ignore(pos))
break;
2012-08-02 19:03:32 +00:00
changes.push_back(SettingChange{});
2012-08-02 19:03:32 +00:00
if (!parseNameValuePair(changes.back(), pos, expected))
return false;
}
2012-08-02 19:03:32 +00:00
2018-02-26 03:37:08 +00:00
auto query = std::make_shared<ASTSetQuery>();
node = query;
2012-08-02 19:03:32 +00:00
query->is_standalone = !parse_only_internals;
query->changes = std::move(changes);
return true;
2012-08-02 19:03:32 +00:00
}
}