From fceec0358a905c36b75ee4b27197d4b270394089 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Sat, 27 Aug 2016 20:53:12 +0000 Subject: [PATCH] add some validation for integrity enforced relationships git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1013 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/impl/DatabaseImpl.java | 2 + .../jackcess/impl/RelationshipCreator.java | 50 ++++++++++++------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java index 5bfac3c..5426f18 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java @@ -1244,6 +1244,8 @@ public class DatabaseImpl implements Database name = baseName + (++i); } + // FIXME, truncate to max identifier length + return ((i == 0) ? origName : (origName + i)); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java index c137f7f..550d498 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java @@ -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(getColumnNames(_primaryCols)).size() != + _primaryCols.size()) || + (new HashSet(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 cols) { @@ -219,6 +231,8 @@ public class RelationshipCreator extends DBMutator suffix = "" + count; } } + + // FIXME, truncate to max index name length } private static List getColumns(TableImpl table, List 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 cols, String tableName, Collection colNames) { -- 2.39.5