empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1481039 - in /empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: DBDDLGenerator.java DBDatabaseDriver.java DBRelation.java oracle/DBDatabaseDriverOracle.java sqlserver/DBDatabaseDriverMSSQL.java
Date Fri, 10 May 2013 14:54:59 GMT
Author: doebele
Date: Fri May 10 14:54:59 2013
New Revision: 1481039

URL: http://svn.apache.org/r1481039
Log:
EMPIREDB-183
new method addEnableRelationStmt on DBDatabaseDriver.

Modified:
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRelation.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/DBDatabaseDriverOracle.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java?rev=1481039&r1=1481038&r2=1481039&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java Fri May
10 14:54:59 2013
@@ -202,7 +202,12 @@ public abstract class DBDDLGenerator<T e
             sql.append(" NOT NULL");
     }
     
-    // GetDDL
+    /**
+     * Appends the required DLL commands to create, drop or alter an object to the supplied
DBDQLScript.
+     * @param type operation to perform (CREATE, DROP, ALTER)
+     * @param dbo the object for which to perform the operation (DBDatabase, DBTable, DBView,
DBColumn, DBRelation) 
+     * @param script the script to which to add the DDL command(s)
+     */
     public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
     {
         // The Object's database must be attached to this driver
@@ -231,7 +236,7 @@ public abstract class DBDDLGenerator<T e
                     createTable((DBTable) dbo, script);
                     return;
                 case DROP:
-                    dropObject(((DBTable) dbo).getName(), "TABLE", script);
+                    dropObject(((DBTable) dbo).getFullName(), "TABLE", script);
                     return;
                 default:
                     throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName()
+ "." + type);
@@ -245,10 +250,10 @@ public abstract class DBDDLGenerator<T e
                     createView((DBView) dbo, script);
                     return;
                 case DROP:
-                    dropObject(((DBView) dbo).getName(), "VIEW", script);
+                    dropObject(((DBView) dbo).getFullName(), "VIEW", script);
                     return;
                 case ALTER:
-                    dropObject(((DBView) dbo).getName(), "VIEW", script);
+                    dropObject(((DBView) dbo).getFullName(), "VIEW", script);
                     createView((DBView) dbo, script);
                     return;
                 default:
@@ -263,7 +268,7 @@ public abstract class DBDDLGenerator<T e
                     createRelation((DBRelation) dbo, script);
                     return;
                 case DROP:
-                    dropObject(((DBRelation) dbo).getName(), "CONSTRAINT", script);
+                    dropObject(((DBRelation) dbo).getFullName(), "CONSTRAINT", script);
                     return;
                 default:
                     throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName()
+ "." + type);
@@ -277,7 +282,7 @@ public abstract class DBDDLGenerator<T e
                     createIndex(((DBIndex) dbo).getTable(), (DBIndex) dbo, script);
                     return;
                 case DROP:
-                    dropObject(((DBIndex) dbo).getName(), "INDEX", script);
+                    dropObject(((DBIndex) dbo).getFullName(), "INDEX", script);
                     return;
                 default:
                     throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName()
+ "." + type);
@@ -293,7 +298,7 @@ public abstract class DBDDLGenerator<T e
             throw new NotSupportedException(this, "getDDLScript() for "+dbo.getClass().getName());
         }
     }
-
+        
     /**
      * Appends the DDL-Script for creating the given database to an SQL-Script<br/>
      * This includes the generation of all tables, views and relations.

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java?rev=1481039&r1=1481038&r2=1481039&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java Fri
May 10 14:54:59 2013
@@ -831,11 +831,10 @@ public abstract class DBDatabaseDriver i
     }
     
     /**
-     * Gets a SQL command for creating, modifying or deleting objects in the database (tables,
columns, constraints, etc.)
-     * 
-     * @param type the command type 
-     * @param dbo the database object
-     * @param script the script to complete
+     * Appends the required DLL commands to create, drop or alter an object to the supplied
DBDQLScript.
+     * @param type operation to perform (CREATE, DROP, ALTER)
+     * @param dbo the object for which to perform the operation (DBDatabase, DBTable, DBView,
DBColumn, DBRelation) 
+     * @param script the script to which to add the DDL command(s)
      */
     public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
     {
@@ -843,6 +842,22 @@ public abstract class DBDatabaseDriver i
     }
     
     /**
+     * Appends a statement to enable or disable a foreign key relation.<br/
+     * The default is to drop or create the relation 
+     * Override this method to provide different behavior for your database.
+     * @param r the foreign key relation which should be enabled or disabled
+     * @param enable true to enable the relation or false to disable
+     * @param script the script to which to add the DDL command(s)
+     */
+    public void addEnableRelationStmt(DBRelation r, boolean enable, DBSQLScript script)
+    {
+        if (enable)
+            getDDLScript(DBCmdType.CREATE, r, script);
+        else
+            getDDLScript(DBCmdType.DROP, r, script);
+    }
+    
+    /**
      * @return <code>true</code> if column default values are created with dll
statements or <code>false</code> if not
      */
     public boolean isDDLColumnDefaults()

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRelation.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRelation.java?rev=1481039&r1=1481038&r2=1481039&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRelation.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRelation.java Fri May 10
14:54:59 2013
@@ -20,6 +20,9 @@ package org.apache.empire.db;
 
 import java.io.Serializable;
 
