Related to issue 24682. The FK performance fix will now also work in Oracle.
authorAntonio Moreno <antonio.moreno@openbravo.com>
Thu, 05 Sep 2013 16:03:48 +0200
changeset 462 253902e2aa5a
parent 461 255f2e040a01
child 463 64ee18562b3a
Related to issue 24682. The FK performance fix will now also work in Oracle.
src/org/apache/ddlutils/platform/PlatformImplBase.java
src/org/apache/ddlutils/platform/oracle/Oracle8Platform.java
--- a/src/org/apache/ddlutils/platform/PlatformImplBase.java	Thu Sep 05 14:24:39 2013 +0200
+++ b/src/org/apache/ddlutils/platform/PlatformImplBase.java	Thu Sep 05 16:03:48 2013 +0200
@@ -2561,7 +2561,7 @@
   @Override
   public void disableDatasetFK(Connection connection, Database model, OBDataset dataset,
       boolean continueOnError) throws DatabaseOperationException {
-    disableAllFK(connection, model, continueOnError);
+    throw new DatabaseOperationException("Error: Operation not supported");
   }
 
   /**
@@ -2583,7 +2583,7 @@
   @Override
   public boolean enableDatasetFK(Connection connection, Database model, OBDataset dataset,
       boolean continueOnError) throws DatabaseOperationException {
-    return enableAllFK(connection, model, continueOnError);
+    throw new DatabaseOperationException("Error: Operation not supported");
   }
 
   /**
--- a/src/org/apache/ddlutils/platform/oracle/Oracle8Platform.java	Thu Sep 05 14:24:39 2013 +0200
+++ b/src/org/apache/ddlutils/platform/oracle/Oracle8Platform.java	Thu Sep 05 16:03:48 2013 +0200
@@ -32,7 +32,9 @@
 import org.apache.ddlutils.DatabaseOperationException;
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.model.Database;
+import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Function;
+import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.model.Trigger;
 import org.apache.ddlutils.platform.PlatformImplBase;
 import org.apache.ddlutils.platform.postgresql.PostgrePLSQLFunctionStandarization;
@@ -43,6 +45,7 @@
 import org.apache.ddlutils.translation.CommentFilter;
 import org.apache.ddlutils.translation.LiteralFilter;
 import org.apache.ddlutils.util.ExtTypes;
+import org.openbravo.ddlutils.util.OBDataset;
 
 /**
  * The platform for Oracle 8.
@@ -161,6 +164,36 @@
     }
   }
 
+  @Override
+  public void disableDatasetFK(Connection connection, Database model, OBDataset dataset,
+      boolean continueOnError) throws DatabaseOperationException {
+
+    try {
+      StringWriter buffer = new StringWriter();
+      getSqlBuilder().setWriter(buffer);
+      for (int i = 0; i < model.getTableCount(); i++) {
+        Table table = model.getTable(i);
+        if (dataset.getTable(table.getName()) != null) {
+          for (int j = 0; j < table.getForeignKeyCount(); j++) {
+            ForeignKey fk = table.getForeignKey(j);
+            getSqlBuilder().writeExternalForeignKeyDropStmt(table, fk);
+          }
+        } else {
+          for (int j = 0; j < table.getForeignKeyCount(); j++) {
+            ForeignKey fk = table.getForeignKey(j);
+            if (dataset.getTable(fk.getForeignTableName()) != null) {
+              getSqlBuilder().writeExternalForeignKeyDropStmt(table, fk);
+            }
+          }
+        }
+      }
+      evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw new DatabaseOperationException("Error while disabling foreign key ", e);
+    }
+  }
+
   /**
    * {@inheritDoc}
    */
@@ -192,6 +225,40 @@
   /**
    * {@inheritDoc}
    */
+  @Override
+  public boolean enableDatasetFK(Connection connection, Database model, OBDataset dataset,
+      boolean continueOnError) throws DatabaseOperationException {
+
+    try {
+      StringWriter buffer = new StringWriter();
+      getSqlBuilder().setWriter(buffer);
+      for (int i = 0; i < model.getTableCount(); i++) {
+        Table table = model.getTable(i);
+        if (dataset.getTable(table.getName()) != null) {
+          getSqlBuilder().createExternalForeignKeys(model, table);
+        } else {
+          for (int j = 0; j < table.getForeignKeyCount(); j++) {
+            ForeignKey fk = table.getForeignKey(j);
+            if (dataset.getTable(fk.getForeignTableName()) != null) {
+              getSqlBuilder().writeExternalForeignKeyCreateStmt(model, table, fk);
+            }
+          }
+        }
+      }
+      int numErrors = evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
+      if (numErrors > 0) {
+        return false;
+      }
+      return true;
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw new DatabaseOperationException("Error while enabling foreign key ", e);
+    }
+  }
+
+  /**
+   * {@inheritDoc}
+   */
   public void disableAllTriggers(Connection connection, Database model, boolean continueOnError)
       throws DatabaseOperationException {