From 3b457daf6b252ef31ec0611e7375aa4badd8e63d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 18 Mar 2013 13:10:41 +0200 Subject: Add support for copying database models --- odb/semantics/relational/changelog.cxx | 5 +++- odb/semantics/relational/changelog.hxx | 12 +++++++++ odb/semantics/relational/changeset.cxx | 10 ++++++- odb/semantics/relational/changeset.hxx | 1 + odb/semantics/relational/column.cxx | 19 +++++++++++++- odb/semantics/relational/column.hxx | 44 +++++++++---------------------- odb/semantics/relational/elements.cxx | 45 +++++++++++++++++++++++++++++++- odb/semantics/relational/elements.hxx | 17 ++++++++++-- odb/semantics/relational/elements.txx | 44 +++++++++++++++---------------- odb/semantics/relational/foreign-key.cxx | 20 +++++++++++++- odb/semantics/relational/foreign-key.hxx | 4 +++ odb/semantics/relational/index.cxx | 24 ++++++++++++++--- odb/semantics/relational/index.hxx | 31 +++++++++------------- odb/semantics/relational/key.cxx | 17 +++++++++++- odb/semantics/relational/key.hxx | 31 +++++----------------- odb/semantics/relational/model.cxx | 10 ++++++- odb/semantics/relational/model.hxx | 1 + odb/semantics/relational/primary-key.cxx | 15 ++++++++++- odb/semantics/relational/primary-key.hxx | 9 ++++--- odb/semantics/relational/table.cxx | 21 ++++++++++++++- odb/semantics/relational/table.hxx | 8 ++++++ 21 files changed, 273 insertions(+), 115 deletions(-) diff --git a/odb/semantics/relational/changelog.cxx b/odb/semantics/relational/changelog.cxx index c8a4053..05dd529 100644 --- a/odb/semantics/relational/changelog.cxx +++ b/odb/semantics/relational/changelog.cxx @@ -5,7 +5,10 @@ #include #include -#include + +#include +#include +#include namespace semantics { diff --git a/odb/semantics/relational/changelog.hxx b/odb/semantics/relational/changelog.hxx index ed2323f..93b327d 100644 --- a/odb/semantics/relational/changelog.hxx +++ b/odb/semantics/relational/changelog.hxx @@ -82,6 +82,12 @@ namespace semantics pointer_iterator contains_changeset_iterator; + contains_changeset const& + contains_changeset_back () const + { + return *contains_changeset_.back (); + } + contains_changeset_iterator contains_changeset_begin () const { @@ -100,6 +106,12 @@ namespace semantics return contains_changeset_.size (); } + bool + contains_changeset_empty () const + { + return contains_changeset_.empty (); + } + public: changelog (): contains_model_ (0) {} changelog (xml::parser&); diff --git a/odb/semantics/relational/changeset.cxx b/odb/semantics/relational/changeset.cxx index ecf02a2..8004844 100644 --- a/odb/semantics/relational/changeset.cxx +++ b/odb/semantics/relational/changeset.cxx @@ -3,13 +3,21 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include namespace semantics { namespace relational { changeset:: + changeset (changeset const& c, graph& g) + : qscope (c, g), + version_ (c.version_) + { + } + + changeset:: changeset (xml::parser& p, graph& g) : qscope (p, g), version_ (p.attribute ("version")) diff --git a/odb/semantics/relational/changeset.hxx b/odb/semantics/relational/changeset.hxx index 74e665b..575eb7b 100644 --- a/odb/semantics/relational/changeset.hxx +++ b/odb/semantics/relational/changeset.hxx @@ -21,6 +21,7 @@ namespace semantics public: changeset (version_type v): version_ (v) {} + changeset (changeset const&, graph&); changeset (xml::parser&, graph&); virtual string diff --git a/odb/semantics/relational/column.cxx b/odb/semantics/relational/column.cxx index a4c39c3..05b8eff 100644 --- a/odb/semantics/relational/column.cxx +++ b/odb/semantics/relational/column.cxx @@ -3,13 +3,24 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include namespace semantics { namespace relational { column:: + column (column const& c, uscope&, graph& g) + : unameable (c, g), + type_ (c.type_), + null_ (c.null_), + default__ (c.default__), + options_ (c.options_) + { + } + + column:: column (xml::parser& p, uscope&, graph& g) : unameable (p, g), type_ (p.attribute ("type", string ())), @@ -20,6 +31,12 @@ namespace semantics p.content (xml::parser::empty); } + column& column:: + clone (uscope& s, graph& g) const + { + return g.new_node (*this, s, g); + } + void column:: serialize (xml::serializer& s) const { diff --git a/odb/semantics/relational/column.hxx b/odb/semantics/relational/column.hxx index 19aed98..07d61bf 100644 --- a/odb/semantics/relational/column.hxx +++ b/odb/semantics/relational/column.hxx @@ -20,40 +20,22 @@ namespace semantics public: string const& - type () const - { - return type_; - } + type () const {return type_;} bool - null () const - { - return null_; - } + null () const {return null_;} string const& - default_ () const - { - return default__; - } + default_ () const {return default__;} void - default_ (string const& d) - { - default__ = d; - } + default_ (string const& d) {default__ = d;} string const& - options () const - { - return options_; - } + options () const {return options_;} void - options (string const& o) - { - options_ = o; - } + options (string const& o) {options_ = o;} public: typedef relational::table table_type; @@ -72,16 +54,10 @@ namespace semantics contained_iterator; contained_iterator - contained_begin () const - { - return contained_.begin (); - } + contained_begin () const {return contained_.begin ();} contained_iterator - contained_end () const - { - return contained_.end (); - } + contained_end () const {return contained_.end ();} public: column (string const& id, string const& type, bool null) @@ -89,8 +65,12 @@ namespace semantics { } + column (column const&, uscope&, graph&); column (xml::parser&, uscope&, graph&); + virtual column& + clone (uscope&, graph&) const; + void add_edge_right (contains& e) { diff --git a/odb/semantics/relational/elements.cxx b/odb/semantics/relational/elements.cxx index 89aa014..43c03c0 100644 --- a/odb/semantics/relational/elements.cxx +++ b/odb/semantics/relational/elements.cxx @@ -3,7 +3,10 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include +#include +#include namespace semantics { @@ -27,6 +30,46 @@ namespace semantics { } + // scope + // + template <> + void scope:: + add_edge_left (names_type& e) + { + nameable_type& n (e.nameable ()); + name_type const& name (e.name ()); + + typename names_map::iterator i (names_map_.find (name)); + + if (i == names_map_.end ()) + { + typename names_list::iterator i; + + // We want the order to be columns first, then the primary key, + // and then the foreign keys. + // + if (n.is_a ()) + i = names_.insert (first_key_, &e); + else + { + if (n.is_a ()) + first_key_ = i = names_.insert (first_key_, &e); + else + { + i = names_.insert (names_.end (), &e); + + if (first_key_ == names_.end ()) + first_key_ = i; + } + } + + names_map_[name] = i; + iterator_map_[&e] = i; + } + else + throw duplicate_name (*this, (*i->second)->nameable (), n); + } + // type info // namespace diff --git a/odb/semantics/relational/elements.hxx b/odb/semantics/relational/elements.hxx index 8dde6bc..2321e1a 100644 --- a/odb/semantics/relational/elements.hxx +++ b/odb/semantics/relational/elements.hxx @@ -196,7 +196,12 @@ namespace semantics // nameable (string const& id): id_ (id), named_ (0) {} - void + virtual nameable& + clone (scope_type&, graph&) const = 0; + + // Virtual because we call it via nameable interface (e.g., in copy). + // + virtual void add_edge_right (names_type& e) { assert (named_ == 0); @@ -206,6 +211,7 @@ namespace semantics using node::add_edge_right; protected: + nameable (nameable const&, graph& g); nameable (xml::parser&, graph& g); void @@ -313,10 +319,13 @@ namespace semantics public: scope (): first_key_ (names_.end ()) {} - void + // Virtual because we call it via scope interface (e.g., in copy). + // + virtual void add_edge_left (names_type&); protected: + scope (scope const&, graph&); scope (xml::parser&, graph&); void @@ -330,6 +339,10 @@ namespace semantics typename names_list::iterator first_key_; }; + template <> + void scope:: + add_edge_left (names_type&); + typedef scope uscope; typedef scope qscope; } diff --git a/odb/semantics/relational/elements.txx b/odb/semantics/relational/elements.txx index 05ae06d..53946a0 100644 --- a/odb/semantics/relational/elements.txx +++ b/odb/semantics/relational/elements.txx @@ -23,6 +23,13 @@ namespace semantics template nameable:: + nameable (nameable const& n, graph&) + : id_ (n.id_), named_ (0) + { + } + + template + nameable:: nameable (xml::parser&, graph&) // : id_ (p.attribute ("id")) : named_ (0) @@ -99,6 +106,19 @@ namespace semantics template scope:: + scope (scope const& s, graph& g) + : first_key_ (names_.end ()) + { + for (names_const_iterator i (s.names_begin ()); + i != s.names_end (); ++i) + { + nameable_type& n (i->nameable ().clone (*this, g)); + g.new_edge (*this, n, i->name ()); + } + } + + template + scope:: scope (xml::parser& p, graph& g) : first_key_ (names_.end ()) { @@ -136,38 +156,18 @@ namespace semantics void scope:: add_edge_left (names_type& e) { - nameable_type& n (e.nameable ()); name_type const& name (e.name ()); typename names_map::iterator i (names_map_.find (name)); if (i == names_map_.end ()) { - typename names_list::iterator i; - - // We want the order to be columns first, then the primary key, - // and then the foreign keys. - // - if (n.is_a ()) - i = names_.insert (first_key_, &e); - else - { - if (n.is_a ()) - first_key_ = i = names_.insert (first_key_, &e); - else - { - i = names_.insert (names_.end (), &e); - - if (first_key_ == names_.end ()) - first_key_ = i; - } - } - + typename names_list::iterator i (names_.insert (names_.end (), &e)); names_map_[name] = i; iterator_map_[&e] = i; } else - throw duplicate_name (*this, (*i->second)->nameable (), n); + throw duplicate_name (*this, (*i->second)->nameable (), e.nameable ()); } } } diff --git a/odb/semantics/relational/foreign-key.cxx b/odb/semantics/relational/foreign-key.cxx index 52316bb..7ef88a6 100644 --- a/odb/semantics/relational/foreign-key.cxx +++ b/odb/semantics/relational/foreign-key.cxx @@ -6,7 +6,8 @@ #include #include -#include + +#include using namespace std; @@ -45,6 +46,16 @@ namespace semantics } foreign_key:: + foreign_key (foreign_key const& k, uscope& s, graph& g) + : key (k, s, g), + referenced_table_ (k.referenced_table_), + referenced_columns_ (k.referenced_columns_), + deferred_ (k.deferred_), + on_delete_ (k.on_delete_) + { + } + + foreign_key:: foreign_key (xml::parser& p, uscope& s, graph& g) : key (p, s, g), deferred_ (p.attribute ("deferred", false)), @@ -72,6 +83,13 @@ namespace semantics p.next_expect (parser::end_element); } + foreign_key& foreign_key:: + clone (uscope& s, graph& g) const + { + return g.new_node (*this, s, g); + } + + void foreign_key:: serialize (xml::serializer& s) const { diff --git a/odb/semantics/relational/foreign-key.hxx b/odb/semantics/relational/foreign-key.hxx index 1c00873..5d551fe 100644 --- a/odb/semantics/relational/foreign-key.hxx +++ b/odb/semantics/relational/foreign-key.hxx @@ -68,8 +68,12 @@ namespace semantics { } + foreign_key (foreign_key const&, uscope&, graph&); foreign_key (xml::parser&, uscope&, graph&); + virtual foreign_key& + clone (uscope&, graph&) const; + virtual string kind () const { diff --git a/odb/semantics/relational/index.cxx b/odb/semantics/relational/index.cxx index 46cc75c..5ff1a26 100644 --- a/odb/semantics/relational/index.cxx +++ b/odb/semantics/relational/index.cxx @@ -3,19 +3,35 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include namespace semantics { namespace relational { index:: + index (index const& i, uscope& s, graph& g) + : key (i, s, g), + type_ (i.type_), + method_ (i.method_), + options_ (i.options_) + { + } + + index:: index (xml::parser& p, uscope& s, graph& g) : key (p, s, g), - type_ (p.attribute ("type", string ())), - method_ (p.attribute ("method", string ())), - options_ (p.attribute ("options", string ())) + type_ (p.attribute ("type", string ())), + method_ (p.attribute ("method", string ())), + options_ (p.attribute ("options", string ())) + { + } + + index& index:: + clone (uscope& s, graph& g) const { + return g.new_node (*this, s, g); } void index:: diff --git a/odb/semantics/relational/index.hxx b/odb/semantics/relational/index.hxx index 802faa7..621f36a 100644 --- a/odb/semantics/relational/index.hxx +++ b/odb/semantics/relational/index.hxx @@ -18,28 +18,13 @@ namespace semantics { public: string const& - type () const - { - return type_; - } + type () const {return type_;} string const& - method () const - { - return method_; - } + method () const {return method_;} string const& - options () const - { - return options_; - } - - virtual string - kind () const - { - return "index"; - } + options () const {return options_;} public: index (string const& id, @@ -50,8 +35,18 @@ namespace semantics { } + index (index const&, uscope&, graph&); index (xml::parser&, uscope&, graph&); + virtual index& + clone (uscope&, graph&) const; + + virtual string + kind () const + { + return "index"; + } + virtual void serialize (xml::serializer&) const; diff --git a/odb/semantics/relational/key.cxx b/odb/semantics/relational/key.cxx index 2358562..e27253c 100644 --- a/odb/semantics/relational/key.cxx +++ b/odb/semantics/relational/key.cxx @@ -3,13 +3,28 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include +#include namespace semantics { namespace relational { key:: + key (key const& k, uscope& s, graph& g) + : unameable (k, g) + { + for (contains_iterator i (k.contains_begin ()); + i != k.contains_end (); ++i) + { + column* c (s.find (i->column ().name ())); + assert (c != 0); + g.new_edge (*this, *c, i->options ()); + } + } + + key:: key (xml::parser& p, uscope& s, graph& g) : unameable (p, g) { diff --git a/odb/semantics/relational/key.hxx b/odb/semantics/relational/key.hxx index edf8c36..8d64993 100644 --- a/odb/semantics/relational/key.hxx +++ b/odb/semantics/relational/key.hxx @@ -21,22 +21,13 @@ namespace semantics typedef relational::column column_type; key_type& - key () const - { - return *key_; - } + key () const {return *key_;} column_type& - column () const - { - return *column_; - } + column () const {return *column_;} string const& - options () const - { - return options_; - } + options () const {return options_;} public: contains (string const& o = string ()) : options_ (o) {} @@ -69,22 +60,13 @@ namespace semantics contains_iterator; contains_iterator - contains_begin () const - { - return contains_.begin (); - } + contains_begin () const {return contains_.begin ();} contains_iterator - contains_end () const - { - return contains_.end (); - } + contains_end () const {return contains_.end ();} contains_list::size_type - contains_size () const - { - return contains_.size (); - } + contains_size () const {return contains_.size ();} public: key (std::string const& id): unameable (id) {} @@ -96,6 +78,7 @@ namespace semantics } protected: + key (key const&, uscope&, graph&); key (xml::parser&, uscope&, graph&); void diff --git a/odb/semantics/relational/model.cxx b/odb/semantics/relational/model.cxx index bdb95d6..6515591 100644 --- a/odb/semantics/relational/model.cxx +++ b/odb/semantics/relational/model.cxx @@ -3,13 +3,21 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include namespace semantics { namespace relational { model:: + model (model const& m, graph& g) + : qscope (m, g), + version_ (m.version_) + { + } + + model:: model (xml::parser& p, graph& g) : qscope (p, g), version_ (p.attribute ("version")) diff --git a/odb/semantics/relational/model.hxx b/odb/semantics/relational/model.hxx index 04fb729..626befd 100644 --- a/odb/semantics/relational/model.hxx +++ b/odb/semantics/relational/model.hxx @@ -21,6 +21,7 @@ namespace semantics public: model (version_type v): version_ (v) {} + model (model const&, graph&); model (xml::parser&, graph&); virtual string diff --git a/odb/semantics/relational/primary-key.cxx b/odb/semantics/relational/primary-key.cxx index 6bcab33..fdfed0e 100644 --- a/odb/semantics/relational/primary-key.cxx +++ b/odb/semantics/relational/primary-key.cxx @@ -3,19 +3,32 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include namespace semantics { namespace relational { primary_key:: + primary_key (primary_key const& k, uscope& s, graph& g) + : key (k, s, g), auto__ (k.auto__) + { + } + + primary_key:: primary_key (xml::parser& p, uscope& s, graph& g) : key (p, s, g), auto__ (p.attribute ("auto", false)) { } + primary_key& primary_key:: + clone (uscope& s, graph& g) const + { + return g.new_node (*this, s, g); + } + void primary_key:: serialize (xml::serializer& s) const { diff --git a/odb/semantics/relational/primary-key.hxx b/odb/semantics/relational/primary-key.hxx index 88b5bcb..073245b 100644 --- a/odb/semantics/relational/primary-key.hxx +++ b/odb/semantics/relational/primary-key.hxx @@ -16,10 +16,7 @@ namespace semantics { public: bool - auto_ () const - { - return auto__; - } + auto_ () const {return auto__;} public: primary_key (bool auto_) @@ -28,8 +25,12 @@ namespace semantics { } + primary_key (primary_key const&, uscope&, graph&); primary_key (xml::parser&, uscope&, graph&); + virtual primary_key& + clone (uscope&, graph&) const; + virtual string kind () const { diff --git a/odb/semantics/relational/table.cxx b/odb/semantics/relational/table.cxx index 4eeaf0d..1c6e784 100644 --- a/odb/semantics/relational/table.cxx +++ b/odb/semantics/relational/table.cxx @@ -3,7 +3,8 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include + +#include namespace semantics { @@ -12,11 +13,23 @@ namespace semantics // table // table:: + table (table const& t, qscope& s, graph& g) + : qnameable (t, g), uscope (t, g) + { + } + + table:: table (xml::parser& p, qscope&, graph& g) : qnameable (p, g), uscope (p, g) { } + table& table:: + clone (qscope& s, graph& g) const + { + return g.new_node (*this, s, g); + } + void table:: serialize (xml::serializer& s) const { @@ -28,6 +41,12 @@ namespace semantics // add_table // + add_table& add_table:: + clone (qscope& s, graph& g) const + { + return g.new_node (*this, s, g); + } + void add_table:: serialize (xml::serializer& s) const { diff --git a/odb/semantics/relational/table.hxx b/odb/semantics/relational/table.hxx index 5a38e6d..768d3d7 100644 --- a/odb/semantics/relational/table.hxx +++ b/odb/semantics/relational/table.hxx @@ -15,8 +15,12 @@ namespace semantics { public: table (string const& id): qnameable (id) {} + table (table const&, qscope&, graph&); table (xml::parser&, qscope&, graph&); + virtual table& + clone (qscope&, graph&) const; + virtual string kind () const {return "table";} @@ -32,8 +36,12 @@ namespace semantics { public: add_table (string const& id): table (id) {} + add_table (table const& t, qscope& s, graph& g): table (t, s, g) {} add_table (xml::parser& p, qscope& s, graph& g): table (p, s, g) {} + virtual add_table& + clone (qscope&, graph&) const; + virtual string kind () const {return "add table";} -- cgit v1.1