aboutsummaryrefslogtreecommitdiff
path: root/hello/driver.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-09-14 15:44:06 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-09-14 15:44:06 +0200
commit87991ad75cf0a33b2503dbb8725c3c6609254961 (patch)
tree7448ed8938f4b28af54c24fda764c7daab88f14f /hello/driver.cxx
parent101089f86e624dbacb0d53b312c021a28beb4eec (diff)
Add hello example
Diffstat (limited to 'hello/driver.cxx')
-rw-r--r--hello/driver.cxx65
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;
+ }
+}