hansva commented on code in PR #9375:
URL: https://github.com/apache/hop/pull/9375#discussion_r4537722429
##########
plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java:
##########
@@ -291,6 +264,75 @@ void processIncludeTable(Object[] outputRow)
}
}
+ private String getCreateTableSql(SchemaObject table) throws
HopDatabaseException {
+ // Get primary key
+ String pk = null;
+ String[] pkc = data.db.getPrimaryKeyColumnNames(table.schema(),
table.name());
Review Comment:
`getTableMap` puts the catalog (TABLE_CAT) in the schema slot when a table
has no schema, and that value is passed here as the schema, while the new
`getPrimaryKeyColumnNames(schema, table)` always passes a null catalog to
`getPrimaryKeys`. On MySQL/MariaDB (default `databaseTerm=CATALOG`) with an
empty schema in the transform and a `users` table in both `db1` (PK `id`) and
`db2` (PK `uid`), the driver ignores the schema argument and, with a null
catalog, searches every database, so both tables get a PK of `(id, uid)`. A
driver that does filter on schema gets a catalog name it can't match and finds
no PK. Keeping the catalog and schema apart (e.g. returning both from the table
listing and passing the catalog to `getPrimaryKeys`) would make this work on
catalog-based databases too.
##########
plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java:
##########
@@ -291,6 +264,75 @@ void processIncludeTable(Object[] outputRow)
}
}
+ private String getCreateTableSql(SchemaObject table) throws
HopDatabaseException {
+ // Get primary key
+ String pk = null;
+ String[] pkc = data.db.getPrimaryKeyColumnNames(table.schema(),
table.name());
+ if (pkc != null && pkc.length == 1) {
+ pk = pkc[0];
+ }
+ // return sql creation
+ // handle simple primary key (one field)
+ String sql =
+ data.db.getCreateTableStatement(
+ table.outputName(),
+ data.db.getTableFieldsMeta(table.schema(), table.name()),
+ null,
+ false,
+ pk,
+ true);
+
+ if (pkc != null) {
+ // add composite primary key (several fields in primary key)
+ int indexOfLastClosedBracket = sql.lastIndexOf(")");
+ if (indexOfLastClosedBracket > -1) {
+ sql = sql.substring(0, indexOfLastClosedBracket);
+ sql += ", PRIMARY KEY (";
+ for (int k = 0; k < pkc.length; k++) {
+ if (k > 0) {
+ sql += ", ";
+ }
+ sql += pkc[k];
+ }
+ sql += ")" + Const.CR + ")" + Const.CR + ";";
+ }
+ }
+ return sql;
+ }
+
+ /**
+ * Keeps the schema and the unquoted name of 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(
Review Comment:
This copies the naming, quoting and case-insensitive sorting of
`Database.getTablenames(String, boolean)` / `getViews(String, boolean)`,
including the different quoting for views. A later change there (preferred
schema, quoting) won't reach this transform. A `Database` method returning
(schema, name, quoted name) entries could serve both. Fine as a follow-up.
##########
plugins/transforms/gettablenames/src/test/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNamesTest.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.pipeline.transforms.gettablenames;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.hop.core.database.Database;
+import org.apache.hop.core.database.DatabaseMeta;
+import org.apache.hop.core.logging.ILoggingObject;
+import org.apache.hop.core.row.IRowMeta;
+import org.apache.hop.core.row.RowMeta;
+import org.apache.hop.core.row.value.ValueMetaString;
+import org.apache.hop.core.variables.Variables;
+import org.apache.hop.pipeline.transforms.mock.TransformMockHelper;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class GetTableNamesTest {
+ private TransformMockHelper<GetTableNamesMeta, GetTableNamesData> helper;
+ private GetTableNamesData data;
+ private Database db;
+ private GetTableNames transform;
+ private final List<Object[]> rows = new ArrayList<>();
+
+ @BeforeEach
+ void setUp() throws Exception {
+ helper =
+ new TransformMockHelper<>(
+ "Get table names", GetTableNamesMeta.class,
GetTableNamesData.class);
+ when(helper.logChannelFactory.create(any(), any(ILoggingObject.class)))
+ .thenReturn(helper.iLogChannel);
+ when(helper.iTransformMeta.isIncludeTable()).thenReturn(true);
+ when(helper.iTransformMeta.isAddSchemaInOutput()).thenReturn(false);
+
+ DatabaseMeta databaseMeta = mock(DatabaseMeta.class);
+ when(databaseMeta.getQuotedSchemaTableCombination(any(), any(),
anyString()))
+ .thenAnswer(
+ invocation -> {
+ String schema = invocation.getArgument(1);
+ String table = invocation.getArgument(2);
+ return schema == null ? table : schema + "." + table;
+ });
+
+ // A real Database, so getTablenames() and getViews() build their lists
from the maps below
+ db = spy(new Database(null, new Variables(), databaseMeta));
+ doAnswer(invocation -> "a_system".equals(invocation.getArgument(0)))
+ .when(db)
+ .isSystemTable(anyString());
+ doReturn(new String[0]).when(db).getPrimaryKeyColumnNames(anyString());
+ doReturn(new String[0]).when(db).getPrimaryKeyColumnNames(any(),
anyString());
Review Comment:
Nit: this stubs the one-argument `getPrimaryKeyColumnNames(anyString())`,
which the code no longer calls, so it can go (it would fail with strict stubs).
##########
plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java:
##########
@@ -227,60 +231,29 @@ void processIncludeTable(Object[] outputRow)
if (meta.isIncludeTable()) {
// Tables
- String[] tableNames = data.db.getTablenames(data.realSchemaName,
meta.isAddSchemaInOutput());
- String[] tableNamesWithoutSchema =
data.db.getTablenames(data.realSchemaName, false);
+ // The schema of each table is needed for the lookups below: with an
empty schema name the
Review Comment:
This is true when the schema is null, but with Dynamic schema an empty value
in the input row gives `""`, and JDBC `getTables` treats `""` as 'objects
without a schema', not 'all schemas'. Normalising an empty schema to null
before calling `getTableMap` would make the comment hold in both cases.
##########
plugins/transforms/gettablenames/src/main/java/org/apache/hop/pipeline/transforms/gettablenames/GetTableNames.java:
##########
@@ -291,6 +264,75 @@ void processIncludeTable(Object[] outputRow)
}
}
+ private String getCreateTableSql(SchemaObject table) throws
HopDatabaseException {
+ // Get primary key
+ String pk = null;
+ String[] pkc = data.db.getPrimaryKeyColumnNames(table.schema(),
table.name());
+ if (pkc != null && pkc.length == 1) {
+ pk = pkc[0];
+ }
+ // return sql creation
+ // handle simple primary key (one field)
+ String sql =
+ data.db.getCreateTableStatement(
+ table.outputName(),
+ data.db.getTableFieldsMeta(table.schema(), table.name()),
+ null,
+ false,
+ pk,
+ true);
+
+ if (pkc != null) {
Review Comment:
Not new in this PR, but since this method is being touched:
`getPrimaryKeyColumnNames` never returns null, so this block always runs. A
table without a PK gets `, PRIMARY KEY ()` (invalid SQL), and a one-column PK,
already passed as `pk` above, can get a second `PRIMARY KEY` clause on dialects
that write it themselves. The new golden row for `t_0024_6` records the
`PRIMARY KEY ()` output as expected. Checking `pkc.length > 1` here (and
updating the golden rows) would fix it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]