]> source.dussan.org Git - jackcess.git/commitdiff
implement method for writing new relationship to db
authorJames Ahlborn <jtahlborn@yahoo.com>
Sat, 13 Aug 2016 04:14:56 +0000 (04:14 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Sat, 13 Aug 2016 04:14:56 +0000 (04:14 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1005 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java

index 26cdc5933fbc17e182d4f0aac86409286ea90d70..079f074a2f664b4eeb5e5a1dfdb5381c755b632d 100644 (file)
@@ -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
index 4241c9901fcef96fed89697b2986e85c8c750a01..273a8aed47956a85269dd40ff0f0c1a30f1e832e 100644 (file)
@@ -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 {