ClickHouse/src/Parsers/parseDatabaseAndTableName.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
3.8 KiB
C++
Raw Normal View History

#include "parseDatabaseAndTableName.h"
#include <Parsers/ASTIdentifier_fwd.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
namespace DB
{
bool parseDatabaseAndTableName(IParser::Pos & pos, Expected & expected, String & database_str, String & table_str)
{
ParserToken s_dot(TokenType::Dot);
ParserIdentifier table_parser;
ASTPtr database;
ASTPtr table;
database_str = "";
table_str = "";
if (!table_parser.parse(pos, database, expected))
return false;
if (s_dot.ignore(pos))
{
if (!table_parser.parse(pos, table, expected))
{
database_str = "";
return false;
}
2019-08-08 20:02:30 +00:00
tryGetIdentifierNameInto(database, database_str);
tryGetIdentifierNameInto(table, table_str);
}
else
{
database_str = "";
2019-08-08 20:02:30 +00:00
tryGetIdentifierNameInto(database, table_str);
}
return true;
}
2021-11-16 08:58:28 +00:00
bool parseDatabaseAndTableAsAST(IParser::Pos & pos, Expected & expected, ASTPtr & database, ASTPtr & table)
{
ParserToken s_dot(TokenType::Dot);
ParserIdentifier table_parser(true);
if (!table_parser.parse(pos, table, expected))
return false;
if (s_dot.ignore(pos))
{
database = table;
if (!table_parser.parse(pos, table, expected))
return false;
2021-09-06 22:24:47 +00:00
}
return true;
}
2021-11-16 08:58:28 +00:00
bool parseDatabaseAsAST(IParser::Pos & pos, Expected & expected, ASTPtr & database)
2021-09-22 13:57:17 +00:00
{
ParserIdentifier identifier_parser(/* allow_query_parameter */true);
2021-10-21 12:50:31 +00:00
return identifier_parser.parse(pos, database, expected);
2021-09-22 13:57:17 +00:00
}
2021-08-27 06:30:21 +00:00
bool parseDatabaseAndTableNameOrAsterisks(IParser::Pos & pos, Expected & expected, String & database, String & table, bool & wildcard, bool & default_database)
{
return IParserBase::wrapParseImpl(pos, [&]
{
if (ParserToken{TokenType::Asterisk}.ignore(pos, expected))
{
auto pos_before_dot = pos;
if (ParserToken{TokenType::Dot}.ignore(pos, expected)
&& ParserToken{TokenType::Asterisk}.ignore(pos, expected))
{
/// *.*
database.clear();
table.clear();
return true;
}
/// *
pos = pos_before_dot;
database.clear();
table.clear();
default_database = true;
return true;
}
ASTPtr ast;
ParserIdentifier identifier_parser;
if (identifier_parser.parse(pos, ast, expected))
{
String first_identifier = getIdentifierName(ast);
if (ParserToken{TokenType::Asterisk}.ignore(pos, expected))
wildcard = true;
auto pos_before_dot = pos;
if (ParserToken{TokenType::Dot}.ignore(pos, expected))
{
if (ParserToken{TokenType::Asterisk}.ignore(pos, expected))
{
/// db.*
database = std::move(first_identifier);
table.clear();
return true;
}
if (identifier_parser.parse(pos, ast, expected))
{
/// db.table
database = std::move(first_identifier);
table = getIdentifierName(ast);
2024-06-14 23:11:24 +00:00
if (ParserToken{TokenType::Asterisk}.ignore(pos, expected))
wildcard = true;
return true;
}
}
/// table
pos = pos_before_dot;
database.clear();
table = std::move(first_identifier);
default_database = true;
2024-06-14 23:11:24 +00:00
if (!wildcard && ParserToken{TokenType::Asterisk}.ignore(pos, expected))
2024-06-14 23:11:24 +00:00
wildcard = true;
return true;
}
return false;
});
}
}