You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/12/07 13:48:47 UTC

[GitHub] [nifi] MikeThomsen commented on a change in pull request #4678: NIFI-8031: Add UPSERT capability for MySQL in PutDatabaseRecord

MikeThomsen commented on a change in pull request #4678:
URL: https://github.com/apache/nifi/pull/4678#discussion_r536077966



##########
File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/impl/MySQLDatabaseAdapter.java
##########
@@ -35,4 +43,50 @@ public String unwrapIdentifier(String identifier) {
         // Removes double quotes and back-ticks.
         return identifier == null ? null : identifier.replaceAll("[\"`]", "");
     }
+
+    @Override
+    public boolean supportsUpsert() {
+        return true;
+    }
+
+    /**
+     * Tells How many times the column values need to be inserted into the prepared statement. Some DBs (such as MySQL) need the values specified twice in the statement,
+     * some need only to specify them once.
+     *
+     * @return An integer corresponding to the number of times to insert column values into the prepared statement for UPSERT, or -1 if upsert is not supported.
+     */
+    @Override
+    public int getTimesToAddColumnObjectsForUpsert() {
+        return 2;
+    }
+
+    @Override
+    public String getUpsertStatement(String table, List<String> columnNames, Collection<String> uniqueKeyColumnNames) {
+        Preconditions.checkArgument(!StringUtils.isEmpty(table), "Table name cannot be null or blank");
+        Preconditions.checkArgument(columnNames != null && !columnNames.isEmpty(), "Column names cannot be null or empty");
+        Preconditions.checkArgument(uniqueKeyColumnNames != null && !uniqueKeyColumnNames.isEmpty(), "Key column names cannot be null or empty");
+
+        String columns = columnNames.stream()
+                .collect(Collectors.joining(", "));
+
+        String parameterizedInsertValues = columnNames.stream()
+                .map(__ -> "?")
+                .collect(Collectors.joining(", "));
+
+        List<String> updateValues = new ArrayList<>();

Review comment:
       I think it would be more succinct as:
   
   ```
   String parameterizedUpdateValues = columnNames.stream().map(f => f + " = ?").collect(Collectors.joining(", "));
   ```




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org