2018-03-13 23:44:23 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Macros.h>
|
|
|
|
#include <Common/Exception.h>
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2018-03-13 23:44:23 +00:00
|
|
|
|
2014-08-11 15:59:01 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 19:10:16 +00:00
|
|
|
Macros::Macros() {}
|
2014-08-11 15:59:01 +00:00
|
|
|
|
|
|
|
Macros::Macros(const Poco::Util::AbstractConfiguration & config, const String & root_key)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(root_key, keys);
|
|
|
|
for (const String & key : keys)
|
|
|
|
{
|
|
|
|
macros[key] = config.getString(root_key + "." + key);
|
|
|
|
}
|
2014-08-11 15:59:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 10:01:10 +00:00
|
|
|
String Macros::expand(const String & s, size_t level, const String & database_name, const String & table_name) const
|
2014-08-11 15:59:01 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (s.find('{') == String::npos)
|
|
|
|
return s;
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (level && s.size() > 65536)
|
|
|
|
throw Exception("Too long string while expanding macros", ErrorCodes::SYNTAX_ERROR);
|
2014-10-15 19:38:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (level >= 10)
|
|
|
|
throw Exception("Too deep recursion while expanding macros: '" + s + "'", ErrorCodes::SYNTAX_ERROR);
|
2014-10-15 19:38:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String res;
|
|
|
|
size_t pos = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t begin = s.find('{', pos);
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (begin == String::npos)
|
|
|
|
{
|
|
|
|
res.append(s, pos, String::npos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.append(s, pos, begin - pos);
|
|
|
|
}
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++begin;
|
|
|
|
size_t end = s.find('}', begin);
|
|
|
|
if (end == String::npos)
|
|
|
|
throw Exception("Unbalanced { and } in string with macros: '" + s + "'", ErrorCodes::SYNTAX_ERROR);
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String macro_name = s.substr(begin, end - begin);
|
2018-10-01 09:01:50 +00:00
|
|
|
auto it = macros.find(macro_name);
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2018-10-01 09:01:50 +00:00
|
|
|
/// Prefer explicit macros over implicit.
|
|
|
|
if (it != macros.end())
|
|
|
|
res += it->second;
|
|
|
|
else if (macro_name == "database" && !database_name.empty())
|
2018-09-27 10:01:10 +00:00
|
|
|
res += database_name;
|
2018-10-01 09:01:50 +00:00
|
|
|
else if (macro_name == "table" && !table_name.empty())
|
2018-09-27 10:01:10 +00:00
|
|
|
res += table_name;
|
|
|
|
else
|
2018-10-01 09:01:50 +00:00
|
|
|
throw Exception("No macro " + macro_name + " in config", ErrorCodes::SYNTAX_ERROR);
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
pos = end + 1;
|
|
|
|
}
|
2014-08-11 15:59:01 +00:00
|
|
|
|
2018-09-27 10:01:10 +00:00
|
|
|
return expand(res, level + 1, database_name, table_name);
|
2014-08-11 15:59:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 09:01:50 +00:00
|
|
|
String Macros::expand(const String & s, const String & database_name, const String & table_name) const
|
|
|
|
{
|
|
|
|
return expand(s, 0, database_name, table_name);
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:08:27 +00:00
|
|
|
Names Macros::expand(const Names & source_names, size_t level) const
|
2018-04-20 13:48:42 +00:00
|
|
|
{
|
2018-04-20 16:08:27 +00:00
|
|
|
Names result_names;
|
|
|
|
result_names.reserve(source_names.size());
|
2018-04-20 13:48:42 +00:00
|
|
|
|
2018-04-20 16:08:27 +00:00
|
|
|
for (const String & name : source_names)
|
|
|
|
result_names.push_back(expand(name, level));
|
2018-05-07 02:01:11 +00:00
|
|
|
|
2018-04-20 16:08:27 +00:00
|
|
|
return result_names;
|
2018-04-20 13:48:42 +00:00
|
|
|
}
|
2014-08-11 15:59:01 +00:00
|
|
|
}
|