diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-04-25 10:45:32 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-04-25 10:45:32 +0200 |
commit | ab994fdada3eebc794d6b1686f55a35420e4d758 (patch) | |
tree | ccfbdd212fc3a06fea8f68b07c01f68989d2e450 /inheritance/polymorphism/driver.cxx | |
parent | 27245519b1a659eb849a31714df0090dc58bb87a (diff) |
New example, inheritance/polymorphism
Also move the inheritance example to inheritance/reuse.
Diffstat (limited to 'inheritance/polymorphism/driver.cxx')
-rw-r--r-- | inheritance/polymorphism/driver.cxx | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/inheritance/polymorphism/driver.cxx b/inheritance/polymorphism/driver.cxx new file mode 100644 index 0000000..e0c9952 --- /dev/null +++ b/inheritance/polymorphism/driver.cxx @@ -0,0 +1,102 @@ +// file : inheritance/polymorphism/driver.cxx +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include "database.hxx" // create_database + +#include "employee.hxx" +#include "employee-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + unsigned long id1, id2; + + // Add a few employee and contractor objects to the database. + // + { + auto_ptr<person> p1 (new employee ("John", "Doe", true)); + auto_ptr<person> p2 (new contractor ("Jane", "Doe", "jane@doe.com")); + + transaction t (db->begin ()); + id1 = db->persist (*p1); // Stores employee. + id2 = db->persist (*p2); // Stores contractor. + t.commit (); + } + + // Load polymorphic objects given their object ids. + // + { + transaction t (db->begin ()); + auto_ptr<person> p1 (db->load<person> (id1)); // Loads employee. + auto_ptr<person> p2 (db->load<person> (id2)); // Loads contractor. + t.commit (); + + p1->print (); + p2->print (); + } + + // Make John Doe a permanent employee. + // + { + transaction t (db->begin ()); + + auto_ptr<employee> e (db->load<employee> (id1)); + e->temporary (false); + person& p (*e); + db->update (p); // Updates employee. + + t.commit (); + } + + // Query all the person objects that have Doe as the last name. + // + { + typedef odb::query<person> query; + typedef odb::result<person> result; + + transaction t (db->begin ()); + + result r (db->query<person> (query::last == "Doe")); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + // We can check the discriminator before loading the object. + // + string d (i.discriminator ()); + cout << "discriminator: " << d << endl; + + i->print (); // Can be employee or contractor. + } + + t.commit (); + } + + // Erase the objects from the database. + // + { + transaction t (db->begin ()); + auto_ptr<person> p (db->load<person> (id1)); // Loads employee. + db->erase (*p); // Erases employee. + db->erase<person> (id2); // Erases contractor. + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} |