diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-11 14:41:28 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-11 14:41:28 +0200 |
commit | f5d28c5526c78c1884ef8a0c66679d43fc5f3878 (patch) | |
tree | 4fe8e52b6d896455401364d5f46c7c4a9306ec6d /odb/relational/oracle | |
parent | 6cea23266b1c309af6d90e2b0a39fc0778a9acc8 (diff) |
Drop Oracle trigger and sequence only if we have auto primary key
Diffstat (limited to 'odb/relational/oracle')
-rw-r--r-- | odb/relational/oracle/schema.cxx | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/odb/relational/oracle/schema.cxx b/odb/relational/oracle/schema.cxx index fd25ef7..e882a4a 100644 --- a/odb/relational/oracle/schema.cxx +++ b/odb/relational/oracle/schema.cxx @@ -93,12 +93,18 @@ namespace relational drop_table (base const& x): base (x) {} virtual void - drop (sema_rel::qname const& table) + traverse (sema_rel::table& t) { + if (pass_ != 1) + return; + + qname const& table (t.name ()); + // Oracle has no IF EXISTS conditional for dropping objects. The // PL/SQL approach below seems to be the least error-prone and the // most widely used of the alternatives. // + pre_statement (); os << "BEGIN" << endl << " BEGIN" << endl << " EXECUTE IMMEDIATE 'DROP TABLE " << quote_id (table) << @@ -106,22 +112,37 @@ namespace relational << " EXCEPTION" << endl << " WHEN OTHERS THEN" << endl << " IF SQLCODE != -942 THEN RAISE; END IF;" << endl - << " END;" << endl - << " BEGIN" << endl - << " EXECUTE IMMEDIATE 'DROP SEQUENCE " << - quote_id (table + "_seq") << "';" << endl - << " EXCEPTION" << endl - << " WHEN OTHERS THEN" << endl - << " IF SQLCODE != -2289 THEN RAISE; END IF;" << endl - << " END;" << endl - << " BEGIN" << endl - << " EXECUTE IMMEDIATE 'DROP TRIGGER " << - quote_id (table + "_trg") << "';" << endl - << " EXCEPTION" << endl - << " WHEN OTHERS THEN" << endl - << " IF SQLCODE != -4080 THEN RAISE; END IF;" << endl - << " END;" << endl - << "END;" << endl; + << " END;" << endl; + + // Drop the sequence and trigger if we have auto primary key. + // + using sema_rel::primary_key; + + sema_rel::table::names_iterator i (t.find ("")); // Special name. + primary_key* pk (i != t.names_end () + ? &dynamic_cast<primary_key&> (i->nameable ()) + : 0); + + if (pk != 0 && pk->auto_ ()) + { + os << " BEGIN" << endl + << " EXECUTE IMMEDIATE 'DROP SEQUENCE " << + quote_id (table + "_seq") << "';" << endl + << " EXCEPTION" << endl + << " WHEN OTHERS THEN" << endl + << " IF SQLCODE != -2289 THEN RAISE; END IF;" << endl + << " END;" << endl + << " BEGIN" << endl + << " EXECUTE IMMEDIATE 'DROP TRIGGER " << + quote_id (table + "_trg") << "';" << endl + << " EXCEPTION" << endl + << " WHEN OTHERS THEN" << endl + << " IF SQLCODE != -4080 THEN RAISE; END IF;" << endl + << " END;" << endl; + } + + os << "END;" << endl; + post_statement (); } }; entry<drop_table> drop_table_; @@ -267,7 +288,6 @@ namespace relational using sema_rel::primary_key; sema_rel::table::names_iterator i (t.find ("")); // Special name. - primary_key* pk (i != t.names_end () ? &dynamic_cast<primary_key&> (i->nameable ()) : 0); |