openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r952371 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/
Date Mon, 07 Jun 2010 19:08:49 GMT
Author: ppoddar
Date: Mon Jun  7 19:08:49 2010
New Revision: 952371

URL: http://svn.apache.org/viewvc?rev=952371&view=rev
Log:
OPENJPA-1682: Unique constraint to a column will imply the column to be non-nullable per database
basis. The commit  adds a new model in
internal schema design by making Column-Constraint relation bi-directional.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/LocalConstraint.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java Mon
Jun  7 19:08:49 2010
@@ -25,6 +25,8 @@ import java.sql.Date;
 import java.sql.Time;
 import java.sql.Timestamp;
 import java.sql.Types;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.openjpa.jdbc.identifier.DBIdentifier;
@@ -78,6 +80,7 @@ public class Column
     private String _comment = null;
     private boolean _XML = false;
     private boolean _isUni1MFK = false;
+    private Set<Constraint> _constraints = new HashSet<Constraint>();
     
     /**
      * Default constructor.
@@ -802,6 +805,9 @@ public class Column
             setXML(from.isXML());
         if (!isUni1MFK())
             setUni1MFK(from.isUni1MFK());
+        for (Constraint c : _constraints) {
+            addConstraint(c);
+        }
     }
     
     /**
@@ -883,4 +889,66 @@ public class Column
     public void setUni1MFK(boolean isUni1MFK) {
         _isUni1MFK = isUni1MFK;
     }
+    
+    /**
+     * Adds the given constraint to this column.
+     */
+    public void addConstraint(Constraint c) {
+        _constraints.add(c);
+    }
+    
+    /**
+     * Removes the given constraint from this column.
+     */
+    public void removeConstraint(Constraint c) {
+        _constraints.remove(c);
+    }
+    
+    /**
+     * Affirms if this column has any constraint of given type.
+     */
+    public boolean hasConstraint(Class<? extends Constraint> type) {
+        return !getConstraints(type).isEmpty();
+    }
+    
+    /**
+     * Gets all constrains attached this column.
+     */
+    public Set<Constraint> getConstraints() {
+        return _constraints;
+    }
+    
+    /**
+     * Gets all constrains of the given type attached to this column.
+     */
+    public <T extends Constraint> Set<T> getConstraints(Class<T> type)
{
+        Set<T> result = new HashSet<T>();
+        for (Constraint c : _constraints) {
+            if (c.getClass() == type) {
+                result.add((T)c);
+            }
+        }
+        return result;
+    }
+    
+    /**
+     * Affirms if any unique constraint is attached to this column.
+     */
+    public boolean isUniqueConstraint() {
+        return hasConstraint(Unique.class);
+    }
+    
+    /**
+     * Affirms if any index constraint is attached to this column.
+     */
+    public boolean isIndex() {
+        return hasConstraint(Index.class);
+    }
+    
+    /**
+     * Affirms if any foreign key constraint is attached to this column.
+     */
+    public boolean isForeignKey() {
+        return hasConstraint(ForeignKey.class);
+    }  
 }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ForeignKey.java
Mon Jun  7 19:08:49 2010
@@ -573,6 +573,7 @@ public class ForeignKey
         if (_joins == null)
             _joins = new LinkedHashMap();
         _joins.put(local, toPK);
+        local.addConstraint(this);
         if (_joinsPK == null)
             _joinsPK = new LinkedHashMap();
         _joinsPK.put(toPK, local);
