ClickHouse/src/Common/Macros.cpp

101 lines
2.7 KiB
C++
Raw Normal View History

2018-03-13 23:44:23 +00:00
#include <Poco/Util/AbstractConfiguration.h>
#include <Common/Macros.h>
#include <Common/Exception.h>
2019-11-05 13:05:48 +00:00
#include <IO/WriteHelpers.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
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
}
2014-08-11 15:59:01 +00:00
Macros::Macros(const Poco::Util::AbstractConfiguration & config, const String & root_key)
{
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
{
if (s.find('{') == String::npos)
return s;
2014-08-11 15:59:01 +00:00
if (level && s.size() > 65536)
throw Exception("Too long string while expanding macros", ErrorCodes::SYNTAX_ERROR);
if (level >= 10)
throw Exception("Too deep recursion while expanding macros: '" + s + "'", ErrorCodes::SYNTAX_ERROR);
String res;
size_t pos = 0;
while (true)
{
size_t begin = s.find('{', pos);
2014-08-11 15:59:01 +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
++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
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
2019-11-05 13:05:48 +00:00
throw Exception("No macro '" + macro_name +
"' in config while processing substitutions in '" + s + "' at "
+ toString(begin), ErrorCodes::SYNTAX_ERROR);
2014-08-11 15:59:01 +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
}
2019-10-09 01:14:57 +00:00
String Macros::getValue(const String & key) const
{
if (auto it = macros.find(key); it != macros.end())
return it->second;
throw Exception("No macro " + key + " in config", ErrorCodes::SYNTAX_ERROR);
}
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 16:08:27 +00:00
Names result_names;
result_names.reserve(source_names.size());
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;
}
2014-08-11 15:59:01 +00:00
}