Project

General

Profile

Wt::Dbo - persisting with getters/setters instead of values?

Added by James Tomson over 10 years ago

Hello,

in the Wt::Dbo tutorial it states:

For brevity, our example uses public members. There is nothing that prevents you to encapsulate your state in private members and provide accessor methods. You may even define the persistence method in terms of accessor methods by differentiating between read and write actions.

I can't seem to find an example in the tutorial or documentation that shows using custom getters and setters, or differentiating between read/write actions, when declaring the persist method. I may be missing something obvious - can someone help point me in the right direction? Thanks!


Replies (4)

RE: Wt::Dbo - persisting with getters/setters instead of values? - Added by Mohamed Samir over 10 years ago

Hey, you can just use a private member variable for persistence, and write your ordinary getters and setters as usual:

for ex:

public:

int age();

void setAge(int age);

private:

int _age; // use that in the persist function.

RE: Wt::Dbo - persisting with getters/setters instead of values? - Added by James Tomson over 10 years ago

Thanks Mohamed - I was hoping that you could define custom getters and setters used to save/load values to/from the db. instead of using member variables to take the value.

For instance if I wanted to perform a transformation on the data that is saved or loaded, something like

dbo::field(std::string, <setter function ptr>);

when loading from the db, or

dbo::field(std::string, <getter function ptr>);

when saving from the db.

This is no big deal though if backing member variables are required - but I would be interesting in differentiating between save and load operations.

I'm guessing it's possible to declare a template specialization of the persist method to achieve this, but are there any examples?

Thanks!

RE: Wt::Dbo - persisting with getters/setters instead of values? - Added by Koen Deforche over 10 years ago

Hey James,

This hasn't been foreseen --- but you can differentiate between getting and setting using the (onducumented) methods: action.getsValue() and action.setsValue() which will return true respectively for an action that gets or sets the value to the object.

Perhaps you can somehow use that to implement a utility function which you give both the getter and setter and which calls the appropriate one.

Regards,

Koen

RE: Wt::Dbo - persisting with getters/setters instead of values? - Added by James Tomson over 10 years ago

Thanks Koen that is helpful to know about these undocumented action methods.

I ended up using a custom value type using sql_value_traits to implement this (since it exposes separate bind and read methods) - my specific use case was an 'encrypted' field.

Unfortunately I ran into an issue when persisting transient data like this with the sqlite backend. I opened a Pull Request to address in github here: https://github.com/kdeforche/wt/pull/65 - but looking at the outstanding pull requests I'm guessing this in not an effective way to communicate issues or suggest patches...

Regardless thanks again for your help!

-James

    (1-4/4)