This is an automated email from the ASF dual-hosted git repository.

abulatski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new 285d752  CAY-2600 Modeler DbImport: Can't retrieve schema for 
databases with no catalog support
285d752 is described below

commit 285d75237d8b0659771c2f219562b57397f19f02
Author: Arseni Bulatski <ancars...@gmail.com>
AuthorDate: Thu Aug 15 14:05:42 2019 +0300

    CAY-2600 Modeler DbImport: Can't retrieve schema for databases with no 
catalog support
---
 RELEASE-NOTES.txt                                  |   1 +
 .../editor/dbimport/DatabaseSchemaLoader.java      | 179 ++++++++++++---------
 .../modeler/editor/dbimport/DbImportTree.java      |  48 ++++--
 3 files changed, 138 insertions(+), 90 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 47cf85c..5b4c2e6 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -75,6 +75,7 @@ CAY-2591 Modeler: project becomes dirty after click on 
dbImport or cgen tab
 CAY-2594 DbImport: AutoAdapter resolving inside an import operation hangs up 
the op on Hana DB
 CAY-2595 ObjAttributes are not sorted in alphabetical ordering on save
 CAY-2596 DbImport xml config changes after dbImport plugin task execution
+CAY-2600 Modeler DbImport: Can't retrieve schema for databases with no catalog 
support
 CAY-2601 Modeler DbImport: result dialog issues
 CAY-2603 NPE reloading project in the model
 CAY-2605 Modeler: Unable to save - java.nio.file.InvalidPathException
diff --git 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DatabaseSchemaLoader.java
 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DatabaseSchemaLoader.java
index c904843..5275238 100644
--- 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DatabaseSchemaLoader.java
+++ 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DatabaseSchemaLoader.java
@@ -26,18 +26,19 @@ import java.sql.SQLException;
 import java.util.Collection;
 
 import org.apache.cayenne.dbsync.reverse.dbimport.Catalog;
+import org.apache.cayenne.dbsync.reverse.dbimport.FilterContainer;
 import org.apache.cayenne.dbsync.reverse.dbimport.IncludeColumn;
 import org.apache.cayenne.dbsync.reverse.dbimport.IncludeProcedure;
 import org.apache.cayenne.dbsync.reverse.dbimport.IncludeTable;
 import org.apache.cayenne.dbsync.reverse.dbimport.ReverseEngineering;
 import org.apache.cayenne.dbsync.reverse.dbimport.Schema;
 import org.apache.cayenne.modeler.ClassLoadingService;
+import org.apache.cayenne.modeler.dialog.db.load.DbImportTreeNode;
 import org.apache.cayenne.modeler.pref.DBConnectionInfo;
 
 public class DatabaseSchemaLoader {
 
     private static final String INCLUDE_ALL_PATTERN = "%";
-    private static final String EMPTY_DEFAULT_CATALOG = "";
     private static final int TABLE_INDEX = 3;
     private static final int SCHEMA_INDEX = 2;
     private static final int CATALOG_INDEX = 1;
@@ -56,36 +57,82 @@ public class DatabaseSchemaLoader {
                     tableTypesFromConfig :
                     new String[]{"TABLE", "VIEW", "SYSTEM TABLE",
                             "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", 
"SYNONYM"};
-            try (ResultSet rs = connection.getMetaData().getCatalogs()) {
-                String defaultCatalog = connection.getCatalog();
-                while (rs.next()) {
-                    ResultSet resultSet;
-                    if (defaultCatalog.equals(EMPTY_DEFAULT_CATALOG)) {
-                        resultSet = connection.getMetaData()
-                                .getTables(rs.getString(1), null, 
INCLUDE_ALL_PATTERN, types);
-                    } else {
-                        resultSet = connection.getMetaData()
-                                .getTables(defaultCatalog, null, 
INCLUDE_ALL_PATTERN, types);
-                    }
-                    String tableName = "";
-                    String schemaName = "";
-                    String catalogName = "";
-                    while (resultSet.next()) {
-                        tableName = resultSet.getString(TABLE_INDEX);
-                        schemaName = resultSet.getString(SCHEMA_INDEX);
-                        catalogName = resultSet.getString(CATALOG_INDEX);
-                        packTable(tableName, catalogName, schemaName, null);
-                    }
-                    packFunctions(connection);
-                }
-            }
+            processCatalogs(connection, types);
         }
         return databaseReverseEngineering;
     }
 
