FEAT: Adding __iter__ and .next functionality for cursor#162
Merged
jahnvi480 merged 4 commits intojahnvi/execute_cursorfrom Aug 27, 2025
Merged
FEAT: Adding __iter__ and .next functionality for cursor#162jahnvi480 merged 4 commits intojahnvi/execute_cursorfrom
jahnvi480 merged 4 commits intojahnvi/execute_cursorfrom
Conversation
…mssql-python into jahnvi/cursor_iter_next
…mssql-python into jahnvi/cursor_iter_next
Contributor
Author
|
@sumitmsft Please review this PR |
sumitmsft
approved these changes
Aug 26, 2025
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below (e.g. AB#37452) For external contributors: Insert Github Issue number below (e.g. #149) Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#34888](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34888) ------------------------------------------------------------------- ### Summary This pull request adds a new DB-API extension attribute, `rownumber`, to the `Cursor` class in `mssql_python/cursor.py`, allowing users to track the current 0-based index of the cursor in the result set. It includes full implementation, integration with fetch methods, and comprehensive tests to verify the new functionality and its logging behavior. ### Feature Addition: Cursor Row Number Tracking * Added the `rownumber` property to the `Cursor` class, which returns the current 0-based index in the result set or `None` if unavailable. This property logs a warning when accessed, as it's a DB-API extension and may not be portable. Supporting methods for managing rownumber state (`_reset_rownumber`, `_increment_rownumber`, `_decrement_rownumber`, `_clear_rownumber`) were also introduced. * Integrated rownumber management into cursor operations: reset/clear on `execute`, increment on successful fetches in `fetchone`, `fetchmany`, and `fetchall`. ### Testing Enhancements * Added new tests in `tests/test_004_cursor.py` to verify basic rownumber functionality, correct progression during fetch operations, warning logging behavior, and correct handling when the cursor is closed. These changes provide users with a convenient way to track cursor position in result sets and ensure the feature is robustly tested and logged. --------- Co-authored-by: Jahnvi Thakkar <jathakkar@microsoft.com>
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
This pull request enhances the
Cursorclass inmssql_pythonto support Python's iterator protocol, adds anext()method for compatibility, and introduces comprehensive test cases to validate these features. The changes improve usability by enabling direct iteration over query results and ensure backward compatibility with existing code.Enhancements to
Cursorfunctionality:__iter__and__next__methods to theCursorclass, allowing it to be used as an iterator in for-loops and with thenext()function. Thenext()method is also included as an alias for__next__to maintain compatibility with older code. (mssql_python/cursor.py, [1] [2]New test cases for iteration and
next()functionality:test_chaining_with_iterationto validate iteration over query results using for-loops. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)test_cursor_next_functionalityto confirm correct behavior of thenext()method, including handling of empty result sets and single-row queries. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)test_cursor_next_with_different_data_typesto ensurenext()handles various data types correctly. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)test_cursor_next_error_conditionsto test edge cases, such as callingnext()on a closed cursor or before executing a query. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)test_future_iterator_protocol_compatibilityto demonstrate future compatibility with Python's iterator protocol. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)Real-world usage examples:
test_execute_chaining_compatibility_examplesto showcase practical use cases of iteration and chaining, such as fetching rows, updating, and deleting records. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1826-R1885)