Commit 6223304
authored
FEAT: Adding __iter__ and .next functionality for cursor (#162)
### 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>1 parent d6e63c7 commit 6223304
File tree
6 files changed
+4029
-52
lines changed- mssql_python
- pybind
- tests
6 files changed
+4029
-52
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
| |||
117 | 116 | | |
118 | 117 | | |
119 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
120 | 127 | | |
121 | 128 | | |
122 | 129 | | |
| |||
0 commit comments