aboutsummaryrefslogtreecommitdiff
path: root/schema/embedded/driver.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-03-01 11:56:33 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-03-01 11:56:33 +0200
commit5a14aa4978cca949f3d07561ba35f8fd3af76ab2 (patch)
tree4c5e54ca7daf83130fd06f16db56b517ea254ce6 /schema/embedded/driver.cxx
parent2ef165437ce47f50110a9b230f302c5cd5fde1d4 (diff)
Add support for embedded database schemas
New options: --schema-format, --default-schema. New example: schema/embedded.
Diffstat (limited to 'schema/embedded/driver.cxx')
-rw-r--r--schema/embedded/driver.cxx97
1 files changed, 97 insertions, 0 deletions
diff --git a/schema/embedded/driver.cxx b/schema/embedded/driver.cxx
new file mode 100644
index 0000000..aeba3ee
--- /dev/null
+++ b/schema/embedded/driver.cxx
@@ -0,0 +1,97 @@
+// file : schema/embedded/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 <odb/schema-catalog.hxx>
+
+#include "database.hxx" // create_database
+
+#include "person.hxx"
+#include "person-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ typedef odb::query<person> query;
+ typedef odb::result<person> result;
+
+ auto_ptr<database> db (create_database (argc, argv));
+
+ // Create the database schema.
+ //
+ {
+ transaction t (db->begin ());
+ schema_catalog::create_schema (*db);
+ t.commit ();
+ }
+
+ // The following alternative version only creates the schema if it
+ // hasn't already been created. To detect the existence of the schema
+ // this version tries to query the database for a person object. If
+ // the corresponding table does not exist, then an exceptions will be
+ // thrown in which case we proceed to creating the schema.
+ //
+ /*
+ {
+ transaction t (db->begin ());
+
+ try
+ {
+ db->query<person> (false);
+ }
+ catch (const odb::exception& e)
+ {
+ schema_catalog::create_schema (*db);
+ }
+
+ t.commit ();
+ }
+ */
+
+ // Create a few persistent person objects.
+ //
+ {
+ person john ("John", "Doe", 33);
+ person jane ("Jane", "Doe", 32);
+ person joe ("Joe", "Dirt", 30);
+
+ transaction t (db->begin ());
+
+ db->persist (john);
+ db->persist (jane);
+ db->persist (joe);
+
+ t.commit ();
+ }
+
+ // Print those over 30.
+ //
+ {
+ transaction t (db->begin ());
+
+ result r (db->query<person> (query::age > 30));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ {
+ cout << i->first () << " " << i->last () << endl;
+ }
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}