remove a class field after version increase?

Open discussion on QxOrm library

remove a class field after version increase?

Postby comicfans44 » Tue Apr 10, 2012 6:56 am

Hello,everyone
I'm learning QxOrm ,and I came to a question:
if I remove a class field after version increase ,is there anyway to tell QxOrm to skip this field data when reading back old version's archive ?
or I must contain all field in old version class ?
for example
class Simple
{
int fieldAll ; //all versions class has this field
int fieldOnlyV2; //only version 2 has this field
}
QX_REGISTER_CLASS ... -------> version 2 here
then I use qx::serialization::txt/xml/binary::to_file write a version 2 file
class Simple
{
int fieldAll;
//int fieldOnlyV2 is removed in version 3
double fieldSinceV3;
}
QX_REGISTER_CLASS... ---------> version 3 here
in version 3 impl cpp file,
register_class(QxClass<Simple>& t)
{
t.data(&fieldAll,"fieldAll");
-----what should I write here to tell QxOrm skip a "fieldOnlyV2" field ?----
-----if no code here,read function crash because of wrong data field-----
t.data(&fieldSinceV3,"fieldSinceV3",3);
}

I tried t.data("fieldOnlyV2",2),but it seems that function is used to dynamic add property to QObject only.
Thank you for help.
comicfans44
 

Re: remove a class field after version increase?

Postby QxOrm admin » Tue Apr 10, 2012 8:20 am

Hi,

if I remove a class field after version increase ,is there anyway to tell QxOrm to skip this field data when reading back old version's archive ?
or I must contain all field in old version class ?

You can't remove a field from a class and then read back an old version archive (due to boost serialization engine).
But you can put this field private and without accessor => so this field will be not visible outside your class.
Moreover, you can use p->setDao(false); to hide this field from ORM engine.
You can also use property bag mechanism to add meta-information about this field, for example : p->setPropertyBag("OBSOLETE_FIELD", "1");

Your Simple class version 3 can looks like something like this :
Code: Select all
class Simple
{
private:
int fieldOnlyV2; // obsolete field : private and without accessor

protected:
int fieldAll; // field with accessors get() - set()
double fieldSinceV3; // field with accessors get() - set()
}
QX_REGISTER_CLASS... ---------> version 3 here

Code: Select all
register_class(QxClass<Simple>& t)
{
qx::IxDataMember * p = NULL;
p = t.data(&fieldAll,"fieldAll");

p = t.data(&fieldOnlyV2,"fieldOnlyV2",2);
p->setDao(false); // Not used by ORM engine
p->setPropertyBag("OBSOLETE_FIELD", "1"); // add meta-information to introspection engine

p = t.data(&fieldSinceV3,"fieldSinceV3",3);
}


Note : it's a good question, and if you have some ideas to improve this mechanism, let me know ;)
QxOrm admin
 

Re: remove a class field after version increase?

Postby comicfans44 » Wed Apr 11, 2012 12:08 am

Thank you for help!
comicfans44
 


Return to QxOrm - Open discussion

Who is online

Users browsing this forum: No registered users and 13 guests