-    public ReverseEngineering loadColumns(DBConnectionInfo connectionInfo, 
ClassLoadingService loadingService, TreePath path) throws SQLException {
-        String catalogName = path.getPathComponent(1).toString();
-        String schemaName = null;
+    private void processCatalogs(Connection connection, String[] types) throws 
SQLException {
+        String defaultCatalog = connection.getCatalog();
+        try (ResultSet rsCatalog = connection.getMetaData().getCatalogs()) {
+            boolean hasCatalogs = false;
+            while (rsCatalog.next()) {
+                hasCatalogs = true;
+                ResultSet resultSet = connection.getMetaData()
+                        .getTables(processFilter(rsCatalog, defaultCatalog, 
CATALOG_INDEX),
+                                null,
+                                INCLUDE_ALL_PATTERN,
+                                types);
+                packTable(resultSet);
+                packFunctions(connection);
+            }
+            if(!hasCatalogs) {
+                processSchemas(connection, types);
+            }
+        }
+    }
+
+    private void processSchemas(Connection connection, String[] types) throws 
SQLException {
+        String defaultSchema = connection.getSchema();
+        try(ResultSet rsSchema = connection.getMetaData().getSchemas()) {
+            boolean hasSchemas = false;
+            while (rsSchema.next()) {
+                hasSchemas = true;
+                ResultSet resultSet = connection.getMetaData()
+                        .getTables(null,
+                                processFilter(rsSchema, defaultSchema, 
SCHEMA_INDEX),
+                                INCLUDE_ALL_PATTERN,
+                                types);
+                packTable(resultSet);
+                packFunctions(connection);
+            }
+            if(!hasSchemas) {
+                ResultSet resultSet = connection.getMetaData()
+                        .getTables(null,
+                                null,
+                                INCLUDE_ALL_PATTERN,
+                                types);
+                packTable(resultSet);
+                packTable(resultSet);
+            }
+        }
+    }
+
+    private void packTable(ResultSet resultSet) throws SQLException {
+        while (resultSet.next()) {
+            String tableName = resultSet.getString(TABLE_INDEX);
+            String schemaName = resultSet.getString(SCHEMA_INDEX);
+            String catalogName = resultSet.getString(CATALOG_INDEX);
+            packTable(tableName, catalogName, schemaName, null);
+        }
+    }
+
+    private String processFilter(ResultSet resultSet, String defaultFilter, 
int filterIndex) throws SQLException {
+        return defaultFilter.isEmpty() ?
+                resultSet.getString(filterIndex) :
+                defaultFilter;
+    }
+
+    public ReverseEngineering loadColumns(DBConnectionInfo connectionInfo,
+                                          ClassLoadingService loadingService,
+                                          TreePath path) throws SQLException {
+        Object userObject = 
((DbImportTreeNode)path.getPathComponent(1)).getUserObject();
+        String catalogName = null, schemaName = null;
+        if(userObject instanceof Catalog) {
+            catalogName = ((Catalog) userObject).getName();
+        } else if(userObject instanceof Schema) {
+            schemaName = ((Schema) userObject).getName();
+        }
         String tableName = path.getPathComponent(2).toString();
 
         try (Connection connection = 
connectionInfo.makeDataSource(loadingService).getConnection()) {
@@ -94,7 +141,6 @@ public class DatabaseSchemaLoader {
                     String column = rs.getString(4);
                     packTable(tableName, catalogName, schemaName, column);
                 }
-
             }
         }
         return databaseReverseEngineering;
@@ -143,71 +189,40 @@ public class DatabaseSchemaLoader {
         IncludeTable table = new IncludeTable();
         table.setPattern(tableName);
 
-        if ((catalogName == null) && (schemaName == null)) {
+        if (catalogName == null && schemaName == null) {
             if 
(!databaseReverseEngineering.getIncludeTables().contains(table)) {
                 databaseReverseEngineering.addIncludeTable(table);
             }
+            return;
         }
 
-        if ((catalogName != null) && (schemaName == null)) {
+        FilterContainer filterContainer;
+        if (catalogName != null && schemaName == null) {
             Catalog parentCatalog = 
getCatalogByName(databaseReverseEngineering.getCatalogs(), catalogName);
 
-            if (parentCatalog != null) {
-
-
-                if (!parentCatalog.getIncludeTables().contains(table)) {
-                    parentCatalog.addIncludeTable(table);
-                }
-            } else {
+            if(parentCatalog == null) {
                 parentCatalog = new Catalog();
                 parentCatalog.setName(catalogName);
-                if (!parentCatalog.getIncludeTables().contains(table)) {
-                    parentCatalog.addIncludeTable(table);
-                }
                 databaseReverseEngineering.addCatalog(parentCatalog);
             }
-
-            IncludeTable foundTable = 
getTableByName(parentCatalog.getIncludeTables(), tableName);
-            table = foundTable != null ? foundTable : table;
-            if (columnName != null ) {
-                IncludeColumn includeColumn = new IncludeColumn(columnName);
-                table.addIncludeColumn(includeColumn);
-            }
-        }
-        if ((catalogName == null) && (schemaName != null)) {
+            filterContainer = parentCatalog;
+        } else if (catalogName == null) {
             Schema parentSchema = 
getSchemaByName(databaseReverseEngineering.getSchemas(), schemaName);
-            if (parentSchema != null) {
-                if (!parentSchema.getIncludeTables().contains(table)) {
-                    parentSchema.addIncludeTable(table);
-                }
-            } else {
+
+            if(parentSchema == null) {
                 parentSchema = new Schema();
                 parentSchema.setName(schemaName);
-                if (!parentSchema.getIncludeTables().contains(table)) {
-                    parentSchema.addIncludeTable(table);
-                }
                 databaseReverseEngineering.addSchema(parentSchema);
             }
-        }
-        if ((catalogName != null) && (schemaName != null)) {
+            filterContainer = parentSchema;
+        } else {
             Catalog parentCatalog = 
getCatalogByName(databaseReverseEngineering.getCatalogs(), catalogName);
-
-
-
             Schema parentSchema;
             if (parentCatalog != null) {
-
                 parentSchema = getSchemaByName(parentCatalog.getSchemas(), 
schemaName);
-                if (parentSchema != null) {
-                    if (!parentSchema.getIncludeTables().contains(table)) {
-                        parentSchema.addIncludeTable(table);
-                    }
-                } else {
+                if(parentSchema == null) {
                     parentSchema = new Schema();
                     parentSchema.setName(schemaName);
-                    if (!parentSchema.getIncludeTables().contains(table)) {
-                        parentSchema.addIncludeTable(table);
-                    }
                     parentCatalog.addSchema(parentSchema);
                 }
             } else {
@@ -215,11 +230,27 @@ public class DatabaseSchemaLoader {
                 parentCatalog.setName(catalogName);
                 parentSchema = new Schema();
                 parentSchema.setName(schemaName);
-                if (!parentSchema.getIncludeTables().contains(table)) {
-                    parentSchema.addIncludeTable(table);
-                }
                 databaseReverseEngineering.addCatalog(parentCatalog);
             }
+            filterContainer = parentSchema;
+        }
+
+        addTable(filterContainer, table);
+        addColumn(filterContainer, table, columnName);
+    }
+
+    private void addTable(FilterContainer parentFilter, IncludeTable table) {
+        if (!parentFilter.getIncludeTables().contains(table)) {
+            parentFilter.addIncludeTable(table);
+        }
+    }
+
+    private void addColumn(FilterContainer filterContainer, IncludeTable 
table, String columnName) {
+        IncludeTable foundTable = 
getTableByName(filterContainer.getIncludeTables(), table.getPattern());
+        table = foundTable != null ? foundTable : table;
+        if (columnName != null ) {
+            IncludeColumn includeColumn = new IncludeColumn(columnName);
+            table.addIncludeColumn(includeColumn);
         }
     }
 
diff --git 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DbImportTree.java
 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DbImportTree.java
index bd7b815..5852ad8 100644
--- 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DbImportTree.java
+++ 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DbImportTree.java
@@ -19,6 +19,15 @@
 
 package org.apache.cayenne.modeler.editor.dbimport;
 
+import javax.swing.JTree;
+import javax.swing.event.TreeExpansionEvent;
+import javax.swing.event.TreeExpansionListener;
+import javax.swing.tree.TreeNode;
+import javax.swing.tree.TreePath;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
 import org.apache.cayenne.dbsync.reverse.dbimport.Catalog;
 import org.apache.cayenne.dbsync.reverse.dbimport.ExcludeTable;
 import org.apache.cayenne.dbsync.reverse.dbimport.FilterContainer;
@@ -32,15 +41,6 @@ import org.apache.cayenne.modeler.action.LoadDbSchemaAction;
 import org.apache.cayenne.modeler.dialog.db.load.DbImportTreeNode;
 import org.apache.cayenne.modeler.dialog.db.load.TransferableNode;
 
-import javax.swing.JTree;
-import javax.swing.event.TreeExpansionEvent;
-import javax.swing.event.TreeExpansionListener;
-import javax.swing.tree.TreeNode;
-import javax.swing.tree.TreePath;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-
 
 /**
  * @since 4.1
@@ -77,15 +77,19 @@ public class DbImportTree extends JTree {
         DbImportModel model = (DbImportModel) this.getModel();
 
         DbImportTreeNode root = (DbImportTreeNode) model.getRoot();
-        reverseEngineering.getCatalogs().forEach(newCatalog -> {
-            DbImportTreeNode catalog = findNodeInParent(root, newCatalog);
-
-            if (catalog == null) {
+        Collection<Catalog> catalogs = reverseEngineering.getCatalogs();
+        Collection<? extends FilterContainer> filterContainers = 
!catalogs.isEmpty() ?
+                catalogs :
+                reverseEngineering.getSchemas();
+        filterContainers.forEach(filterContainer -> {
+            DbImportTreeNode container = findNodeInParent(root, 
filterContainer);
+
+            if (container == null) {
                 return;
             }
 
-            newCatalog.getIncludeTables().forEach(newTable -> {
-                DbImportTreeNode table = findNodeInParent(catalog, newTable);
+            filterContainer.getIncludeTables().forEach(newTable -> {
+                DbImportTreeNode table = findNodeInParent(container, newTable);
                 if (table == null) {
                     return;
                 }
@@ -101,7 +105,7 @@ public class DbImportTree extends JTree {
     }
 
     private DbImportTreeNode findNodeInParent(DbImportTreeNode parent, Object 
object) {
-        for (int i = 0; i <= parent.getChildCount(); i++) {
+        for (int i = 0; i < parent.getChildCount(); i++) {
             DbImportTreeNode node = (DbImportTreeNode) parent.getChildAt(i);
             Object userObject = node.getUserObject();
 
@@ -117,6 +121,18 @@ public class DbImportTree extends JTree {
                 }
             }
 
+            if(object instanceof Schema) {
+                Schema schema = (Schema) object;
+                if(!(userObject instanceof Schema)) {
+                    continue;
+                }
+
+                Schema currentSchema = (Schema) userObject;
+                if(currentSchema.getName().equals(schema.getName())) {
+                    return node;
+                }
+            }
+
             if (object instanceof IncludeTable) {
                 IncludeTable table = (IncludeTable) object;
                 if (!(userObject instanceof IncludeTable)) {

Reply via email to