2020-03-04 22:27:03 +00:00
|
|
|
#include <Access/SettingsProfileElement.h>
|
|
|
|
#include <Access/SettingsConstraints.h>
|
2020-03-18 14:11:44 +00:00
|
|
|
#include <Access/AccessControlManager.h>
|
|
|
|
#include <Access/SettingsProfile.h>
|
|
|
|
#include <Parsers/ASTSettingsProfileElement.h>
|
2020-03-04 22:27:03 +00:00
|
|
|
#include <Core/Settings.h>
|
2020-07-22 12:02:47 +00:00
|
|
|
#include <Common/SettingsChanges.h>
|
2020-03-18 14:11:44 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-03-04 22:27:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-03-18 14:11:44 +00:00
|
|
|
SettingsProfileElement::SettingsProfileElement(const ASTSettingsProfileElement & ast)
|
|
|
|
{
|
|
|
|
init(ast, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsProfileElement::SettingsProfileElement(const ASTSettingsProfileElement & ast, const AccessControlManager & manager)
|
|
|
|
{
|
|
|
|
init(ast, &manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsProfileElement::init(const ASTSettingsProfileElement & ast, const AccessControlManager * manager)
|
|
|
|
{
|
|
|
|
auto name_to_id = [id_mode{ast.id_mode}, manager](const String & name_) -> UUID
|
|
|
|
{
|
|
|
|
if (id_mode)
|
|
|
|
return parse<UUID>(name_);
|
|
|
|
assert(manager);
|
|
|
|
return manager->getID<SettingsProfile>(name_);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ast.parent_profile.empty())
|
|
|
|
parent_profile = name_to_id(ast.parent_profile);
|
|
|
|
|
2020-04-21 10:21:03 +00:00
|
|
|
if (!ast.setting_name.empty())
|
2020-03-18 14:11:44 +00:00
|
|
|
{
|
2020-07-19 14:59:07 +00:00
|
|
|
setting_name = ast.setting_name;
|
2020-03-18 14:11:44 +00:00
|
|
|
value = ast.value;
|
|
|
|
min_value = ast.min_value;
|
|
|
|
max_value = ast.max_value;
|
|
|
|
readonly = ast.readonly;
|
|
|
|
|
|
|
|
if (!value.isNull())
|
2020-07-20 09:57:17 +00:00
|
|
|
value = Settings::castValueUtil(setting_name, value);
|
2020-03-18 14:11:44 +00:00
|
|
|
if (!min_value.isNull())
|
2020-07-20 09:57:17 +00:00
|
|
|
min_value = Settings::castValueUtil(setting_name, min_value);
|
2020-03-18 14:11:44 +00:00
|
|
|
if (!max_value.isNull())
|
2020-07-20 09:57:17 +00:00
|
|
|
max_value = Settings::castValueUtil(setting_name, max_value);
|
2020-07-28 23:39:18 +00:00
|
|
|
|
|
|
|
/// Optionally check if a setting with that name is allowed.
|
|
|
|
if (manager)
|
|
|
|
manager->checkSettingNameIsAllowed(setting_name);
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<ASTSettingsProfileElement> SettingsProfileElement::toAST() const
|
|
|
|
{
|
|
|
|
auto ast = std::make_shared<ASTSettingsProfileElement>();
|
|
|
|
ast->id_mode = true;
|
|
|
|
|
|
|
|
if (parent_profile)
|
|
|
|
ast->parent_profile = ::DB::toString(*parent_profile);
|
|
|
|
|
2020-07-19 14:59:07 +00:00
|
|
|
ast->setting_name = setting_name;
|
2020-03-18 14:11:44 +00:00
|
|
|
ast->value = value;
|
|
|
|
ast->min_value = min_value;
|
|
|
|
ast->max_value = max_value;
|
|
|
|
ast->readonly = readonly;
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<ASTSettingsProfileElement> SettingsProfileElement::toASTWithNames(const AccessControlManager & manager) const
|
|
|
|
{
|
|
|
|
auto ast = std::make_shared<ASTSettingsProfileElement>();
|
|
|
|
|
|
|
|
if (parent_profile)
|
|
|
|
{
|
|
|
|
auto parent_profile_name = manager.tryReadName(*parent_profile);
|
|
|
|
if (parent_profile_name)
|
|
|
|
ast->parent_profile = *parent_profile_name;
|
|
|
|
}
|
|
|
|
|
2020-07-19 14:59:07 +00:00
|
|
|
ast->setting_name = setting_name;
|
2020-03-18 14:11:44 +00:00
|
|
|
ast->value = value;
|
|
|
|
ast->min_value = min_value;
|
|
|
|
ast->max_value = max_value;
|
|
|
|
ast->readonly = readonly;
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SettingsProfileElements::SettingsProfileElements(const ASTSettingsProfileElements & ast)
|
|
|
|
{
|
|
|
|
for (const auto & ast_element : ast.elements)
|
|
|
|
emplace_back(*ast_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsProfileElements::SettingsProfileElements(const ASTSettingsProfileElements & ast, const AccessControlManager & manager)
|
|
|
|
{
|
|
|
|
for (const auto & ast_element : ast.elements)
|
|
|
|
emplace_back(*ast_element, manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<ASTSettingsProfileElements> SettingsProfileElements::toAST() const
|
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTSettingsProfileElements>();
|
|
|
|
for (const auto & element : *this)
|
|
|
|
res->elements.push_back(element.toAST());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ASTSettingsProfileElements> SettingsProfileElements::toASTWithNames(const AccessControlManager & manager) const
|
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTSettingsProfileElements>();
|
|
|
|
for (const auto & element : *this)
|
|
|
|
res->elements.push_back(element.toASTWithNames(manager));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-04 22:27:03 +00:00
|
|
|
void SettingsProfileElements::merge(const SettingsProfileElements & other)
|
|
|
|
{
|
|
|
|
insert(end(), other.begin(), other.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Settings SettingsProfileElements::toSettings() const
|
|
|
|
{
|
|
|
|
Settings res;
|
|
|
|
for (const auto & elem : *this)
|
|
|
|
{
|
2020-07-19 14:59:07 +00:00
|
|
|
if (!elem.setting_name.empty() && !elem.value.isNull())
|
|
|
|
res.set(elem.setting_name, elem.value);
|
2020-03-04 22:27:03 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsChanges SettingsProfileElements::toSettingsChanges() const
|
|
|
|
{
|
|
|
|
SettingsChanges res;
|
|
|
|
for (const auto & elem : *this)
|
|
|
|
{
|
2020-07-19 14:59:07 +00:00
|
|
|
if (!elem.setting_name.empty() && !elem.value.isNull())
|
|
|
|
res.push_back({elem.setting_name, elem.value});
|
2020-03-04 22:27:03 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:39:18 +00:00
|
|
|
SettingsConstraints SettingsProfileElements::toSettingsConstraints(const AccessControlManager & manager) const
|
2020-03-04 22:27:03 +00:00
|
|
|
{
|
2020-07-28 23:39:18 +00:00
|
|
|
SettingsConstraints res{manager};
|
2020-03-04 22:27:03 +00:00
|
|
|
for (const auto & elem : *this)
|
|
|
|
{
|
2020-07-19 14:59:07 +00:00
|
|
|
if (!elem.setting_name.empty())
|
2020-03-04 22:27:03 +00:00
|
|
|
{
|
|
|
|
if (!elem.min_value.isNull())
|
2020-07-19 14:59:07 +00:00
|
|
|
res.setMinValue(elem.setting_name, elem.min_value);
|
2020-03-04 22:27:03 +00:00
|
|
|
if (!elem.max_value.isNull())
|
2020-07-19 14:59:07 +00:00
|
|
|
res.setMaxValue(elem.setting_name, elem.max_value);
|
2020-03-04 22:27:03 +00:00
|
|
|
if (elem.readonly)
|
2020-07-19 14:59:07 +00:00
|
|
|
res.setReadOnly(elem.setting_name, *elem.readonly);
|
2020-03-04 22:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|