From e89a00af0b935183af40479f634db0a098c34e4a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 9 Mar 2012 09:01:44 +0200 Subject: Don't override NOT NULL on Oracle VARCHAR column if it's primary key --- doc/manual.xhtml | 9 ++++++--- odb/relational/oracle/schema.cxx | 25 +++++++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/doc/manual.xhtml b/doc/manual.xhtml index 6257cf7..e14f2cf 100644 --- a/doc/manual.xhtml +++ b/doc/manual.xhtml @@ -13261,10 +13261,13 @@ SHOW integer_datetimes

In Oracle empty VARCHAR2 and NVARCHAR2 strings are represented as NULL values. As a result, - in the generated schema, columns of these types are always - declared as NULL, even if explicitly declared as + in the generated schema, columns of these types are declared as + NULL even if explicitly declared as NOT NULL with the db not_null pragma - (Section 12.4.5, "null/not_null").

+ (Section 12.4.5, "null/not_null"), + except for primary key columns. This also means that for object ids + that are mapped to these Oracle types, an empty string is an invalid + value.

The Oracle ODB runtime library also provides support for mapping the std::string type to the Oracle CHAR, diff --git a/odb/relational/oracle/schema.cxx b/odb/relational/oracle/schema.cxx index 2fd0602..2835520 100644 --- a/odb/relational/oracle/schema.cxx +++ b/odb/relational/oracle/schema.cxx @@ -153,9 +153,9 @@ namespace relational null (sema_rel::column& c) { // Oracle interprets empty VARCHAR2 and NVARCHAR2 strings as - // NULL. As an empty string is always valid within the C++ - // context, VARCHAR2 and NVARCHAR2 columns are always - // specified as nullable. + // NULL. As an empty string is valid within the C++ context, + // VARCHAR2 and NVARCHAR2 columns are always specified as + // nullable, except when are a part of a primary key. // if (!c.null ()) { @@ -164,7 +164,24 @@ namespace relational sql_type const& t (parse_sql_type (c.type ())); if (t.type == sql_type::VARCHAR2 || t.type == sql_type::NVARCHAR2) - return; + { + // See if this column is a part of a primary key. + // + bool pk (false); + + for (sema_rel::column::contained_iterator i ( + c.contained_begin ()); i != c.contained_end (); ++i) + { + if (i->key ().is_a ()) + { + pk = true; + break; + } + } + + if (!pk) + return; + } } base::null (c); -- cgit v1.1