From: James Ahlborn Date: Tue, 6 Jun 2017 02:33:51 +0000 (+0000) Subject: Add option to specify relationship name, fixes pull request #4 X-Git-Tag: jackcess-2.1.8~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=16fa12b67a699032bfb1846f5a7c0a32f91beede;p=jackcess.git Add option to specify relationship name, fixes pull request #4 git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1103 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 94c0767..d751010 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -9,6 +9,10 @@ Fix parsing of certain internal-use queries (such as those used as the data source for the fields in a form). + + Add option to specify relationship name, thanks to Gord Thompson. + diff --git a/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java b/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java index e52ea80..3ca3e85 100644 --- a/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java +++ b/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java @@ -57,7 +57,8 @@ public class RelationshipBuilder private String _fromTable; private String _toTable; private List _fromCols = new ArrayList(); - private List _toCols = new ArrayList(); + private List _toCols = new ArrayList(); + private String _name = null; public RelationshipBuilder(Table fromTable, Table toTable) { this(fromTable.getName(), toTable.getName()); @@ -142,6 +143,16 @@ public class RelationshipBuilder } return this; } + + /** + * Sets a specific name for this relationship. + * + * Default = null, meaning that the standard Access naming convention will be used. + */ + public RelationshipBuilder setName(String relationshipName) { + _name = relationshipName; + return this; + } public boolean hasReferentialIntegrity() { return !hasFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG); @@ -167,6 +178,10 @@ public class RelationshipBuilder return _toCols; } + public String getName() { + return _name; + } + /** * Creates a new Relationship in the given Database with the currently * configured attributes. diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java index 31d2635..15c79e0 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java @@ -1237,12 +1237,15 @@ public class DatabaseImpl implements Database // - the total name is limited to (max - 3) int maxIdLen = getFormat().MAX_INDEX_NAME_LENGTH; int limit = (maxIdLen / 2) - 3; - String origName = creator.getPrimaryTable().getName(); - if(origName.length() > limit) { - origName = origName.substring(0, limit); + String origName = creator.getName(); + if (origName == null) { + origName = creator.getPrimaryTable().getName(); + if(origName.length() > limit) { + origName = origName.substring(0, limit); + } + origName += creator.getSecondaryTable().getName(); } limit = maxIdLen - 3; - origName += creator.getSecondaryTable().getName(); if(origName.length() > limit) { origName = origName.substring(0, limit); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java index d8fcbbd..4408678 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java @@ -60,6 +60,10 @@ public class RelationshipCreator extends DBMutator { super(database); } + + public String getName() { + return _name; + } public TableImpl getPrimaryTable() { return _primaryTable; @@ -89,6 +93,7 @@ public class RelationshipCreator extends DBMutator throws IOException { _relationship = relationship; + _name = relationship.getName(); validate(); diff --git a/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java b/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java index 564c334..2d97d37 100644 --- a/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java @@ -44,7 +44,7 @@ public class TableUpdaterTest extends TestCase for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { Database db = create(fileFormat); - doTestUpdating(db, false, true); + doTestUpdating(db, false, true, null); db.close(); } @@ -54,7 +54,7 @@ public class TableUpdaterTest extends TestCase for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { Database db = create(fileFormat); - doTestUpdating(db, true, true); + doTestUpdating(db, true, true, null); db.close(); } @@ -64,13 +64,23 @@ public class TableUpdaterTest extends TestCase for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { Database db = create(fileFormat); - doTestUpdating(db, false, false); + doTestUpdating(db, false, false, null); db.close(); } } - private void doTestUpdating(Database db, boolean oneToOne, boolean enforce) + public void testTableUpdatingNamedRelationship() throws Exception { + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + + doTestUpdating(db, false, true, "FKnun3jvv47l9kyl74h85y8a0if"); + + db.close(); + } + } + + private void doTestUpdating(Database db, boolean oneToOne, boolean enforce, String relationshipName) throws Exception { Table t1 = new TableBuilder("TestTable") @@ -111,10 +121,18 @@ public class TableUpdaterTest extends TestCase rb.setReferentialIntegrity() .setCascadeDeletes(); } + + if (relationshipName != null) { + rb.setName(relationshipName); + } Relationship rel = rb.toRelationship(db); - assertEquals("TestTableTestTable2", rel.getName()); + if (relationshipName == null) { + assertEquals("TestTableTestTable2", rel.getName()); + } else { + assertEquals(relationshipName, rel.getName()); + } assertSame(t1, rel.getFromTable()); assertEquals(Arrays.asList(t1.getColumn("id")), rel.getFromColumns()); assertSame(t2, rel.getToTable());