ClickHouse/dbms/src/Parsers/ParserRenameQuery.cpp

96 lines
2.3 KiB
C++
Raw Normal View History

#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTRenameQuery.h>
2012-06-18 06:19:13 +00:00
#include <Parsers/CommonParsers.h>
#include <Parsers/ParserRenameQuery.h>
2012-06-18 06:19:13 +00:00
#include <Common/typeid_cast.h>
2016-11-20 12:43:20 +00:00
2012-06-18 06:19:13 +00:00
namespace DB
{
2017-04-02 17:37:49 +00:00
/// Parse database.table or table.
static bool parseDatabaseAndTable(
ASTRenameQuery::Table & db_and_table, IParser::Pos & pos, IParser::Pos end, IParser::Pos & max_parsed_pos, Expected & expected)
2012-06-18 06:19:13 +00:00
{
ParserIdentifier name_p;
ParserWhiteSpaceOrComments ws;
ParserString s_dot(".");
ASTPtr database;
ASTPtr table;
2012-06-18 06:19:13 +00:00
ws.ignore(pos, end);
2012-06-18 06:19:13 +00:00
if (!name_p.parse(pos, end, table, max_parsed_pos, expected))
return false;
2012-06-18 06:19:13 +00:00
ws.ignore(pos, end);
2012-06-18 06:19:13 +00:00
if (s_dot.ignore(pos, end, max_parsed_pos, expected))
{
database = table;
if (!name_p.parse(pos, end, table, max_parsed_pos, expected))
return false;
2012-06-18 06:19:13 +00:00
ws.ignore(pos, end);
}
2012-06-18 06:19:13 +00:00
db_and_table.database = database ? typeid_cast<const ASTIdentifier &>(*database).name : "";
db_and_table.table = typeid_cast<const ASTIdentifier &>(*table).name;
2012-06-18 06:19:13 +00:00
return true;
2012-06-18 06:19:13 +00:00
}
bool ParserRenameQuery::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
2012-06-18 06:19:13 +00:00
{
Pos begin = pos;
2012-06-18 06:19:13 +00:00
ParserWhiteSpaceOrComments ws;
ParserString s_rename("RENAME", true, true);
ParserString s_table("TABLE", true, true);
ParserString s_to("TO", true, true);
ParserString s_comma(",");
ws.ignore(pos, end);
2012-06-18 06:19:13 +00:00
if (!s_rename.ignore(pos, end, max_parsed_pos, expected))
return false;
2012-06-18 06:19:13 +00:00
ws.ignore(pos, end);
2012-06-18 06:19:13 +00:00
if (!s_table.ignore(pos, end, max_parsed_pos, expected))
return false;
2012-06-18 06:19:13 +00:00
ASTRenameQuery::Elements elements;
2012-06-18 06:19:13 +00:00
while (true)
{
ws.ignore(pos, end);
if (!elements.empty() && !s_comma.ignore(pos, end))
break;
2012-06-18 06:19:13 +00:00
ws.ignore(pos, end);
2012-06-18 06:19:13 +00:00
elements.push_back(ASTRenameQuery::Element());
2012-06-18 06:19:13 +00:00
if (!parseDatabaseAndTable(elements.back().from, pos, end, max_parsed_pos, expected)
|| !s_to.ignore(pos, end)
|| !parseDatabaseAndTable(elements.back().to, pos, end, max_parsed_pos, expected))
return false;
}
2012-06-18 06:19:13 +00:00
auto query = std::make_shared<ASTRenameQuery>(StringRange(begin, pos));
node = query;
2012-06-18 06:19:13 +00:00
query->elements = elements;
return true;
2012-06-18 06:19:13 +00:00
}
}