-
Notifications
You must be signed in to change notification settings - Fork 46
FIX: cursor.execute() with reset_cursor=False raises Invalid cursor state #521
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15961,3 +15961,84 @@ def reader(reader_id): | |||||
| finally: | ||||||
| stop_event.set() | ||||||
| mssql_python.native_uuid = original | ||||||
|
|
||||||
|
|
||||||
| def test_execute_reset_cursor_false_reuses_prepared_plan(db_connection): | ||||||
| """Test that reset_cursor=False reuses the prepared statement handle | ||||||
| and successfully re-executes after consuming the previous result set.""" | ||||||
| cursor = db_connection.cursor() | ||||||
| try: | ||||||
| cursor.execute("SELECT 1 AS val WHERE 1 = ?", (1,)) | ||||||
| row = cursor.fetchone() | ||||||
| assert row[0] == 1 | ||||||
| _ = cursor.fetchall() # consume remaining | ||||||
|
|
||||||
| # Re-execute with reset_cursor=False — this was raising | ||||||
| # ProgrammingError: Invalid cursor state before the fix. | ||||||
| cursor.execute("SELECT 1 AS val WHERE 1 = ?", (2,), reset_cursor=False) | ||||||
| row = cursor.fetchone() | ||||||
| assert row is None # No match for WHERE 1 = 2 | ||||||
| finally: | ||||||
| cursor.close() | ||||||
|
|
||||||
|
|
||||||
| def test_execute_reset_cursor_false_returns_new_results(db_connection): | ||||||
| """Test that reset_cursor=False correctly returns results from the | ||||||
| second execution with different parameter values.""" | ||||||
| cursor = db_connection.cursor() | ||||||
| try: | ||||||
| cursor.execute("SELECT ? AS val", (42,)) | ||||||
| row = cursor.fetchone() | ||||||
| assert row[0] == 42 | ||||||
| _ = cursor.fetchall() | ||||||
|
|
||||||
| cursor.execute("SELECT ? AS val", (99,), reset_cursor=False) | ||||||
| row = cursor.fetchone() | ||||||
| assert row[0] == 99 | ||||||
| finally: | ||||||
| cursor.close() | ||||||
|
|
||||||
|
|
||||||
| def test_execute_reset_cursor_false_multiple_iterations(db_connection): | ||||||
| """Test that reset_cursor=False works across several consecutive | ||||||
| re-executions on the same cursor.""" | ||||||
| cursor = db_connection.cursor() | ||||||
| try: | ||||||
| for i in range(5): | ||||||
| kwargs = {"reset_cursor": False} if i > 0 else {} | ||||||
| cursor.execute("SELECT ? AS iter", (i,), **kwargs) | ||||||
| row = cursor.fetchone() | ||||||
| assert row[0] == i, f"Expected {i}, got {row[0]}" | ||||||
|
||||||
| assert row[0] == i, f"Expected {i}, got {row[0]}" | |
| assert row == (i,), f"Expected {(i,)}, got {row}" |
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.
close_cursor()silently no-ops whenSQLFreeStmt_ptris not loaded and also ignores theSQLRETURNfromSQLFreeStmt. This can mask failures and leave the cursor open, potentially preserving the original 'Invalid cursor state' behavior without surfacing an actionable error. Consider aligning behavior with other wrappers: (1) throw whenSQLFreeStmt_ptris not loaded, and (2) check the return code and raise via the existing error/diagnostics path when the call fails (treatSQL_SUCCESS_WITH_INFOappropriately).