added basic support for containers with pointers to models
added to Model: template<typename ContainerItemType> void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list<ContainerItemType*> & field_value, const FT & field_type = FT::default_type); template<typename ContainerItemType> void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector<ContainerItemType*> & field_value, const FT & field_type = FT::default_type);
This commit is contained in:
227
src/model.h
227
src/model.h
@@ -445,6 +445,17 @@ protected:
|
||||
field_vector(field_name, field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
template<typename ContainerItemType>
|
||||
void field(const wchar_t * field_name, std::list<ContainerItemType*> & field_value, const FT & field_type = FT::default_type)
|
||||
{
|
||||
field_list(field_name, field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
template<typename ContainerItemType>
|
||||
void field(const wchar_t * field_name, std::vector<ContainerItemType*> & field_value, const FT & field_type = FT::default_type)
|
||||
{
|
||||
field_vector(field_name, field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -549,23 +560,34 @@ protected:
|
||||
|
||||
void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_value, const FT & field_type = FT::default_type)
|
||||
{
|
||||
// has_foreign_key was here
|
||||
field_model(db_field_name, flat_field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
template<typename ModelClass>
|
||||
void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list<ModelClass> & field_value, const FT & field_type = FT::default_type)
|
||||
template<typename ContainerItemType>
|
||||
void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list<ContainerItemType> & field_value, const FT & field_type = FT::default_type)
|
||||
{
|
||||
field_list(db_field_name, flat_field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
template<typename ModelClass>
|
||||
void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector<ModelClass> & field_value, const FT & field_type = FT::default_type)
|
||||
template<typename ContainerItemType>
|
||||
void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector<ContainerItemType> & field_value, const FT & field_type = FT::default_type)
|
||||
{
|
||||
field_vector(db_field_name, flat_field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
// FIXME we need to correct handle such pointerns, cctor, dtor etc.
|
||||
template<typename ContainerItemType>
|
||||
void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list<ContainerItemType*> & field_value, const FT & field_type = FT::default_type)
|
||||
{
|
||||
field_list(db_field_name, flat_field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
// FIXME we need to correct handle such pointerns, cctor, dtor etc.
|
||||
template<typename ContainerItemType>
|
||||
void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector<ContainerItemType*> & field_value, const FT & field_type = FT::default_type)
|
||||
{
|
||||
field_vector(db_field_name, flat_field_name, field_value, field_type);
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
@@ -857,17 +879,18 @@ protected:
|
||||
void field_model_save_key(const wchar_t * db_field_name);
|
||||
void field_model_set_parent_key_in_child(const wchar_t * db_field_name, Model & field_model);
|
||||
void field_model_set_parent_key(const wchar_t * db_field_name, Model & field_model);
|
||||
void field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model, const FT & field_type);
|
||||
void field_model_iterate_through_childs(Model & field_model, const FT & field_type);
|
||||
void field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, const FT & field_type);
|
||||
void field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, const FT & field_type);
|
||||
void field_model_clear_values(Model & field_model);
|
||||
void field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, const FT & field_type);
|
||||
void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, const FT & field_type);
|
||||
void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model * field_model, const FT & field_type);
|
||||
void field_model_for_db(const wchar_t * db_field_name, Model & field_model, const FT & field_type);
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType>
|
||||
void field_list_set_parent_key(const wchar_t * db_field_name, ModelContainer & field_container, ModelContainerType * model_container_type)
|
||||
template<typename ModelContainer, typename ModelContainerType, typename IsContainerByValueRenameMe>
|
||||
void field_list_set_parent_key(const wchar_t * db_field_name, ModelContainer & field_container, ModelContainerType * model_container_type, IsContainerByValueRenameMe * foo)
|
||||
{
|
||||
FieldValueHelper helper;
|
||||
helper.db_field_name = db_field_name;
|
||||
@@ -881,10 +904,13 @@ protected:
|
||||
model_env->field_value_helper_tab = &helper_tab;
|
||||
field_model_save_key(db_field_name);
|
||||
|
||||
for(ModelContainerType & child_model : field_container)
|
||||
if constexpr (std::is_base_of<Model, IsContainerByValueRenameMe>())
|
||||
{
|
||||
child_model.set_connector(model_connector);
|
||||
field_model_set_parent_key_in_child(db_field_name, child_model);
|
||||
field_list_set_parent_key_by_value<ModelContainer, ModelContainerType>(db_field_name, field_container);
|
||||
}
|
||||
else
|
||||
{
|
||||
field_list_set_parent_key_by_pointer<ModelContainer, ModelContainerType>(db_field_name, field_container);
|
||||
}
|
||||
|
||||
model_env->field_value_helper_tab = nullptr;
|
||||
@@ -892,46 +918,84 @@ protected:
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType>
|
||||
void field_list_iterate_through_childs(const wchar_t * db_field_name, ModelContainer & field_container, ModelContainerType * model_container_type)
|
||||
void field_list_set_parent_key_by_value(const wchar_t * db_field_name, ModelContainer & field_container)
|
||||
{
|
||||
for(ModelContainerType & child_model : field_container)
|
||||
{
|
||||
ModelEnv model_env_local;
|
||||
model_env_local.copy_global_objects(*model_env);
|
||||
model_env_local.model = &child_model;
|
||||
|
||||
child_model.model_env = &model_env_local;
|
||||
child_model.model_env->has_primary_key_set = child_model.has_primary_key_set;
|
||||
child_model.set_connector(model_connector);
|
||||
child_model.table();
|
||||
|
||||
if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT )
|
||||
{
|
||||
child_model.insert_tree(true);
|
||||
}
|
||||
|
||||
if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE )
|
||||
{
|
||||
child_model.update_tree(true);
|
||||
}
|
||||
|
||||
if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE )
|
||||
{
|
||||
child_model.remove_tree(true);
|
||||
}
|
||||
|
||||
if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE )
|
||||
{
|
||||
child_model.save_tree(true);
|
||||
}
|
||||
|
||||
child_model.model_env = nullptr;
|
||||
field_model_set_parent_key_in_child(db_field_name, child_model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer>
|
||||
void field_list_generate_flat_string(const wchar_t * flat_field_name, ModelContainer & field_container, const FT & field_type)
|
||||
template<typename ModelContainer, typename ModelContainerType>
|
||||
void field_list_set_parent_key_by_pointer(const wchar_t * db_field_name, ModelContainer & field_container)
|
||||
{
|
||||
for(ModelContainerType * child_model : field_container)
|
||||
{
|
||||
child_model->set_connector(model_connector);
|
||||
field_model_set_parent_key_in_child(db_field_name, *child_model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType, typename IsContainerByValueRenameMe>
|
||||
void field_list_iterate_through_childs(ModelContainer & field_container, ModelContainerType * model_container_type, const FT & field_type, IsContainerByValueRenameMe * foo)
|
||||
{
|
||||
if constexpr (std::is_base_of<Model, IsContainerByValueRenameMe>())
|
||||
{
|
||||
field_list_iterate_through_childs_by_value(field_container, model_container_type, field_type);
|
||||
}
|
||||
else
|
||||
{
|
||||
field_list_iterate_through_childs_by_pointer(field_container, model_container_type, field_type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType>
|
||||
void field_list_iterate_through_childs_by_value(ModelContainer & field_container, ModelContainerType * model_container_type, const FT & field_type)
|
||||
{
|
||||
for(ModelContainerType & child_model : field_container)
|
||||
{
|
||||
field_list_iterate_through_childs(child_model, field_type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType>
|
||||
void field_list_iterate_through_childs_by_pointer(ModelContainer & field_container, ModelContainerType * model_container_type, const FT & field_type)
|
||||
{
|
||||
for(ModelContainerType * child_model : field_container)
|
||||
{
|
||||
field_list_iterate_through_childs(*child_model, field_type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename ModelContainerType>
|
||||
void field_list_iterate_through_childs(ModelContainerType & child_model, const FT & field_type)
|
||||
{
|
||||
ModelEnv model_env_local;
|
||||
model_env_local.copy_global_objects(*model_env);
|
||||
model_env_local.model = &child_model;
|
||||
|
||||
child_model.model_env = &model_env_local;
|
||||
child_model.model_env->has_primary_key_set = child_model.has_primary_key_set;
|
||||
child_model.set_connector(model_connector);
|
||||
child_model.table();
|
||||
|
||||
field_model_iterate_through_childs(child_model, field_type);
|
||||
|
||||
child_model.model_env = nullptr;
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType, typename IsContainerByValueRenameMe>
|
||||
void field_list_generate_flat_string(const wchar_t * flat_field_name, ModelContainer & field_container, ModelContainerType * model_container_type,
|
||||
const FT & field_type, IsContainerByValueRenameMe * foo)
|
||||
{
|
||||
FlatConnector * flat_connector = model_connector->get_flat_connector();
|
||||
|
||||
@@ -941,20 +1005,20 @@ protected:
|
||||
|
||||
if( flat_expression )
|
||||
{
|
||||
flat_expression->field_list(flat_field_name, field_container, field_type, model_connector, model_env);
|
||||
flat_expression->field_list(flat_field_name, field_container, model_container_type, field_type, model_connector, model_env, foo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer>
|
||||
void field_list_clearing_values(ModelContainer & field_container)
|
||||
template<typename ModelContainer, typename ModelContainerType, typename IsContainerByValueRenameMe>
|
||||
void field_list_clearing_values(ModelContainer & field_container, ModelContainerType * model_container_type, IsContainerByValueRenameMe * foo)
|
||||
{
|
||||
Clearer * clearer = model_connector->get_clearer();
|
||||
|
||||
if( clearer )
|
||||
{
|
||||
clearer->clear_container(field_container);
|
||||
clearer->clear_container(field_container, model_container_type, foo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -978,7 +1042,8 @@ protected:
|
||||
}
|
||||
else
|
||||
{
|
||||
field_list_generic(db_field_name, flat_field_name, field_value, item_type_null_pointer, field_type);
|
||||
ContainerItemType * pointer = nullptr;
|
||||
field_list_generic(db_field_name, flat_field_name, field_value, item_type_null_pointer, field_type, pointer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1002,15 +1067,65 @@ protected:
|
||||
}
|
||||
else
|
||||
{
|
||||
field_list_generic(db_field_name, flat_field_name, field_value, item_type_null_pointer, field_type);
|
||||
ContainerItemType * pointer = nullptr;
|
||||
field_list_generic(db_field_name, flat_field_name, field_value, item_type_null_pointer, field_type, pointer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ContainerItemType>
|
||||
void field_vector(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector<ContainerItemType*> & field_value, const FT & field_type)
|
||||
{
|
||||
ContainerItemType * item_type_null_pointer = nullptr;
|
||||
|
||||
if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GET_MODEL_WRAPPER )
|
||||
{
|
||||
if constexpr (std::is_base_of<Model, ContainerItemType>())
|
||||
{
|
||||
if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) &&
|
||||
(is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) &&
|
||||
!model_env->model_wrapper )
|
||||
{
|
||||
model_env->model_wrapper = new ModelWrapperVectorPointer(&field_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
void * pointer = nullptr;
|
||||
field_list_generic(db_field_name, flat_field_name, field_value, item_type_null_pointer, field_type, pointer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType>
|
||||
void field_list_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelContainer & field_container, ModelContainerType * model_container_type, const FT & field_type)
|
||||
template<typename ContainerItemType>
|
||||
void field_list(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list<ContainerItemType*> & field_value, const FT & field_type)
|
||||
{
|
||||
ContainerItemType * item_type_null_pointer = nullptr;
|
||||
|
||||
if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GET_MODEL_WRAPPER )
|
||||
{
|
||||
if constexpr (std::is_base_of<Model, ContainerItemType>())
|
||||
{
|
||||
if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) &&
|
||||
(is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) &&
|
||||
!model_env->model_wrapper )
|
||||
{
|
||||
model_env->model_wrapper = new ModelWrapperListPointer(&field_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
void * pointer = nullptr;
|
||||
field_list_generic(db_field_name, flat_field_name, field_value, item_type_null_pointer, field_type, pointer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename ModelContainer, typename ModelContainerType, typename IsContainerByValueRenameMe>
|
||||
void field_list_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelContainer & field_container, ModelContainerType * model_container_type,
|
||||
const FT & field_type, IsContainerByValueRenameMe * foo)
|
||||
{
|
||||
if( model_connector && model_env )
|
||||
{
|
||||
@@ -1038,12 +1153,14 @@ protected:
|
||||
{
|
||||
if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID )
|
||||
{
|
||||
field_list_set_parent_key(db_field_name, field_container, model_container_type);
|
||||
field_list_set_parent_key(db_field_name, field_container, model_container_type, foo);
|
||||
}
|
||||
|
||||
if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY )
|
||||
{
|
||||
field_list_iterate_through_childs(db_field_name, field_container, model_container_type);
|
||||
// IMPROVEME it would be good to make some optimisation before going through all items
|
||||
// such as: if UPDATE is called then we check if field_type allows to make update
|
||||
field_list_iterate_through_childs(field_container, model_container_type, field_type, foo);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -1071,13 +1188,13 @@ protected:
|
||||
{
|
||||
if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING )
|
||||
{
|
||||
field_list_generate_flat_string(flat_field_name, field_container, field_type);
|
||||
field_list_generate_flat_string(flat_field_name, field_container, model_container_type, field_type, foo);
|
||||
}
|
||||
}
|
||||
|
||||
if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_CLEARING_VALUE )
|
||||
{
|
||||
field_list_clearing_values(field_container);
|
||||
field_list_clearing_values(field_container, model_container_type, foo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user