std::vector<ValueMap> * results=this->dao->QueryTable(table, vmfields); //ValueMap(s) here represent lines from db for(auto it = results->begin(); it != results->end(); ++it) S_FILLER s(*it); //S_FILLER is defined in a .sch file //Do something with s }
TABLE_ (FILLER) STRING (KMP,8) PRIMARY_KEY REFERENCES(ITEM.KMP) STRING_ (DESCRIPTION,50) STRING (STATUS,8) INT_ (NOELEMENTS) DOUBLE_ (DENSITY) END_TABLE
Hi there,
I have the following code: the idea is to get data from db, put them in a vector of ValueMap and then put them in a S_type structure.
std::vector<ValueMap> * results=this->dao->QueryTable(table, vmfields); //ValueMap(s) here represent lines from db for(auto it = results->begin(); it != results->end(); ++it) S_FILLER s(*it); //S_FILLER is defined in a .sch file //Do something with s }
S_FILLER is defined as follows:
TABLE_ (FILLER) STRING (KMP,8) PRIMARY_KEY REFERENCES(ITEM.KMP) STRING_ (DESCRIPTION,50) STRING (STATUS,8) INT_ (NOELEMENTS) DOUBLE_ (DENSITY) END_TABLE
The problem is that the code crashes if in the S_type structure there are numerical types, if they are just strings the code runs perfectly.
Is there a way to force some kind of "automatic" conversion from String to the correct type?
Thanks,
gio
[...] //S_FILLER s(*vm); Crashes if it contains int or double S_FILLER s; S_convert(&s,vm); [...] template <class T> void S_convert(T * s, ValueMap * vm){ for(int i=0;i<vm->GetCount();i++){ Ref r=s->GetRef(SqlId(vm->GetKey(i))); if(r.Is<double>()) s->Set(SqlId(vm->GetKey(i)),StrDbl(AsString(vm->GetValue(i)))); if(r.Is<int>()) s->Set(SqlId(vm->GetKey(i)),StrInt(AsString(vm->GetValue(i)))); if(r.Is<String>()) s->Set(SqlId(vm->GetKey(i)),AsString(vm->GetValue(i))); } }
Hi,
thank you for your suggestion.
This is what I came up with, I post it here for reference and improvements.
[...] //S_FILLER s(*vm); Crashes if it contains int or double S_FILLER s; S_convert(&s,vm); [...] template <class T> void S_convert(T * s, ValueMap * vm){ for(int i=0;i<vm->GetCount();i++){ Ref r=s->GetRef(SqlId(vm->GetKey(i))); if(r.Is<double>()) s->Set(SqlId(vm->GetKey(i)),StrDbl(AsString(vm->GetValue(i)))); if(r.Is<int>()) s->Set(SqlId(vm->GetKey(i)),StrInt(AsString(vm->GetValue(i)))); if(r.Is<String>()) s->Set(SqlId(vm->GetKey(i)),AsString(vm->GetValue(i))); } }