#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;
|
}
|