From a6630dff5bf2002c0c133ea6c5e16d8a0a22138f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 11 Mar 2011 14:21:52 +0200 Subject: Create indexes in separate SQL statements This is more vendor-neutral. --- odb/relational/mysql/schema.cxx | 2 +- odb/relational/schema.cxx | 5 +-- odb/relational/schema.hxx | 76 +++++++++++++++++++++++++++-------------- odb/relational/source.hxx | 7 ++-- 4 files changed, 59 insertions(+), 31 deletions(-) diff --git a/odb/relational/mysql/schema.cxx b/odb/relational/mysql/schema.cxx index 37f3412..0b44c53 100644 --- a/odb/relational/mysql/schema.cxx +++ b/odb/relational/mysql/schema.cxx @@ -23,7 +23,7 @@ namespace relational struct create_common: virtual relational::create_common { virtual void - create_post () + create_table_post () { os << ")"; diff --git a/odb/relational/schema.cxx b/odb/relational/schema.cxx index 0836e03..6afdb32 100644 --- a/odb/relational/schema.cxx +++ b/odb/relational/schema.cxx @@ -36,8 +36,9 @@ namespace relational virtual void post () { - os << ';' << endl - << endl; + if (!first_) // Ignore empty statements. + os << ';' << endl + << endl; } private: diff --git a/odb/relational/schema.hxx b/odb/relational/schema.hxx index 9da6624..6c481da 100644 --- a/odb/relational/schema.hxx +++ b/odb/relational/schema.hxx @@ -50,10 +50,20 @@ namespace relational struct drop_common: virtual context { virtual void - drop (string const& table) + drop_table (string const& table) { os << "DROP TABLE IF EXISTS " << quote_id (table) << endl; } + + virtual void + drop_index (string const& /*table*/, string const& /*column*/) + { + // Most database systems drop indexes together with the table. + // + + //os << "DROP INDEX IF EXISTS " << quote_id (table + '_' + column) + // << endl; + } }; struct member_drop: object_members_base, common, virtual drop_common @@ -78,11 +88,26 @@ namespace relational if (tables_.count (name)) return; + // Drop table. + // pre_statement (); - drop (name); + drop_table (name); post_statement (); tables_.insert (name); + + // Drop indexes. + // + pre_statement (); + drop_index (name, column_name (m, "id", "object_id")); + post_statement (); + + if (container_kind (m.type ()) == ck_ordered && !unordered (m)) + { + pre_statement (); + drop_index (name, column_name (m, "index", "index")); + post_statement (); + } } protected: @@ -120,7 +145,7 @@ namespace relational return; pre_statement (); - drop (name); + drop_table (name); post_statement (); tables_.insert (name); @@ -187,25 +212,26 @@ namespace relational struct create_common: virtual context { virtual void - create_pre (string const& table) + create_table_pre (string const& table) { os << "CREATE TABLE " << quote_id (table) << " (" << endl; } virtual void - index (string const& column) + create_table_post () { - os << "INDEX (" << quote_id (column) << ")"; + os << ")" << endl; } virtual void - create_post () + create_index (string const& table, string const& column) { - os << ")" << endl; + os << "CREATE INDEX " << quote_id (table + '_' + column) << endl + << " ON " << quote_id (table) << " (" << quote_id (column) << ")" + << endl; } }; - struct member_create: object_members_base, common, virtual create_common { typedef member_create base; @@ -236,7 +262,7 @@ namespace relational return; pre_statement (); - create_pre (name); + create_table_pre (name); // object_id (simple value) // @@ -298,25 +324,23 @@ namespace relational } } - // object_id index - // - os << "," << endl - << " "; - index (id_name); + create_table_post (); + post_statement (); + + tables_.insert (name); - // index index + // Create indexes. // + pre_statement (); + create_index (name, id_name); + post_statement (); + if (ordered) { - os << "," << endl - << " "; - index (index_name); + pre_statement (); + create_index (name, index_name); + post_statement (); } - - create_post (); - post_statement (); - - tables_.insert (name); } protected: @@ -357,14 +381,14 @@ namespace relational return; pre_statement (); - create_pre (name); + create_table_pre (name); { instance oc; oc->traverse (c); } - create_post (); + create_table_post (); post_statement (); tables_.insert (name); diff --git a/odb/relational/source.hxx b/odb/relational/source.hxx index 37dfa27..106bf27 100644 --- a/odb/relational/source.hxx +++ b/odb/relational/source.hxx @@ -27,14 +27,16 @@ namespace relational pre () { first_ = true; - os << "db.execute ("; } virtual void line (const std::string& l) { if (first_) + { first_ = false; + os << "db.execute ("; + } else os << endl; @@ -44,7 +46,8 @@ namespace relational virtual void post () { - os << ");" << endl; + if (!first_) // Ignore empty statements. + os << ");" << endl; } private: -- cgit v1.1