]> source.dussan.org Git - jackcess.git/commitdiff
begin work on adding relationships
authorJames Ahlborn <jtahlborn@yahoo.com>
Thu, 21 Jul 2016 01:53:23 +0000 (01:53 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Thu, 21 Jul 2016 01:53:23 +0000 (01:53 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1003 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java [new file with mode: 0644]
src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java [new file with mode: 0644]

diff --git a/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java b/src/main/java/com/healthmarketscience/jackcess/RelationshipBuilder.java
new file mode 100644 (file)
index 0000000..8f7ae44
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+Copyright (c) 2016 James Ahlborn
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package com.healthmarketscience.jackcess;
+
+import java.io.IOException;
+
+import com.healthmarketscience.jackcess.impl.RelationshipImpl;
+
+/**
+ *
+ * @author James Ahlborn
+ */
+public class RelationshipBuilder 
+{
+  /** relationship flags (default to "don't enforce") */
+  private int _flags = RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG;
+
+  // - primary table must have unique index
+  // - primary table index name ".rC", ".rD"...
+  // - secondary index name "<PTable><STable>"
+  // - add <name>1, <name>2 after names to make unique (index names and
+  //   relationship names)
+
+  public RelationshipBuilder() 
+  {
+  }
+
+  public RelationshipBuilder setReferentialIntegrity() {
+    return clearFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
+  }
+
+  public RelationshipBuilder setCascadeDeletes() {
+    return setFlag(RelationshipImpl.CASCADE_DELETES_FLAG);
+  }
+  
+  public RelationshipBuilder setCascadeUpdates() {
+    return setFlag(RelationshipImpl.CASCADE_UPDATES_FLAG);
+  }
+
+  public RelationshipBuilder setCascadeNullOnDelete() {
+    return setFlag(RelationshipImpl.CASCADE_NULL_FLAG);
+  }  
+  
+  public RelationshipBuilder setJoinType(Relationship.JoinType joinType) {
+    switch(joinType) {
+    case INNER:
+      // nothing to do
+      break;
+    case LEFT_OUTER:
+      _flags |= RelationshipImpl.LEFT_OUTER_JOIN_FLAG;
+      break;
+    case RIGHT_OUTER:
+      _flags |= RelationshipImpl.RIGHT_OUTER_JOIN_FLAG;
+      break;
+    default:
+      throw new RuntimeException("unexpected join type " + joinType);
+    }
+    return this;
+  }
+
+  public boolean hasReferentialIntegrity() {
+    return !hasFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
+  }
+
+  public int getFlags() {
+    return _flags;
+  }
+
+  /**
+   * Creates a new Relationship in the given Database with the currently
+   * configured attributes.
+   */
+  public Relationship toRelationship(Database db)
+    throws IOException
+  {
+    
+
+    // FIXME writeme
+    return null;
+  }
+
+  private RelationshipBuilder setFlag(int flagMask) {
+    _flags |= flagMask;
+    return this;
+  }
+
+  private RelationshipBuilder clearFlag(int flagMask) {
+    _flags &= ~flagMask;
+    return this;
+  }
+
+  private boolean hasFlag(int flagMask) {
+    return((_flags & flagMask) != 0);
+  }
+
+}
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java
new file mode 100644 (file)
index 0000000..988564e
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+Copyright (c) 2016 James Ahlborn
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package com.healthmarketscience.jackcess.impl;
+
+import java.io.IOException;
+import java.util.List;
+import com.healthmarketscience.jackcess.RelationshipBuilder;
+
+/**
+ *
+ * @author James Ahlborn
+ */
+public class RelationshipCreator extends DBMutator
+{
+  private TableImpl _primaryTable;
+  private TableImpl _secondaryTable;
+  private RelationshipBuilder _relationship;
+  private List<ColumnImpl> _primaryCols; 
+  private List<ColumnImpl> _secondaryCols;
+  private int _flags;
+
+  public RelationshipCreator(DatabaseImpl database) 
+  {
+    super(database);
+  }
+
+  /**
+   * Creates the relationship in the database.
+   * @usage _advanced_method_
+   */
+  public RelationshipImpl createRelationship(RelationshipBuilder relationship) 
+    throws IOException 
+  {
+    _relationship = relationship;
+
+    validate();
+
+    // FIXME 
+    return null;
+  }
+
+  private void validate() throws IOException {
+
+    if((_primaryTable == null) || (_secondaryTable == null)) {
+      throw new IllegalArgumentException(
+          "Two tables are required in relationship");
+    }
+    if(_primaryTable.getDatabase() != _secondaryTable.getDatabase()) {
+      throw new IllegalArgumentException("Tables are not from same database");
+    }
+
+    if((_primaryCols == null) || (_primaryCols.isEmpty()) || 
+       (_secondaryCols == null) || (_secondaryCols.isEmpty())) {
+      throw new IllegalArgumentException("Missing columns in relationship");
+    }
+
+    if(_primaryCols.size() != _secondaryCols.size()) {
+      throw new IllegalArgumentException(
+          "Must have same number of columns on each side of relationship");
+    }
+
+    for(int i = 0; i < _primaryCols.size(); ++i) {
+      ColumnImpl pcol = _primaryCols.get(i);
+      ColumnImpl scol = _primaryCols.get(i);
+
+      if(pcol.getType() != scol.getType()) {
+        throw new IllegalArgumentException(
+            "Matched columns must have the same data type");
+      }
+    }
+
+    if(!_relationship.hasReferentialIntegrity()) {
+      return;
+    }
+
+    
+
+    // - same number cols
+    // - cols come from right tables, tables from right db
+    // - (cols can be duped in index)
+    // - cols have same data types
+    // - if enforce, require unique index on primary (auto-create?), index on secondary
+    // - advanced, check for enforce cycles?
+    // - index must be ascending
+
+    // FIXME
+  }
+}