ClickHouse/dbms/src/Interpreters/TranslateQualifiedNamesVisitor.h

70 lines
1.9 KiB
C++
Raw Normal View History

2018-09-28 15:01:13 +00:00
#pragma once
#include <memory>
#include <vector>
#include <Common/typeid_cast.h>
#include <Parsers/DumpASTNode.h>
#include <Interpreters/DatabaseAndTableWithAlias.h>
namespace DB
{
class ASTIdentifier;
class ASTQualifiedAsterisk;
class ASTSelectQuery;
struct ASTTableJoin;
class NamesAndTypesList;
/// It visits nodes, find identifiers and translate their names to needed form.
class TranslateQualifiedNamesVisitor
{
public:
TranslateQualifiedNamesVisitor(const NamesAndTypesList & source_columns_, const std::vector<DatabaseAndTableWithAlias> & tables_,
std::ostream * ostr_ = nullptr)
: source_columns(source_columns_),
tables(tables_),
visit_depth(0),
ostr(ostr_)
{}
void visit(ASTPtr & ast) const
{
2018-09-28 11:46:52 +00:00
DumpASTNode dump(*ast, ostr, visit_depth, "translateQualifiedNames");
if (!tryVisit<ASTIdentifier>(ast, dump) &&
!tryVisit<ASTQualifiedAsterisk>(ast, dump) &&
!tryVisit<ASTTableJoin>(ast, dump) &&
!tryVisit<ASTSelectQuery>(ast, dump))
visitChildren(ast); /// default: do nothing, visit children
}
private:
const NamesAndTypesList & source_columns;
const std::vector<DatabaseAndTableWithAlias> & tables;
mutable size_t visit_depth;
std::ostream * ostr;
2018-09-28 11:46:52 +00:00
void visit(ASTIdentifier * node, ASTPtr & ast, const DumpASTNode & dump) const;
void visit(ASTQualifiedAsterisk * node, ASTPtr & ast, const DumpASTNode & dump) const;
void visit(ASTTableJoin * node, ASTPtr & ast, const DumpASTNode & dump) const;
void visit(ASTSelectQuery * ast, ASTPtr &, const DumpASTNode & dump) const;
void visitChildren(ASTPtr &) const;
template <typename T>
2018-09-28 11:46:52 +00:00
bool tryVisit(ASTPtr & ast, const DumpASTNode & dump) const
{
if (T * t = typeid_cast<T *>(ast.get()))
{
2018-09-28 11:46:52 +00:00
visit(t, ast, dump);
return true;
}
return false;
}
};
}