changed: SetFieldValueHelper -> FieldValueHelper

it's a much simpler structure now
removed: FieldValueBase and FieldValueContainer




git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1207 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2019-09-11 18:16:22 +00:00
parent 0be9feda44
commit c133e949ce
7 changed files with 165 additions and 282 deletions

View File

@ -14,7 +14,7 @@ main.o: ../../morm/src/queryresult.h ../../morm/src/flatconnector.h
main.o: ../../morm/src/dbexpression.h ../../morm/src/baseexpression.h
main.o: ../../morm/src/modelenv.h ../../morm/src/modeldata.h
main.o: ../../morm/src/cursorhelper.h ../../morm/src/finderhelper.h
main.o: ../../morm/src/setfieldvaluehelper.h ../../morm/src/flatexpression.h
main.o: ../../morm/src/fieldvaluehelper.h ../../morm/src/flatexpression.h
main.o: ../../morm/src/finder.h ../../pikotools/utf8/utf8.h
main.o: ../../morm/src/cursor.h ../../morm/src/jsonexpression.h
main.o: ../../morm/src/postgresqlexpression.h

View File

@ -76,10 +76,7 @@ public:
field(L"id", id, false, false, true);
field(L"person_id", person_id);
field(L"name", name);
field(L"language_id", language);
field(L"content", content);
field(L"some_flags", some_flags);
field(L"created_date", created_date);

View File

