Handle SQL Comments at Start of Query in getGenerateKeys() Method #2731
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.
Summary
This update addresses a bug where getGeneratedKeys() fails for INSERT statements that begin with SQL comments (e.g., /* ... */ INSERT ...). The method previously relied on a naive string check that only matched queries starting directly with "INSERT", causing errors or missing generated keys. This PR revises the logic to correctly identify and handle INSERT statements even when preceded by comments.
Background (Issue #2729)
Issue: When executing an INSERT statement that begins with a comment and requesting generated keys, the call to getGeneratedKeys() fails with an unrelated error:
.
What Changed
Updated the logic in SQLServerStatement (within the getGenerateKeys() handling) to use the internal isInsert() utility method, which:
This change ensures that identity retrieval is correctly triggered for valid INSERT statements even if comments precede them.
Impact
Fixes getGeneratedKeys() issue when SQL statements have leading comments.
Resolves the user-reported bug in #2729, aligning behavior with user expectations.
Testing
Manually tested locally and added a test case with an INSERT statement that starts with a comment:
Now, the generated keys are returned as intended
Closes: Issue #2729