From 7ae497743c7b042904fe1f6b4153ab3f4763ff2b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 10 Mar 2011 08:44:28 +0200 Subject: Split MySQL code generator into common and db-specific parts The common part (in relational/) still has some MySQL-specific parts. Also, add the notion of the current context which is used to avoid explicitly passing the context object to every generator's c-tor. --- odb/context.cxx | 71 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 16 deletions(-) (limited to 'odb/context.cxx') diff --git a/odb/context.cxx b/odb/context.cxx index 6fde943..944756a 100644 --- a/odb/context.cxx +++ b/odb/context.cxx @@ -4,6 +4,7 @@ // license : GNU GPL v3; see accompanying LICENSE file #include // std::toupper, std::is{alpha,upper,lower} +#include #include #include @@ -93,47 +94,85 @@ namespace } context:: +~context () +{ + if (current_ == this) + current_ = 0; +} + +context:: context (ostream& os_, semantics::unit& u, options_type const& ops, data_ptr d) - : data_ (d ? d : data_ptr (new (shared) data)), - os (os_), + : data_ (d ? d : data_ptr (new (shared) data (os_))), + os (data_->os_), unit (u), options (ops), keyword_set (data_->keyword_set_), embedded_schema (ops.generate_schema () && - ops.schema_format ().count (schema_format::embedded)) + ops.schema_format ().count (schema_format::embedded)), + object (data_->object_) { + assert (current_ == 0); + current_ = this; + for (size_t i (0); i < sizeof (keywords) / sizeof (char*); ++i) data_->keyword_set_.insert (keywords[i]); } context:: -context (context& c) +context (const context& c) : data_ (c.data_), os (c.os), unit (c.unit), options (c.options), keyword_set (c.keyword_set), - embedded_schema (c.embedded_schema) + embedded_schema (c.embedded_schema), + object (c.object) { } context:: -context (context& c, ostream& os_) - : data_ (c.data_), - os (os_), - unit (c.unit), - options (c.options), - keyword_set (c.keyword_set), - embedded_schema (c.embedded_schema) +context () + : data_ (current ().data_), + os (current ().os), + unit (current ().unit), + options (current ().options), + keyword_set (current ().keyword_set), + embedded_schema (current ().embedded_schema), + object (current ().object) { } -context:: -~context () +context* context::current_; + +void context:: +diverge (streambuf* sb) +{ + data_->os_stack_.push (data_->os_.rdbuf ()); + data_->os_.rdbuf (sb); +} + +void context:: +restore () { + data_->os_.rdbuf (data_->os_stack_.top ()); + data_->os_stack_.pop (); +} + +semantics::type& context:: +member_type (semantics::data_member& m, string const& key_prefix) +{ + if (key_prefix.empty ()) + return m.type (); + + string const key ("tree-" + key_prefix + "-type"); + + if (m.count (key)) + return *indirect_value (m, key); + + return *indirect_value (m.type (), key); } string context:: @@ -223,11 +262,11 @@ column_name (semantics::data_member& m, string const& p, string const& d) const } string context:: -column_type (semantics::data_member& m, string const& kp) const +column_type (semantics::data_member& m, string const& kp) { return kp.empty () ? m.get ("column-type") - : m.get (kp + "-column-type"); + : indirect_value (m, kp + "-column-type"); } string context::data:: -- cgit v1.1