diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-07-22 14:49:29 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-07-22 17:57:00 +0200 |
commit | 81ea37904e4959414b53b225b4b5e56e1b561bdc (patch) | |
tree | 6ae70d4adb9c0c3ed57679873de8061d5b9c5b19 /odb/relational/mysql | |
parent | 246b1557bf84082cc72ec2cbe089262b21c8bc97 (diff) |
Add pragma for setting type's or member's default value
New pragma: default. New test: default.
Diffstat (limited to 'odb/relational/mysql')
-rw-r--r-- | odb/relational/mysql/schema.cxx | 79 |
1 files changed, 79 insertions, 0 deletions
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<enumerator&> (*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<long long> (er.value ()); + } + } + + virtual void constraints (semantics::data_member& m) { base::constraints (m); |