//Create a quick DBView called db_example.
//Rows in this view will be of type db_example_row.
//See DTL_TABLE for details.
DTL_TABLE3(db_example,
   int, int_value,
   string, string_value,
   double, double_value
);


// Random access container example
void RandomDBViewExample()
{
   RandomDBView<db_example_row> view(db_example);

   cout << "Objects read from DB:" << endl;
   copy(view.begin(), view.end(), ostream_iterator<db_example_row>(cout, "\n"));

   cout << "\nElements in reverse order:" << endl;
   copy(view.rbegin(), view.rend(), ostream_iterator<db_example_row>(cout, "\n"));

   // modify the third row in the table
   RandomDBView<db_example_row>::iterator it = view.begin();
   db_example_row row(it[2]);
   row.int_value++;
   *it = row;
   
   // NOTE THAT FOR THE MODIFIED ROW TO SHOW UP WE MUST CALL ReQuery().
   // Note that there is no guarantee that the third row will show the updated record,
   // since other users may have modified the database or the DB may return records in a different
   // order.  See the ReQuery() function for details.
   cout << "Show updated result row:" << endl;
   view.ReQuery();
   cout << it[2] << endl;
 
   // show distance functions 
   cout << "Distance from first to last: " << view.end() - view.begin() << endl;
   cout << "Container size: " << view.size() << endl;

   // insert and delete rows
   db_example_row row_insert(it[2]);
   row_insert.int_value = 666;
   view.insert(row_insert);
   view.erase(it+(ptrdiff_t)2);
   cout << "Show result set with inserted/deleted row:" << endl;
   view.ReQuery();
   copy(view.begin(), view.end(), ostream_iterator<db_example_row>(cout, "\n"));

   // show container comparison operators
   RandomDBView<db_example_row> view2(view);
   cout << boolalpha;
   cout << "view == view2 : " << (view == view2) << endl;
   cout << "view < view2 : " << (view < view2) << endl;
}