From bb2358220adfe274b54d9b155205b60ddfe625c6 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. --- odb/exceptions.cxx | 24 ++++++++++++++++ odb/exceptions.hxx | 25 +++++++++++++++++ odb/makefile | 14 ++++++---- odb/schema-catalog-impl.hxx | 44 +++++++++++++++++++++++++++++ odb/schema-catalog.cxx | 67 +++++++++++++++++++++++++++++++++++++++++++++ odb/schema-catalog.hxx | 34 +++++++++++++++++++++++ 6 files changed, 202 insertions(+), 6 deletions(-) create mode 100644 odb/schema-catalog-impl.hxx create mode 100644 odb/schema-catalog.cxx create mode 100644 odb/schema-catalog.hxx (limited to 'odb') diff --git a/odb/exceptions.cxx b/odb/exceptions.cxx index 1d4be70..b79f189 100644 --- a/odb/exceptions.cxx +++ b/odb/exceptions.cxx @@ -3,8 +3,12 @@ // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file +#include + #include +using namespace std; + namespace odb { const char* null_pointer:: @@ -84,4 +88,24 @@ namespace odb { return "object already persistent"; } + + unknown_schema:: + unknown_schema (const std::string& name) + : name_ (name) + { + ostringstream ostr; + ostr << "unknown database schema '" << name << "'"; + what_ = ostr.str (); + } + + unknown_schema:: + ~unknown_schema () throw () + { + } + + const char* unknown_schema:: + what () const throw () + { + return what_.c_str (); + } } diff --git a/odb/exceptions.hxx b/odb/exceptions.hxx index 1185876..b1e4711 100644 --- a/odb/exceptions.hxx +++ b/odb/exceptions.hxx @@ -8,6 +8,8 @@ #include +#include + #include #include @@ -106,6 +108,27 @@ namespace odb { }; + // Schema catalog exceptions. + // + struct LIBODB_EXPORT unknown_schema: exception + { + unknown_schema (const std::string& name); + ~unknown_schema () throw (); + + const std::string& + name () const + { + return name_; + } + + virtual const char* + what () const throw (); + + private: + std::string name_; + std::string what_; + }; + namespace core { using odb::null_pointer; @@ -126,6 +149,8 @@ namespace odb using odb::object_already_persistent; using odb::result_not_cached; using odb::database_exception; + + using odb::unknown_schema; } } diff --git a/odb/makefile b/odb/makefile index caa54d4..3790bbc 100644 --- a/odb/makefile +++ b/odb/makefile @@ -5,12 +5,14 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make -cxx := \ -exceptions.cxx \ -database.cxx \ -session.cxx \ -transaction.cxx \ -lazy-ptr-impl.cxx +cxx := \ +exceptions.cxx \ +database.cxx \ +lazy-ptr-impl.cxx \ +schema-catalog.cxx \ +session.cxx \ +transaction.cxx + # Implementation details. # diff --git a/odb/schema-catalog-impl.hxx b/odb/schema-catalog-impl.hxx new file mode 100644 index 0000000..782536a --- /dev/null +++ b/odb/schema-catalog-impl.hxx @@ -0,0 +1,44 @@ +// file : odb/schema-catalog-impl.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SCHEMA_CATALOG_IMPL_HXX +#define ODB_SCHEMA_CATALOG_IMPL_HXX + +#include + +#include + +#include + +#include + +namespace odb +{ + struct schema_catalog_impl; + + // Translation unit initializer. + // + struct LIBODB_EXPORT schema_catalog_init + { + static schema_catalog_impl* catalog; + static std::size_t count; + + schema_catalog_init (); + ~schema_catalog_init (); + }; + + static const schema_catalog_init schema_catalog_init_; + + // Catalog entry registration. + // + struct LIBODB_EXPORT schema_catalog_entry + { + schema_catalog_entry (const char* name, void (*entry) (database&)); + }; +} + +#include + +#endif // ODB_SCHEMA_CATALOG_IMPL_HXX diff --git a/odb/schema-catalog.cxx b/odb/schema-catalog.cxx new file mode 100644 index 0000000..5332cce --- /dev/null +++ b/odb/schema-catalog.cxx @@ -0,0 +1,67 @@ +// file : odb/schema-catalog.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include +#include + +#include +#include +#include + +using namespace std; + +namespace odb +{ + typedef void (*create_function) (database&); + typedef vector create_functions; + struct schema_catalog_impl: map {}; + + schema_catalog_impl* schema_catalog_init::catalog = 0; + size_t schema_catalog_init::count = 0; + + void schema_catalog:: + create_schema (database& db, const string& name) + { + const schema_catalog_impl& c (*schema_catalog_init::catalog); + + schema_catalog_impl::const_iterator i (c.find (name)); + + if (i == c.end ()) + throw unknown_schema (name); + + const create_functions& fs (i->second); + + for (create_functions::const_iterator j (fs.begin ()), e (fs.end ()); + j != e; ++j) + (*j) (db); + } + + // schema_catalog_init + // + schema_catalog_init:: + schema_catalog_init () + { + if (count == 0) + catalog = new schema_catalog_impl; + + ++count; + } + + schema_catalog_init:: + ~schema_catalog_init () + { + if (--count == 0) + delete catalog; + } + + // schema_catalog_entry + // + schema_catalog_entry:: + schema_catalog_entry (const char* name, create_function entry) + { + schema_catalog_impl& c (*schema_catalog_init::catalog); + c[name].push_back (entry); + } +} diff --git a/odb/schema-catalog.hxx b/odb/schema-catalog.hxx new file mode 100644 index 0000000..e0e0435 --- /dev/null +++ b/odb/schema-catalog.hxx @@ -0,0 +1,34 @@ +// file : odb/schema-catalog.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SCHEMA_CATALOG_HXX +#define ODB_SCHEMA_CATALOG_HXX + +#include + +#include + +#include + +#include + +namespace odb +{ + class LIBODB_EXPORT schema_catalog + { + public: + static void + create_schema (database&, const std::string& name = ""); + }; + + namespace core + { + using odb::schema_catalog; + } +} + +#include + +#endif // ODB_SCHEMA_CATALOG_HXX -- cgit v1.1