-
Notifications
You must be signed in to change notification settings - Fork 27
FIX: Execute returning cursor object #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request enhances the Cursor class by adding method chaining support and iteration capabilities. The changes enable developers to chain operations like cursor.execute().fetchone() and iterate directly over cursor results using for row in cursor.execute(sql).
Key changes include:
- Modified the
executemethod to return the cursor object itself for method chaining - Added
__iter__and__next__methods to make the cursor iterable - Comprehensive test coverage validating chaining operations, iteration, error handling, and compatibility
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mssql_python/cursor.py |
Updated execute method return type and added iterator methods for cursor iteration |
tests/test_004_cursor.py |
Added extensive test suite covering method chaining and iteration scenarios |
Comments suppressed due to low confidence (1)
tests/test_004_cursor.py:632
- The test assumes that rows have attribute access (row.user_id, row.user_name) but the cursor returns list/tuple objects. This will cause an AttributeError. Use index access instead: row[0] for user_id and row[1] for user_name.
""")
bewithgaurav
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one minor query in test
… jahnvi/execute_cursor
### 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#34896](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34896) > [AB#34895](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/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]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R44) [[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R540-R581) ### 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](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-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](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-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](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-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](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-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](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-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](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1826-R1885)) --------- Co-authored-by: Jahnvi Thakkar <jathakkar@microsoft.com>
6223304
3c47985
Work Item / Issue Reference
Summary
This pull request enhances the
Cursorclass inmssql_python/cursor.pyto support method chaining by modifying theexecutemethod to return the cursor itself. It also introduces comprehensive test coverage for method chaining intests/test_004_cursor.py.Enhancements to the
Cursorclass:executemethod inmssql_python/cursor.pyto return theCursorinstance, enabling method chaining. (`[[1]]Updates to test cases:
test_longwvarchartest by removing unnecessary loops and assertions, improving readability.executewithfetchone,fetchall,fetchmany,rowcount, anddescription.