From 81ea37904e4959414b53b225b4b5e56e1b561bdc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 22 Jul 2011 14:49:29 +0200 Subject: Add pragma for setting type's or member's default value New pragma: default. New test: default. --- odb/relational/mysql/schema.cxx | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'odb/relational/mysql/schema.cxx') diff --git a/odb/relational/mysql/schema.cxx b/odb/relational/mysql/schema.cxx index 3848c75..4a7171f 100644 --- a/odb/relational/mysql/schema.cxx +++ b/odb/relational/mysql/schema.cxx @@ -58,6 +58,85 @@ namespace relational } virtual void + default_bool (semantics::data_member&, bool v) + { + // MySQL has TRUE and FALSE as just aliases for 1 and 0. Still + // use them for self-documentation. + // + os << " DEFAULT " << (v ? "TRUE" : "FALSE"); + } + + virtual void + default_enum (semantics::data_member& m, tree en, string const& name) + { + // Make sure the column is mapped to an ENUM or integer type. + // + sql_type const& t (column_sql_type (m)); + + switch (t.type) + { + case sql_type::ENUM: + case sql_type::TINYINT: + case sql_type::SMALLINT: + case sql_type::MEDIUMINT: + case sql_type::INT: + case sql_type::BIGINT: + break; + default: + { + cerr << m.file () << ":" << m.line () << ":" << m.column () + << ": error: column with default value specified as C++ " + << "enumerator must map to MySQL ENUM or integer type" + << endl; + + throw generation_failed (); + } + } + + using semantics::enum_; + using semantics::enumerator; + + enumerator& er (dynamic_cast (*unit.find (en))); + enum_& e (er.enum_ ()); + + if (t.type == sql_type::ENUM) + { + // Assuming the enumerators in the C++ enum and MySQL ENUM are + // in the same order, calculate the poistion of the C++ + // enumerator and use that as an index in the MySQL ENUM. + // + size_t pos (0); + + for (enum_::enumerates_iterator i (e.enumerates_begin ()), + end (e.enumerates_end ()); i != end; ++i) + { + if (&i->enumerator () == &er) + break; + + pos++; + } + + if (pos < t.enumerators.size ()) + os << " DEFAULT " << t.enumerators[pos]; + else + { + cerr << m.file () << ":" << m.line () << ":" << m.column () + << ": error: unable to map C++ enumerator '" << name + << "' to MySQL ENUM value" << endl; + + throw generation_failed (); + } + } + else + { + if (e.unsigned_ ()) + os << " DEFAULT " << er.value (); + else + os << " DEFAULT " << static_cast (er.value ()); + } + } + + virtual void constraints (semantics::data_member& m) { base::constraints (m); -- cgit v1.1