1#include <Access/RowPolicy.h>
2#include <Interpreters/Context.h>
3#include <Common/quoteString.h>
4#include <boost/range/algorithm/equal.hpp>
5
6
7namespace DB
8{
9namespace
10{
11 void generateFullNameImpl(const String & database_, const String & table_name_, const String & policy_name_, String & full_name_)
12 {
13 full_name_.clear();
14 full_name_.reserve(database_.length() + table_name_.length() + policy_name_.length() + 6);
15 full_name_ += backQuoteIfNeed(policy_name_);
16 full_name_ += " ON ";
17 if (!database_.empty())
18 {
19 full_name_ += backQuoteIfNeed(database_);
20 full_name_ += '.';
21 }
22 full_name_ += backQuoteIfNeed(table_name_);
23 }
24}
25
26
27String RowPolicy::FullNameParts::getFullName() const
28{
29 String full_name;
30 generateFullNameImpl(database, table_name, policy_name, full_name);
31 return full_name;
32}
33
34
35String RowPolicy::FullNameParts::getFullName(const Context & context) const
36{
37 String full_name;
38 generateFullNameImpl(database.empty() ? context.getCurrentDatabase() : database, table_name, policy_name, full_name);
39 return full_name;
40}
41
42
43void RowPolicy::setDatabase(const String & database_)
44{
45 database = database_;
46 generateFullNameImpl(database, table_name, policy_name, full_name);
47}
48
49
50void RowPolicy::setTableName(const String & table_name_)
51{
52 table_name = table_name_;
53 generateFullNameImpl(database, table_name, policy_name, full_name);
54}
55
56
57void RowPolicy::setName(const String & policy_name_)
58{
59 policy_name = policy_name_;
60 generateFullNameImpl(database, table_name, policy_name, full_name);
61}
62
63
64void RowPolicy::setFullName(const String & database_, const String & table_name_, const String & policy_name_)
65{
66 database = database_;
67 table_name = table_name_;
68 policy_name = policy_name_;
69 generateFullNameImpl(database, table_name, policy_name, full_name);
70}
71
72
73bool RowPolicy::equal(const IAccessEntity & other) const
74{
75 if (!IAccessEntity::equal(other))
76 return false;
77 const auto & other_policy = typeid_cast<const RowPolicy &>(other);
78 return (database == other_policy.database) && (table_name == other_policy.table_name) && (policy_name == other_policy.policy_name)
79 && boost::range::equal(conditions, other_policy.conditions) && restrictive == other_policy.restrictive
80 && (roles == other_policy.roles) && (all_roles == other_policy.all_roles) && (except_roles == other_policy.except_roles);
81}
82
83
84const char * RowPolicy::conditionIndexToString(ConditionIndex index)
85{
86 switch (index)
87 {
88 case SELECT_FILTER: return "SELECT_FILTER";
89 case INSERT_CHECK: return "INSERT_CHECK";
90 case UPDATE_FILTER: return "UPDATE_FILTER";
91 case UPDATE_CHECK: return "UPDATE_CHECK";
92 case DELETE_FILTER: return "DELETE_FILTER";
93 }
94 __builtin_unreachable();
95}
96
97
98const char * RowPolicy::conditionIndexToColumnName(ConditionIndex index)
99{
100 switch (index)
101 {
102 case SELECT_FILTER: return "select_filter";
103 case INSERT_CHECK: return "insert_check";
104 case UPDATE_FILTER: return "update_filter";
105 case UPDATE_CHECK: return "update_check";
106 case DELETE_FILTER: return "delete_filter";
107 }
108 __builtin_unreachable();
109}
110
111}
112