From ab51fa65f9e8cad4ef5a1db85029dfe6404e9a1f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 13 Jan 2011 11:31:14 +0200 Subject: Add composite, relationship, and inverse examples All add the TR1 test for the latter two examples. --- composite/driver.cxx | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 composite/driver.cxx (limited to 'composite/driver.cxx') diff --git a/composite/driver.cxx b/composite/driver.cxx new file mode 100644 index 0000000..f8feb2f --- /dev/null +++ b/composite/driver.cxx @@ -0,0 +1,101 @@ +// file : composite/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Create a person object. + // + unsigned int id; + { + person p ("Joe", "Dirt", "Mr"); + + transaction t (db->begin ()); + id = db->persist (p); + t.commit (); + } + + // Update the extra name information. + // + { + transaction t (db->begin ()); + + auto_ptr joe (db->load (id)); + name_extras& ne (joe->name ().extras ()); + ne.nickname ("Squeaky"); + ne.aliases ().push_back (basic_name ("Anthony", "Clean")); + + db->update (*joe); + + t.commit (); + } + + // Print the name information. + // + { + transaction t (db->begin ()); + auto_ptr joe (db->load (id)); + t.commit (); + + name& n (joe->name ()); + + cout << n.title () << " " << n.first () << " " << n.last () << endl; + + name_extras& ne (n.extras ()); + + if (!ne.nickname ().empty ()) + cout << " nickname: " << ne.nickname () << endl; + + for (basic_names::iterator i (ne.aliases ().begin ()); + i != ne.aliases ().end (); + ++i) + { + cout << " alias: " << i->first () << " " << i->last () << endl; + } + } + + // Query the database for a person object. + // + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query ( + query::name::extras::nickname == "Squeaky")); + + if (!r.empty ()) + { + name& n (r.begin ()->name ()); + cout << n.title () << " " << n.first () << " " << n.last () << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} -- cgit v1.1