tpalfy commented on code in PR #9640: URL: https://github.com/apache/nifi/pull/9640#discussion_r1922763811
########## nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/impl/DatabaseAdapterDatabaseDialectService.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.nifi.processors.standard.db.impl; + +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.database.dialect.service.api.ColumnDefinition; +import org.apache.nifi.database.dialect.service.api.DatabaseDialectService; +import org.apache.nifi.database.dialect.service.api.PageRequest; +import org.apache.nifi.database.dialect.service.api.QueryClause; +import org.apache.nifi.database.dialect.service.api.QueryClauseType; +import org.apache.nifi.database.dialect.service.api.QueryStatementRequest; +import org.apache.nifi.database.dialect.service.api.StandardStatementResponse; +import org.apache.nifi.database.dialect.service.api.StatementRequest; +import org.apache.nifi.database.dialect.service.api.StatementResponse; +import org.apache.nifi.database.dialect.service.api.StatementType; +import org.apache.nifi.database.dialect.service.api.TableDefinition; +import org.apache.nifi.processors.standard.db.ColumnDescription; +import org.apache.nifi.processors.standard.db.DatabaseAdapter; +import org.apache.nifi.processors.standard.db.DatabaseAdapterDescriptor; +import org.apache.nifi.processors.standard.db.TableSchema; + +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Transitional implementation of Database Dialect Service bridging to existing Database Adapters + */ +public class DatabaseAdapterDatabaseDialectService extends AbstractControllerService implements DatabaseDialectService { + private static final char SPACE_SEPARATOR = ' '; + + private static final char COMMA_SEPARATOR = ','; + + private static final int COLUMN_SIZE_IGNORED = -1; + + private static final String DOUBLE_QUOTE = "\""; + + private final DatabaseAdapter databaseAdapter; + + private final Set<StatementType> supportedStatementTypes; + + public DatabaseAdapterDatabaseDialectService(final String databaseType) { + Objects.requireNonNull(databaseType, "Database Type required"); + databaseAdapter = DatabaseAdapterDescriptor.getDatabaseAdapter(databaseType); + Objects.requireNonNull(databaseAdapter, "Database Adapter required"); + + final Set<StatementType> statementTypes = new LinkedHashSet<>(); + statementTypes.add(StatementType.ALTER); + statementTypes.add(StatementType.CREATE); + statementTypes.add(StatementType.SELECT); + + if (databaseAdapter.supportsInsertIgnore()) { + statementTypes.add(StatementType.INSERT_IGNORE); + } + if (databaseAdapter.supportsUpsert()) { + statementTypes.add(StatementType.UPSERT); + } + supportedStatementTypes = Collections.unmodifiableSet(statementTypes); + } + + @Override + public StatementResponse getStatement(final StatementRequest statementRequest) { + final StatementType statementType = statementRequest.statementType(); + + final TableDefinition tableDefinition = statementRequest.tableDefinition(); + final List<String> columnNames = tableDefinition.columns() + .stream() + .map(ColumnDefinition::columnName) + .toList(); + final List<String> primaryKeyColumnNames = tableDefinition.columns() + .stream() + .filter(ColumnDefinition::primaryKey) + .map(ColumnDefinition::columnName) + .toList(); + final List<ColumnDescription> columnDescriptions = getColumnDescriptions(tableDefinition); + + final String sql; + + if (StatementType.ALTER == statementType) { + final List<String> statements = databaseAdapter.getAlterTableStatements(tableDefinition.tableName(), columnDescriptions, true, true); + sql = statements.getFirst(); Review Comment: In theory a DatabaseAdapter may return a separate "ADD COLUMN" for each new individual column. The Derby adapter does this in practice (although exists only in tests). Does this work in such cases? If we rely on this feature being basically deprecated and support only a single ALTER statement, we might want to add some comments/docs to clarify this or change the interface altogether. -- 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]
