Repository: cayenne
Updated Branches:
  refs/heads/master bcd834171 -> 0349c974f


CAY-2432 DbImport: No relationship is created for table with Include Columns


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/0349c974
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/0349c974
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/0349c974

Branch: refs/heads/master
Commit: 0349c974fccce2d18dcb3a865260e67c6c2a376f
Parents: bcd8341
Author: Nikita Timofeev <stari...@gmail.com>
Authored: Thu May 17 17:13:57 2018 +0300
Committer: Nikita Timofeev <stari...@gmail.com>
Committed: Thu May 17 17:13:57 2018 +0300

----------------------------------------------------------------------
 .../dbsync/merge/DbRelationshipDictionary.java  |  3 +-
 .../reverse/dbload/RelationshipLoader.java      | 32 ++++++++++++++----
 .../reverse/filters/FiltersConfigBuilder.java   |  8 +++--
 .../reverse/filters/IncludeTableFilter.java     | 22 ++++++++++++-
 .../dbsync/reverse/filters/PatternFilter.java   | 14 +++-----
 .../dbsync/reverse/filters/TableFilter.java     | 34 +++++++++++++-------
 6 files changed, 81 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/0349c974/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipDictionary.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipDictionary.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipDictionary.java
index 80424db..ae014d2 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipDictionary.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipDictionary.java
@@ -62,7 +62,7 @@ class DbRelationshipDictionary extends 
MergerDictionary<DbRelationship> {
         Collection<DbRelationship> existingFiltered = new LinkedList<>();
         TableFilter tableFilter = 
filtersConfig.tableFilter(container.getCatalog(), container.getSchema());
         if(tableFilter != null && 
tableFilter.isIncludeTable(container.getName())){
-            PatternFilter patternFilter = 
tableFilter.getIncludeTableColumnFilter(container.getName());
+            PatternFilter patternFilter = 
tableFilter.getIncludeTableRelationshipFilter(container.getName());
             for(DbRelationship rel : container.getRelationships()){
                 if(patternFilter.isIncluded(rel.getName())){
                     existingFiltered.add(rel);
@@ -71,6 +71,7 @@ class DbRelationshipDictionary extends 
MergerDictionary<DbRelationship> {
         }
         return existingFiltered;
     }
+
     /**
      * Signature of DbRelationship is sorted strings generated from its DbJoins
      */

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0349c974/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
index d51db9d..73a0277 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
@@ -26,6 +26,7 @@ import java.util.Set;
 
 import org.apache.cayenne.dbsync.naming.NameBuilder;
 import org.apache.cayenne.dbsync.naming.ObjectNameGenerator;
+import org.apache.cayenne.dbsync.reverse.filters.TableFilter;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbJoin;
@@ -125,11 +126,31 @@ public class RelationshipLoader extends AbstractLoader {
     }
 
     private void checkAndAddRelationship(DbEntity entity, DbRelationship 
relationship){
-        boolean isIncluded = config.getFiltersConfig()
-                .tableFilter(entity.getCatalog(), entity.getSchema())
-                .getIncludeTableColumnFilter(entity.getName())
-                .isIncluded(relationship.getName());
-        if (isIncluded && delegate.dbRelationshipLoaded(entity, relationship)) 
{
+        TableFilter tableFilter = config.getFiltersConfig()
+                .tableFilter(entity.getCatalog(), entity.getSchema());
+
+        // check that relationship can be included
+        if(!tableFilter.getIncludeTableRelationshipFilter(entity.getName())
+                .isIncluded(relationship.getName())) {
+            return;
+        }
+
+        // this can be because of filtered out columns, so next check can be 
excessive,
+        // but still better to check everything here too, so we can assert 
that added relationship is valid.
+        if(relationship.getJoins().isEmpty()) {
+            return;
+        }
+
+        // check that all join attributes are included
+        for(DbJoin join : relationship.getJoins()) {
+            
if(!tableFilter.getIncludeTableColumnFilter(entity.getName()).isIncluded(join.getSourceName())
 ||
+                    
!tableFilter.getIncludeTableColumnFilter(relationship.getTargetEntityName()).isIncluded(join.getTargetName()))
 {
+                return;
+            }
+        }
+
+        // add relationship if delegate permit it
+        if (delegate.dbRelationshipLoaded(entity, relationship)) {
             entity.addRelationship(relationship);
         }
     }
@@ -172,7 +193,6 @@ public class RelationshipLoader extends AbstractLoader {
 
             addJoin(forwardRelationship, pkName, fkName);
             addJoin(reverseRelationship, fkName, pkName);
-
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0349c974/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java
index dbc2111..8756528 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java
@@ -31,6 +31,7 @@ import 
org.apache.cayenne.dbsync.reverse.dbimport.ReverseEngineering;
 import org.apache.cayenne.dbsync.reverse.dbimport.Schema;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.regex.Pattern;
@@ -88,9 +89,10 @@ public final class FiltersConfigBuilder {
     private SortedSet<IncludeTableFilter> 
transformIncludeTable(Collection<IncludeTable> includeTables) {
         SortedSet<IncludeTableFilter> includeTableFilters = new TreeSet<>();
         for (IncludeTable includeTable : includeTables) {
-            includeTableFilters.add(new 
IncludeTableFilter(includeTable.getPattern(),
-                    transform(includeTable.getIncludeColumns(), 
includeTable.getExcludeColumns(), includeTable.getExcludeRelationship())));
-
+            includeTableFilters.add(new 
IncludeTableFilter(includeTable.getPattern()
+                    , transform(includeTable.getIncludeColumns(), 
includeTable.getExcludeColumns())
+                    , transform(Collections.emptyList(), 
includeTable.getExcludeRelationship())
+            ));
         }
 
         return includeTableFilters;

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0349c974/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java
index f431b3b..b229088 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java
@@ -28,19 +28,39 @@ public class IncludeTableFilter implements 
Comparable<IncludeTableFilter> {
 
     public final PatternFilter columnsFilter;
 
+    /**
+     * @since 4.1
+     */
+    public final PatternFilter relationshipFilter;
+
     public IncludeTableFilter(String pattern) {
-        this(pattern, PatternFilter.INCLUDE_EVERYTHING);
+        this(pattern, PatternFilter.INCLUDE_EVERYTHING, 
PatternFilter.INCLUDE_EVERYTHING);
     }
 
     public IncludeTableFilter(String pattern, PatternFilter columnsFilter) {
+        this(pattern, columnsFilter, PatternFilter.INCLUDE_EVERYTHING);
+    }
+
+    /**
+     * @since 4.1
+     */
+    public IncludeTableFilter(String pattern, PatternFilter columnsFilter, 
PatternFilter relationshipFilter) {
         this.pattern = PatternFilter.pattern(pattern);
         this.columnsFilter = columnsFilter;
+        this.relationshipFilter = relationshipFilter;
     }
 
     public boolean isIncludeColumn (String name) {
         return columnsFilter.isIncluded(name);
     }
 
+    /**
+     * @since 4.1
+     */
+    public boolean isIncludeRelationship (String name) {
+        return relationshipFilter.isIncluded(name);
+    }
+
     @Override
     public int compareTo(IncludeTableFilter o) {
         if (pattern == null && o.pattern == null) {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0349c974/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java
index 54eb641..c3d723f 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java
@@ -55,15 +55,11 @@ public class PatternFilter {
         }
     };
 
-    public static final Comparator<Pattern> PATTERN_COMPARATOR = new 
Comparator<Pattern>() {
-        @Override
-        public int compare(Pattern o1, Pattern o2) {
-            if (o1 != null && o2 != null) {
-                return o1.pattern().compareTo(o2.pattern());
-            }
-            else {
-                return -1;
-            }
+    public static final Comparator<Pattern> PATTERN_COMPARATOR = (o1, o2) -> {
+        if (o1 != null && o2 != null) {
+            return o1.pattern().compareTo(o2.pattern());
+        } else {
+            return -1;
         }
     };
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/0349c974/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/TableFilter.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/TableFilter.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/TableFilter.java
index ecf6dc3..ac7a4e7 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/TableFilter.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/TableFilter.java
@@ -35,9 +35,6 @@ public class TableFilter {
 
     /**
      * Includes can contain only one include table
-     *
-     * @param includes
-     * @param excludes
      */
     public TableFilter(SortedSet<IncludeTableFilter> includes, 
SortedSet<Pattern> excludes) {
         if (includes.isEmpty()) {
@@ -56,11 +53,25 @@ public class TableFilter {
 
     /**
      * Return filter for columns in case we should take this table
-     *
-     * @param tableName
-     * @return
      */
     public PatternFilter getIncludeTableColumnFilter(String tableName) {
+        IncludeTableFilter include = getIncludeTableFilter(tableName);
+        if (include == null) return null;
+
+        return include.columnsFilter;
+    }
+
+    /**
+     * @since 4.1
+     */
+    public PatternFilter getIncludeTableRelationshipFilter(String tableName) {
+        IncludeTableFilter include = getIncludeTableFilter(tableName);
+        if (include == null) return null;
+
+        return include.relationshipFilter;
+    }
+
+    private IncludeTableFilter getIncludeTableFilter(String tableName) {
         IncludeTableFilter include = null;
         for (IncludeTableFilter p : includes) {
             if (p.pattern == null || p.pattern.matcher(tableName).matches()) {
@@ -80,8 +91,7 @@ public class TableFilter {
                 }
             }
         }
-
-        return include.columnsFilter;
+        return include;
     }
 
     public SortedSet<IncludeTableFilter> getIncludes() {
@@ -89,17 +99,17 @@ public class TableFilter {
     }
 
     public static TableFilter include(String tablePattern) {
-        TreeSet<IncludeTableFilter> includes = new 
TreeSet<IncludeTableFilter>();
+        TreeSet<IncludeTableFilter> includes = new TreeSet<>();
         includes.add(new IncludeTableFilter(tablePattern == null ? null : 
tablePattern.replaceAll("%", ".*")));
 
-        return new TableFilter(includes, new TreeSet<Pattern>());
+        return new TableFilter(includes, new TreeSet<>());
     }
 
     public static TableFilter everything() {
-        TreeSet<IncludeTableFilter> includes = new 
TreeSet<IncludeTableFilter>();
+        TreeSet<IncludeTableFilter> includes = new TreeSet<>();
         includes.add(new IncludeTableFilter(null));
 
-        return new TableFilter(includes, new TreeSet<Pattern>());
+        return new TableFilter(includes, new TreeSet<>());
     }
 
     protected StringBuilder toString(StringBuilder res, String prefix) {

Reply via email to