Page 1 of 1

qx::IxClass Instance Primary Key Dump

PostPosted: Fri Jan 06, 2017 9:46 pm
by Kasuax
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

Re: qx::IxClass Instance Primary Key Dump

PostPosted: Sat Jan 07, 2017 11:26 am
by qxorm
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);

Re: qx::IxClass Instance Primary Key Dump

PostPosted: Mon Jan 09, 2017 3:50 pm
by Kasuax
Code: Select all
typedef QSharedPointer<qx::IxPersistable> QxObject_ptr;


obj.data() is the raw pointer to the qx:IxPersistable instance.

Re: qx::IxClass Instance Primary Key Dump

PostPosted: Tue Jan 10, 2017 8:45 am
by qxorm
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.

Re: qx::IxClass Instance Primary Key Dump

PostPosted: Thu Jan 12, 2017 4:30 am
by Kasuax
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!

Re: qx::IxClass Instance Primary Key Dump

PostPosted: Thu Jan 12, 2017 2:47 pm
by Kasuax
Everything appears to be working correctly now that I've restructured everything to model the second example from above.

Re: qx::IxClass Instance Primary Key Dump

PostPosted: Thu Jan 12, 2017 7:49 pm
by qxorm
Everything appears to be working correctly now

Great !