From 7f6559f8f199f1135adf774cb9ce10a7ed88ede6 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Wed, 3 Aug 2016 03:52:07 +0000 Subject: [PATCH] add some utility methods for creating relationship indexes git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1004 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/impl/RelationshipCreator.java | 60 +++++++++++++++++++ .../jackcess/impl/TableUpdater.java | 53 +++++++++------- 2 files changed, 93 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java index 988564e..4241c99 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java @@ -18,6 +18,9 @@ package com.healthmarketscience.jackcess.impl; import java.io.IOException; import java.util.List; +import java.util.Set; + +import com.healthmarketscience.jackcess.IndexBuilder; import com.healthmarketscience.jackcess.RelationshipBuilder; /** @@ -99,4 +102,61 @@ public class RelationshipCreator extends DBMutator // FIXME } + + private IndexBuilder createPrimaryIndex() { + String name = getUniqueIndexName(_primaryTable); + // FIXME? + return createIndex(name, _primaryCols).setUnique(); + } + + private IndexBuilder createSecondaryIndex() { + String name = getUniqueIndexName(_secondaryTable); + // FIXME? + + return createIndex(name, _primaryCols); + } + + private static IndexBuilder createIndex(String name, List cols) { + IndexBuilder idx = new IndexBuilder(name); + for(ColumnImpl col : cols) { + idx.addColumns(col.getName()); + } + return idx; + } + + private String getUniqueIndexName(TableImpl table) { + Set idxNames = TableUpdater.getIndexNames(table, null); + + boolean isPrimary = (table == _primaryTable); + String baseName = null; + String suffix = null; + if(isPrimary) { + // primary naming scheme: ".rC", ".rD", "rE" ... + baseName = ".r"; + suffix = "C"; + } else { + // secondary naming scheme: "", "1", "2" + baseName = _primaryTable.getName() + _secondaryTable.getName(); + suffix = ""; + } + + int count = 0; + while(true) { + String idxName = baseName + suffix; + if(!idxNames.contains(idxName.toUpperCase())) { + return idxName; + } + + ++count; + if(isPrimary) { + char c = (char)(suffix.charAt(0) + 1); + if(c == '[') { + c = 'a'; + } + suffix = "" + c; + } else { + suffix = "" + count; + } + } + } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java b/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java index 7ed52f5..ecaa10e 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java @@ -144,8 +144,8 @@ public class TableUpdater extends TableMutator int indexNumber = _table.getLogicalIndexCount(); _index.setIndexNumber(indexNumber); - // find backing index state - findIndexDataState(); + // initialize backing index state + initIndexDataState(); getPageChannel().startExclusiveWrite(); try { @@ -208,7 +208,7 @@ public class TableUpdater extends TableMutator } boolean foundPk[] = new boolean[1]; - Set idxNames = getIndexNames(foundPk); + Set idxNames = getIndexNames(_table, foundPk); // next, validate the index definition validateIndex(getColumnNames(), idxNames, foundPk, _index); } @@ -221,39 +221,50 @@ public class TableUpdater extends TableMutator return colNames; } - private Set getIndexNames(boolean[] foundPk) { + static Set getIndexNames(TableImpl table, boolean[] foundPk) { Set idxNames = new HashSet(); - for(IndexImpl index : _table.getIndexes()) { + for(IndexImpl index : table.getIndexes()) { idxNames.add(index.getName().toUpperCase()); - if(index.isPrimaryKey()) { + if(index.isPrimaryKey() && (foundPk != null)) { foundPk[0] = true; } } return idxNames; } - private void findIndexDataState() { + private void initIndexDataState() { _idxDataState = new IndexDataState(); _idxDataState.addIndex(_index); // search for an existing index which matches the given index (in terms of // the backing data) - for(IndexData idxData : _table.getIndexDatas()) { - if(sameIndexData(_index, idxData)) { - _idxDataState.setIndexDataNumber(idxData.getIndexDataNumber()); - return; - } - } + IndexData idxData = findIndexData(_index, _table, (byte)0, (byte)0); - // no matches found, need new index data state - _idxDataState.setIndexDataNumber(_table.getIndexCount()); + int idxDataNumber = ((idxData != null) ? + idxData.getIndexDataNumber() : + _table.getIndexCount()); + + _idxDataState.setIndexDataNumber(idxDataNumber); } - private static boolean sameIndexData(IndexBuilder idx1, IndexData idx2) { + static IndexData findIndexData(IndexBuilder idx, TableImpl table, + byte ignoreIdxFlags, byte ignoreColFlags) + { + for(IndexData idxData : table.getIndexDatas()) { + if(sameIndexData(idx, idxData, ignoreIdxFlags, ignoreColFlags)) { + return idxData; + } + } + return null; + } + + private static boolean sameIndexData(IndexBuilder idx1, IndexData idx2, + byte ignoreIdxFlags, byte ignoreColFlags) { // index data can be combined if flags match and columns (and col flags) // match - if(idx1.getFlags() != idx2.getIndexFlags()) { + if((idx1.getFlags() | ignoreIdxFlags) != + (idx2.getIndexFlags() | ignoreIdxFlags)) { return false; } @@ -265,7 +276,7 @@ public class TableUpdater extends TableMutator IndexBuilder.Column col1 = idx1.getColumns().get(i); IndexData.ColumnDescriptor col2 = idx2.getColumns().get(i); - if(!sameIndexData(col1, col2)) { + if(!sameIndexData(col1, col2, ignoreColFlags)) { return false; } } @@ -274,8 +285,10 @@ public class TableUpdater extends TableMutator } private static boolean sameIndexData( - IndexBuilder.Column col1, IndexData.ColumnDescriptor col2) { + IndexBuilder.Column col1, IndexData.ColumnDescriptor col2, + int ignoreColFlags) { return (col1.getName().equals(col2.getName()) && - (col1.getFlags() == col2.getFlags())); + ((col1.getFlags() | ignoreColFlags) == + (col2.getFlags() | ignoreColFlags))); } } -- 2.39.5