2019-01-25 18:35:16 +00:00
|
|
|
#include "applySubstitutions.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
void constructSubstitutions(ConfigurationPtr & substitutions_view, StringToVector & out_substitutions)
|
|
|
|
{
|
2019-01-28 16:20:29 +00:00
|
|
|
Strings xml_substitutions;
|
2019-01-25 18:35:16 +00:00
|
|
|
substitutions_view->keys(xml_substitutions);
|
|
|
|
|
|
|
|
for (size_t i = 0; i != xml_substitutions.size(); ++i)
|
|
|
|
{
|
|
|
|
const ConfigurationPtr xml_substitution(substitutions_view->createView("substitution[" + std::to_string(i) + "]"));
|
|
|
|
|
|
|
|
/// Property values for substitution will be stored in a vector
|
|
|
|
/// accessible by property name
|
2019-01-28 16:20:29 +00:00
|
|
|
Strings xml_values;
|
2019-01-25 18:35:16 +00:00
|
|
|
xml_substitution->keys("values", xml_values);
|
|
|
|
|
2019-01-28 16:20:29 +00:00
|
|
|
std::string name = xml_substitution->getString("name");
|
2019-01-25 18:35:16 +00:00
|
|
|
|
|
|
|
for (size_t j = 0; j != xml_values.size(); ++j)
|
|
|
|
{
|
|
|
|
out_substitutions[name].push_back(xml_substitution->getString("values.value[" + std::to_string(j) + "]"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursive method which goes through all substitution blocks in xml
|
|
|
|
/// and replaces property {names} by their values
|
2019-12-15 06:34:43 +00:00
|
|
|
static void runThroughAllOptionsAndPush(StringToVector::iterator substitutions_left,
|
2019-01-25 18:35:16 +00:00
|
|
|
StringToVector::iterator substitutions_right,
|
2019-01-28 16:20:29 +00:00
|
|
|
const std::string & template_query,
|
|
|
|
Strings & out_queries)
|
2019-01-25 18:35:16 +00:00
|
|
|
{
|
|
|
|
if (substitutions_left == substitutions_right)
|
|
|
|
{
|
|
|
|
out_queries.push_back(template_query); /// completely substituted query
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-28 16:20:29 +00:00
|
|
|
std::string substitution_mask = "{" + substitutions_left->first + "}";
|
2019-01-25 18:35:16 +00:00
|
|
|
|
2019-01-28 16:20:29 +00:00
|
|
|
if (template_query.find(substitution_mask) == std::string::npos) /// nothing to substitute here
|
2019-01-25 18:35:16 +00:00
|
|
|
{
|
|
|
|
runThroughAllOptionsAndPush(std::next(substitutions_left), substitutions_right, template_query, out_queries);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-28 16:20:29 +00:00
|
|
|
for (const std::string & value : substitutions_left->second)
|
2019-01-25 18:35:16 +00:00
|
|
|
{
|
|
|
|
/// Copy query string for each unique permutation
|
|
|
|
std::string query = template_query;
|
|
|
|
size_t substr_pos = 0;
|
|
|
|
|
2019-01-28 16:20:29 +00:00
|
|
|
while (substr_pos != std::string::npos)
|
2019-01-25 18:35:16 +00:00
|
|
|
{
|
|
|
|
substr_pos = query.find(substitution_mask);
|
|
|
|
|
2019-01-28 16:20:29 +00:00
|
|
|
if (substr_pos != std::string::npos)
|
2019-01-25 18:35:16 +00:00
|
|
|
query.replace(substr_pos, substitution_mask.length(), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
runThroughAllOptionsAndPush(std::next(substitutions_left), substitutions_right, query, out_queries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 16:20:29 +00:00
|
|
|
Strings formatQueries(const std::string & query, StringToVector substitutions_to_generate)
|
2019-01-25 18:35:16 +00:00
|
|
|
{
|
2019-01-28 16:20:29 +00:00
|
|
|
Strings queries_res;
|
2019-01-25 18:35:16 +00:00
|
|
|
runThroughAllOptionsAndPush(
|
|
|
|
substitutions_to_generate.begin(),
|
|
|
|
substitutions_to_generate.end(),
|
|
|
|
query,
|
|
|
|
queries_res);
|
|
|
|
return queries_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|