Skip to content

Conversation

@jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Aug 14, 2025

Work Item / Issue Reference

AB#34892


Summary

This pull request introduces support for emulated scrolling (relative and absolute) in the Cursor class for forward-only result sets, including a new scroll() method and comprehensive tests for scroll behavior. It also refines row position tracking, improves error handling, and updates some test assertions for clarity. The most important changes are grouped below.

Cursor Scrolling Feature

  • Added a new scroll() method to the Cursor class, allowing movement to a specific row position in the result set using relative or absolute modes. This is implemented by consuming rows for forward-only cursors, with robust error handling for unsupported operations and invalid parameters.
  • Implemented helper methods _scroll_relative, _scroll_absolute, and _consume_rows_for_scroll to handle the logic for moving the cursor position and updating internal counters.

Row Position Tracking Improvements

  • Refined row position tracking by introducing _next_row_index and updating how rownumber is managed after fetch and scroll operations, ensuring consistency with DB-API semantics.

Error Handling and Validation

  • Improved error handling in cursor methods by providing more descriptive error messages and raising appropriate exceptions for invalid scroll operations or parameters.
  • Updated import statements to include new exception types used for scroll validation.

Test Coverage for Scrolling

  • Added a suite of new tests to verify scroll behavior, including relative and absolute scrolling, backward scroll rejection, empty result set handling, mixed fetch/scroll operations, and edge case validation.

Minor Test Assertion Updates

  • Updated test assertions and comments in transaction-related tests for clarity and to better reflect the intended behavior.

@github-actions github-actions bot added the pr-size: medium Moderate update size label Aug 14, 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.

I just have the apprehension around scrolling many rows forward may result in high memory usage due to row by row operation. Let's make the correction and do the above mentioned fix.

@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 25, 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#34924](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34924)

-------------------------------------------------------------------
### Summary   
This pull request introduces a new convenience method, `skip`, to the
`Cursor` class in `mssql_python/cursor.py`, which allows users to
advance the cursor position by a specified number of rows without
fetching them. Comprehensive tests have been added to validate the
method's behavior, including edge cases and integration with existing
fetch methods.

**New feature: Cursor skipping**
* Added `skip(count: int)` method to the `Cursor` class, enabling users
to efficiently advance the cursor by a given number of rows without
returning those rows. The method checks for closed cursors, validates
arguments, supports no-op for zero, and raises appropriate errors for
invalid usage.

**Testing and validation**
* Added `test_cursor_skip_basic_functionality` to verify that `skip`
advances the cursor as expected and integrates correctly with
`fetchone`.
* Added tests for edge cases: skipping zero rows
(`test_cursor_skip_zero_is_noop`), empty result sets
(`test_cursor_skip_empty_result_set`), skipping past the end
(`test_cursor_skip_past_end`), invalid arguments
(`test_cursor_skip_invalid_arguments`), and closed cursors
(`test_cursor_skip_closed_cursor`).
* Added integration tests to ensure `skip` works correctly with
`fetchone`, `fetchmany`, and `fetchall` methods
(`test_cursor_skip_integration_with_fetch_methods`).

---------

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 27, 2025
@jahnvi480 jahnvi480 merged commit c2c3f2f into jahnvi/cursor_commit 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