diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-07-20 14:08:09 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-07-20 14:08:09 +0200 |
commit | 5f71c55a1c24c23af1eeb0d664922497a0e5c071 (patch) | |
tree | b9d871f01893d515f0a5bf62995b75b4fc43d246 | |
parent | 0e595e5d7bcc25b18027f3bda5d4508d42dacb39 (diff) |
Add mapping of basic C++ types to DB types
-rw-r--r-- | odb/context.cxx | 79 | ||||
-rw-r--r-- | odb/context.hxx | 4 |
2 files changed, 79 insertions, 4 deletions
diff --git a/odb/context.cxx b/odb/context.cxx index 33aa73d..eb0aa5d 100644 --- a/odb/context.cxx +++ b/odb/context.cxx @@ -87,6 +87,38 @@ namespace "xor", "xor_eq" }; + + struct type_map_entry + { + const char* const cxx_type; + const char* const db_type; + }; + + type_map_entry mysql_type_map[] = + { + {"bool", "TINYINT(1)"}, + + {"char", "TINYINT"}, + {"signed char", "TINYINT"}, + {"unsigned char", "TINYINT UNSIGNED"}, + + {"short int", "SMALLINT"}, + {"short unsigned int", "SMALLINT UNSIGNED"}, + + {"int", "INT"}, + {"unsigned int", "INT UNSIGNED"}, + + {"long int", "BIGINT"}, + {"long unsigned int", "BIGINT UNSIGNED"}, + + {"long long int", "BIGINT"}, + {"long long unsigned int", "BIGINT UNSIGNED"}, + + {"float", "FLOAT"}, + {"double", "DOUBLE"}, + + {"::std::string", "TEXT"} + }; } context:: @@ -101,6 +133,33 @@ context (ostream& os_, { for (size_t i (0); i < sizeof (keywords) / sizeof (char*); ++i) data_->keyword_set_.insert (keywords[i]); + + // Populate the C++ type to DB type map. + // + { + size_t n; + type_map_entry* p; + + switch (options.database ()) + { + case database::mysql: + { + p = mysql_type_map; + n = sizeof (mysql_type_map) / sizeof (type_map_entry); + break; + } + default: + { + p = 0; + n = 0; + break; + } + } + + for (size_t i (0); i < n; ++i) + data_->type_map_.insert ( + type_map_type::value_type (p[i].cxx_type, p[i].db_type)); + } } context:: @@ -154,10 +213,22 @@ db_type (semantics::data_member& m) const { if (m.count ("type")) return m.get<string> ("type"); - else - { - return "INT"; - } + + string const& name (m.type ().fq_name ()); + type_map_type::const_iterator i (data_->type_map_.find (name)); + + if (i != data_->type_map_.end ()) + return i->second; + + cerr << m.file () << ":" << m.line () << ":" << m.column () + << " error: unable to map C++ type '" << name << "' used in " + << "data member '" << m.name () << "' to a database type" << endl; + + cerr << m.file () << ":" << m.line () << ":" << m.column () + << " info: use '#pragma odb type' to specify the database type" + << endl; + + throw generation_failed (); } string context:: diff --git a/odb/context.hxx b/odb/context.hxx index 064b0d5..700e604 100644 --- a/odb/context.hxx +++ b/odb/context.hxx @@ -6,6 +6,7 @@ #ifndef ODB_CONTEXT_HXX #define ODB_CONTEXT_HXX +#include <map> #include <set> #include <string> #include <ostream> @@ -61,10 +62,13 @@ public: typedef std::set<string> keyword_set_type; keyword_set_type const& keyword_set; + typedef std::map<string, string> type_map_type; + private: struct data { keyword_set_type keyword_set_; + type_map_type type_map_; }; public: |