ClickHouse/src/Interpreters/QueryParameterVisitor.cpp

54 lines
1.2 KiB
C++
Raw Normal View History

2020-09-19 14:47:22 +00:00
#include <Interpreters/QueryParameterVisitor.h>
2020-09-19 15:02:11 +00:00
#include <Parsers/IAST.h>
2020-09-19 14:47:22 +00:00
#include <Parsers/ASTQueryParameter.h>
#include <Parsers/ParserQuery.h>
#include <Parsers/parseQuery.h>
namespace DB
{
2020-09-19 15:02:11 +00:00
class QueryParameterVisitor
2020-09-19 14:47:22 +00:00
{
2020-09-19 15:02:11 +00:00
public:
2020-09-19 16:29:15 +00:00
explicit QueryParameterVisitor(NameSet & parameters_name)
2020-09-19 15:02:11 +00:00
: query_parameters(parameters_name)
2020-09-19 14:47:22 +00:00
{
}
2020-09-19 15:02:11 +00:00
void visit(const ASTPtr & ast)
{
for (const auto & child : ast->children)
{
if (const auto & query_parameter = child->as<ASTQueryParameter>())
visitQueryParameter(*query_parameter);
else
visit(child);
}
}
private:
NameSet & query_parameters;
void visitQueryParameter(const ASTQueryParameter & query_parameter)
{
query_parameters.insert(query_parameter.name);
}
};
2020-09-19 14:47:22 +00:00
NameSet analyzeReceiveQueryParams(const std::string & query)
{
NameSet query_params;
const char * query_begin = query.data();
const char * query_end = query.data() + query.size();
ParserQuery parser(query_end, false);
ASTPtr extract_query_ast = parseQuery(parser, query_begin, query_end, "analyzeReceiveQueryParams", 0, 0);
QueryParameterVisitor(query_params).visit(extract_query_ast);
return query_params;
}
}