From a5f24411433aeb61ad015129354a664820affab0 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 12 Dec 2012 11:26:44 +0200 Subject: Add support for SQL name transformations --- odb/relational/oracle/context.cxx | 19 +++++++++++++++++++ odb/relational/oracle/context.hxx | 6 ++++++ odb/relational/oracle/schema.cxx | 28 +++++++--------------------- odb/relational/oracle/source.cxx | 11 ++++++----- 4 files changed, 38 insertions(+), 26 deletions(-) (limited to 'odb/relational/oracle') diff --git a/odb/relational/oracle/context.cxx b/odb/relational/oracle/context.cxx index 4ba8659..df62ce8 100644 --- a/odb/relational/oracle/context.cxx +++ b/odb/relational/oracle/context.cxx @@ -85,6 +85,8 @@ namespace relational insert_send_auto_id = false; delay_freeing_statement_result = false; need_image_clone = true; + global_index = true; + global_fkey = true; data_->bind_vector_ = "oracle::bind*"; // Populate the C++ type to DB type map. @@ -168,6 +170,23 @@ namespace relational s == "long long unsigned int"; } + qname context:: + sequence_name (qname const& table) + { + string n; + + if (options.sequence_suffix ().count (db) != 0) + n = table.uname () + options.sequence_suffix ()[db]; + else + n = compose_name (table.uname (), "seq"); + + n = transform_name (n, sql_name_sequence); + + qname r (table.qualifier ()); + r.append (n); + return r; + } + // // SQL type parsing. // diff --git a/odb/relational/oracle/context.hxx b/odb/relational/oracle/context.hxx index 6b07a5f..0008724 100644 --- a/odb/relational/oracle/context.hxx +++ b/odb/relational/oracle/context.hxx @@ -106,6 +106,12 @@ namespace relational static bool unsigned_integer (semantics::type&); + public: + // Construct sequence name from a given table name. + // + qname + sequence_name (qname const& table); + protected: virtual string const& convert_expr (string const&, semantics::data_member&, bool); diff --git a/odb/relational/oracle/schema.cxx b/odb/relational/oracle/schema.cxx index f07e0d6..ffe4b31 100644 --- a/odb/relational/oracle/schema.cxx +++ b/odb/relational/oracle/schema.cxx @@ -127,7 +127,7 @@ namespace relational { os << " BEGIN" << endl << " EXECUTE IMMEDIATE 'DROP SEQUENCE " << - quote_id (table + "_seq") << "';" << endl + quote_id (sequence_name (table)) << "';" << endl << " EXCEPTION" << endl << " WHEN OTHERS THEN" << endl << " IF SQLCODE != -2289 THEN RAISE; END IF;" << endl @@ -184,18 +184,6 @@ namespace relational fk.set ("oracle-fk-defined", true); // Mark it as defined. } } - - virtual string - name (sema_rel::foreign_key& fk) - { - // In Oracle, foreign key names are schema-global. Make them - // unique by prefixing the key name with table name. Note, - // however, that they cannot have a schema. - // - return quote_id ( - static_cast (fk.scope ()).name ().uname () - + "_" + fk.name ()); - } }; entry create_foreign_key_; @@ -244,7 +232,8 @@ namespace relational if (pk != 0 && pk->auto_ ()) { pre_statement (); - os_ << "CREATE SEQUENCE " << quote_id (t.name () + "_seq") << endl + os_ << "CREATE SEQUENCE " << + quote_id (sequence_name (t.name ())) << endl << " START WITH 1 INCREMENT BY 1" << endl; post_statement (); } @@ -266,14 +255,11 @@ namespace relational virtual string name (sema_rel::index& in) { - // In Oracle, index names are database-global. Make them unique - // by prefixing the index name with table name (preserving the - // schema). + // In Oracle, index names can be qualified with the schema. // - sema_rel::qname n ( - static_cast (in.scope ()).name ()); - - n.uname () += "_" + in.name (); + sema_rel::table& t (static_cast (in.scope ())); + sema_rel::qname n (t.name ().qualifier ()); + n.append (in.name ()); return quote_id (n); } }; diff --git a/odb/relational/oracle/source.cxx b/odb/relational/oracle/source.cxx index 85be6cc..f6cdfcd 100644 --- a/odb/relational/oracle/source.cxx +++ b/odb/relational/oracle/source.cxx @@ -17,7 +17,7 @@ namespace relational { namespace relational = relational::source; - struct query_parameters: relational::query_parameters + struct query_parameters: relational::query_parameters, context { query_parameters (base const& x): base (x), i_ (0) {} @@ -33,9 +33,7 @@ namespace relational virtual string auto_id () { - // The same sequence name as used in schema.cxx. - // - return quote_id (table_ + "_seq") + ".nextval"; + return quote_id (sequence_name (table_)) + ".nextval"; } private: @@ -600,9 +598,12 @@ namespace relational if (id != 0 && !poly_derived && id->count ("auto")) { + // Top-level auto id. + // os << endl << strlit (" RETURNING " + - convert_from (column_qname (*id), *id) + + convert_from (column_qname (*id, column_prefix ()), + *id) + " INTO " + qp.next ()); } -- cgit v1.1