@ -8,7 +8,7 @@ baseexpression.o: ../../pikotools/convert/inttostr.h
baseexpression.o: ../../pikotools/membuffer/membuffer.h
baseexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h
baseexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h
baseexpression.o: setfieldvaluehelper.h model.h modelconnector.h clearer.h
baseexpression.o: fieldvaluehelper.h model.h modelconnector.h clearer.h
baseexpression.o: dbconnector.h ../../pikotools/log/log.h
baseexpression.o: ../../pikotools/log/filelog.h flatconnector.h
baseexpression.o: dbexpression.h flatexpression.h ../../pikotools/utf8/utf8.h
@ -21,7 +21,7 @@ clearer.o: ../../pikotools/textstream/types.h modelconnector.h dbconnector.h
clearer.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h
clearer.o: queryresult.h flatconnector.h dbexpression.h baseexpression.h
clearer.o: morm_types.h modelenv.h modeldata.h cursorhelper.h finderhelper.h
clearer.o: setfieldvaluehelper.h flatexpression.h
clearer.o: fieldvaluehelper.h flatexpression.h
dbconnector.o: dbconnector.h ../../pikotools/textstream/textstream.h
dbconnector.o: ../../pikotools/space/space.h
dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h
@ -30,7 +30,7 @@ dbconnector.o: ../../pikotools/membuffer/membuffer.h
dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/log/log.h
dbconnector.o: ../../pikotools/log/filelog.h queryresult.h dbexpression.h
dbconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h
dbconnector.o: cursorhelper.h finderhelper.h setfieldvaluehelper.h model.h
dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h model.h
dbconnector.o: modelconnector.h clearer.h flatconnector.h flatexpression.h
dbconnector.o: ../../pikotools/utf8/utf8.h ../../pikotools/convert/convert.h
dbconnector.o: ../../pikotools/convert/inttostr.h
@ -46,7 +46,7 @@ dbexpression.o: ../../pikotools/convert/inttostr.h
dbexpression.o: ../../pikotools/membuffer/membuffer.h
dbexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h
dbexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h
dbexpression.o: setfieldvaluehelper.h
dbexpression.o: fieldvaluehelper.h
dochtmlconnector.o: dochtmlconnector.h flatconnector.h
dochtmlconnector.o: ../../pikotools/textstream/textstream.h
dochtmlconnector.o: ../../pikotools/space/space.h
@ -57,7 +57,7 @@ dochtmlconnector.o: ../../pikotools/membuffer/membuffer.h
dochtmlconnector.o: ../../pikotools/textstream/types.h dochtmlexpression.h
dochtmlconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h
dochtmlconnector.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h
dochtmlconnector.o: setfieldvaluehelper.h
dochtmlconnector.o: fieldvaluehelper.h
dochtmlexpression.o: dochtmlexpression.h flatexpression.h baseexpression.h
dochtmlexpression.o: ../../pikotools/textstream/textstream.h
dochtmlexpression.o: ../../pikotools/space/space.h
@ -67,7 +67,7 @@ dochtmlexpression.o: ../../pikotools/convert/inttostr.h
dochtmlexpression.o: ../../pikotools/membuffer/membuffer.h
dochtmlexpression.o: ../../pikotools/textstream/types.h morm_types.h
dochtmlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h
dochtmlexpression.o: finderhelper.h setfieldvaluehelper.h
dochtmlexpression.o: finderhelper.h fieldvaluehelper.h
flatconnector.o: flatconnector.h ../../pikotools/textstream/textstream.h
flatconnector.o: ../../pikotools/space/space.h
flatconnector.o: ../../pikotools/textstream/types.h
@ -77,7 +77,7 @@ flatconnector.o: ../../pikotools/membuffer/membuffer.h
flatconnector.o: ../../pikotools/textstream/types.h flatexpression.h
flatconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h
flatconnector.o: cursorhelper.h queryresult.h finderhelper.h
flatconnector.o: setfieldvaluehelper.h model.h modelconnector.h clearer.h
flatconnector.o: fieldvaluehelper.h model.h modelconnector.h clearer.h
flatconnector.o: dbconnector.h ../../pikotools/log/log.h
flatconnector.o: ../../pikotools/log/filelog.h dbexpression.h
flatexpression.o: flatexpression.h baseexpression.h
@ -89,7 +89,7 @@ flatexpression.o: ../../pikotools/convert/inttostr.h
flatexpression.o: ../../pikotools/membuffer/membuffer.h
flatexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h
flatexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h
flatexpression.o: setfieldvaluehelper.h
flatexpression.o: fieldvaluehelper.h
jsonconnector.o: jsonconnector.h flatconnector.h
jsonconnector.o: ../../pikotools/textstream/textstream.h
jsonconnector.o: ../../pikotools/space/space.h
@ -100,7 +100,7 @@ jsonconnector.o: ../../pikotools/membuffer/membuffer.h
jsonconnector.o: ../../pikotools/textstream/types.h jsonexpression.h
jsonconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h
jsonconnector.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h
jsonconnector.o: setfieldvaluehelper.h
jsonconnector.o: fieldvaluehelper.h
jsonexpression.o: jsonexpression.h flatexpression.h baseexpression.h
jsonexpression.o: ../../pikotools/textstream/textstream.h
jsonexpression.o: ../../pikotools/space/space.h
@ -110,7 +110,7 @@ jsonexpression.o: ../../pikotools/convert/inttostr.h
jsonexpression.o: ../../pikotools/membuffer/membuffer.h
jsonexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h
jsonexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h
jsonexpression.o: setfieldvaluehelper.h
jsonexpression.o: fieldvaluehelper.h
model.o: model.h ../../pikotools/textstream/textstream.h
model.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h
model.o: ../../pikotools/date/date.h ../../pikotools/convert/inttostr.h
@ -119,7 +119,7 @@ model.o: ../../pikotools/textstream/types.h modelconnector.h clearer.h
model.o: dbconnector.h ../../pikotools/log/log.h
model.o: ../../pikotools/log/filelog.h queryresult.h flatconnector.h
model.o: dbexpression.h baseexpression.h morm_types.h modelenv.h modeldata.h
model.o: cursorhelper.h finderhelper.h setfieldvaluehelper.h flatexpression.h
model.o: cursorhelper.h finderhelper.h fieldvaluehelper.h flatexpression.h
modelconnector.o: modelconnector.h clearer.h ../../pikotools/date/date.h
modelconnector.o: ../../pikotools/convert/inttostr.h dbconnector.h
modelconnector.o: ../../pikotools/textstream/textstream.h
@ -142,7 +142,7 @@ postgresqlconnector.o: ../../pikotools/log/filelog.h queryresult.h
postgresqlconnector.o: postgresqlqueryresult.h ../../pikotools/utf8/utf8.h
postgresqlconnector.o: postgresqlexpression.h dbexpression.h baseexpression.h
postgresqlconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h
postgresqlconnector.o: finderhelper.h setfieldvaluehelper.h
postgresqlconnector.o: finderhelper.h fieldvaluehelper.h
postgresqlconnector.o: ../../pikotools/convert/strtoint.h
postgresqlconnector.o: ../../pikotools/convert/text.h
postgresqlconnector.o: ../../pikotools/convert/misc.h
@ -156,7 +156,7 @@ postgresqlexpression.o: ../../pikotools/convert/inttostr.h
postgresqlexpression.o: ../../pikotools/membuffer/membuffer.h
postgresqlexpression.o: ../../pikotools/textstream/types.h morm_types.h
postgresqlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h
postgresqlexpression.o: finderhelper.h setfieldvaluehelper.h
postgresqlexpression.o: finderhelper.h fieldvaluehelper.h
postgresqlqueryresult.o: postgresqlqueryresult.h queryresult.h
queryresult.o: queryresult.h ../../pikotools/utf8/utf8.h
queryresult.o: ../../pikotools/textstream/textstream.h

