From 5a14aa4978cca949f3d07561ba35f8fd3af76ab2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 1 Mar 2011 11:56:33 +0200 Subject: Add support for embedded database schemas New options: --schema-format, --default-schema. New example: schema/embedded. --- schema/embedded/driver.cxx | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 schema/embedded/driver.cxx (limited to 'schema/embedded/driver.cxx') 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 +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include +#include + +#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 query; + typedef odb::result result; + + auto_ptr 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 (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 (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; + } +} -- cgit v1.1