diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-24 16:32:51 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-24 16:32:51 +0200 |
commit | 08a47c70ad517b80b72914d47d547463f576bcd3 (patch) | |
tree | 8a6ab07cf05e8668ea3c91735dfe97e2a98f3f05 /odb/semantics/relational/elements.cxx | |
parent | a976183dc95a8b7a9bd7a308c3ea94f08982c426 (diff) |
Generate database schema from database model instead of C++ model
We now first create the so-called database model from C++ model and
then use that to generate the database schema. The new approach also
adds more general support for primary/foreign keys, including multi-
column keys. Finally, for MySQL we now generate out-of-line foreign
key definitions. Because MySQL does not support deferred constraints
checking, deferred foreign keys are written commented out, for
documentation.
Diffstat (limited to 'odb/semantics/relational/elements.cxx')
-rw-r--r-- | odb/semantics/relational/elements.cxx | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/odb/semantics/relational/elements.cxx b/odb/semantics/relational/elements.cxx new file mode 100644 index 0000000..6ab977a --- /dev/null +++ b/odb/semantics/relational/elements.cxx @@ -0,0 +1,137 @@ +// file : odb/semantics/relational/elements.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v3; see accompanying LICENSE file + +#include <cutl/compiler/type-info.hxx> + +#include <odb/semantics/relational/elements.hxx> +#include <odb/semantics/relational/column.hxx> +#include <odb/semantics/relational/primary-key.hxx> + +namespace semantics +{ + namespace relational + { + // scope + // + + scope::names_iterator scope:: + find (string const& name) + { + names_map::iterator i (names_map_.find (name)); + + if (i == names_map_.end ()) + return names_.end (); + else + return i->second; + } + + scope::names_const_iterator scope:: + find (string const& name) const + { + names_map::const_iterator i (names_map_.find (name)); + + if (i == names_map_.end ()) + return names_.end (); + else + return names_const_iterator (i->second); + } + + scope::names_iterator scope:: + find (names const& e) + { + names_iterator_map::iterator i (iterator_map_.find (&e)); + return i != iterator_map_.end () ? i->second : names_.end (); + } + + scope::names_const_iterator scope:: + find (names const& e) const + { + names_iterator_map::const_iterator i (iterator_map_.find (&e)); + return i != iterator_map_.end () ? i->second : names_.end (); + } + + void scope:: + add_edge_left (names& e) + { + nameable& n (e.nameable ()); + string const& name (e.name ()); + + names_map::iterator i (names_map_.find (name)); + + if (i == names_map_.end ()) + { + 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<column> ()) + i = names_.insert (first_key_, &e); + else + { + if (n.is_a<primary_key> ()) + 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 + { + struct init + { + init () + { + using compiler::type_info; + + // node + // + insert (type_info (typeid (node))); + + // edge + // + insert (type_info (typeid (edge))); + + // names + // + { + type_info ti (typeid (names)); + ti.add_base (typeid (edge)); + insert (ti); + } + + // nameable + // + { + type_info ti (typeid (nameable)); + ti.add_base (typeid (node)); + insert (ti); + } + + // scope + // + { + type_info ti (typeid (scope)); + ti.add_base (typeid (node)); + insert (ti); + } + } + } init_; + } + } +} |