Skip to content

Commit 6223304

Browse files
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

6 files changed

+4029
-52
lines changed

mssql_python/constants.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class ConstantsDDBC(Enum):
9797
SQL_ATTR_ROW_ARRAY_SIZE = 27
9898
SQL_ATTR_ROWS_FETCHED_PTR = 26
9999
SQL_ATTR_ROW_STATUS_PTR = 25
100-
SQL_FETCH_NEXT = 1
101100
SQL_ROW_SUCCESS = 0
102101
SQL_ROW_SUCCESS_WITH_INFO = 1
103102
SQL_ROW_NOROW = 100
@@ -117,6 +116,14 @@ class ConstantsDDBC(Enum):
117116
SQL_NULLABLE = 1
118117
SQL_MAX_NUMERIC_LEN = 16
119118

119+
SQL_FETCH_NEXT = 1
120+
SQL_FETCH_FIRST = 2
121+
SQL_FETCH_LAST = 3
122+
SQL_FETCH_PRIOR = 4
123+
SQL_FETCH_ABSOLUTE = 5
124+
SQL_FETCH_RELATIVE = 6
125+
SQL_FETCH_BOOKMARK = 8
126+
120127
class AuthType(Enum):
121128
"""Constants for authentication types"""
122129
INTERACTIVE = "activedirectoryinteractive"

0 commit comments

Comments
 (0)