2018-09-28 15:01:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-27 19:25:18 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
2018-10-09 14:32:11 +00:00
|
|
|
#include <Parsers/DumpASTNode.h>
|
2018-10-30 16:31:21 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2018-09-27 19:25:18 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class ASTIdentifier;
|
|
|
|
class ASTQualifiedAsterisk;
|
|
|
|
class ASTSelectQuery;
|
|
|
|
struct ASTTableJoin;
|
|
|
|
|
|
|
|
class NamesAndTypesList;
|
|
|
|
|
|
|
|
|
2018-10-09 14:41:19 +00:00
|
|
|
/// It visits nodes, find identifiers and translate their names to needed form.
|
2018-09-27 19:25:18 +00:00
|
|
|
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))
|
2018-09-27 19:25:18 +00:00
|
|
|
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;
|
2018-09-27 19:25:18 +00:00
|
|
|
|
|
|
|
void visitChildren(ASTPtr &) const;
|
|
|
|
|
|
|
|
template <typename T>
|
2018-09-28 11:46:52 +00:00
|
|
|
bool tryVisit(ASTPtr & ast, const DumpASTNode & dump) const
|
2018-09-27 19:25:18 +00:00
|
|
|
{
|
|
|
|
if (T * t = typeid_cast<T *>(ast.get()))
|
|
|
|
{
|
2018-09-28 11:46:52 +00:00
|
|
|
visit(t, ast, dump);
|
2018-09-27 19:25:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|