diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2016-09-01 12:17:25 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2016-09-01 12:17:25 +0000 |
commit | 7362dd23925c9c8ced0b5c97e28ee22890c527d2 (patch) | |
tree | 0bb042eeeaef08118ec9066d063b7caca1bda300 | |
parent | 4d05cd6a3fa74125e098bb47a4eaf8e36e4644a2 (diff) | |
download | jackcess-7362dd23925c9c8ced0b5c97e28ee22890c527d2.tar.gz jackcess-7362dd23925c9c8ced0b5c97e28ee22890c527d2.zip |
implement writing fk indexes
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1019 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java | 20 | ||||
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java | 15 |
2 files changed, 23 insertions, 12 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java index a7551fb..f73acb3 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java @@ -39,7 +39,11 @@ public class RelationshipCreator extends DBMutator RelationshipImpl.CASCADE_DELETES_FLAG | RelationshipImpl.CASCADE_UPDATES_FLAG | RelationshipImpl.CASCADE_NULL_FLAG; - + + // for the purposes of choosing a backing index for a foreign key, there are + // certain index flags that can be ignored (we don't care how they are set) + private final static byte IGNORED_INDEX_FLAGS = + IndexData.IGNORE_NULLS_INDEX_FLAG | IndexData.REQUIRED_INDEX_FLAG; private TableImpl _primaryTable; private TableImpl _secondaryTable; @@ -115,13 +119,13 @@ public class RelationshipCreator extends DBMutator private void addPrimaryIndex() throws IOException { TableUpdater updater = new TableUpdater(_primaryTable); updater.setForeignKey(createFKReference(true)); - updater.addIndex(createPrimaryIndex(), true); + updater.addIndex(createPrimaryIndex(), true, IGNORED_INDEX_FLAGS, (byte)0); } private void addSecondaryIndex() throws IOException { TableUpdater updater = new TableUpdater(_secondaryTable); updater.setForeignKey(createFKReference(false)); - updater.addIndex(createSecondaryIndex(), true); + updater.addIndex(createSecondaryIndex(), true, IGNORED_INDEX_FLAGS, (byte)0); } private IndexImpl.ForeignKeyReference createFKReference(boolean isPrimary) { @@ -131,11 +135,15 @@ public class RelationshipCreator extends DBMutator if(isPrimary) { tableType = IndexImpl.PRIMARY_TABLE_TYPE; otherTableNum = _secondaryTable.getTableDefPageNumber(); + // we create the primary index first, so the secondary index does not + // exist yet otherIdxNum = _secondaryTable.getLogicalIndexCount(); } else { tableType = IndexImpl.SECONDARY_TABLE_TYPE; otherTableNum = _primaryTable.getTableDefPageNumber(); - otherIdxNum = _primaryTable.getLogicalIndexCount(); + // at this point, we've already created the primary index, it's the last + // one on the primary table + otherIdxNum = _primaryTable.getLogicalIndexCount() - 1; } boolean cascadeUpdates = ((_flags & RelationshipImpl.CASCADE_UPDATES_FLAG) != 0); boolean cascadeDeletes = ((_flags & RelationshipImpl.CASCADE_DELETES_FLAG) != 0); @@ -267,9 +275,9 @@ public class RelationshipCreator extends DBMutator String baseName = null; String suffix = null; if(isPrimary) { - // primary naming scheme: ".rC", ".rD", "rE" ... + // primary naming scheme: ".rB", .rC", ".rD", "rE" ... baseName = ".r"; - suffix = "C"; + suffix = "B"; } else { // secondary naming scheme: "<t1><t2>", "<t1><t2>1", "<t1><t2>2" baseName = _primaryTable.getName() + _secondaryTable.getName(); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java b/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java index 43896be..de208c9 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java @@ -145,11 +145,13 @@ public class TableUpdater extends TableMutator } public IndexImpl addIndex(IndexBuilder index) throws IOException { - return addIndex(index, false); + return addIndex(index, false, (byte)0, (byte)0); } - IndexImpl addIndex(IndexBuilder index, boolean isInternal) throws IOException { - + IndexImpl addIndex(IndexBuilder index, boolean isInternal, byte ignoreIdxFlags, + byte ignoreColFlags) + throws IOException + { _index = index; if(!isInternal) { @@ -161,7 +163,7 @@ public class TableUpdater extends TableMutator _index.setIndexNumber(indexNumber); // initialize backing index state - initIndexDataState(); + initIndexDataState(ignoreIdxFlags, ignoreColFlags); if(!isInternal) { getPageChannel().startExclusiveWrite(); @@ -254,14 +256,15 @@ public class TableUpdater extends TableMutator return idxNames; } - private void initIndexDataState() { + private void initIndexDataState(byte ignoreIdxFlags, byte ignoreColFlags) { _idxDataState = new IndexDataState(); _idxDataState.addIndex(_index); // search for an existing index which matches the given index (in terms of // the backing data) - IndexData idxData = findIndexData(_index, _table, (byte)0, (byte)0); + IndexData idxData = findIndexData( + _index, _table, ignoreIdxFlags, ignoreColFlags); int idxDataNumber = ((idxData != null) ? idxData.getIndexDataNumber() : |