Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-14346 #9817

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

NIFI-14346 #9817

wants to merge 2 commits into from

Conversation

TomaszK-stack
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant