qx::IxClass Instance Primary Key Dump

Open discussion on QxOrm library

qx::IxClass Instance Primary Key Dump

Postby Kasuax » Fri Jan 06, 2017 9:46 pm

I'm not sure why but I can't successfully pull the primary key values from my objects. I've tried several approaches which are all consistent and wrong.
Values the following code examples produce are huge address like values. 194083216 The long-type value should have been a 3.

Code: Select all
typedef QSharedPointer<qx::IxPersistable> QxObject_ptr;

QxObjectInfo::QxObjectInfo(const QxObject_ptr& obj)
{
    if (!obj.isNull())
    {
        const qx::IxClass* ic = obj->qxClass();       

        if(ic)
        {
            qx::IxDataMemberX* icdmx = ic->getDataMemberX();

            for (int i = 0; i < icdmx->count_WithDaoStrategy(); i++)
            {
                const qx::IxDataMember* icdm = icdmx->get_WithDaoStrategy(i);

                if (icdm)
                {
                    if (icdm->getIsPrimaryKey())
                    {
                        QString classType = ic->getKey();
                        key = icdm->toVariant(obj.data());
                        hash = classType % key.toString();
                        break;
                    }
                }
            }
        }
    }
}


Code: Select all
typedef QSharedPointer<qx::IxPersistable> QxObject_ptr;

QxObjectInfo::QxObjectInfo(const QxObject_ptr& obj)
{
    if (!obj.isNull())
    {
        const qx::IxClass* ic = obj->qxClass();
        if(ic)
        {
            const qx::IxDataMember* idm = ic->getId(true);
            if (idm)
            {
                QString classType = ic->getKey();
                key = idm->toVariant(obj.data());
                hash = classType % key.toString();
            }
        }
    }
}


Thanks,
-SH
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: qx::IxClass Instance Primary Key Dump

Postby qxorm » Sat Jan 07, 2017 11:26 am

Hello,

Your second version is better (faster) because you don't have to iterate over all data members to find the id, so use : getId() like in your second code.

Then, I think your problem comes from obj.data().
I don't know what is obj.data(), but the toVariant() method requires a pointer to your persistent instance.
For example :
Code: Select all
MyPersistentClass c;
pDataMember->toVariant(& c);

Or :
Code: Select all
MyPersistentClass * c = ....;
pDataMember->toVariant(c);
qxorm
Site Admin
 
Posts: 483
Joined: Mon Apr 12, 2010 7:45 am

Re: qx::IxClass Instance Primary Key Dump

Postby Kasuax » Mon Jan 09, 2017 3:50 pm

Code: Select all
typedef QSharedPointer<qx::IxPersistable> QxObject_ptr;


obj.data() is the raw pointer to the qx:IxPersistable instance.
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: qx::IxClass Instance Primary Key Dump

Postby qxorm » Tue Jan 10, 2017 8:45 am

I don't understand because it should work if obj.data() is the raw pointer to your persistable instance.
If you do a qx::dump() before calling your function, are you sure that your id has the correct value ?
It looks like a value which is not initialized or as you said an address like value.
qxorm
Site Admin
 
Posts: 483
Joined: Mon Apr 12, 2010 7:45 am

Re: qx::IxClass Instance Primary Key Dump

Postby Kasuax » Thu Jan 12, 2017 4:30 am

Ok so I've discovered the problem. Maybe a lot of my problems...

This is the way I setup my objects inheritance tree.

Code: Select all
class MyClass : public MyBaseClass, public qx::ixPersistable;


You were probably expecting us to use it linearly...

Code: Select all
class MyBaseClass : public qx::ixPersistable;
class MyClass : public MyBaseClass;


I think since MyBaseClassis the first base class and also does not inherit from qx::ixPersistable (I did this for some reason but I don't remember why...) the pointer arithmetic got messed up. If I cast my class to MyBaseClass instead of ixPersistable and feed that address in I get the right datamember value back. All that said, I think that solves this problem. Now I'm really scared because this issue could be causing lots of other consequential ramifications I've yet to discover...

Thanks!
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: qx::IxClass Instance Primary Key Dump

Postby Kasuax » Thu Jan 12, 2017 2:47 pm

Everything appears to be working correctly now that I've restructured everything to model the second example from above.
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: qx::IxClass Instance Primary Key Dump

Postby qxorm » Thu Jan 12, 2017 7:49 pm

Everything appears to be working correctly now

Great !
qxorm
Site Admin
 
Posts: 483
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Open discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron