Skip to content

Conversation

jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Aug 4, 2025

Work Item / Issue Reference

AB#34896
AB#34895


Summary

This pull request enhances the Cursor class in mssql_python to support Python's iterator protocol, adds a next() 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 Cursor functionality:

  • Added __iter__ and __next__ methods to the Cursor class, allowing it to be used as an iterator in for-loops and with the next() function. The next() 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:

  • Introduced test_chaining_with_iteration to validate iteration over query results using for-loops. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)
  • Added test_cursor_next_functionality to confirm correct behavior of the next() method, including handling of empty result sets and single-row queries. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)
  • Created test_cursor_next_with_different_data_types to ensure next() handles various data types correctly. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)
  • Added test_cursor_next_error_conditions to test edge cases, such as calling next() on a closed cursor or before executing a query. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)
  • Developed test_future_iterator_protocol_compatibility to demonstrate future compatibility with Python's iterator protocol. (tests/test_004_cursor.py, tests/test_004_cursor.pyR1529-R1774)

Real-world usage examples:

  • Added test_execute_chaining_compatibility_examples to 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)

@github-actions github-actions bot added the pr-size: medium Moderate update size label Aug 4, 2025
Copy link
Collaborator

@bewithgaurav bewithgaurav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 13, 2025
@jahnvi480
Copy link
Contributor Author

@sumitmsft Please review this PR
@bewithgaurav has already approved

### 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>
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: medium Moderate update size labels Aug 27, 2025
@jahnvi480 jahnvi480 merged commit 6223304 into jahnvi/execute_cursor Aug 27, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-size: large Substantial code update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants