Project

General

Profile

What operator declarations are needed for Dbo Tutorial8.C?

Added by Rob Farmelo over 13 years ago

Hi,

Tutorial8.C shows how to handle composite foreign key primary keys. I specifies that you must define a < operator for the MembershipId struct as shown here:

  bool operator< (const MembershipId& other) const {
    if (person < other.person)
      return true;
    else if (person == other.person)
      return organisation < other.organisation;
    else
      return false;
  }

This method relys in definitions for the < and == operators for person and the < operator for organization yet I don't see those declared in the tutorial. Is it necessary to do so? If so, is it acceptable to compare IDs of the two objects if those IDs are simple, autoincrementing IDs?

Thanks,

Rob


Replies (6)

RE: What operator declarations are needed for Dbo Tutorial8.C? - Added by Rob Farmelo over 13 years ago

Hi,

I'm guessing that in the snippet above, the comparison of person to other.person is ending up just comparing the memory address of the two objects which doesn't seem like what we want to do.

I assume we need to compare something more meaningful and could use the id field to yield consistent results comparison results.

I'm going to go ahead and implement the < and == operators for the equivalents of person and organization (from above) in my code. Can someone please let me know how to compare the ids of two objects? I see method dbo::id() but it looks like that is returning an idType, not the actual id.

Thanks,

Rob

RE: What operator declarations are needed for Dbo Tutorial8.C? - Added by Koen Deforche over 13 years ago

Hey Rob,

dbo::ptr<> defines an operator< (by comparing pointer values, actually) so that this works.

You could also compare the IDs, that is perfectly fine, and perhaps preferable since this will return consistently the same ordering also across different runs, while the pointer values are only consistent in a single run. You could even decide to do the comparison based on other properties of the objects, but for the purpose of using the composite key properly within Wt::Dbo, the actual order defined is not important, so you would only do this if you intend to use the sort order also for other purposes in your application.

dbo::id() returns the id --- it is of type IdType, which is a long long for autoincrmenting IDs, or the natural ID type you defined for natural keys.

Regards,

koen

RE: What operator declarations are needed for Dbo Tutorial8.C? - Added by Rob Farmelo over 13 years ago

Great, thanks Koen. I'll just rely on the dbo::ptr version of the operator...

Rob

RE: What operator declarations are needed for Dbo Tutorial8.C? - Added by Rob Farmelo over 13 years ago

Hi Koen,

I'm implementing the < operator a little bit differently than tutorial8 because it's easier to automate generation of code using this form. Does it look OK? Can I rely on the > operator from Dbo::Wt::ptr too?

bool operator< (const Table1_has_Table2_id& other) const

{

if(table1 < other.table1)

return true;

if(table1 > other.table1)

return false;

if(table2 < other.table2)

return true;

if(table2 > other.table2)

return false;

if(table3 < other.table3)

return true;

if(table3 > other.table3)

return false;

// If we get here the two structures are identical.

return false;

}

Thanks,

Rob

RE: What operator declarations are needed for Dbo Tutorial8.C? - Added by Koen Deforche over 13 years ago

Hey Rob,

That looks valid to me, and in fact implementing exactly the same thing, no ?

ptr::operator> is not defined I think (we probably should), but you could simply switch around the operands and call operator< ?

Regards,

koen

RE: What operator declarations are needed for Dbo Tutorial8.C? - Added by Rob Farmelo over 13 years ago

Hi Koen,

Yes, the intent was to have the exact same behavior as in tutorial8. I've coded it up and it's compiling and running so the > definition is coming from somewhere. I'll see if I can rework it by switching the operands as you suggest, or perhaps taking advantage of the Dbo::ptr's == operator.

Thanks,

Rob

    (1-6/6)