Boost logo

Boost Users :

From: Christian Henning (chhenning_at_[hidden])
Date: 2007-04-04 13:08:46


Hi there, beware I'm a new to Multiindex and haven't digested the
whole capabilities of the library, yet.

In my example I have two entities that are connected via a foreign
key. In this case the key is type of an int. When retrieving the
objects from a database I end up having two containers containing
those objects. In the code below there are a customer_row and a
order_row class. There is a one-to_many relationship between them. One
customer can have several order but one order is can only one
customer. So, my goal is to find an way to create order objects that
are pointing to the correct row objects.

So far, I don't really see how the MultiIndex lib can be of help for
me. I have looked at the example six which I think came pretty close
to my goal. So, taken this example, is it possible to use an key type
instead of the car_manufacturer pointer?

Please consider the following code:

using namespace std;
using namespace boost;
using namespace boost::multi_index;

struct primary {};
struct foreign {};

struct customer_row
{
   int _id;
   string _name;

   customer_row( int id, const string& name )
   : _id( id ), _name( name ) {}

   friend ostream& operator<<( ostream& os, const customer_row& c )
   {
      os << c._id << " " << c._name << endl;

      return os;
   }
};

typedef multi_index_container<
   customer_row,
   indexed_by<
      ordered_unique< tag< primary >,
         BOOST_MULTI_INDEX_MEMBER(customer_row,int,_id)
>
>
> customer_table;

struct order_row
{
   int _id;
   int _customer_id;
   string _article;

   order_row( int id, int customer_id, const string& article )
   : _id( id ), _customer_id( customer_id ), _article( article ) {}

   friend ostream& operator<<( ostream& os, const order_row& o )
   {
      os << o._id << " " << o._customer_id << " " << o._article << endl;

      return os;
   }
};

typedef multi_index_container<
   order_row,
   indexed_by<
      ordered_unique< tag< primary >,
         BOOST_MULTI_INDEX_MEMBER(order_row,int,_id)
>,
      ordered_non_unique< tag< foreign >,
         BOOST_MULTI_INDEX_MEMBER(order_row,int,_customer_id)
>
>
> order_table;

struct order
{
   const order_row* _order_row;
   const customer_row* _customer_row;
};

int main()
{
   customer_table ct;
   ct.insert( customer_row( 97, "Bon Jovi" ));
   ct.insert( customer_row( 98, "Billy Joel" ));
   ct.insert( customer_row( 99, "Morgan Freeman" ));

   order_table ot;
   ot.insert( order_row( 0, 97, "Book" ));
   ot.insert( order_row( 1, 97, "Car" ));
   ot.insert( order_row( 2, 97, "Dish Washer" ));

   order_table::iterator it = ot.get<primary>().begin();
   order_table::iterator end = ot.get<primary>().end();

   vector<order> orders;

   while( it != end )
   {
      // find customer
      typedef customer_table::index<primary>::type customer_table_by_primary;
      customer_table_by_primary::iterator ic = ct.get<primary>().find(
it->_customer_id );

      order o;
      o._order_row = &( *it );
      o._customer_row = &*ct.get<primary>().find( it->_customer_id );

      orders.push_back( o );

      ++it;
   }

   return 0;
}

Is there a more advanced way using the MultiIndex lib of combining two
related objects so I can created my orders array? Or in different
words can I structure my code more efficiently? Since this is a very
simplified version it should be extensible to more entities with more
than one foreign keys.

Thanks ahead,
Christian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net