Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "transformation_object.hpp"
#include <algorithm>
 
// Конструктор
TransformationObject::TransformationObject(TransformType type)
    : type_(type)
{
    initDefaultParams();
    obj_type_ = DEFAULT_OBJECT_TYPE;
}
 
TransformationObject::TransformationObject(const TransformType type, 
                                           const int32_t handle,
                                           const char* user_file, 
                                           const int encoding,
                                           const int obj_type)
    : type_(type), handle_(handle), user_file_(user_file ? user_file : ""),
      encoding_(encoding), obj_type_(obj_type)
{
    //initDefaultParams();
}
 
// Публичные методы установки (обёртки над шаблонным setParam)
void TransformationObject::setBool(const std::string& name, bool value,
                                   const int object_type) {
    setParam(name, value, object_type);
}
 
void TransformationObject::setString(const std::string& name, 
                                     const std::string& value,
                                     const int object_type) {
    setParam(name, value, object_type);
}
 
void TransformationObject::setRemapParams(const std::string& name,
                                          const std::string& old_value,
                                          const std::string& new_value,
                                          const int obj_type)
{
    // we can't have more than 10 param packs of the same remap parameter name
    if (remap_params_.count(name) > 10)
    {
        throw std::length_error("Error: you can't specify more than ten "
                            + name + " parameters in one transform object");
    }
 
    RemapParamPack rpp = {old_value, new_value, obj_type};
 
    // can't have duplicates
    auto range = remap_params_.equal_range(name);
    for (auto it = range.first; it != range.second; ++it)
    {
        if (it->second == rpp)
        {
            throw std::invalid_argument("Error: duplicate transformation "
                            "parameter " + name + " and values");
        }
    }
 
    // remap_params_.emplace(
    //         std::piecewise_construct,
    //         std::forward_as_tuple(name),
    //         std::forward_as_tuple(old_value, new_value, obj_type)
    // );
 
    remap_params_.insert({name, std::move(rpp)});
}
 
void TransformationObject::setInt(const std::string& name, int value,
                                  const int obj_type) {
    setParam(name, value, obj_type);
}
 
// Получение параметра
std::optional<TransformationObject::ParamValue>
TransformationObject::getParam(const std::string& name,
                               const int obj_type) const
{
    ParamValue default_value;
    bool is_def_val_set = false;
    int search_obj_type = obj_type;
 
    /* if obj_type is not specified then this is transform object level 
       object type
    */
   if (search_obj_type == DEFAULT_OBJECT_TYPE)
   {
        search_obj_type = obj_type_;
   }
 
    auto range = params_.equal_range(name);
 
    for (auto it = range.first; it != range.second; ++it)
    {
        // need to get exact object type
        if (it->second.object_type == search_obj_type)
        {
            return it->second.param_value;
        }
        // or maybe we have a default value (session_transform case)
        if (it->second.object_type == DEFAULT_OBJECT_TYPE)
        {
            default_value = it->second.param_value;
            is_def_val_set = true;
        }
    }
 
    /* if we don't have object and even don't have a default value then let's
       return nothing
    */
    if (!is_def_val_set)
    {
        return std::nullopt;
    }
 
    return default_value;
}
 
bool TransformationObject::hasParam(const std::string& name,
                                    const int obj_type) const {
    auto range = params_.equal_range(name);
 
    for (auto it = range.first; it != range.second; ++it)
    {
        // need to get exact object type
        if (it->second.object_type == obj_type)
        {
            return true;
        }
    }
 
    return false;
}
 
void TransformationObject::resetToDefaults() {
    initDefaultParams();
}
 
TransformType TransformationObject::getType() const {
    return type_;
}
 
int32_t TransformationObject::getOpenCtxHandle() const {
    return handle_;
}
 
int TransformationObject::getObjType() const {
    return obj_type_;
}
 
// const std::unordered_map<std::string, TransformationObject::ParamValue>& 
// TransformationObject::getAllParams() const {
//     return params_;
// }
 
// Инициализация параметров значениями по умолчанию
void TransformationObject::initDefaultParams() {
    params_.clear();
    for (const auto& def : getDefinitions(type_)) {
        params_[def.name] = {def.defaultValue, DEFAULT_OBJECT_TYPE};
    }
}
 
