diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-03-01 11:56:33 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-03-01 11:56:33 +0200 |
commit | bb2358220adfe274b54d9b155205b60ddfe625c6 (patch) | |
tree | f8822c33ff25a0a9058ce92ce3a8716343a399f0 | |
parent | 1233c11bf05cc3a039afa548faf06f3610f24269 (diff) |
Add support for embedded database schemas
New options: --schema-format, --default-schema. New example: schema/embedded.
-rw-r--r-- | odb/exceptions.cxx | 24 | ||||
-rw-r--r-- | odb/exceptions.hxx | 25 | ||||
-rw-r--r-- | odb/makefile | 14 | ||||
-rw-r--r-- | odb/schema-catalog-impl.hxx | 44 | ||||
-rw-r--r-- | odb/schema-catalog.cxx | 67 | ||||
-rw-r--r-- | odb/schema-catalog.hxx | 34 |
6 files changed, 202 insertions, 6 deletions
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 <sstream> + #include <odb/exceptions.hxx> +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 <odb/pre.hxx> +#include <string> + #include <odb/exception.hxx> #include <odb/details/export.hxx> @@ -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 <boris@codesynthesis.com> +// 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 <odb/pre.hxx> + +#include <cstddef> + +#include <odb/forward.hxx> + +#include <odb/details/export.hxx> + +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 <odb/post.hxx> + +#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 <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <map> +#include <vector> + +#include <odb/exceptions.hxx> +#include <odb/schema-catalog.hxx> +#include <odb/schema-catalog-impl.hxx> + +using namespace std; + +namespace odb +{ + typedef void (*create_function) (database&); + typedef vector<create_function> create_functions; + struct schema_catalog_impl: map<string, create_functions> {}; + + 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 <boris@codesynthesis.com> +// 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 <odb/pre.hxx> + +#include <string> + +#include <odb/forward.hxx> + +#include <odb/details/export.hxx> + +namespace odb +{ + class LIBODB_EXPORT schema_catalog + { + public: + static void + create_schema (database&, const std::string& name = ""); + }; + + namespace core + { + using odb::schema_catalog; + } +} + +#include <odb/post.hxx> + +#endif // ODB_SCHEMA_CATALOG_HXX |