From f808a1be9a56f7da2986842b7315a70d38942bd1 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Tue, 30 Aug 2016 13:19:01 +0000 Subject: [PATCH] fix directionality of relationship git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1017 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/RelationshipBuilder.java | 32 +++++++++---------- .../jackcess/impl/RelationshipCreator.java | 23 ++++++------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java b/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java index 795bbd9..f826a7c 100644 --- a/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java +++ b/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java @@ -39,30 +39,30 @@ public class RelationshipBuilder /** relationship flags (default to "don't enforce") */ private int _flags = RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG; - private String _toTable; private String _fromTable; - private List _toCols = new ArrayList(); + private String _toTable; private List _fromCols = new ArrayList(); + private List _toCols = new ArrayList(); - public RelationshipBuilder(String toTable, String fromTable) + public RelationshipBuilder(String fromTable, String toTable) { - _toTable = toTable; _fromTable = fromTable; + _toTable = toTable; } /** * Adds a pair of columns to the relationship. */ - public RelationshipBuilder addColumns(String toCol, String fromCol) { - _toCols.add(toCol); + public RelationshipBuilder addColumns(String fromCol, String toCol) { _fromCols.add(fromCol); + _toCols.add(toCol); return this; } /** * Enables referential integrity enforcement for this relationship. * - * Note, this requires the "to" table to have an existing unique index on + * Note, this requires the "from" table to have an existing unique index on * the relevant columns. */ public RelationshipBuilder setReferentialIntegrity() { @@ -70,7 +70,7 @@ public class RelationshipBuilder } /** - * Enables deletes to be cascaded from the "to" table to the "from" table. + * Enables deletes to be cascaded from the "from" table to the "to" table. * * Note, this requires referential integrity to be enforced. */ @@ -79,7 +79,7 @@ public class RelationshipBuilder } /** - * Enables updates to be cascaded from the "to" table to the "from" table. + * Enables updates to be cascaded from the "from" table to the "to" table. * * Note, this requires referential integrity to be enforced. */ @@ -88,7 +88,7 @@ public class RelationshipBuilder } /** - * Enables deletes in the "to" table to be cascaded as "null" to the "from" + * Enables deletes in the "from" table to be cascaded as "null" to the "to" * table. * * Note, this requires referential integrity to be enforced. @@ -126,21 +126,21 @@ public class RelationshipBuilder return _flags; } - public String getToTable() { - return _toTable; - } - public String getFromTable() { return _fromTable; } - public List getToColumns() { - return _toCols; + public String getToTable() { + return _toTable; } public List getFromColumns() { return _fromCols; } + + public List getToColumns() { + return _toCols; + } /** * Creates a new Relationship in the given Database with the currently diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java index a326700..b0659af 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java @@ -70,8 +70,8 @@ public class RelationshipCreator extends DBMutator public RelationshipImpl createRelationshipImpl(String name) { RelationshipImpl newRel = new RelationshipImpl( - name, _secondaryTable, _primaryTable, _flags, - _secondaryCols, _primaryCols); + name, _primaryTable, _secondaryTable, _flags, + _primaryCols, _secondaryCols); return newRel; } @@ -145,16 +145,16 @@ public class RelationshipCreator extends DBMutator private void validate() throws IOException { - _primaryTable = getDatabase().getTable(_relationship.getToTable()); - _secondaryTable = getDatabase().getTable(_relationship.getFromTable()); + _primaryTable = getDatabase().getTable(_relationship.getFromTable()); + _secondaryTable = getDatabase().getTable(_relationship.getToTable()); if((_primaryTable == null) || (_secondaryTable == null)) { throw new IllegalArgumentException(withErrorContext( "Two valid tables are required in relationship")); } - _primaryCols = getColumns(_primaryTable, _relationship.getToColumns()); - _secondaryCols = getColumns(_secondaryTable, _relationship.getFromColumns()); + _primaryCols = getColumns(_primaryTable, _relationship.getFromColumns()); + _secondaryCols = getColumns(_secondaryTable, _relationship.getToColumns()); if((_primaryCols == null) || (_primaryCols.isEmpty()) || (_secondaryCols == null) || (_secondaryCols.isEmpty())) { @@ -212,7 +212,7 @@ public class RelationshipCreator extends DBMutator Object[] entryValues = new Object[_secondaryCols.size()]; for(Row row : _secondaryTable.newCursor().toCursor() .newIterable().addColumns(_secondaryCols)) { - // grab the from table values + // grab the secondary table values boolean hasValues = false; for(int i = 0; i < _secondaryCols.size(); ++i) { entryValues[i] = _secondaryCols.get(i).getRowValue(row); @@ -224,6 +224,7 @@ public class RelationshipCreator extends DBMutator continue; } + // check that they exist in the primary table if(!primaryCursor.findFirstRowByEntry(entryValues)) { throw new ConstraintViolationException(withErrorContext( "Integrity constraint violation found for relationship")); @@ -343,10 +344,10 @@ public class RelationshipCreator extends DBMutator private String withErrorContext(String msg) { return msg + "(Rel=" + getTableErrorContext(_primaryTable, _primaryCols, - _relationship.getToTable(), - _relationship.getToColumns()) + " <- " + - getTableErrorContext(_secondaryTable, _secondaryCols, _relationship.getFromTable(), - _relationship.getFromColumns()) + ")"; + _relationship.getFromColumns()) + " -> " + + getTableErrorContext(_secondaryTable, _secondaryCols, + _relationship.getToTable(), + _relationship.getToColumns()) + ")"; } } -- 2.39.5