+import org.apache.empire.exceptions.InvalidArgumentException;
+import org.apache.empire.exceptions.UnexpectedReturnValueException;
+
 /**
  * This class creates a DBReferene object for a foreing key relation.
  * 
@@ -212,6 +215,32 @@ public class DBRelation extends DBObject
         setOnDeleteAction(DBCascadeAction.CASCADE_RECORDS);
     }
     
+    /**
+     * Appends the required DLL command to enable or disable a foreign key constraint to
the supplied DBDQLScript.
+     * @param enable true if the constraint should be enabled or false to disable the constraint
+     * @param the driver for which to enable or disable the relation
+     */
+    public String getEnableDisableStmt(boolean enable, DBDatabaseDriver driver)
+    {
+        if (driver==null)
+            throw new InvalidArgumentException("driver", driver);
+        // get Statement
+        DBSQLScript script = new DBSQLScript();
+        driver.addEnableRelationStmt(this, enable, script);
+        if (script.getCount()!=1)
+            throw new UnexpectedReturnValueException(script.getCount(), "driver.addEnableRelationStatement");
+        return script.getStmt(0);
+    }
+
+    /**
+     * Appends the required DLL command to enable or disable a foreign key constraint to
the supplied DBDQLScript.
+     * @param enable true if the constraint should be enabled or false to disable the constraint
+     */
+    public final String getEnableDisableStatement(boolean enable)
+    {
+        return getEnableDisableStmt(enable, getDatabase().getDriver());
+    }
+    
     @Override
     public String toString()
     {

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/DBDatabaseDriverOracle.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/DBDatabaseDriverOracle.java?rev=1481039&r1=1481038&r2=1481039&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/DBDatabaseDriverOracle.java
(original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/DBDatabaseDriverOracle.java
Fri May 10 14:54:59 2013
@@ -31,8 +31,10 @@ import org.apache.empire.db.DBDDLGenerat
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBDriverFeature;
+import org.apache.empire.db.DBExpr;
 import org.apache.empire.db.DBObject;
 import org.apache.empire.db.DBReader;
+import org.apache.empire.db.DBRelation;
 import org.apache.empire.db.DBSQLScript;
 import org.apache.empire.db.DBTable;
 import org.apache.empire.db.DBView;
@@ -355,6 +357,23 @@ public class DBDatabaseDriverOracle exte
         // forward request
         ddlGenerator.getDDLScript(type, dbo, script); 
     }
+
+    /**
+     * @see DBDatabaseDriver#addEnableRelationStmt(DBRelation, boolean, DBSQLScript)  
+     */
+    @Override
+    public void addEnableRelationStmt(DBRelation r, boolean enable, DBSQLScript script)
+    {
+        // ALTER TABLE {table.name} {ENABLE|DISABLE} CONSTRAINT {relation.name}
+        StringBuilder b = new StringBuilder();
+        b.append("ALTER TABLE ");
+        r.getForeignKeyTable().addSQL(b, DBExpr.CTX_FULLNAME);
+        b.append(enable ? " ENABLE " : " DISABLE ");
+        b.append("CONSTRAINT ");
+        b.append(r.getName());
+        // add
+        script.addStmt(b);
+    }
     
     /**
      * Checks whether the database definition matches the real database structure.

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java?rev=1481039&r1=1481038&r2=1481039&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java
(original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java
Fri May 10 14:54:59 2013
@@ -30,7 +30,9 @@ import org.apache.empire.db.DBDDLGenerat
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBDriverFeature;
+import org.apache.empire.db.DBExpr;
 import org.apache.empire.db.DBObject;
+import org.apache.empire.db.DBRelation;
 import org.apache.empire.db.DBSQLScript;
 import org.apache.empire.db.DBTable;
 import org.apache.empire.db.DBTableColumn;
@@ -433,4 +435,21 @@ public class DBDatabaseDriverMSSQL exten
         ddlGenerator.getDDLScript(type, dbo, script); 
     }
 
+    /**
+     * @see DBDatabaseDriver#addEnableRelationStmt(DBRelation, boolean, DBSQLScript)  
+     */
+    @Override
+    public void addEnableRelationStmt(DBRelation r, boolean enable, DBSQLScript script)
+    {
+        // ALTER TABLE {table.name} {CHECK|NOCHECK} CONSTRAINT {relation.name}
+        StringBuilder b = new StringBuilder();
+        b.append("ALTER TABLE ");
+        r.getForeignKeyTable().addSQL(b, DBExpr.CTX_FULLNAME);
+        b.append(enable ? " CHECK " : " NOCHECK ");
+        b.append("CONSTRAINT ");
+        b.append(r.getName());
+        // add
+        script.addStmt(b);
+    }
+
 }



Mime
View raw message