@@ -629,6 +630,7 @@ public class ForeignKey
 
         if (_joins != null) {
             rem = _joins.remove(col);
+            col.removeConstraint(this);
             if (rem != null) {
                 _locals = null;
                 _pks = null;

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/LocalConstraint.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/LocalConstraint.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/LocalConstraint.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/LocalConstraint.java
Mon Jun  7 19:08:49 2010
@@ -67,6 +67,9 @@ public abstract class LocalConstraint
      */
     void remove() {
         // remove all columns
+        for (Column c : _cols) {
+            c.removeConstraint(this);
+        }
         setColumns(null);
         super.remove();
     }
@@ -110,6 +113,7 @@ public abstract class LocalConstraint
 
         _colList.add(col);
         _cols = null;
+        col.addConstraint(this);
     }
 
     /**
@@ -123,6 +127,7 @@ public abstract class LocalConstraint
             return false;
         if (_colList.remove(col)) {
             _cols = null;
+            col.removeConstraint(this);
             return true;
         }
         return false;

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java Mon
Jun  7 19:08:49 2010
@@ -52,6 +52,17 @@ public class Unique
     public boolean isLogical() {
         return false;
     }
+    
+    /**
+     * Adds the given column. 
+     * The added column is set to non-nullable because a unique constraint
+     * on the database requires that its constituent columns are NOT NULL. 
+     * @see Column#setNotNull(boolean)
+     */
+    public void addColumn(Column col) {
+        super.addColumn(col);
+        col.setNotNull(true);
+    }
         
     /**
      * Set the name of the constraint. This method cannot be called if the

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
Mon Jun  7 19:08:49 2010
@@ -77,12 +77,9 @@ public class DB2Dictionary
     protected static final String withCSClause = "WITH CS";
     protected static final String withRSClause = "WITH RS";
     protected static final String withRRClause = "WITH RR";
-    protected static final String useKeepShareLockClause
-        = "USE AND KEEP SHARE LOCKS";
-    protected static final String useKeepUpdateLockClause
-        = "USE AND KEEP UPDATE LOCKS";
-    protected static final String useKeepExclusiveLockClause
-        = "USE AND KEEP EXCLUSIVE LOCKS";
+    protected static final String useKeepShareLockClause     = "USE AND KEEP SHARE LOCKS";
+    protected static final String useKeepUpdateLockClause    = "USE AND KEEP UPDATE LOCKS";
+    protected static final String useKeepExclusiveLockClause = "USE AND KEEP EXCLUSIVE LOCKS";
     protected static final String forReadOnlyClause = "FOR READ ONLY";
     protected String databaseProductName = "";
     protected String databaseProductVersion = "";
@@ -93,14 +90,12 @@ public class DB2Dictionary
     
     public DB2Dictionary() {
         platform = "DB2";
-        validationSQL = "SELECT DISTINCT(CURRENT TIMESTAMP) FROM "
-            + "SYSIBM.SYSTABLES";
+        validationSQL = "SELECT DISTINCT(CURRENT TIMESTAMP) FROM SYSIBM.SYSTABLES";
         supportsSelectEndIndex = true;
 
         nextSequenceQuery = "VALUES NEXTVAL FOR {0}";
 
-        sequenceSQL = "SELECT SEQSCHEMA AS SEQUENCE_SCHEMA, "
-            + "SEQNAME AS SEQUENCE_NAME FROM SYSCAT.SEQUENCES";
+        sequenceSQL = "SELECT SEQSCHEMA AS SEQUENCE_SCHEMA, SEQNAME AS SEQUENCE_NAME FROM
SYSCAT.SEQUENCES";
         sequenceSchemaSQL = "SEQSCHEMA = ?";
         sequenceNameSQL = "SEQNAME = ?";
         characterColumnSize = 254;
@@ -125,6 +120,7 @@ public class DB2Dictionary
         supportsDefaultDeleteAction = false;
         supportsAlterTableWithDropColumn = false;
         supportsLockingWithOrderClause = true;
+        supportsNullUniqueColumn = false;
 
         supportsNullTableForGetColumns = false;
         requiresCastForMathFunctions = true;

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Mon Jun  7 19:08:49 2010
@@ -196,6 +196,7 @@ public class DBDictionary
     public boolean supportsRestrictDeleteAction = true;
     public boolean supportsCascadeDeleteAction = true;
     public boolean supportsNullDeleteAction = true;
+    public boolean supportsNullUniqueColumn = true;
     public boolean supportsDefaultDeleteAction = true;
     public boolean supportsRestrictUpdateAction = true;
     public boolean supportsCascadeUpdateAction = true;
@@ -1262,8 +1263,12 @@ public class DBDictionary
                 if (col != null && (col.getType() == Types.CLOB
                     || col.getType() == Types.LONGVARCHAR))
                     setClobString(stmnt, idx, (String) val, col);
-                else
-                    setString(stmnt, idx, (String) val, col);
+                else {
+                    if (val instanceof String)
+                        setString(stmnt, idx, (String) val, col);
+                    else
+                        setString(stmnt, idx, val.toString(), col);
+                }
                 break;
             case JavaTypes.OBJECT:
                 setBlobObject(stmnt, idx, val, col, store);
@@ -3542,7 +3547,7 @@ public class DBDictionary
         if (!alter) {
             if (col.getDefaultString() != null && !col.isAutoAssigned())
                 buf.append(" DEFAULT ").append(col.getDefaultString());
-            if (col.isNotNull())
+            if (col.isNotNull() || (!supportsNullUniqueColumn && col.hasConstraint(Unique.class)))
                 buf.append(" NOT NULL");
         }
         if (col.isAutoAssigned()) {

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
Mon Jun  7 19:08:49 2010
@@ -66,7 +66,8 @@ public class DerbyDictionary
         requiresCastForMathFunctions = true;
         requiresCastForComparisons = true;
         supportsSimpleCaseExpression = false;
-
+        supportsNullUniqueColumn = false;
+        
         supportsComments = true;
 
         fixedSizeTypeNameSet.addAll(Arrays.asList(new String[]{

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java Mon
Jun  7 19:08:49 2010
@@ -690,8 +690,6 @@ public class RowImpl
                 && col.getDefaultString() != null)
                 return;
         }
-        if (val == null && col.isNotNull())
-            val = JavaSQLTypes.getEmptyValue(col.getJavaType());
         flush(col, val, metaType, true);
     }
 

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/NullValues.java
Mon Jun  7 19:08:49 2010
@@ -84,6 +84,7 @@ public class NullValues {
 		setNotNullableBlob(new BlobValue());
 		setOptionalBlob(new BlobValue());
 		setNotOptionalBlob(new BlobValue());
+		setUniqueNullable("");
 	}
 	
 	public long getId() {

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java?rev=952371&r1=952370&r2=952371&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/nullity/TestBasicFieldNullity.java
Mon Jun  7 19:08:49 2010
@@ -21,8 +21,10 @@ package org.apache.openjpa.persistence.n
 import java.util.List;
 
 import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
 import javax.persistence.RollbackException;
 
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
 import org.apache.openjpa.persistence.InvalidStateException;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 
@@ -119,12 +121,18 @@ public class TestBasicFieldNullity exten
     
     
     public void testUniqueStringColumnCanBeNull() {
+        if (!isUniqueColumnNullable()) {
+            return;
+        }
         NullValues pc = new NullValues();
         pc.setUniqueNullable(null);
         assertCommitSucceeds(pc, NEW);
     }
     
     public void testUniqueStringColumnAsNull() {
+        if (!isUniqueColumnNullable()) {
+            return;
+        }
         NullValues pc = new NullValues();
         pc.setUniqueNullable(null);
         assertCommitSucceeds(pc, NEW);
@@ -155,5 +163,9 @@ public class TestBasicFieldNullity exten
             assertEquals(EMPTY_STRING, n.getUniqueNullable());
         
     }
+    
+    boolean isUniqueColumnNullable() {
+        return ((JDBCConfiguration)emf.getConfiguration()).getDBDictionaryInstance().supportsNullUniqueColumn;
+    }
 }
 



Mime
View raw message