Переглянути джерело

implement writing fk indexes

git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1019 f203690c-595d-4dc9-a70b-905162fa7fd2
tags/jackcess-2.1.5
James Ahlborn 7 роки тому
джерело
коміт
7362dd2392

+ 14
- 6
src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java Переглянути файл

RelationshipImpl.CASCADE_DELETES_FLAG | RelationshipImpl.CASCADE_DELETES_FLAG |
RelationshipImpl.CASCADE_UPDATES_FLAG | RelationshipImpl.CASCADE_UPDATES_FLAG |
RelationshipImpl.CASCADE_NULL_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 _primaryTable;
private TableImpl _secondaryTable; private TableImpl _secondaryTable;
private void addPrimaryIndex() throws IOException { private void addPrimaryIndex() throws IOException {
TableUpdater updater = new TableUpdater(_primaryTable); TableUpdater updater = new TableUpdater(_primaryTable);
updater.setForeignKey(createFKReference(true)); updater.setForeignKey(createFKReference(true));
updater.addIndex(createPrimaryIndex(), true);
updater.addIndex(createPrimaryIndex(), true, IGNORED_INDEX_FLAGS, (byte)0);
} }


private void addSecondaryIndex() throws IOException { private void addSecondaryIndex() throws IOException {
TableUpdater updater = new TableUpdater(_secondaryTable); TableUpdater updater = new TableUpdater(_secondaryTable);
updater.setForeignKey(createFKReference(false)); updater.setForeignKey(createFKReference(false));
updater.addIndex(createSecondaryIndex(), true);
updater.addIndex(createSecondaryIndex(), true, IGNORED_INDEX_FLAGS, (byte)0);
} }


private IndexImpl.ForeignKeyReference createFKReference(boolean isPrimary) { private IndexImpl.ForeignKeyReference createFKReference(boolean isPrimary) {
if(isPrimary) { if(isPrimary) {
tableType = IndexImpl.PRIMARY_TABLE_TYPE; tableType = IndexImpl.PRIMARY_TABLE_TYPE;
otherTableNum = _secondaryTable.getTableDefPageNumber(); otherTableNum = _secondaryTable.getTableDefPageNumber();
// we create the primary index first, so the secondary index does not
// exist yet
otherIdxNum = _secondaryTable.getLogicalIndexCount(); otherIdxNum = _secondaryTable.getLogicalIndexCount();
} else { } else {
tableType = IndexImpl.SECONDARY_TABLE_TYPE; tableType = IndexImpl.SECONDARY_TABLE_TYPE;
otherTableNum = _primaryTable.getTableDefPageNumber(); 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 cascadeUpdates = ((_flags & RelationshipImpl.CASCADE_UPDATES_FLAG) != 0);
boolean cascadeDeletes = ((_flags & RelationshipImpl.CASCADE_DELETES_FLAG) != 0); boolean cascadeDeletes = ((_flags & RelationshipImpl.CASCADE_DELETES_FLAG) != 0);
String baseName = null; String baseName = null;
String suffix = null; String suffix = null;
if(isPrimary) { if(isPrimary) {
// primary naming scheme: ".rC", ".rD", "rE" ...
// primary naming scheme: ".rB", .rC", ".rD", "rE" ...
baseName = ".r"; baseName = ".r";
suffix = "C";
suffix = "B";
} else { } else {
// secondary naming scheme: "<t1><t2>", "<t1><t2>1", "<t1><t2>2" // secondary naming scheme: "<t1><t2>", "<t1><t2>1", "<t1><t2>2"
baseName = _primaryTable.getName() + _secondaryTable.getName(); baseName = _primaryTable.getName() + _secondaryTable.getName();

+ 9
- 6
src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java Переглянути файл

} }


public IndexImpl addIndex(IndexBuilder index) throws IOException { 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; _index = index;


if(!isInternal) { if(!isInternal) {
_index.setIndexNumber(indexNumber); _index.setIndexNumber(indexNumber);


// initialize backing index state // initialize backing index state
initIndexDataState();
initIndexDataState(ignoreIdxFlags, ignoreColFlags);


if(!isInternal) { if(!isInternal) {
getPageChannel().startExclusiveWrite(); getPageChannel().startExclusiveWrite();
return idxNames; return idxNames;
} }


private void initIndexDataState() {
private void initIndexDataState(byte ignoreIdxFlags, byte ignoreColFlags) {


_idxDataState = new IndexDataState(); _idxDataState = new IndexDataState();
_idxDataState.addIndex(_index); _idxDataState.addIndex(_index);
// search for an existing index which matches the given index (in terms of // search for an existing index which matches the given index (in terms of
// the backing data) // the backing data)
IndexData idxData = findIndexData(_index, _table, (byte)0, (byte)0);
IndexData idxData = findIndexData(
_index, _table, ignoreIdxFlags, ignoreColFlags);


int idxDataNumber = ((idxData != null) ? int idxDataNumber = ((idxData != null) ?
idxData.getIndexDataNumber() : idxData.getIndexDataNumber() :

Завантаження…
Відмінити
Зберегти