Move some functions to utils file

This commit is contained in:
Aleksei Nikiforov 2020-01-14 11:55:05 +03:00
parent 8e2910ab94
commit 149a7e7a44
3 changed files with 46 additions and 43 deletions

View File

@ -95,49 +95,6 @@ static void term_handler(int sig)
stop_running();
}
bool is_valid_table_name(const std::string &value)
{
return (std::find_if_not(value.begin(), value.end(), [] (const char c) { return (std::isalnum(c) || (strchr("_", c) != NULL)); } ) == value.end());
}
template <typename T>
bool always_true(const T &)
{
return true;
}
template <typename T>
void optional_set(T &value, const boost::property_tree::ptree &tree, const char *element_name, bool (*check_function)(const T &))
{
auto element = tree.get_child_optional(element_name);
if (element)
{
if (check_function(element->get_value<T>()))
{
value = element->get_value<T>();
}
else
{
throw std::runtime_error(std::string("Invalid value for option '") + std::string(element_name) + std::string("'"));
}
}
}
template <typename T>
void optional_set(T &value, const boost::property_tree::ptree &tree, const char *element_name)
{
optional_set(value, tree, element_name, &always_true<T>);
}
std::string sanitize_column_name(const std::string &name)
{
auto result = name;
std::replace(result.begin(), result.end(), '-', '_');
return result;
}
void initialize_data_block(
std::map<std::string, clickhouse::ColumnRef > &data,
const std::map<std::string, std::string> &datatypes_map,

View File

@ -24,3 +24,17 @@ std::string string_or_null(const char *data)
{
return (data) ? data : std::string();
}
std::string sanitize_column_name(const std::string &name)
{
auto result = name;
std::replace(result.begin(), result.end(), '-', '_');
return result;
}
bool is_valid_table_name(const std::string &value)
{
return (std::find_if_not(value.begin(), value.end(), [] (const char c) { return (std::isalnum(c) || (strchr("_", c) != NULL)); } ) == value.end());
}

View File

@ -23,8 +23,11 @@
#include <string>
#include <memory>
#include <boost/property_tree/ptree.hpp>
std::string string_or_null(const char *data);
std::string sanitize_column_name(const std::string &name);
bool is_valid_table_name(const std::string &value);
template <typename T>
std::shared_ptr<T> ensure_not_null(const std::shared_ptr<T> &value)
@ -37,4 +40,33 @@ std::shared_ptr<T> ensure_not_null(const std::shared_ptr<T> &value)
return value;
}
template <typename T>
bool always_true(const T &)
{
return true;
}
template <typename T>
void optional_set(T &value, const boost::property_tree::ptree &tree, const char *element_name, bool (*check_function)(const T &))
{
auto element = tree.get_child_optional(element_name);
if (element)
{
if (check_function(element->get_value<T>()))
{
value = element->get_value<T>();
}
else
{
throw std::runtime_error(std::string("Invalid value for option '") + std::string(element_name) + std::string("'"));
}
}
}
template <typename T>
void optional_set(T &value, const boost::property_tree::ptree &tree, const char *element_name)
{
optional_set(value, tree, element_name, &always_true<T>);
}
#endif /* AUDITD_PLUGIN_CLICKHOUSE_UTILS_HPP */