From 4cde011f27cd406804f05aa8fd1d28ed91a26738 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 1 Aug 2012 11:16:20 +0200 Subject: Add support for empty column names in composite value types --- odb/common.cxx | 4 +++- odb/context.cxx | 30 +++++++++++++++++++++++++++- odb/context.hxx | 5 +++++ odb/relational/common.cxx | 47 +++++++++++++++++++++++++------------------- odb/relational/model.hxx | 40 +++++++++++++++++++------------------ odb/relational/processor.cxx | 8 +------- odb/relational/source.hxx | 40 +++++++++++++++++++------------------ 7 files changed, 107 insertions(+), 67 deletions(-) diff --git a/odb/common.cxx b/odb/common.cxx index 81a416b..667afc0 100644 --- a/odb/common.cxx +++ b/odb/common.cxx @@ -496,7 +496,9 @@ traverse_member (semantics::data_member& m, semantics::type& t) } else { - string name (column_prefix_ + column_name (m, key_prefix_, default_name_)); + string name ( + compose_name ( + column_prefix_, column_name (m, key_prefix_, default_name_))); if (traverse_column (m, name, first_)) { diff --git a/odb/context.cxx b/odb/context.cxx index 8335ec4..df94dfb 100644 --- a/odb/context.cxx +++ b/odb/context.cxx @@ -897,7 +897,7 @@ column_name (data_member_path const& mp) const if (composite_wrapper (utype (m))) r += object_columns_base::column_prefix (m); else - r += column_name (m); + r = compose_name (r, column_name (m)); } return r; @@ -928,6 +928,34 @@ column_name (semantics::data_member& m, string const& p, string const& d) const } string context:: +compose_name (string const& prefix, string const& name) +{ + string r (prefix); + size_t n (r.size ()); + + // Add an underscore unless one is already in the prefix or + // the name is empty. Similarly, remove it if it is there but + // the name is empty. + // + if (n != 0) + { + if (r[n - 1] != '_') + { + if (!name.empty ()) + r += '_'; + } + else + { + if (name.empty ()) + r.resize (n - 1); + } + } + + r += name; + return r; +} + +string context:: column_type (const data_member_path& mp, string const& kp, bool id) { if (kp.empty ()) diff --git a/odb/context.hxx b/odb/context.hxx index e4b9418..f201596 100644 --- a/odb/context.hxx +++ b/odb/context.hxx @@ -564,6 +564,11 @@ public: string const& key_prefix, string const& default_name) const; + // Compose the name by inserting/removing an underscore, as necessary. + // + static string + compose_name (string const& prefix, string const& name); + string column_type (const data_member_path&, string const& key_prefix = string (), diff --git a/odb/relational/common.cxx b/odb/relational/common.cxx index 1e1009d..3916184 100644 --- a/odb/relational/common.cxx +++ b/odb/relational/common.cxx @@ -42,20 +42,23 @@ namespace relational // public name. // string alias; - - if (composite_wrapper (utype ((*id_member (c))))) { - string p (column_prefix (m, key_prefix_, default_name_)); + string n; + + if (composite_wrapper (utype (*id_member (c)))) + { + n = column_prefix (m, key_prefix_, default_name_); - if (p.empty ()) - p = public_name_db (m); + if (n.empty ()) + n = public_name_db (m); + else + n.resize (n.size () - 1); // Remove trailing underscore. + } else - p.resize (p.size () - 1); // Remove trailing underscore. + n = column_name (m, key_prefix_, default_name_); - alias = column_prefix_ + p; + alias = compose_name (column_prefix_, n); } - else - alias = column_prefix_ + column_name (m, key_prefix_, default_name_); generate (alias, c); } @@ -219,20 +222,23 @@ namespace relational // public name. // string alias; - - if (composite_wrapper (utype ((*id_member (c))))) { - string p (column_prefix (m, key_prefix_, default_name_)); + string n; + + if (composite_wrapper (utype (*id_member (c)))) + { + n = column_prefix (m, key_prefix_, default_name_); - if (p.empty ()) - p = public_name_db (m); + if (n.empty ()) + n = public_name_db (m); + else + n.resize (n.size () - 1); // Remove trailing underscore. + } else - p.resize (p.size () - 1); // Remove trailing underscore. + n = column_name (m, key_prefix_, default_name_); - alias = column_prefix_ + p; + alias = compose_name (column_prefix_, n); } - else - alias = column_prefix_ + column_name (m, key_prefix_, default_name_); string tag (escape (alias + "_alias_tag")); string const& fq_name (class_fq_name (c)); @@ -489,8 +495,9 @@ namespace relational // Simple id. // string type (t.fq_name (hint)); - string column (column_prefix_ + - column_name (m, key_prefix_, default_name_)); + string column ( + compose_name ( + column_prefix_, column_name (m, key_prefix_, default_name_))); // For pointer_query_columns generate normal column mapping. // diff --git a/odb/relational/model.hxx b/odb/relational/model.hxx index 6ffffca..b3edd93 100644 --- a/odb/relational/model.hxx +++ b/odb/relational/model.hxx @@ -303,15 +303,11 @@ namespace relational if (p.empty ()) p = public_name_db (m); - name = column_prefix_ + p; + name = compose_name (column_prefix_, p); } - // Add an underscore unless we already have one. - // - if (name[name.size () - 1] != '_') - name += '_'; - - model_.new_edge (table_, fk, name + "fk"); + model_.new_edge ( + table_, fk, compose_name (name, "fk")); } protected: @@ -505,17 +501,14 @@ namespace relational // key code above. // // Note also that id_name can be a column prefix (if id is - // composite), in which case it can be empty and if not, then - // it will most likely already contain a trailing underscore. + // composite), in which case it can be empty. In this case + // we just fallback on the default name. // string id_name (column_name (m, "id", "object_id")); if (id_name.empty ()) id_name = "object_id"; - if (id_name[id_name.size () - 1] != '_') - id_name += '_'; - // Foreign key. // sema_rel::foreign_key& fk ( @@ -525,7 +518,8 @@ namespace relational false, // immediate sema_rel::foreign_key::cascade)); fk.set ("cxx-location", m.location ()); - model_.new_edge (t, fk, id_name + "fk"); + model_.new_edge ( + t, fk, compose_name (id_name, "fk")); // Get referenced columns. // @@ -562,13 +556,16 @@ namespace relational id + ".id", sin->type, sin->method, sin->options); in->set ("cxx-location", sin->loc); model_.new_edge ( - t, *in, (sin->name.empty () ? id_name + "i" : sin->name)); + t, + *in, + (sin->name.empty () ? compose_name (id_name, "i") : sin->name)); } else { in = &model_.new_node (id + ".id"); in->set ("cxx-location", m.location ()); - model_.new_edge (t, *in, id_name + "i"); + model_.new_edge ( + t, *in, compose_name (id_name, "i")); } // All the columns we have in this table so far are for the @@ -594,7 +591,9 @@ namespace relational instance oc (model_, t); oc->traverse (m, container_it (ct), "index", "index"); - string col_name (column_name (m, "index", "index")); + // This is a simple value so the name cannot be empty. + // + string col (column_name (m, "index", "index")); // Index. See if we have a custom index. // @@ -609,18 +608,21 @@ namespace relational id + ".index", sin->type, sin->method, sin->options); in->set ("cxx-location", sin->loc); model_.new_edge ( - t, *in, (sin->name.empty () ? col_name + "_i" : sin->name)); + t, + *in, + (sin->name.empty () ? compose_name (col, "i") : sin->name)); } else { in = &model_.new_node (id + ".index"); in->set ("cxx-location", m.location ()); - model_.new_edge (t, *in, col_name + "_i"); + model_.new_edge ( + t, *in, compose_name (col, "i")); } model_.new_edge ( *in, - dynamic_cast (t.find (col_name)->nameable ()), + dynamic_cast (t.find (col)->nameable ()), (sin != 0 ? sin->members.back ().options : "")); } diff --git a/odb/relational/processor.cxx b/odb/relational/processor.cxx index f496a0d..64987a2 100644 --- a/odb/relational/processor.cxx +++ b/odb/relational/processor.cxx @@ -2172,13 +2172,7 @@ namespace relational if (in.name.empty ()) in.name = public_name_db (*in.members.front ().path.back ()); - // In case of a composite member, column_name() return a column - // prefix which already includes the trailing underscore. - // - if (in.name[in.name.size () - 1] != '_') - in.name += '_'; - - in.name += 'i'; + in.name = compose_name (in.name, "i"); } ++i; diff --git a/odb/relational/source.hxx b/odb/relational/source.hxx index 2b2c90e..a1f1190 100644 --- a/odb/relational/source.hxx +++ b/odb/relational/source.hxx @@ -186,22 +186,21 @@ namespace relational if (!table_name_.empty ()) { + string n; + if (composite_wrapper (idt)) { - string p (column_prefix (m, key_prefix_, default_name_)); + n = column_prefix (m, key_prefix_, default_name_); - if (p.empty ()) - p = public_name_db (m); + if (n.empty ()) + n = public_name_db (m); else - p.resize (p.size () - 1); // Remove trailing underscore. - - table = column_prefix_ + p; + n.resize (n.size () - 1); // Remove trailing underscore. } else - table = column_prefix_ + - column_name (m, key_prefix_, default_name_); + n = column_name (m, key_prefix_, default_name_); - table = quote_id (table); + table = quote_id (compose_name (column_prefix_, n)); } instance oc (table, sk_, sc_); @@ -410,7 +409,7 @@ namespace relational tbl = quote_id (i->table); col += tbl; col += '.'; - col += quote_id (column_name (i->member_path)); + col += column_qname (i->member_path); break; } } @@ -591,20 +590,23 @@ namespace relational // fall back on the member's public name. // string alias; - - if (composite_wrapper (utype (*id_member (c)))) { - string p (column_prefix (m, key_prefix_, default_name_)); + string n; + + if (composite_wrapper (utype (*id_member (c)))) + { + n = column_prefix (m, key_prefix_, default_name_); - if (p.empty ()) - p = public_name_db (m); + if (n.empty ()) + n = public_name_db (m); + else + n.resize (n.size () - 1); // Remove trailing underscore. + } else - p.resize (p.size () - 1); // Remove trailing underscore. + n = column_name (m, key_prefix_, default_name_); - alias = column_prefix_ + p; + alias = compose_name (column_prefix_, n); } - else - alias = column_prefix_ + column_name (m, key_prefix_, default_name_); semantics::class_* poly_root (polymorphic (c)); bool poly (poly_root != 0); -- cgit v1.1