68
src/fieldvaluehelper.h Normal file
View File

@ -0,0 +1,68 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2019, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_fieldvaluehelper
#define headerfile_morm_fieldvaluehelper
#include <vector>
#include <typeinfo>
namespace morm
{
struct FieldValueHelper
{
const wchar_t * db_field_name;
const wchar_t * flat_field_name;
const void * value_object;
const std::type_info * value_type_info;
FieldValueHelper()
{
db_field_name = nullptr;
flat_field_name = nullptr;
value_object = nullptr;
value_type_info = nullptr;
}
};
}
#endif

View File

@ -38,6 +38,7 @@
#include <string>
#include <list>
#include <vector>
#include <typeinfo>
#include "textstream/textstream.h"
#include "modelconnector.h"
@ -142,11 +143,15 @@ public:
model_env->model_connector_mode = MORM_MODEL_CONNECTOR_MODE_SET_FIELD_VALUE;
model_env->field_index = 0;
SetFieldValueHelper set_field_value_helper;
FieldValueContainer<FieldValue> helper_object(field_value);
FieldValueHelper field_value_helper;
field_value_helper.db_field_name = db_field_name;
field_value_helper.flat_field_name = flat_field_name;
field_value_helper.value_object = &field_value;
field_value_helper.value_type_info = &typeid(field_value);
set_field_value_helper.add(db_field_name, flat_field_name, &helper_object, false);
model_env->set_field_value_helper = &set_field_value_helper;
std::vector<FieldValueHelper> helper_tab;
helper_tab.push_back(field_value_helper);
model_env->field_value_helper_tab = &helper_tab;
map_fields();
@ -673,53 +678,57 @@ protected:
template<typename FieldValue>
void field_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key)
{
PT::Log * log = model_connector->get_logger();
if( model_connector && model_env )
{
if( model_env->model_connector_mode == MORM_MODEL_CONNECTOR_MODE_SET_FIELD_VALUE )
{
if( model_env->set_field_value_helper )
if( model_env->field_value_helper_tab )
{
if( model_env->field_index >= 0 && (size_t)model_env->field_index < model_env->set_field_value_helper->size() )
if( model_env->field_index >= 0 && (size_t)model_env->field_index < model_env->field_value_helper_tab->size() )
{
if( is_the_same_field(db_field_name, model_env->set_field_value_helper->get_db_field_name(model_env->field_index)) &&
is_the_same_field(flat_field_name, model_env->set_field_value_helper->get_flat_field_name(model_env->field_index)) )
FieldValueHelper & helper = (*model_env->field_value_helper_tab)[model_env->field_index];
PT::Log * log = model_connector->get_logger();
if( is_the_same_field(db_field_name, helper.db_field_name) &&
is_the_same_field(flat_field_name, helper.flat_field_name) )
{
model_env->set_field_value_helper->set_value(model_env->field_index, field_value);
if( helper.value_object && helper.value_type_info )
{
if( typeid(field_value) == *helper.value_type_info )
{
field_value = *(FieldValue*)helper.value_object;
}
else
{
table_name(model_env->table_name);
(*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << model_env->table_name << ", ";
if( !is_empty_field(db_field_name) )
(*log) << "database field name: " << db_field_name << ", ";
if( !is_empty_field(flat_field_name) )
(*log) << "flat field name: " << flat_field_name << ", ";
(*log) << "type expected " << typeid(field_value).name()
<< " got " << helper.value_type_info->name() << PT::Log::logend;
}
}
model_env->field_index += 1;
}
}
else
{
// the log has to be generated by a parent call
// it should compare model_env->field_index with the number of keys
// if( log )
// {
// // IMPROVE ME a better log message is required
// (*log) << PT::Log::log1 << "Morm: incorrect number of keys found in model objects" << PT::Log::logend;
// }
}
}
}
if( model_env->model_connector_mode == MORM_MODEL_CONNECTOR_MODE_ITERATE_PRIMARY_KEY_VALUES && is_primary_key )
{
if( model_env->set_field_value_helper )
if( model_env->field_value_helper_tab )
{
if( model_env->field_index >= 0 && (size_t)model_env->field_index < model_env->set_field_value_helper->size() )
if( model_env->field_index >= 0 && (size_t)model_env->field_index < model_env->field_value_helper_tab->size() )
{
FieldValueBase * helper_object = new FieldValueContainer<FieldValue>(field_value);
model_env->set_field_value_helper->add_field_value_container(model_env->field_index, helper_object, true);
}
else
{
if( log )
{
// IMPROVE ME a better log message is required
// the log can be generated by a parent call
(*log) << PT::Log::log1 << "Morm: incorrect number of keys found in model objects" << PT::Log::logend;
}
FieldValueHelper & helper = (*model_env->field_value_helper_tab)[model_env->field_index];
helper.value_object = &field_value;
helper.value_type_info = &typeid(field_value);
}
}
@ -1001,6 +1010,7 @@ protected:
void field_model_set_parent_key(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model)
{
DbConnector * db_connector = model_connector->get_db_connector();
PT::Log * log = model_connector->get_logger();
if( db_connector )
{
@ -1011,39 +1021,58 @@ protected:
int old_connector_mode = model_env->model_connector_mode;
model_env->model_connector_mode = MORM_MODEL_CONNECTOR_MODE_ITERATE_PRIMARY_KEY_VALUES;
SetFieldValueHelper set_field_value_helper;
set_field_value_helper.add(db_field_name, flat_field_name); // in the future we can have a primary key from more than one column
model_env->set_field_value_helper = &set_field_value_helper;
FieldValueHelper helper; // in the future we can have a primary key from more than one column
helper.db_field_name = db_field_name;
helper.flat_field_name = flat_field_name;
std::vector<FieldValueHelper> helper_tab;
helper_tab.push_back(helper);
model_env->field_value_helper_tab = &helper_tab;
model_env->field_index = 0;
/*
* first we iterate through fields and save primary key values to helper_tab
*/
map_fields();
if( set_field_value_helper.empty() )
{
// IMPROVE ME
// put some log: there is no a primary key in this model
}
else
if( set_field_value_helper.size() == 1 )
if( (size_t)model_env->field_index == helper_tab.size() )
{
ModelEnv model_env_local;
model_env_local.copy_global_objects(*model_env);
model_env_local.model_connector_mode = MORM_MODEL_CONNECTOR_MODE_SET_FIELD_VALUE;
model_env_local.set_field_value_helper = &set_field_value_helper;
model_env_local.field_value_helper_tab = &helper_tab;
model_env_local.field_index = 0;
field_model.model_env = &model_env_local;
/*
* now we iterate through fields in field_model and save primary key values from *this object to the specified fields in field_model
*/
field_model.map_fields();
field_model.model_env = nullptr;
model_env->set_field_value_helper = nullptr;
model_env->model_connector_mode = old_connector_mode;
if( (size_t)field_model.model_env->field_index != helper_tab.size() && log )
{
table_name(model_env->table_name);
field_model.table_name(field_model.model_env->table_name);
(*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of " << model_env->field_index << " column(s)"
<< " but in " << field_model.model_env->table_name << " there are only "
<< field_model.model_env->field_index << " matching column(s)" << PT::Log::logend;
}
field_model.model_env = nullptr;
}
else
if( log )
{
// IMPROVE ME
// put some log: at the moment we only support a primary key from only one column
table_name(model_env->table_name);
(*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of incorrect number of columns"
<< ", expected " << helper_tab.size() << " column(s) but got " << model_env->field_index << PT::Log::logend;
}
model_env->field_value_helper_tab = nullptr;
model_env->model_connector_mode = old_connector_mode;
}
}
}

View File

@ -38,7 +38,7 @@
#include "modeldata.h"
#include "cursorhelper.h"
#include "finderhelper.h"
#include "setfieldvaluehelper.h"
#include "fieldvaluehelper.h"
#include "morm_types.h"
@ -63,7 +63,7 @@ public:
int field_index;
std::vector<const wchar_t *> * set_field_name_helper;
SetFieldValueHelper * set_field_value_helper;
std::vector<FieldValueHelper> * field_value_helper_tab;
ModelEnv()
@ -86,7 +86,7 @@ public:
table_index = e.table_index;
doc_field_pointer = e.doc_field_pointer;
set_field_name_helper = e.set_field_name_helper;
set_field_value_helper = e.set_field_value_helper;
field_value_helper_tab = e.field_value_helper_tab;
field_index = e.field_index;
// table_name and table_name_short don't have to bo copied
@ -115,7 +115,7 @@ public:
table_name_short.clear();
table_index = 0;
set_field_name_helper = nullptr;
set_field_value_helper = nullptr;
field_value_helper_tab = nullptr;
field_index = 0;
}

View File

@ -1,211 +0,0 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2019, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_setfieldvaluehelper
#define headerfile_morm_setfieldvaluehelper
#include <vector>
#include <typeinfo>
namespace morm
{
struct FieldValueBase
{
FieldValueBase()
{
}
virtual ~FieldValueBase()
{
}
virtual void set(void * value, const std::type_info & value_type_info) = 0;
};
template<typename FieldValue>
struct FieldValueContainer : public FieldValueBase
{
FieldValueContainer(const FieldValue & value)
{
this->value = &value;
}
void set(void * value, const std::type_info & value_type_info)
{
if( value_type_info == typeid(FieldValue) )
{
FieldValue * field_value = reinterpret_cast<FieldValue*>(value);
*field_value = *this->value;
}
}
const FieldValue * value;
};
class SetFieldValueHelper
{
public:
struct Item
{
const wchar_t * db_field_name;
const wchar_t * flat_field_name;
// IMPROVE ME
// instead of FieldValueBase can we have only a pointer to void (to the value object) and a pointer to std::type_info?
FieldValueBase * field_value_base;
bool auto_remove_field_value_object;
Item()
{
db_field_name = nullptr;
flat_field_name = nullptr;
field_value_base = nullptr;
auto_remove_field_value_object = false;
}
~Item()
{
}
};
std::vector<Item> item_tab;
SetFieldValueHelper()
{
clear();
}
virtual ~SetFieldValueHelper()
{
remove_field_value_base_objects();
}
virtual void remove_field_value_base_objects()
{
for(Item & item : item_tab)
{
if( item.auto_remove_field_value_object )
{
delete item.field_value_base;
item.field_value_base = nullptr;
}
}
}
virtual void clear()
{
item_tab.clear();
}
virtual void add(const wchar_t * db_field_name, const wchar_t * flat_field_name)
{
add(db_field_name, flat_field_name, nullptr, false);
}
virtual void add(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValueBase * field_value_base, bool auto_remove_field_value_object)
{
Item item;
item.db_field_name = db_field_name;
item.flat_field_name = flat_field_name;
item.field_value_base = field_value_base;
item.auto_remove_field_value_object = auto_remove_field_value_object;
item_tab.push_back(item);
}
virtual size_t size()
{
return item_tab.size();
}
virtual bool empty()
{
return item_tab.empty();
}
virtual void add_field_value_container(size_t index, FieldValueBase * field_value_base, bool auto_remove_field_value_object)
{
if( item_tab[index].field_value_base && item_tab[index].auto_remove_field_value_object )
{
delete item_tab[index].field_value_base;
}
item_tab[index].field_value_base = field_value_base;
item_tab[index].auto_remove_field_value_object = auto_remove_field_value_object;
}
template<typename FieldValue>
void set_value(size_t index, FieldValue & value)
{
if( item_tab[index].field_value_base )
{
item_tab[index].field_value_base->set(&value, typeid(value));
}
}
virtual const wchar_t * get_db_field_name(size_t index)
{
return item_tab[index].db_field_name;
}
virtual const wchar_t * get_flat_field_name(size_t index)
{
return item_tab[index].flat_field_name;
}
};
}
#endif