Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.