Verify ENUM integrity

Forum for posting problems using QxOrm library

Verify ENUM integrity

Postby gianks » Mon Aug 29, 2016 11:16 pm

Hi,
C++ is unable by itself to provide a safe way to manage externally provided values for enums since no retrospection is available...
i was guessing if QxOrm introspection engine can give us a way to check if the enum value (coming from a form for example) is in the real list of the enum or if this is already done implicitly during save/insert operations (and so how to do that manually).

Thanks
gianks
 
Posts: 45
Joined: Mon Jul 04, 2016 1:04 pm

Re: Verify ENUM integrity

Postby qxorm » Tue Aug 30, 2016 8:42 am

Hello,

No, QxOrm library doesn't check enum values, you could use property bag if you want to use introspection engine of QxOrm library but this is not a perfect solution.
Some databases support enum type, so you can put a control on database side.

If you want to check on C++ side and want introspection on enums, because QxOrm is based on Qt, you can use Q_ENUM : http://doc.qt.io/qt-5/qobject.html#Q_ENUM
I think that Q_ENUM provides everything to know all enum's values by introspection.
I would recommend (but this is not necessary) to put all your enums in a seperate entity (not directly in the persistent class), for example QxEntityEditor creates a specific entity for each enum, for example :
Code: Select all
class QXBLOG_EXPORT sex : public QObject
{

   Q_OBJECT
   Q_ENUMS(sex::enum_sex)

public:

   enum enum_sex
   {
      male = 1,
      female = 2,
      unknown = 3
   };

private:

   sex();
   virtual ~sex();

};


Then, if you want to check enums values before inserting/updating in database, you can use a validator method.
This is explained in the QxOrm manual here : https://www.qxorm.com/qxorm_en/manual.html#manual_420
Your validator method could be a generic method using Q_ENUM introspection.
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron