Browse Source

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
tags/jackcess-2.1.8
James Ahlborn 7 years ago
parent
commit
16fa12b67a

+ 4
- 0
src/changes/changes.xml View File

@@ -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).
</action>
<action dev="jahlborn" type="update" system="GitHubPullRequests"
issue="4">
Add option to specify relationship name, thanks to Gord Thompson.
</action>
</release>
<release version="2.1.7" date="2017-05-17">
<action dev="jahlborn" type="update">

+ 16
- 1
src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java View File

@@ -57,7 +57,8 @@ public class RelationshipBuilder
private String _fromTable;
private String _toTable;
private List<String> _fromCols = new ArrayList<String>();
private List<String> _toCols = new ArrayList<String>();
private List<String> _toCols = new ArrayList<String>();
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.

+ 7
- 4
src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java View File

@@ -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);
}

+ 5
- 0
src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java View File

@@ -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();


+ 23
- 5
src/test/java/com/healthmarketscience/jackcess/TableUpdaterTest.java View File

@@ -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());

Loading…
Cancel
Save