diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-14 15:44:06 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-14 15:44:06 +0200 |
commit | 87991ad75cf0a33b2503dbb8725c3c6609254961 (patch) | |
tree | 7448ed8938f4b28af54c24fda764c7daab88f14f /hello/driver.cxx | |
parent | 101089f86e624dbacb0d53b312c021a28beb4eec (diff) |
Add hello example
Diffstat (limited to 'hello/driver.cxx')
-rw-r--r-- | hello/driver.cxx | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/hello/driver.cxx b/hello/driver.cxx new file mode 100644 index 0000000..2c3edff --- /dev/null +++ b/hello/driver.cxx @@ -0,0 +1,65 @@ +// file : hello/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 "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // Create a few persistent person objects. + // + { + person john_doe ("John", "Doe", 29); + person jane_doe ("Jane", "Doe", 28); + person joe_dirt ("Joe", "Dirt", 31); + + transaction t (db->begin_transaction ()); + + db->persist (john_doe); + db->persist (jane_doe); + db->persist (joe_dirt); + + t.commit (); + } + + // Say hello to those under 30. + // + { + typedef odb::query<person> query; + typedef odb::result<person> result; + + transaction t (db->begin_transaction ()); + + result r (db->query<person> (query::age < 30)); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << "Hello, " << i->first () << " " << i->last () << "!" << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} |