2022-10-03 14:42:44 +00:00
|
|
|
#include <Parsers/QueryParameterVisitor.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:
|
2022-12-19 14:05:38 +00:00
|
|
|
explicit QueryParameterVisitor(NameToNameMap & parameters)
|
|
|
|
: query_parameters(parameters)
|
2020-09-19 14:47:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-19 15:02:11 +00:00
|
|
|
void visit(const ASTPtr & ast)
|
|
|
|
{
|
2022-09-27 14:30:59 +00:00
|
|
|
if (const auto & query_parameter = ast->as<ASTQueryParameter>())
|
|
|
|
visitQueryParameter(*query_parameter);
|
|
|
|
else
|
2020-09-19 15:02:11 +00:00
|
|
|
{
|
2022-09-27 14:30:59 +00:00
|
|
|
for (const auto & child : ast->children)
|
2020-09-19 15:02:11 +00:00
|
|
|
visit(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-12-19 14:05:38 +00:00
|
|
|
NameToNameMap & query_parameters;
|
2020-09-19 15:02:11 +00:00
|
|
|
|
|
|
|
void visitQueryParameter(const ASTQueryParameter & query_parameter)
|
|
|
|
{
|
2023-08-10 22:00:36 +00:00
|
|
|
query_parameters[query_parameter.name] = query_parameter.type;
|
2020-09-19 15:02:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-19 14:47:22 +00:00
|
|
|
|
|
|
|
NameSet analyzeReceiveQueryParams(const std::string & query)
|
|
|
|
{
|
2022-12-19 14:05:38 +00:00
|
|
|
NameToNameMap query_params;
|
2020-09-19 14:47:22 +00:00
|
|
|
const char * query_begin = query.data();
|
|
|
|
const char * query_end = query.data() + query.size();
|
|
|
|
|
2020-11-02 12:47:12 +00:00
|
|
|
ParserQuery parser(query_end);
|
2020-09-19 14:47:22 +00:00
|
|
|
ASTPtr extract_query_ast = parseQuery(parser, query_begin, query_end, "analyzeReceiveQueryParams", 0, 0);
|
|
|
|
QueryParameterVisitor(query_params).visit(extract_query_ast);
|
2022-12-19 14:05:38 +00:00
|
|
|
|
|
|
|
NameSet query_param_names;
|
|
|
|
for (const auto & query_param : query_params)
|
|
|
|
query_param_names.insert(query_param.first);
|
|
|
|
return query_param_names;
|
2020-09-19 14:47:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 14:30:59 +00:00
|
|
|
NameSet analyzeReceiveQueryParams(const ASTPtr & ast)
|
|
|
|
{
|
2022-12-19 14:05:38 +00:00
|
|
|
NameToNameMap query_params;
|
|
|
|
QueryParameterVisitor(query_params).visit(ast);
|
|
|
|
NameSet query_param_names;
|
|
|
|
for (const auto & query_param : query_params)
|
|
|
|
query_param_names.insert(query_param.first);
|
|
|
|
return query_param_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
NameToNameMap analyzeReceiveQueryParamsWithType(const ASTPtr & ast)
|
|
|
|
{
|
|
|
|
NameToNameMap query_params;
|
2022-09-27 14:30:59 +00:00
|
|
|
QueryParameterVisitor(query_params).visit(ast);
|
|
|
|
return query_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-19 14:47:22 +00:00
|
|
|
}
|