bamaer commented on code in PR #9375:
URL: https://github.com/apache/hop/pull/9375#discussion_r4540169050
##########
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:
Thanks @hansva, good catch. `Database` now has `getTableObjectNames()` /
`getViewObjectNames()`, which keep TABLE_CAT and TABLE_SCHEM apart
(`getTableMap()` / `getViewMap()` are built on them, unchanged). The PK lookup
gets catalog, schema and table separately. A unit test covers `users` in `db1`
and `db2` without a schema.
##########
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:
Fixed, with one change: `pkc.length > 1` alone would drop single-column keys
on PostgreSQL, which writes `BIGSERIAL` without a key clause (MySQL and others
only mark numeric keys inline). The clause is now added for a composite key,
and for a one-column key only when the statement has none yet. Golden rows
updated. The IT now also has a single-column (`t_0024_7`) and a composite
(`t_0024_8`) key table.
##########
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:
Done, an empty dynamic schema is now treated as null.
##########
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:
Partly done here: the listing itself now lives in `Database`. The naming and
quoting are in #9424.
##########
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:
Removed.
--
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]