-
Notifications
You must be signed in to change notification settings - Fork 27
FEAT: Adding cursor.rownumber - DBAPI 2.0 #163
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bewithgaurav
approved these changes
Aug 5, 2025
sumitmsft
reviewed
Aug 6, 2025
sumitmsft
reviewed
Aug 6, 2025
sumitmsft
reviewed
Aug 6, 2025
sumitmsft
reviewed
Aug 6, 2025
sumitmsft
reviewed
Aug 6, 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]>
sumitmsft
reviewed
Aug 6, 2025
sumitmsft
reviewed
Aug 6, 2025
sumitmsft
reviewed
Aug 6, 2025
sumitmsft
requested changes
Aug 6, 2025
Contributor
sumitmsft
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.
Left a few comments on the PR. Please address them.
sumitmsft
approved these changes
Aug 7, 2025
…mssql-python into jahnvi/cursor_rownumber
…t/mssql-python into jahnvi/cursor_rownumber
sumitmsft
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#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]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
Summary
This pull request adds a new DB-API extension attribute,
rownumber, to theCursorclass inmssql_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
rownumberproperty to theCursorclass, which returns the current 0-based index in the result set orNoneif 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.execute, increment on successful fetches infetchone,fetchmany, andfetchall.Testing Enhancements
tests/test_004_cursor.pyto 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.