From 5f71c55a1c24c23af1eeb0d664922497a0e5c071 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 20 Jul 2010 14:08:09 +0200 Subject: Add mapping of basic C++ types to DB types --- odb/context.cxx | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 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 ("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 #include #include #include @@ -61,10 +62,13 @@ public: typedef std::set keyword_set_type; keyword_set_type const& keyword_set; + typedef std::map type_map_type; + private: struct data { keyword_set_type keyword_set_; + type_map_type type_map_; }; public: -- cgit v1.1