diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-04-26 09:14:56 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-04-26 09:14:56 +0200 |
commit | e159da71e25aa50fc73479f9aa7aef7185c28a1c (patch) | |
tree | bf4fae4b9fd76983c69b67a76dc3be8974ce943a /inheritance/driver.cxx | |
parent | ba7a7b6ed8b81df116b1c2352665b03192386793 (diff) |
Add inheritance example
Diffstat (limited to 'inheritance/driver.cxx')
-rw-r--r-- | inheritance/driver.cxx | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/inheritance/driver.cxx b/inheritance/driver.cxx new file mode 100644 index 0000000..44fd721 --- /dev/null +++ b/inheritance/driver.cxx @@ -0,0 +1,82 @@ +// file : inheritance/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// 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)); + + // Add a few employees and contractors to the database. + // + { + permanent_employee p1 ("John", "Doe"); + permanent_employee p2 ("Jane", "Doe"); + + temporary_employee t1 ("John", "Smith", 6); + temporary_employee t2 ("Jane", "Smith", 12); + + contractor c1 ("Joe", "Doe", "j.doe@example.com"); + contractor c2 ("Joe", "Smith", "j.smith@example.com"); + + transaction t (db->begin ()); + db->persist (p1); + db->persist (p2); + db->persist (t1); + db->persist (t2); + db->persist (c1); + db->persist (c2); + t.commit (); + } + + // Lookup a contractor based on the email address. + // + { + transaction t (db->begin ()); + auto_ptr<contractor> c (db->load<contractor> ("j.smith@example.com")); + t.commit (); + + cout << c->first () << " " << c->last () << " " << c->email () << endl; + } + + // Query for temporary employees that have John as first name. + // + { + typedef odb::query<temporary_employee> query; + typedef odb::result<temporary_employee> result; + + transaction t (db->begin ()); + + result r (db->query<temporary_employee> (query::first == "John")); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << i->first () << " " << i->last () << " " + << i->duration () << " months" << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} |