// Статический метод с определениями всех возможных параметров для каждого типа трансформации
const std::vector<TransformationObject::ParamDef>& 
TransformationObject::getDefinitions(TransformType type) {
    // Определения для DDL (таблица 115-23)
    static const std::vector<ParamDef> ddlDefs = {
        // Булевы
        {"PRETTY",                  true},
        {"SQLTERMINATOR",           false},
        {"CONSTRAINTS",             true},
        {"REF_CONSTRAINTS",         false},
        {"CONSTRAINTS_AS_ALTER",    false},
        {"OID",                     false},
        {"SIZE_BYTE_KEYWORD",       false},
        {"PARTITIONING",            true},
        {"SEGMENT_ATTRIBUTES",      true},
        {"STORAGE",                 true},
        {"TABLESPACE",              true},
        {"SPECIFICATION",           true},
        {"BODY",                    true},
        {"FORCE",                   true},
        {"REUSE",                   false},
        {"OMIT_ENCRYPTION_CLAUSE",  false},
        {"DWCS_CVT_IOTS",           false},
        {"DWCS_CVT_CONSTRAINTS",    false},
        {"CONSTRAINT_USE_DEFAULT_INDEX", false},
        {"CONSTRAINT_NAME_FROM_INDEX",   false},
        {"DEFAULT",                 false},
        {"INHERIT",                 false},
        // Строковые
        {"COLLATION_CLAUSE",        std::string("NEVER")},
        {"REVOKE_FROM",             std::string("")},
        {"LOB_STORAGE",             std::string("NO_CHANGE")},
        {"TABLE_COMPRESSION_CLAUSE", std::string("NONE")},
        // Числовые
        {"PCTSPACE",                100}
    };
 
    // Определения для MODIFY (таблицы 115-24 и 115-25)
    // NEEDS A SEPARATE MECHANISM  TODO
    static const std::vector<ParamDef> modifyDefs = {
        {"OBJECT_ROW",              0},                // числовой
        {"REMAP_DATAFILE",          std::string("")},  // строковый
        {"REMAP_NAME",              std::string("")},
        {"REMAP_SCHEMA",            std::string("")},
        {"REMAP_TABLESPACE",        std::string("")}
    };
 
    // Определения для SXML (таблица 115-26)
    static const std::vector<ParamDef> sxmlDefs = {
        {"COLLATION_CLAUSE",        std::string("NEVER")},
        {"OID",                     false},
        {"STORAGE",                 true},
        {"TABLESPACE",              true},
        {"REF_CONSTRAINTS",         true},
        {"PHYSICAL_PROPERTIES",     true},
        {"SEGMENT_ATTRIBUTES",      true},
        {"PARTITIONING",            true},
        {"CONSTRAINTS",             true}
    };
 
    // Определения для MODIFYSXML (таблица 115-27)
    static const std::vector<ParamDef> modifysxmlDefs = {
        {"STORAGE",                 true},
        {"TABLESPACE",              true},
        {"REF_CONSTRAINTS",         true},
        {"REMAP_NAME",              std::string("")},
        {"REMAP_SCHEMA",            std::string("")},
        {"REMAP_COLUMN_NAME",       std::string("")},
        {"SEGMENT_ATTRIBUTES",      true},
        {"CONSTRAINTS",             true}
    };
 
    // Определения для SXMLDDL (таблица 115-28)
    static const std::vector<ParamDef> sxmlddlDefs = {
        {"COLLATION_CLAUSE",        std::string("NEVER")},
        {"OID",                     true},
        {"TABLESPACE",              true},
        {"STORAGE",                 true},
        {"REF_CONSTRAINTS",         true},
        {"PRESERVE_LOCAL",          false},
        {"SEGMENT_ATTRIBUTES",      true},
        {"REUSE",                   false},
        {"PARTITIONING",            true},
        {"CONSTRAINTS",             true}
    };
 
    switch (type) {
        case TransformType::DDL:        return ddlDefs;
        case TransformType::MODIFY:     return modifyDefs;
        case TransformType::SXML:       return sxmlDefs;
        case TransformType::MODIFYSXML: return modifysxmlDefs;
        case TransformType::SXMLDDL:    return sxmlddlDefs;
        default:
            static const std::vector<ParamDef> empty;
            return empty;
    }
}
 
// Реализация статической функции преобразования строки в TransformType
std::optional<TransformType> TransformationObject::stringToTransformType(const std::string& name) {
    // Создаём копию строки в верхнем регистре для регистронезависимого сравнения
    std::string upper = name;
    std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
 
    if (upper == "DDL")          return TransformType::DDL;
    if (upper == "MODIFY")       return TransformType::MODIFY;
    if (upper == "SXML")         return TransformType::SXML;
    if (upper == "MODIFYSXML")   return TransformType::MODIFYSXML;
    if (upper == "SXMLDDL")      return TransformType::SXMLDDL;
    if (upper == "ALTERDDL")     return TransformType::ALTERDDL;
    if (upper == "ALTERXML")     return TransformType::ALTERXML;
 
    return std::nullopt;
}