Skip to content

Conversation

@jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Aug 4, 2025

Work Item / Issue Reference

AB#3806


Summary

This pull request enhances the Cursor class in mssql_python/cursor.py to support method chaining by modifying the execute method to return the cursor itself. It also introduces comprehensive test coverage for method chaining in tests/test_004_cursor.py.

Enhancements to the Cursor class:

  • Modified the execute method in mssql_python/cursor.py to return the Cursor instance, enabling method chaining. (`[[1]]

Updates to test cases:

  • Simplified the test_longwvarchar test by removing unnecessary loops and assertions, improving readability.
  • Added new test cases to validate method chaining functionality, including:
    • Chaining execute with fetchone, fetchall, fetchmany, rowcount, and description.
    • Testing multiple sequential operations and parameterized queries.
    • Ensuring chaining works correctly after errors and with statement reuse for performance.

Copilot AI review requested due to automatic review settings August 4, 2025 11:10
@github-actions github-actions bot added the pr-size: medium Moderate update size label Aug 4, 2025
Copy link
Contributor

Copilot AI left a 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 execute method 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.
        """)

@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 4, 2025
@jahnvi480 jahnvi480 changed the title FIX: Execute returning cursor object and making cursor iterable FIX: Execute returning cursor object Aug 4, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels 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.

one minor query in test

@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 5, 2025
bewithgaurav
bewithgaurav previously approved these changes Aug 6, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 13, 2025
gargsaumya
gargsaumya previously approved these changes Aug 25, 2025
sumitmsft
sumitmsft previously 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#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>
@jahnvi480 jahnvi480 dismissed stale reviews from sumitmsft, gargsaumya, and bewithgaurav via 6223304 August 27, 2025 11:40
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: medium Moderate update size labels Aug 27, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 28, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 28, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 28, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 28, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 28, 2025
sumitmsft
sumitmsft previously approved these changes Aug 28, 2025
gargsaumya
gargsaumya previously approved these changes Aug 28, 2025
bewithgaurav
bewithgaurav previously approved these changes Aug 28, 2025
@jahnvi480 jahnvi480 dismissed stale reviews from bewithgaurav, gargsaumya, and sumitmsft via 3c47985 August 28, 2025 08:30
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 28, 2025
@jahnvi480 jahnvi480 merged commit 12245e8 into main Aug 28, 2025
18 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.

5 participants