2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTRenameQuery.h>
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ParserRenameQuery.h>
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 09:19:00 +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.
|
2015-04-11 03:10:23 +00:00
|
|
|
static bool parseDatabaseAndTable(
|
2017-07-10 03:28:12 +00:00
|
|
|
ASTRenameQuery::Table & db_and_table, IParser::Pos & pos, Expected & expected)
|
2012-06-18 06:19:13 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifier name_p;
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_dot(TokenType::Dot);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr database;
|
|
|
|
ASTPtr table;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, table, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_dot.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
database = table;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, table, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2012-06-18 06:19:13 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserRenameQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2012-06-18 06:19:13 +00:00
|
|
|
{
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_rename_table("RENAME TABLE");
|
|
|
|
ParserKeyword s_to("TO");
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken s_comma(TokenType::Comma);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_rename_table.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTRenameQuery::Elements elements;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elements.empty() && !s_comma.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
elements.push_back(ASTRenameQuery::Element());
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!parseDatabaseAndTable(elements.back().from, pos, expected)
|
|
|
|
|| !s_to.ignore(pos)
|
|
|
|
|| !parseDatabaseAndTable(elements.back().to, pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
String cluster_str;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword{"ON"}.ignore(pos, expected))
|
2017-04-21 12:39:28 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
|
2017-04-21 12:39:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto query = std::make_shared<ASTRenameQuery>();
|
2017-04-21 12:39:28 +00:00
|
|
|
query->cluster = cluster_str;
|
2017-04-01 07:20:54 +00:00
|
|
|
node = query;
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
query->elements = elements;
|
|
|
|
return true;
|
2012-06-18 06:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|