]> source.dussan.org Git - jackcess.git/commitdiff
add some validation for integrity enforced relationships
authorJames Ahlborn <jtahlborn@yahoo.com>
Sat, 27 Aug 2016 20:53:12 +0000 (20:53 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Sat, 27 Aug 2016 20:53:12 +0000 (20:53 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1013 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java

index 5bfac3cdb939d647a138bb7dd051ff5c5ff865ee..5426f18d526af0460ca6f415ab40f6aa68e4b36e 100644 (file)
@@ -1244,6 +1244,8 @@ public class DatabaseImpl implements Database
       name = baseName + (++i);
     }
 
+    // FIXME, truncate to max identifier length
+
     return ((i == 0) ? origName : (origName + i));
   }
   
index c137f7fe0ca6a76164993db239ae73c8827945b8..550d498a68a4868e6878af7287d0708a1762c1a0 100644 (file)
@@ -96,6 +96,8 @@ public class RelationshipCreator extends DBMutator
 
       // FIXME, handle indexes
 
+      // FIXME, enforce ref integ before adding indexes!
+
       return newRel;
 
     } finally {
@@ -149,32 +151,42 @@ public class RelationshipCreator extends DBMutator
 
     // for now, we will require the unique index on the primary table (just
     // like access does).  we could just create it auto-magically...
-    // FIXME
-    
-
-    // - same number cols
-    // - cols come from right tables, tables from right db
-    // - (cols can be duped in index)
-    // - cols have same data types
-    // - if enforce, require unique index on primary,
-    // - auto-create index on secondary
-    // - advanced, check for enforce cycles?
-    // - index must be ascending
+    IndexImpl primaryIdx = getPrimaryUniqueIndex();
+    if(primaryIdx == null) {
+      throw new IllegalArgumentException(withErrorContext(
+          "Missing unique index on primary table required to enforce integrity"));
+    }
 
+    // while relationships can have "dupe" columns, indexes (and therefore
+    // integrity enforced relationships) cannot
+    if((new HashSet<String>(getColumnNames(_primaryCols)).size() != 
+        _primaryCols.size()) ||
+       (new HashSet<String>(getColumnNames(_secondaryCols)).size() != 
+        _secondaryCols.size())) {
+      throw new IllegalArgumentException(withErrorContext(
+          "Cannot have duplicate columns in an integrity enforced relationship"));
+    }
+    
+    // check referential integrity
     // FIXME
+
+    // TODO: future, check for enforce cycles?
   }
 
   private IndexBuilder createPrimaryIndex() {
     String name = getUniqueIndexName(_primaryTable);
     // FIXME?
-    return createIndex(name, _primaryCols).setUnique();
+    return createIndex(name, _primaryCols)
+      .setUnique()
+      .setType(IndexImpl.FOREIGN_KEY_INDEX_TYPE);
   }
   
   private IndexBuilder createSecondaryIndex() {
     String name = getUniqueIndexName(_secondaryTable);
     // FIXME?
 
-    return createIndex(name, _primaryCols);
+    return createIndex(name, _primaryCols)
+      .setType(IndexImpl.FOREIGN_KEY_INDEX_TYPE);
   }
   
   private static IndexBuilder createIndex(String name, List<ColumnImpl> cols) {
@@ -219,6 +231,8 @@ public class RelationshipCreator extends DBMutator
         suffix = "" + count;
       }      
     }    
+
+    // FIXME, truncate to max index name length
   }
 
   private static List<ColumnImpl> getColumns(TableImpl table, List<String> colNames) {
@@ -240,16 +254,18 @@ public class RelationshipCreator extends DBMutator
   private boolean isOneToOne() {
     // a relationship is one to one if the two sides of the relationship have
     // unique indexes on the relevant columns
-    IndexImpl idx = _primaryTable.findIndexForColumns(
-        getColumnNames(_primaryCols), true);
-    if(idx == null) {
+    if(getPrimaryUniqueIndex() == null) {
       return false;
     }
-    idx = _secondaryTable.findIndexForColumns(
+    IndexImpl idx = _secondaryTable.findIndexForColumns(
         getColumnNames(_secondaryCols), true);
     return (idx != null);
   }
 
+  private IndexImpl getPrimaryUniqueIndex() {
+    return _primaryTable.findIndexForColumns(getColumnNames(_primaryCols), true);
+  }
+
   private static String getTableErrorContext(
       TableImpl table, List<ColumnImpl> cols,
       String tableName, Collection<String> colNames) {