FEAT: Adding __iter__ and .next functionality for cursor #162
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
Cursor
class inmssql_python
to 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
Cursor
functionality:__iter__
and__next__
methods to theCursor
class, 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_iteration
to validate iteration over query results using for-loops. (tests/test_004_cursor.py
, tests/test_004_cursor.pyR1529-R1774)test_cursor_next_functionality
to 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_types
to ensurenext()
handles various data types correctly. (tests/test_004_cursor.py
, tests/test_004_cursor.pyR1529-R1774)test_cursor_next_error_conditions
to 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_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:
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)