diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2016-08-27 20:53:12 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2016-08-27 20:53:12 +0000 |
commit | fceec0358a905c36b75ee4b27197d4b270394089 (patch) | |
tree | 0d33ad559d6240005ee743e43357831c444ac650 /src | |
parent | 8d88e1737d30f0017cb8fe4de6856482d38067d8 (diff) | |
download | jackcess-fceec0358a905c36b75ee4b27197d4b270394089.tar.gz jackcess-fceec0358a905c36b75ee4b27197d4b270394089.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java | 2 | ||||
-rw-r--r-- | src/main/java/com/healthmarketscience/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<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) { |