Skip to content

Conversation

@jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Aug 5, 2025

Work Item / Issue Reference

AB#34888


Summary

This pull request adds a new DB-API extension attribute, rownumber, to the Cursor class in mssql_python/cursor.py, allowing users to track the current 0-based index of the cursor in the result set. It includes full implementation, integration with fetch methods, and comprehensive tests to verify the new functionality and its logging behavior.

Feature Addition: Cursor Row Number Tracking

  • Added the rownumber property to the Cursor class, which returns the current 0-based index in the result set or None if unavailable. This property logs a warning when accessed, as it's a DB-API extension and may not be portable. Supporting methods for managing rownumber state (_reset_rownumber, _increment_rownumber, _decrement_rownumber, _clear_rownumber) were also introduced.
  • Integrated rownumber management into cursor operations: reset/clear on execute, increment on successful fetches in fetchone, fetchmany, and fetchall.

Testing Enhancements

  • Added new tests in tests/test_004_cursor.py to verify basic rownumber functionality, correct progression during fetch operations, warning logging behavior, and correct handling when the cursor is closed.

These changes provide users with a convenient way to track cursor position in result sets and ensure the feature is robustly tested and logged.

@github-actions github-actions bot added the pr-size: medium Moderate update size label Aug 5, 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#34891](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34891)
-------------------------------------------------------------------
### Summary   
This pull request introduces the DB-API 2.0-compliant `connection`
attribute to the `Cursor` class in `mssql_python`, ensuring that each
cursor exposes a read-only reference to its parent connection. The
implementation is thoroughly tested with new and updated test cases
covering polymorphic code, multi-cursor scenarios, connection lifecycle,
and transaction control.

### DB-API 2.0 compliance and attribute exposure

* Added a read-only `connection` property to the `Cursor` class,
exposing the connection object used to create the cursor, and updated
internal usage to store the connection as a private attribute
(`_connection`). (`mssql_python/cursor.py`)
[[1]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L57-R57)
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R567-R584)

### Internal refactoring

* Refactored internal references from `self.connection` to
`self._connection` to enforce encapsulation and ensure the public
attribute is read-only. (`mssql_python/cursor.py`)

### Test coverage for the new attribute

* Added comprehensive tests to verify the existence, correctness, and
read-only nature of the `cursor.connection` attribute, including
multi-cursor and multi-connection environments, accessibility after
cursor/connection closure, and polymorphic code examples.
(`tests/test_004_cursor.py`)

### Transaction and polymorphic usage examples

* Added tests demonstrating transaction control and polymorphic code
patterns using the new `cursor.connection` attribute, confirming correct
behavior in real-world scenarios. (`tests/test_004_cursor.py`)

---------

Co-authored-by: Jahnvi Thakkar <[email protected]>
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: medium Moderate update size labels Aug 6, 2025
Copy link
Contributor

@sumitmsft sumitmsft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments on the PR. Please address them.

@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 6, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 6, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 6, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 6, 2025
@jahnvi480 jahnvi480 requested a review from sumitmsft August 7, 2025 05:50
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 13, 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#34921](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34921)

-------------------------------------------------------------------
### Summary   
This pull request introduces a new convenience method, `fetchval`, to
the `cursor.py` module, designed to simplify retrieval of single values
from SQL queries. Comprehensive tests have been added to ensure its
correctness, robustness, and compatibility with various SQL scenarios
and data types.

Addition of `fetchval` method:

* Added `fetchval` to the `Cursor` class in `mssql_python/cursor.py`,
allowing users to easily fetch the first column of the first row from a
query result. This is especially useful for aggregate queries or scalar
selects and supports method chaining.

Testing and validation of `fetchval`:

* Extensive tests added in `tests/test_004_cursor.py` to validate
`fetchval` for basic functionality, multiple data types, NULL values,
empty results, multiple columns/rows, aggregate functions, method
chaining, closed cursor behavior, rownumber tracking, error scenarios,
and common performance patterns.

These changes make it easier and safer for users to retrieve single
values from queries, improving developer experience and reliability.

---------

Co-authored-by: Jahnvi Thakkar <[email protected]>
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 27, 2025
@jahnvi480 jahnvi480 merged commit 104837b into jahnvi/cursor_iter_next Aug 27, 2025
3 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.

4 participants