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
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)