diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2016-08-13 04:14:56 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2016-08-13 04:14:56 +0000 |
commit | 05c3c85cc9d9b7048f909888af2ba2c7ce29b91e (patch) | |
tree | b9d1d4dbfcb083ebc387b7f320ae368ac95248dd /src | |
parent | 7f6559f8f199f1135adf774cb9ce10a7ed88ede6 (diff) | |
download | jackcess-05c3c85cc9d9b7048f909888af2ba2c7ce29b91e.tar.gz jackcess-05c3c85cc9d9b7048f909888af2ba2c7ce29b91e.zip |
implement method for writing new relationship to db
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1005 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java | 47 | ||||
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java | 31 |
2 files changed, 72 insertions, 6 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java index 26cdc59..079f074 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java @@ -1150,12 +1150,12 @@ public class DatabaseImpl implements Database List<Relationship> relationships = new ArrayList<Relationship>(); if(table1 != null) { - Cursor cursor = createCursorWithOptionalIndex( - _relationships, REL_COL_FROM_TABLE, table1.getName()); + Cursor cursor = createCursorWithOptionalIndex( + _relationships, REL_COL_FROM_TABLE, table1.getName()); collectRelationships(cursor, table1, table2, relationships, includeSystemTables); - cursor = createCursorWithOptionalIndex( - _relationships, REL_COL_TO_TABLE, table1.getName()); + cursor = createCursorWithOptionalIndex( + _relationships, REL_COL_TO_TABLE, table1.getName()); collectRelationships(cursor, table2, table1, relationships, includeSystemTables); } else { @@ -1166,6 +1166,45 @@ public class DatabaseImpl implements Database return relationships; } + RelationshipImpl writeRelationship(RelationshipCreator creator) + throws IOException + { + // the relationships table does not get loaded until first accessed + if(_relationships == null) { + _relationships = getRequiredSystemTable(TABLE_SYSTEM_RELATIONSHIPS); + } + + String name = creator.getPrimaryTable().getName() + + creator.getSecondaryTable().getName(); // FIXME make unique + RelationshipImpl newRel = creator.createRelationshipImpl(name); + + ColumnImpl ccol = _relationships.getColumn(REL_COL_COLUMN_COUNT); + ColumnImpl flagCol = _relationships.getColumn(REL_COL_FLAGS); + ColumnImpl icol = _relationships.getColumn(REL_COL_COLUMN_INDEX); + ColumnImpl nameCol = _relationships.getColumn(REL_COL_NAME); + ColumnImpl fromTableCol = _relationships.getColumn(REL_COL_FROM_TABLE); + ColumnImpl fromColCol = _relationships.getColumn(REL_COL_FROM_COLUMN); + ColumnImpl toTableCol = _relationships.getColumn(REL_COL_TO_TABLE); + ColumnImpl toColCol = _relationships.getColumn(REL_COL_TO_COLUMN); + + int numCols = newRel.getFromColumns().size(); + List<Object[]> rows = new ArrayList<Object[]>(numCols); + for(int i = 0; i < numCols; ++i) { + Object[] row = new Object[_relationships.getColumnCount()]; + ccol.setRowValue(row, ccol); + flagCol.setRowValue(row, newRel.getFlags()); + icol.setRowValue(row, i); + nameCol.setRowValue(row, name); + fromTableCol.setRowValue(row, newRel.getFromTable().getName()); + fromColCol.setRowValue(row, newRel.getFromColumns().get(i).getName()); + toTableCol.setRowValue(row, newRel.getToTable().getName()); + toColCol.setRowValue(row, newRel.getToColumns().get(i).getName()); + } + _relationships.addRows(rows); + + return newRel; + } + public List<Query> getQueries() throws IOException { // the queries table does not get loaded until first accessed diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java index 4241c99..273a8ae 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java @@ -41,6 +41,23 @@ public class RelationshipCreator extends DBMutator super(database); } + public TableImpl getPrimaryTable() { + return _primaryTable; + } + + public TableImpl getSecondaryTable() { + return _secondaryTable; + } + + public RelationshipImpl createRelationshipImpl(String name) { + RelationshipImpl newRel = new RelationshipImpl( + name, _primaryTable, _secondaryTable, _relationship.getFlags(), + _primaryCols.size()); + newRel.getFromColumns().addAll(_primaryCols); + newRel.getToColumns().addAll(_secondaryCols); + return newRel; + } + /** * Creates the relationship in the database. * @usage _advanced_method_ @@ -52,8 +69,18 @@ public class RelationshipCreator extends DBMutator validate(); - // FIXME - return null; + getPageChannel().startExclusiveWrite(); + try { + + RelationshipImpl newRel = getDatabase().writeRelationship(this); + + // FIXME, handle indexes + + return newRel; + + } finally { + getPageChannel().finishWrite(); + } } private void validate() throws IOException { |