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

bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new 14d35e2521 Get table names reuses the table and view naming of 
Database, fixes #9424 (#9496)
14d35e2521 is described below

commit 14d35e2521e60f35c20224fcd36e1e536c0ef318
Author: Srini1415 <[email protected]>
AuthorDate: Mon Nov 16 03:45:36 2026 -0500

    Get table names reuses the table and view naming of Database, fixes #9424 
(#9496)
---
 .../org/apache/hop/core/database/Database.java     | 98 ++++++++++++++++------
 .../hop/core/database/ListedDatabaseObject.java    | 28 +++++++
 .../org/apache/hop/core/database/DatabaseTest.java | 45 ++++++++++
 .../transforms/gettablenames/GetTableNames.java    | 49 ++---------
 4 files changed, 153 insertions(+), 67 deletions(-)

diff --git a/core/src/main/java/org/apache/hop/core/database/Database.java 
b/core/src/main/java/org/apache/hop/core/database/Database.java
index 2059938c61..8541e3cb9a 100644
--- a/core/src/main/java/org/apache/hop/core/database/Database.java
+++ b/core/src/main/java/org/apache/hop/core/database/Database.java
@@ -38,6 +38,7 @@ import java.sql.Types;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -4220,20 +4221,22 @@ public class Database implements IVariables, 
ILoggingObject, AutoCloseable {
   public String[] getTablenames(
       String schemanamein, boolean includeSchema, Map<String, String> props)
       throws HopDatabaseException {
-    Map<String, Collection<String>> tableMap = getTableMap(schemanamein, 
props);
-    List<String> res = new ArrayList<>();
-    for (String schema : tableMap.keySet()) {
-      Collection<String> tables = tableMap.get(schema);
-      for (String table : tables) {
-        if (includeSchema) {
-          res.add(databaseMeta.getQuotedSchemaTableCombination(this, schema, 
table));
-        } else {
-          res.add(databaseMeta.getQuotedSchemaTableCombination(this, null, 
table));
-        }
-      }
-    }
-    res.sort(String.CASE_INSENSITIVE_ORDER);
-    return res.toArray(new String[0]);
+    return outputNames(getListedTables(schemanamein, includeSchema, props));
+  }
+
+  /**
+   * List the tables with the names {@link #getTablenames(String, boolean, 
Map)} returns, sorted on
+   * those names, keeping the catalog and schema of each table for lookups 
that need them.
+   *
+   * @param schemanamein The schema to list, null for all schemas (or the 
user's schema on databases
+   *     that list tables per user).
+   * @param includeSchema Put the schema in the output name.
+   * @param props Extra table properties that have to match, may be null.
+   */
+  public List<ListedDatabaseObject> getListedTables(
+      String schemanamein, boolean includeSchema, Map<String, String> props)
+      throws HopDatabaseException {
+    return listObjects(getTableObjectNames(schemanamein, props), 
includeSchema, true);
   }
 
   public Map<String, Collection<String>> getTableMap() throws 
HopDatabaseException {
@@ -4348,20 +4351,63 @@ public class Database implements IVariables, 
ILoggingObject, AutoCloseable {
   }
 
   public String[] getViews(String schemanamein, boolean includeSchema) throws 
HopDatabaseException {
-    Map<String, Collection<String>> viewMap = getViewMap(schemanamein);
-    List<String> res = new ArrayList<>();
-    for (String schema : viewMap.keySet()) {
-      Collection<String> views = viewMap.get(schema);
-      for (String view : views) {
-        if (includeSchema) {
-          res.add(databaseMeta.getQuotedSchemaTableCombination(this, schema, 
view));
-        } else {
-          res.add(view);
-        }
+    return outputNames(getListedViews(schemanamein, includeSchema));
+  }
+
+  /**
+   * List the views with the names {@link #getViews(String, boolean)} returns, 
sorted on those
+   * names, keeping the catalog and schema of each view for lookups that need 
them.
+   *
+   * @param schemanamein The schema to list, null for all schemas (or the 
user's schema on databases
+   *     that list tables per user).
+   * @param includeSchema Put the schema in the output name.
+   */
+  public List<ListedDatabaseObject> getListedViews(String schemanamein, 
boolean includeSchema)
+      throws HopDatabaseException {
+    return listObjects(getViewObjectNames(schemanamein), includeSchema, false);
+  }
+
+  /**
+   * Pair each object with its output name and sort on that name.
+   *
+   * @param quoteWithoutSchema quote the name also when the schema is left 
out, as is done for
+   *     tables but not for views.
+   */
+  private List<ListedDatabaseObject> listObjects(
+      List<DatabaseObjectName> objectNames, boolean includeSchema, boolean 
quoteWithoutSchema) {
+    List<ListedDatabaseObject> objects = new ArrayList<>();
+    for (DatabaseObjectName object : objectNames) {
+      String outputName;
+      if (includeSchema) {
+        outputName =
+            databaseMeta.getQuotedSchemaTableCombination(
+                this, object.schemaOrCatalog(), object.name());
+      } else if (quoteWithoutSchema) {
+        outputName = databaseMeta.getQuotedSchemaTableCombination(this, null, 
object.name());
+      } else {
+        outputName = object.name();
       }
+      objects.add(new ListedDatabaseObject(object, outputName));
     }
-    res.sort(String.CASE_INSENSITIVE_ORDER);
-    return res.toArray(new String[0]);
+    objects.sort(
+        Comparator.comparing(ListedDatabaseObject::outputName, 
String.CASE_INSENSITIVE_ORDER));
+    return objects;
+  }
+
+  /**
+   * The output names, with an object that is listed twice under the same 
schema (or catalog) and
+   * name counted once, as in {@link #getTableMap(String)} and {@link 
#getViewMap(String)}.
+   */
+  private static String[] outputNames(List<ListedDatabaseObject> objects) {
+    Set<DatabaseObjectName> seen = new HashSet<>();
+    List<String> names = new ArrayList<>();
+    for (ListedDatabaseObject listed : objects) {
+      DatabaseObjectName object = listed.object();
+      if (seen.add(new DatabaseObjectName(null, object.schemaOrCatalog(), 
object.name()))) {
+        names.add(listed.outputName());
+      }
+    }
+    return names.toArray(new String[0]);
   }
 
   public Map<String, Collection<String>> getViewMap() throws 
HopDatabaseException {
diff --git 
a/core/src/main/java/org/apache/hop/core/database/ListedDatabaseObject.java 
b/core/src/main/java/org/apache/hop/core/database/ListedDatabaseObject.java
new file mode 100644
index 0000000000..76c1904f29
--- /dev/null
+++ b/core/src/main/java/org/apache/hop/core/database/ListedDatabaseObject.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.hop.core.database;
+
+/**
+ * A table or view as listed by the JDBC metadata, with the name that {@link
+ * Database#getTablenames(String, boolean)} and {@link 
Database#getViews(String, boolean)} return
+ * for it.
+ *
+ * @param object the catalog, schema and unquoted name, for lookups that need 
them apart
+ * @param outputName the name to show or write, with the schema and quoting of 
the database
+ */
+public record ListedDatabaseObject(DatabaseObjectName object, String 
outputName) {}
diff --git a/core/src/test/java/org/apache/hop/core/database/DatabaseTest.java 
b/core/src/test/java/org/apache/hop/core/database/DatabaseTest.java
index 8f3f07603c..d8223d036e 100644
--- a/core/src/test/java/org/apache/hop/core/database/DatabaseTest.java
+++ b/core/src/test/java/org/apache/hop/core/database/DatabaseTest.java
@@ -743,6 +743,51 @@ class DatabaseTest {
     assertEquals(1, tableNames.length);
   }
 
+  @Test
+  void listsTablesAndViewsWithTheNamesOfGetTablenamesAndGetViews() throws 
Exception {
+    DatabaseMeta databaseMeta = mock(DatabaseMeta.class);
+    when(databaseMeta.getQuotedSchemaTableCombination(any(), any(), 
anyString()))
+        .thenAnswer(
+            invocation -> {
+              String schema = invocation.getArgument(1);
+              String name = "\"" + invocation.getArgument(2) + "\"";
+              return schema == null ? name : "\"" + schema + "\"." + name;
+            });
+    Database db = spy(new Database(log, variables, databaseMeta));
+    List<DatabaseObjectName> objects =
+        List.of(
+            new DatabaseObjectName("hop", "sales", "Orders"),
+            new DatabaseObjectName("hop", "public", "customers"),
+            new DatabaseObjectName("other", "public", "customers"));
+    doReturn(objects).when(db).getTableObjectNames(nullable(String.class), 
any());
+    doReturn(objects).when(db).getViewObjectNames(nullable(String.class));
+
+    List<ListedDatabaseObject> tables = db.getListedTables(null, false, null);
+    assertEquals(
+        List.of("\"customers\"", "\"customers\"", "\"Orders\""),
+        tables.stream().map(ListedDatabaseObject::outputName).toList());
+    assertEquals(new DatabaseObjectName("hop", "public", "customers"), 
tables.get(0).object());
+    assertEquals(new DatabaseObjectName("other", "public", "customers"), 
tables.get(1).object());
+    assertEquals(
+        List.of("\"public\".\"customers\"", "\"public\".\"customers\"", 
"\"sales\".\"Orders\""),
+        db.getListedTables(null, true, null).stream()
+            .map(ListedDatabaseObject::outputName)
+            .toList());
+    assertEquals(
+        List.of("customers", "customers", "Orders"),
+        db.getListedViews(null, 
false).stream().map(ListedDatabaseObject::outputName).toList());
+
+    // The same schema and name in two catalogs is one name in the plain lists
+    assertEquals(List.of("\"customers\"", "\"Orders\""), 
List.of(db.getTablenames(null, false)));
+    assertEquals(
+        List.of("\"public\".\"customers\"", "\"sales\".\"Orders\""),
+        List.of(db.getTablenames(null, true)));
+    assertEquals(List.of("customers", "Orders"), List.of(db.getViews(null, 
false)));
+    assertEquals(
+        List.of("\"public\".\"customers\"", "\"sales\".\"Orders\""),
+        List.of(db.getViews(null, true)));
+  }
+
   @Test
   void testCheckTableExistsNoProperty() throws Exception {
     DatabaseMeta databaseMeta = new DatabaseMeta();
diff --git 
a/plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java
 
b/plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java
index 6cfe511e42..2ce34b9321 100644
--- 
a/plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java
+++ 
b/plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java
@@ -18,14 +18,13 @@
 package org.apache.hop.pipeline.transforms.gettablenames;
 
 import com.google.common.annotations.VisibleForTesting;
-import java.util.ArrayList;
-import java.util.Comparator;
 import java.util.List;
 import java.util.Locale;
 import org.apache.hop.core.Const;
 import org.apache.hop.core.database.Database;
 import org.apache.hop.core.database.DatabaseMeta;
 import org.apache.hop.core.database.DatabaseObjectName;
+import org.apache.hop.core.database.ListedDatabaseObject;
 import org.apache.hop.core.exception.HopDatabaseException;
 import org.apache.hop.core.exception.HopException;
 import org.apache.hop.core.exception.HopTransformException;
@@ -197,14 +196,14 @@ public class GetTableNames extends 
BaseTransform<GetTableNamesMeta, GetTableName
     // Views
     if (meta.isIncludeView()) {
       try {
-        List<SchemaObject> views =
-            
sortedSchemaObjects(data.db.getViewObjectNames(data.realSchemaName), false);
+        List<ListedDatabaseObject> views =
+            data.db.getListedViews(data.realSchemaName, 
meta.isAddSchemaInOutput());
         String objectType = BaseMessages.getString(PKG, 
"GetTableNamesDialog.ObjectType.View");
         for (int i = 0; i < views.size() && !isStopped(); i++) {
           Object[] outputRowView = outputRow.clone();
           int outputIndex = data.totalpreviousfields;
 
-          SchemaObject view = views.get(i);
+          ListedDatabaseObject view = views.get(i);
           outputRowView[outputIndex++] = view.outputName();
 
           if (!Utils.isEmpty(data.realObjectTypeFieldName)) {
@@ -236,8 +235,8 @@ public class GetTableNames extends 
BaseTransform<GetTableNamesMeta, GetTableName
 
       // The catalog and schema of each table are needed for the lookups 
below: with an empty
       // schema name the list holds the tables of every schema, not only those 
of the default one.
-      List<SchemaObject> tables =
-          sortedSchemaObjects(data.db.getTableObjectNames(data.realSchemaName, 
null), true);
+      List<ListedDatabaseObject> tables =
+          data.db.getListedTables(data.realSchemaName, 
meta.isAddSchemaInOutput(), null);
 
       String objectType = BaseMessages.getString(PKG, 
"GetTableNamesDialog.ObjectType.Table");
 
@@ -246,7 +245,7 @@ public class GetTableNames extends 
BaseTransform<GetTableNamesMeta, GetTableName
 
         int outputIndex = data.totalpreviousfields;
 
-        SchemaObject table = tables.get(i);
+        ListedDatabaseObject table = tables.get(i);
         outputRowTable[outputIndex++] = table.outputName();
 
         if (!Utils.isEmpty(data.realObjectTypeFieldName)) {
@@ -267,7 +266,7 @@ public class GetTableNames extends 
BaseTransform<GetTableNamesMeta, GetTableName
     }
   }
 
-  private String getCreateTableSql(SchemaObject table) throws 
HopDatabaseException {
+  private String getCreateTableSql(ListedDatabaseObject table) throws 
HopDatabaseException {
     DatabaseObjectName object = table.object();
     String[] pkc =
         data.db.getPrimaryKeyColumnNames(object.catalog(), object.schema(), 
object.name());
@@ -300,38 +299,6 @@ public class GetTableNames extends 
BaseTransform<GetTableNamesMeta, GetTableName
     return sql;
   }
 
-  /**
-   * Keeps each table or view together with the name that goes to the output, 
sorted on that output
-   * name like {@link Database#getTablenames(String, boolean)}.
-   *
-   * @param quoteWithoutSchema quote the output name also when the schema is 
left out, as {@link
-   *     Database#getTablenames(String, boolean)} does and {@link 
Database#getViews(String,
-   *     boolean)} does not.
-   */
-  private List<SchemaObject> sortedSchemaObjects(
-      List<DatabaseObjectName> objectNames, boolean quoteWithoutSchema) {
-    DatabaseMeta databaseMeta = data.db.getDatabaseMeta();
-    List<SchemaObject> objects = new ArrayList<>();
-    for (DatabaseObjectName object : objectNames) {
-      String outputName;
-      if (meta.isAddSchemaInOutput()) {
-        outputName =
-            databaseMeta.getQuotedSchemaTableCombination(
-                data.db, object.schemaOrCatalog(), object.name());
-      } else if (quoteWithoutSchema) {
-        outputName = databaseMeta.getQuotedSchemaTableCombination(data.db, 
null, object.name());
-      } else {
-        outputName = object.name();
-      }
-      objects.add(new SchemaObject(object, outputName));
-    }
-    objects.sort(Comparator.comparing(SchemaObject::outputName, 
String.CASE_INSENSITIVE_ORDER));
-    return objects;
-  }
-
-  /** A table or view and the name written to the output. */
-  private record SchemaObject(DatabaseObjectName object, String outputName) {}
-
   private void processIncludeSchema(Object[] outputRow)
       throws HopDatabaseException, HopTransformException, HopValueException {
     // Schemas

Reply via email to