From b8554760aa3a5c5697c77d11e507a2bb46dbf8e4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Jul 2012 15:17:16 +0200 Subject: Add support for custom database type mapping New pragma qualifier, map, and specifiers: as, to, from. New tests: /custom. --- odb/relational/context.hxx | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'odb/relational/context.hxx') diff --git a/odb/relational/context.hxx b/odb/relational/context.hxx index 60ed934..ca55650 100644 --- a/odb/relational/context.hxx +++ b/odb/relational/context.hxx @@ -23,6 +23,19 @@ namespace relational statement_where // WHERE clause. }; + // Custom database type mapping. + // + struct custom_db_type + { + regex type; + std::string as; + std::string to; + std::string from; + location_t loc; + }; + + typedef std::vector custom_db_types; + class context: public virtual ::context { public: @@ -94,6 +107,58 @@ namespace relational return quote_id (table_name (m, p)); } + // Custom database type conversion. + // + public: + string + convert_to (string const& expr, + string const& sqlt, + semantics::data_member& m) + { + string const& conv (current ().convert_expr (sqlt, m, true)); + return conv.empty () ? expr : convert (expr, conv); + } + + string + convert_from (string const& expr, + string const& sqlt, + semantics::data_member& m) + { + string const& conv (current ().convert_expr (sqlt, m, false)); + return conv.empty () ? expr : convert (expr, conv); + } + + // These shortcut versions should only be used on special members + // (e.g., auto id, version, etc) since they may not determine the + // proper SQL type in other cases (prefixes, composite ids, etc). + // + string + convert_to (string const& expr, semantics::data_member& m) + { + return convert_to (expr, column_type (m), m); + } + + string + convert_from (string const& expr, semantics::data_member& m) + { + return convert_from (expr, column_type (m), m); + } + + // Return the conversion expression itself. + // + string const& + convert_to_expr (string const& sqlt, semantics::data_member& m) + { + return current ().convert_expr (sqlt, m, true); + } + + protected: + virtual string const& + convert_expr (string const& sqlt, semantics::data_member&, bool to); + + string + convert (string const& expr, string const& conv); + protected: // The default implementation returns false. // -- cgit v1.1