diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-01-26 12:43:16 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-01-26 12:43:16 +0200 |
commit | af12ffe836de09ec84f666effa4df347eeb07a43 (patch) | |
tree | dc0aec9f8fee545c84be098414772cf2b277c30d /odb/relational/mssql | |
parent | c1d2ec5bbd5969332f3278f39d2a7a8f0abc0493 (diff) |
Implement support for database schema
New pragma qualifier: namespace. New pragma specifier: schema. The table
specifier was extended to accept a schema prefix. New option: --default-
schema. The common/schema test was extended to cover the new functionality.
Diffstat (limited to 'odb/relational/mssql')
-rw-r--r-- | odb/relational/mssql/context.cxx | 23 | ||||
-rw-r--r-- | odb/relational/mssql/context.hxx | 2 | ||||
-rw-r--r-- | odb/relational/mssql/schema.cxx | 32 |
3 files changed, 42 insertions, 15 deletions
diff --git a/odb/relational/mssql/context.cxx b/odb/relational/mssql/context.cxx index 7832c24..c426d2e 100644 --- a/odb/relational/mssql/context.cxx +++ b/odb/relational/mssql/context.cxx @@ -108,13 +108,26 @@ namespace relational } string context:: - quote_id_impl (string const& id) const + quote_id_impl (qname const& id) const { string r; - r.reserve (130); // Max MSSQL identifier length is 128. - r += '['; - r.append (id, 0, 128); - r += ']'; + + bool f (true); + for (qname::iterator i (id.begin ()); i < id.end (); ++i) + { + if (i->empty ()) + continue; + + if (f) + f = false; + else + r += '.'; + + r += '['; + r.append (*i, 0, 128); // Max identifier length is 128. + r += ']'; + } + return r; } diff --git a/odb/relational/mssql/context.hxx b/odb/relational/mssql/context.hxx index 8f250ea..2074cc3 100644 --- a/odb/relational/mssql/context.hxx +++ b/odb/relational/mssql/context.hxx @@ -104,7 +104,7 @@ namespace relational protected: virtual string - quote_id_impl (string const&) const; + quote_id_impl (qname const&) const; protected: virtual string diff --git a/odb/relational/mssql/schema.cxx b/odb/relational/mssql/schema.cxx index 2cbeb5f..66a2278 100644 --- a/odb/relational/mssql/schema.cxx +++ b/odb/relational/mssql/schema.cxx @@ -46,15 +46,13 @@ namespace relational drop_table (base const& x): base (x) {} virtual void - drop (string const& table) + drop (sema_rel::qname const& table) { // SQL Server has no IF EXISTS conditional for dropping table. // The following approach appears to be the recommended way to // drop a table if it exists. // - string const& qt (); - - os << "IF OBJECT_ID(" << quote_string (table) << + os << "IF OBJECT_ID(" << quote_string (table.string ()) << ", " << quote_string ("U") << ") IS NOT NULL" << endl << " DROP TABLE " << quote_id (table) << endl; } @@ -88,7 +86,7 @@ namespace relational private: friend class create_foreign_key; - set<string> tables_; // Set of tables we have already defined. + set<qname> tables_; // Set of tables we have already defined. }; entry<create_table> create_table_; @@ -145,10 +143,12 @@ namespace relational name (sema_rel::foreign_key& fk) { // In SQL Server, foreign key names are schema-global. Make them - // unique by prefixing the key name with table name. + // unique by prefixing the key name with table name. Note, however, + // that they cannot have a schema. // - return static_cast<sema_rel::table&> (fk.scope ()).name () + - '_' + fk.name (); + return quote_id ( + static_cast<sema_rel::table&> (fk.scope ()).name ().uname () + + "_" + fk.name ()); } virtual void @@ -216,9 +216,23 @@ namespace relational // Add foreign keys. // instance<add_foreign_key> fk (format_, *this); - trav_rel::names n (*fk); + trav_rel::unames n (*fk); names (t, n); } + + struct create_index: relational::create_index, context + { + create_index (base const& x): base (x) {} + + virtual string + name (sema_rel::index& in) + { + // In SQL Server indexes cannot have a schema. + // + return quote_id (in.name ().uname ()); + } + }; + entry<create_index> create_index_; } } } |