Skip to content

Conversation

@Ananya2
Copy link
Contributor

@Ananya2 Ananya2 commented Aug 7, 2025

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:

com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained.
This happens because the driver does not send the identity retrieval command when the SQL string doesn’t start with "INSERT" 
[GitHub](https://github.com/microsoft/mssql-jdbc/issues/2729)

.
What Changed
Updated the logic in SQLServerStatement (within the getGenerateKeys() handling) to use the internal isInsert() utility method, which:

  • Trims leading whitespace.
  • Skips initial SQL comments.
  • Recursively checks if the actual statement begins with "INSERT"
    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:

stmt.executeUpdate("/* comment */ INSERT INTO ...", Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();

Now, the generated keys are returned as intended

Closes: Issue #2729

@Ananya2 Ananya2 added this to the 13.2.0 milestone Aug 7, 2025
@codecov
Copy link

codecov bot commented Aug 7, 2025

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 51.51%. Comparing base (c8c028f) to head (d7bb44f).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...m/microsoft/sqlserver/jdbc/SQLServerStatement.java 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2731      +/-   ##
============================================
- Coverage     51.66%   51.51%   -0.15%     
+ Complexity     4112     4067      -45     
============================================
  Files           149      149              
  Lines         34240    34242       +2     
  Branches       5718     5719       +1     
============================================
- Hits          17690    17641      -49     
- Misses        14110    14112       +2     
- Partials       2440     2489      +49     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@divang divang changed the title Fix statement getGenerateKeys() throwing an error is query starts with a comment (#2729) Handle SQL Comments at Start of Query in getGenerateKeys() Method Aug 7, 2025
@Ananya2 Ananya2 merged commit 1ed161f into main Aug 11, 2025
18 of 20 checks passed
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.

7 participants