Repository navigation
FIX: Update rowcount in nextset() - #864
Open
LP Bispo (phelipelui) wants to merge 1 commit into
Open
LP Bispo (phelipelui) wants to merge 1 commit into
LP Bispo (phelipelui) wants to merge 1 commit into
Conversation
nextset() moved to the next result with SQLMoreResults and refreshed description and messages, but left rowcount with the value execute() set for the first statement. The affected-row count of later statements in a batch (e.g. each INSERT of a T-SQL WHILE loop) could not be read. nextset() now sets rowcount like execute(): -1 for a result set, the SQLRowCount of the statement otherwise. This matches pyodbc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
PR Performance ReportPerformance could not be assessed. No matching performance run appeared within the 220-minute wait for |
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Reported integration results use published-wheel bindings; SQL Server validation against the current native build still needs human confirmation.
No open issues 🎉
What changed in this PR
Addresses #863 by refreshing the synchronous cursor’s rowcount when nextset() advances, aligning it with execute() behavior.
Changes:
- Reports affected-row counts for non-query results and resets
rowcountto-1for result sets. - Adds regression tests for mixed-statement batches and loops with differing insert counts.
- Documents the fix in the changelog; asynchronous cursors remain unchanged.
| File | Description |
|---|---|
| tests/test_004_cursor.py | Tests per-result row counts in mixed batches and loops. |
| mssql_python/cursor.py | Refreshes rowcount after advancing to a new result. |
| CHANGELOG.md | Records the fix for #863. |
Review effort: Balanced
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Work Item / Issue Reference
Summary
Cursor.nextset()callsSQLMoreResultsand refreshesdescriptionandmessages, but it leavesrowcountat the valueexecute()set for the first statement. In a multi-statement batch, the affected-row count of every later INSERT/UPDATE/DELETE was lost. This hits hardest with T-SQL control flow: aWHILEloop sends one result per INSERT, so a tool that sumsrowcountovernextset()gets a stale number.Change (
mssql_python/cursor.py,nextset()). After the describe step,rowcountis set the same wayexecute()sets it:-1when the new result has columns;fetch*()then updates it as it already does;DDBCSQLRowCount(self.hstmt)otherwise.The
SQL_NO_DATApath (nextset()returningFalse) is unchanged. This matches pyodbc, which callsSQLRowCountinCursor_nextsetafterSQLMoreResults.Tests (
tests/test_004_cursor.py):test_nextset_updates_rowcountruns a batch of INSERT (1 row), INSERT (2 rows), SELECT, UPDATE (3 rows), and DELETE (0 rows). It checksrowcountafter eachnextset():1, 2, -1, 3, 0.test_nextset_rowcount_in_loop_batchruns aWHILEloop whose iterations insert 1, 2 and 3 rows. It expects per-result counts[1, 2, 3]. The counts differ on purpose, so a stalerowcountcannot reach the right total by accident.CHANGELOG: entry under Unreleased → Fixed.
Verification. I ran against SQL Server 2022 (16.0.4305.5) on Python 3.12, Debian 12, with the native bindings from the published 1.16.0 wheel, whose
cursor.pyis identical tomainbefore this change.tests/test_004_cursor.pyassert 1 == 2,[1, 1, 1] == [1, 2, 3])The only failures without the fix are the two new tests, and no other test changes result. Black 26.5.1, the version pinned in
.pre-commit-config.yaml, reports both files unchanged.Out of scope:
AsyncCursor.nextset()goes through py-core and was not changed or checked.🤖 Generated with Claude Code