Commit 28e3b9f
authored
FIX: cursor.execute() with reset_cursor=False raises Invalid cursor state (#521)
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below
For external contributors: Insert Github Issue number below
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
>
[AB#44009](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/44009)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #506
-------------------------------------------------------------------
### Summary
This pull request improves the handling of cursor state when executing
prepared statements with `reset_cursor=False`, ensuring that the
prepared plan is correctly reused and the cursor state is properly
managed between executions. It introduces a new `close_cursor` method at
the ODBC statement handle level, updates the Python bindings, and adds
comprehensive tests to verify the new behavior.
**Enhancements to cursor state management:**
- Added logic in `cursor.py` to call `hstmt.close_cursor()` (which
issues an ODBC `SQLFreeStmt(SQL_CLOSE)`) when `reset_cursor=False`,
allowing prepared statements to be reused safely without resetting the
entire cursor state. This resolves issues with invalid cursor state on
re-execution.
**ODBC handle and Python bindings updates:**
- Implemented the `close_cursor` method in the `SqlHandle` C++ class,
which closes only the cursor on the statement handle without freeing the
prepared statement.
- Exposed the new `close_cursor` method to Python via the
`ddbc_bindings` Pybind11 module, allowing it to be called from Python
code.
**Testing improvements:**
- Added multiple test cases in `test_004_cursor.py` to verify that
`reset_cursor=False` correctly reuses prepared plans, returns new
results, works across multiple iterations, handles queries without
parameters, and functions correctly when the previous result set was not
fully consumed.1 parent ed33bc5 commit 28e3b9f
4 files changed
Lines changed: 111 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1365 | 1365 | | |
1366 | 1366 | | |
1367 | 1367 | | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
1368 | 1378 | | |
1369 | 1379 | | |
1370 | 1380 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1362 | 1362 | | |
1363 | 1363 | | |
1364 | 1364 | | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
1365 | 1381 | | |
1366 | 1382 | | |
1367 | 1383 | | |
| |||
5740 | 5756 | | |
5741 | 5757 | | |
5742 | 5758 | | |
5743 | | - | |
| 5759 | + | |
| 5760 | + | |
5744 | 5761 | | |
5745 | 5762 | | |
5746 | 5763 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
| 381 | + | |
381 | 382 | | |
382 | 383 | | |
383 | 384 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16253 | 16253 | | |
16254 | 16254 | | |
16255 | 16255 | | |
| 16256 | + | |
| 16257 | + | |
| 16258 | + | |
| 16259 | + | |
| 16260 | + | |
| 16261 | + | |
| 16262 | + | |
| 16263 | + | |
| 16264 | + | |
| 16265 | + | |
| 16266 | + | |
| 16267 | + | |
| 16268 | + | |
| 16269 | + | |
| 16270 | + | |
| 16271 | + | |
| 16272 | + | |
| 16273 | + | |
| 16274 | + | |
| 16275 | + | |
| 16276 | + | |
| 16277 | + | |
| 16278 | + | |
| 16279 | + | |
| 16280 | + | |
| 16281 | + | |
| 16282 | + | |
| 16283 | + | |
| 16284 | + | |
| 16285 | + | |
| 16286 | + | |
| 16287 | + | |
| 16288 | + | |
| 16289 | + | |
| 16290 | + | |
| 16291 | + | |
| 16292 | + | |
| 16293 | + | |
| 16294 | + | |
| 16295 | + | |
| 16296 | + | |
| 16297 | + | |
| 16298 | + | |
| 16299 | + | |
| 16300 | + | |
| 16301 | + | |
| 16302 | + | |
| 16303 | + | |
| 16304 | + | |
| 16305 | + | |
| 16306 | + | |
| 16307 | + | |
| 16308 | + | |
| 16309 | + | |
| 16310 | + | |
| 16311 | + | |
| 16312 | + | |
| 16313 | + | |
| 16314 | + | |
| 16315 | + | |
| 16316 | + | |
| 16317 | + | |
| 16318 | + | |
| 16319 | + | |
| 16320 | + | |
| 16321 | + | |
| 16322 | + | |
| 16323 | + | |
| 16324 | + | |
| 16325 | + | |
| 16326 | + | |
| 16327 | + | |
| 16328 | + | |
| 16329 | + | |
| 16330 | + | |
| 16331 | + | |
| 16332 | + | |
| 16333 | + | |
| 16334 | + | |
| 16335 | + | |
| 16336 | + | |
| 16337 | + | |
0 commit comments