From 92b705cd0de55091bba8186b8598cb226c7e74b9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 19 Apr 2011 17:55:24 +0200 Subject: Map only continuous, zero-based enums to MySQL ENUM The others map to INT. --- odb/relational/mysql/context.cxx | 53 +++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'odb/relational/mysql') diff --git a/odb/relational/mysql/context.cxx b/odb/relational/mysql/context.cxx index b413b85..4e31361 100644 --- a/odb/relational/mysql/context.cxx +++ b/odb/relational/mysql/context.cxx @@ -242,29 +242,54 @@ namespace relational return r; using semantics::enum_; + using semantics::enumerator; if (enum_* e = dynamic_cast (&t)) { - enum_::enumerates_iterator b (e->enumerates_begin ()), - end (e->enumerates_end ()); - - if (b != end) + // We can only map to ENUM if the C++ enumeration is contiguous + // and starts with 0. + // + if (e->unsigned_ ()) { - r += "ENUM ("; - for (enum_::enumerates_iterator i (b); i != end; ++i) + enum_::enumerates_iterator i (e->enumerates_begin ()), + end (e->enumerates_end ()); + + if (i != end) { - if (i != b) - r += ", "; + r += "ENUM ("; + + for (unsigned long long j (0); i != end; ++i, ++j) + { + enumerator const& er (i->enumerator ()); + + if (er.value () != j) + break; + + if (j != 0) + r += ", "; - r += '\''; - r += i->enumerator ().name (); - r += '\''; + r += '\''; + r += er.name (); + r += '\''; + } + + if (i == end) + r += ")"; + else + r.clear (); } - r += ")"; + } - if ((f & ctf_default_null) == 0) - r += " NOT NULL"; + if (r.empty ()) + { + r = "INT"; + + if (e->unsigned_ ()) + r += " UNSIGNED"; } + + if ((f & ctf_default_null) == 0) + r += " NOT NULL"; } return r; -- cgit v1.1