TomaszK-stack opened a new pull request, #9817:
URL: https://github.com/apache/nifi/pull/9817

   Description of the Change:
   This change addresses an issue in the PutDatabaseRecord processor when the 
Statement Type is set to UPSERT and connected to a MySQL database. The original 
code for binding parameters to the UPSERT SQL statement was incorrectly 
calculating the number of repetitions for parameter setting, leading to the 
error: java.sql.SQLException: No value specified for parameter 1.
   
   Problem:
   When executing the UPSERT query, the processor calculates how many times 
parameters should be set based on the ratio of field indexes to the parameter 
count. In the original code, the formula used was:
   
   final int timesToAddObjects = fieldIndexes.size() / 
preparedSqlAndColumns.parameterCount;
   This led to a situation where the number of times parameters were set was 
calculated incorrectly. For example, if there were 5 fields 
(fieldIndexes.size() = 5) and 10 parameters 
(preparedSqlAndColumns.parameterCount = 10), the result would be 0, which meant 
that no parameters were being set.
   
   Solution:
   To fix the issue, the formula for calculating the number of repetitions was 
reversed:
   final int timesToAddObjects = preparedSqlAndColumns.parameterCount / 
fieldIndexes.size();
   This ensures that the number of times parameters are set is correctly 
calculated based on the number of parameters and the number of fields in the 
UPSERT statement, where each field has two parameters: one for the insert case 
and one for the update case.
   
   Impact:
   The change ensures that parameters are properly bound in UPSERT statements, 
fixing the SQLException.
   
   It improves the flexibility and correctness of the parameter binding logic, 
especially when the number of fields and parameters differs significantly.
   
   It resolves the issue with parameter setting in MySQL UPSERT queries, where 
two placeholders (?) are used per field (one for the insert and one for the 
update).
   
   Testing:
   The change has been tested by executing UPSERT statements on a MySQL 
database with multiple fields to verify that parameters are set correctly for 
both insert and update operations.


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

Reply via email to