From ce03afff5ef2e8da677def73079864c31c6618d8 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Jan 2013 15:10:22 +0200 Subject: Add support for mapping char[N] to CHAR/VARCHAR database types Also improve query support for arrays (decaying). --- odb/relational/oracle/context.cxx | 51 ++++++++++++++++++++++++++++++++++++--- odb/relational/oracle/context.hxx | 2 +- 2 files changed, 49 insertions(+), 4 deletions(-) (limited to 'odb/relational/oracle') diff --git a/odb/relational/oracle/context.cxx b/odb/relational/oracle/context.cxx index df62ce8..7602dda 100644 --- a/odb/relational/oracle/context.cxx +++ b/odb/relational/oracle/context.cxx @@ -30,7 +30,7 @@ namespace relational { {"bool", "NUMBER(1)", 0, false}, - {"char", "NUMBER(3)", 0, false}, + {"char", "CHAR(1)", 0, false}, {"signed char", "NUMBER(3)", 0, false}, {"unsigned char", "NUMBER(3)", 0, false}, @@ -142,17 +142,62 @@ namespace relational } string context:: - database_type_impl (semantics::type& t, semantics::names* hint, bool id) + database_type_impl (semantics::type& t, + semantics::names* hint, + bool id, + bool* null) { - string r (base_context::database_type_impl (t, hint, id)); + string r (base_context::database_type_impl (t, hint, id, null)); if (!r.empty ()) return r; using semantics::enum_; + using semantics::array; + // Enum mapping. + // if (t.is_a ()) + { r = "NUMBER(10)"; + } + // char[N] mapping. + // + else if (array* a = dynamic_cast (&t)) + { + semantics::type& bt (a->base_type ()); + if (bt.is_a ()) + { + unsigned long long n (a->size ()); + + if (n == 0) + return r; + else if (n == 1) + r = "CHAR"; + else + { + r = "VARCHAR2"; + n--; + } + + // Oracle VARCHAR2 limit is 4000 bytes. Since there are no good + // alternatives (CLOB?), let the user specify the mapping. + // + if (n > 4000) + return ""; + + // Allow empty VARCHAR2 values. + // + if (null != 0 && r == "VARCHAR2") + *null = true; + + ostringstream ostr; + ostr << n; + r += '('; + r += ostr.str (); + r += ')'; + } + } return r; } diff --git a/odb/relational/oracle/context.hxx b/odb/relational/oracle/context.hxx index 0008724..a53e25d 100644 --- a/odb/relational/oracle/context.hxx +++ b/odb/relational/oracle/context.hxx @@ -121,7 +121,7 @@ namespace relational protected: virtual string - database_type_impl (semantics::type&, semantics::names*, bool); + database_type_impl (semantics::type&, semantics::names*, bool, bool*); public: virtual -- cgit v1.1