diff --git a/contrib/babelfishpg_tsql/src/cursor.c b/contrib/babelfishpg_tsql/src/cursor.c index e79d8d1b8d..abc2f95e3a 100644 --- a/contrib/babelfishpg_tsql/src/cursor.c +++ b/contrib/babelfishpg_tsql/src/cursor.c @@ -67,12 +67,12 @@ typedef struct cursorhashent static HTAB *CursorHashTable = NULL; -typedef struct cursorpreparedhandlehashent +typedef struct CursorPreparedHandleHashEnt { uint32 handle; SPIPlanPtr plan; int cursor_options; -} CurosrPreparedHandleHashEnt; +} CursorPreparedHandleHashEnt; static HTAB *CursorPreparedHandleHashTable = NULL; @@ -479,7 +479,7 @@ pltsql_create_cursor_htab() /* CursorPreparedHandleHashTable */ MemSet(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(uint32); - ctl.entrysize = sizeof(CurosrPreparedHandleHashEnt); + ctl.entrysize = sizeof(CursorPreparedHandleHashEnt); ctl.hcxt = CursorHashtabContext; CursorPreparedHandleHashTable = hash_create("T-SQL cursor prepared handle", 16 /* PORTALS_PER_USER */ , &ctl, HASH_ELEM | HASH_BLOBS | HASH_CONTEXT); @@ -1110,7 +1110,7 @@ execute_sp_cursorunprepare(int stmt_handle) { int rc; MemoryContext savedPortalCxt; - CurosrPreparedHandleHashEnt *phentry; + CursorPreparedHandleHashEnt *phentry; bool found; /* @@ -1124,7 +1124,7 @@ execute_sp_cursorunprepare(int stmt_handle) elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc)); PortalContext = savedPortalCxt; - phentry = (CurosrPreparedHandleHashEnt *) hash_search(CursorPreparedHandleHashTable, &stmt_handle, HASH_FIND, NULL); + phentry = (CursorPreparedHandleHashEnt *) hash_search(CursorPreparedHandleHashTable, &stmt_handle, HASH_FIND, NULL); if (phentry == NULL) elog(ERROR, "can't find prepared handle: %u", stmt_handle); @@ -1444,7 +1444,7 @@ execute_sp_cursoropen_common(int *stmt_handle, int *cursor_handle, const char *s int cursor_options; bool found; SPIPlanPtr plan; - CurosrPreparedHandleHashEnt *phentry; + CursorPreparedHandleHashEnt *phentry; CursorHashEnt *hentry; Portal portal; MemoryContext oldcontext; @@ -1476,7 +1476,7 @@ execute_sp_cursoropen_common(int *stmt_handle, int *cursor_handle, const char *s if (save_plan) { *stmt_handle = get_next_cursor_prepared_handle(); - phentry = (CurosrPreparedHandleHashEnt *) hash_search(CursorPreparedHandleHashTable, stmt_handle, HASH_ENTER, &found); + phentry = (CursorPreparedHandleHashEnt *) hash_search(CursorPreparedHandleHashTable, stmt_handle, HASH_ENTER, &found); Assert(!found); /* already checked in * get_next_cursor_prepared_handle() */ @@ -1489,7 +1489,7 @@ execute_sp_cursoropen_common(int *stmt_handle, int *cursor_handle, const char *s } else /* !prepare */ { - phentry = (CurosrPreparedHandleHashEnt *) hash_search(CursorPreparedHandleHashTable, stmt_handle, HASH_FIND, NULL); + phentry = (CursorPreparedHandleHashEnt *) hash_search(CursorPreparedHandleHashTable, stmt_handle, HASH_FIND, NULL); if (phentry == NULL) elog(ERROR, "can't find stmt_handle: %u", *stmt_handle); if (phentry->plan == NULL) @@ -1828,3 +1828,63 @@ pltsql_get_last_stmt_handle(PG_FUNCTION_ARGS) { return current_cursor_prepared_handle; } + +/* + * reset_cached_cursor: + * Cleans up all the stale cursor states and resets the cursor handles. + * This function should be called when a connection is cancelled or terminated. + */ +void +reset_cached_cursor(void) +{ + HASH_SEQ_STATUS hash_seq; + CursorHashEnt *hentry; + CursorPreparedHandleHashEnt *phentry; + + /* Iterate through the CursorHashTable and clean up each cursor. */ + hash_seq_init(&hash_seq, CursorHashTable); + while ((hentry = (CursorHashEnt *) hash_seq_search(&hash_seq)) != NULL) + { + /* Clean up the cursor data. */ + if (hentry && hentry->tupdesc) + { + FreeTupleDesc(hentry->tupdesc); + hentry->tupdesc = NULL; + } + if (hentry && hentry->fetch_buffer) + { + tuplestore_end(hentry->fetch_buffer); + hentry->fetch_buffer = NULL; + } + if (hentry && hentry->textptr_only_bitmap) + { + pfree(hentry->textptr_only_bitmap); + hentry->textptr_only_bitmap = NULL; + } + } + + /* Iterate through the CursorPreparedHandleHashTable and clean up each prepared cursor. */ + hash_seq_init(&hash_seq, CursorPreparedHandleHashTable); + while ((phentry = (CursorPreparedHandleHashEnt *) hash_seq_search(&hash_seq)) != NULL) + { + if (phentry && phentry->plan) + { + SPI_freeplan(phentry->plan); + phentry->plan = NULL; + } + } + + hash_destroy(CursorHashTable); + hash_destroy(CursorPreparedHandleHashTable); + CursorHashTable = NULL; + CursorPreparedHandleHashTable = NULL; + + /* Re-create the cursor-related data structures. */ + pltsql_create_cursor_htab(); + /* Reset cursor handles. */ + current_cursor_handle = CURSOR_HANDLE_INVALID; + current_cursor_prepared_handle = CURSOR_PREPARED_HANDLE_START; + + /* Reset sp_cursor_params. */ + reset_sp_cursor_params(); +} \ No newline at end of file diff --git a/contrib/babelfishpg_tsql/src/pltsql.h b/contrib/babelfishpg_tsql/src/pltsql.h index f5453319c4..aefbf9a9df 100644 --- a/contrib/babelfishpg_tsql/src/pltsql.h +++ b/contrib/babelfishpg_tsql/src/pltsql.h @@ -2242,6 +2242,7 @@ int execute_sp_cursorfetch(int cursor_handle, int *fetchtype, int *rownum, int int execute_sp_cursoroption(int cursor_handle, int code, int value); int execute_sp_cursoroption2(int cursor_handle, int code, const char *value); int execute_sp_cursorclose(int cursor_handle); +void reset_cached_cursor(void); /* * Functions in string.c diff --git a/contrib/babelfishpg_tsql/src/session.c b/contrib/babelfishpg_tsql/src/session.c index 0470d0d0cd..f0caf46032 100644 --- a/contrib/babelfishpg_tsql/src/session.c +++ b/contrib/babelfishpg_tsql/src/session.c @@ -210,6 +210,7 @@ void reset_session_properties(void) { reset_cached_batch(); + reset_cached_cursor(); } void diff --git a/test/JDBC/expected/Test-sp_reset_connection.out b/test/JDBC/expected/Test-sp_reset_connection.out index 370524a81f..28dd97afb7 100644 --- a/test/JDBC/expected/Test-sp_reset_connection.out +++ b/test/JDBC/expected/Test-sp_reset_connection.out @@ -190,3 +190,326 @@ GO -- tsql DROP DATABASE reset_con_db3 GO + +-- Cursor reset testing +CREATE TABLE babel_cursor_t1 (i INT, d double precision, c varchar(10), u uniqueidentifier, v sql_variant); +INSERT INTO babel_cursor_t1 VALUES (1, 1.1, 'a', '1E984725-C51C-4BF4-9960-E1C80E27ABA0', 1); +INSERT INTO babel_cursor_t1 VALUES (2, 22.22, 'bb', '2E984725-C51C-4BF4-9960-E1C80E27ABA0', 22.22); +INSERT INTO babel_cursor_t1 VALUES (3, 333.333, 'cccc', '3E984725-C51C-4BF4-9960-E1C80E27ABA0', 'cccc'); +INSERT INTO babel_cursor_t1 VALUES (4, 4444.4444, 'dddddd', '4E984725-C51C-4BF4-9960-E1C80E27ABA0', cast('4E984725-C51C-4BF4-9960-E1C80E27ABA0' as uniqueidentifier)); +INSERT INTO babel_cursor_t1 VALUES (NULL, NULL, NULL, NULL, NULL); +GO +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + +~~ROW COUNT: 1~~ + + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + + +-- This will work since we declared 180150001 handle +EXEC sp_cursorfetch 180150001, 2, 0, 1; +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: cursor "180150001" does not exist)~~ + +EXEC sp_cursor 180150001, 40, 1, 0; +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: cursor "180150001" does not exist)~~ + + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + +~~START~~ +int +1073741824 +~~END~~ + +~~START~~ +int +180150000 +~~END~~ + +~~START~~ +int +0 +~~END~~ + +-- This will not work since we 80150001 handle should have been cleaned up +EXEC sp_cursorfetch 180150001, 2, 0, 1; +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: cursor "180150001" does not exist)~~ + +EXEC sp_cursor 180150001, 40, 1, 0; +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: cursor "180150001" does not exist)~~ + + +-- Testing with only Cursor Open +DECLARE @cursor_handle int; +EXEC sp_cursoropen @cursor_handle OUTPUT, 'select i, d, c, u from babel_cursor_t1', 2, 8193; +GO + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +NULL#!#NULL#!#2#!#1#!#1#!#1#!#1#!#1#!#-1#!#0#!#4#!#0#!#1#!#180150001 +~~END~~ + +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + +~~START~~ +int +1073741824 +~~END~~ + +~~START~~ +int +180150000 +~~END~~ + +~~START~~ +int +0 +~~END~~ + + +-- Testing with only Cursor Prepare +DECLARE @stmt_handle int; +EXEC sp_cursorprepare @stmt_handle OUTPUT, N'', 'select i, d, c, u from babel_cursor_t1', 0, 2, 1; +GO + +-- check the status of the cursor should give 1073741825 +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +exec sys.sp_reset_connection +GO +~~START~~ +int +1073741825 +~~END~~ + + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + +~~START~~ +int +1073741824 +~~END~~ + +~~START~~ +int +180150000 +~~END~~ + +~~START~~ +int +0 +~~END~~ + + +-- Testing with only Cursor Prepare and Fetch +DECLARE @stmt_handle int; +DECLARE @cursor_handle int; +DECLARE @cursor_handle2 int; +EXEC sp_cursorprepare @stmt_handle OUTPUT, N'', 'select i, d, c, u from babel_cursor_t1', 0, 2, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle OUTPUT, 2, 1; +EXEC sp_cursorfetch @cursor_handle, 2, 0, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle2 OUTPUT, 2, 1; +GO +~~START~~ +int#!#float#!#varchar#!#uniqueidentifier +1#!#1.1#!#a#!#1E984725-C51C-4BF4-9960-E1C80E27ABA0 +~~END~~ + + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +NULL#!#NULL#!#2#!#1#!#1#!#1#!#1#!#1#!#-1#!#0#!#4#!#1#!#2#!#180150001 +~~END~~ + +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +NULL#!#NULL#!#2#!#1#!#1#!#1#!#1#!#1#!#-1#!#0#!#4#!#0#!#1#!#180150002 +~~END~~ + +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + +-- check the status of the cursor should give 1073741825 +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +exec sys.sp_reset_connection +GO +~~START~~ +int +1073741825 +~~END~~ + + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + +~~START~~ +int +1073741824 +~~END~~ + +~~START~~ +int +180150000 +~~END~~ + +~~START~~ +int +0 +~~END~~ + + +-- Testing with only Cursor Prepare and Fetch but this time calling cursor close +DECLARE @stmt_handle int; +DECLARE @cursor_handle int; +DECLARE @cursor_handle2 int; +EXEC sp_cursorprepare @stmt_handle OUTPUT, N'', 'select i, d, c, u from babel_cursor_t1', 0, 2, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle OUTPUT, 2, 1; +EXEC sp_cursorfetch @cursor_handle, 2, 0, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle2 OUTPUT, 2, 1; +EXEC sp_cursorclose @cursor_handle; +EXEC sp_cursorclose @cursor_handle2; +EXEC sp_cursorunprepare @stmt_handle; +GO +~~START~~ +int#!#float#!#varchar#!#uniqueidentifier +1#!#1.1#!#a#!#1E984725-C51C-4BF4-9960-E1C80E27ABA0 +~~END~~ + + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + +-- check the status of the cursor should give 1073741825 +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +exec sys.sp_reset_connection +GO +~~START~~ +int +1073741825 +~~END~~ + + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle() +SELECT @@cursor_rows; +GO +~~START~~ +varchar#!#varchar#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#smallint#!#numeric#!#smallint#!#smallint#!#numeric#!#smallint#!#int +~~END~~ + +~~START~~ +int +1073741824 +~~END~~ + +~~START~~ +int +180150000 +~~END~~ + +~~START~~ +int +0 +~~END~~ + + +drop table babel_cursor_t1; +GO diff --git a/test/JDBC/input/storedProcedures/Test-sp_reset_connection.mix b/test/JDBC/input/storedProcedures/Test-sp_reset_connection.mix index ff2613caaa..9282b5adaf 100644 --- a/test/JDBC/input/storedProcedures/Test-sp_reset_connection.mix +++ b/test/JDBC/input/storedProcedures/Test-sp_reset_connection.mix @@ -98,4 +98,150 @@ GO -- tsql DROP DATABASE reset_con_db3 +GO + +-- Cursor reset testing +CREATE TABLE babel_cursor_t1 (i INT, d double precision, c varchar(10), u uniqueidentifier, v sql_variant); +INSERT INTO babel_cursor_t1 VALUES (1, 1.1, 'a', '1E984725-C51C-4BF4-9960-E1C80E27ABA0', 1); +INSERT INTO babel_cursor_t1 VALUES (2, 22.22, 'bb', '2E984725-C51C-4BF4-9960-E1C80E27ABA0', 22.22); +INSERT INTO babel_cursor_t1 VALUES (3, 333.333, 'cccc', '3E984725-C51C-4BF4-9960-E1C80E27ABA0', 'cccc'); +INSERT INTO babel_cursor_t1 VALUES (4, 4444.4444, 'dddddd', '4E984725-C51C-4BF4-9960-E1C80E27ABA0', cast('4E984725-C51C-4BF4-9960-E1C80E27ABA0' as uniqueidentifier)); +INSERT INTO babel_cursor_t1 VALUES (NULL, NULL, NULL, NULL, NULL); +GO + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go + +-- This will work since we declared 180150001 handle +EXEC sp_cursorfetch 180150001, 2, 0, 1; +GO +EXEC sp_cursor 180150001, 40, 1, 0; +GO + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO +-- This will not work since we 80150001 handle should have been cleaned up +EXEC sp_cursorfetch 180150001, 2, 0, 1; +GO +EXEC sp_cursor 180150001, 40, 1, 0; +GO + +-- Testing with only Cursor Open +DECLARE @cursor_handle int; +EXEC sp_cursoropen @cursor_handle OUTPUT, 'select i, d, c, u from babel_cursor_t1', 2, 8193; +GO + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO + +-- Testing with only Cursor Prepare +DECLARE @stmt_handle int; +EXEC sp_cursorprepare @stmt_handle OUTPUT, N'', 'select i, d, c, u from babel_cursor_t1', 0, 2, 1; +GO + +-- check the status of the cursor should give 1073741825 +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO + +-- Testing with only Cursor Prepare and Fetch +DECLARE @stmt_handle int; +DECLARE @cursor_handle int; +DECLARE @cursor_handle2 int; +EXEC sp_cursorprepare @stmt_handle OUTPUT, N'', 'select i, d, c, u from babel_cursor_t1', 0, 2, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle OUTPUT, 2, 1; +EXEC sp_cursorfetch @cursor_handle, 2, 0, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle2 OUTPUT, 2, 1; +GO + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go +-- check the status of the cursor should give 1073741825 +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +exec sys.sp_reset_connection +GO + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle(); +SELECT @@cursor_rows; +GO + +-- Testing with only Cursor Prepare and Fetch but this time calling cursor close +DECLARE @stmt_handle int; +DECLARE @cursor_handle int; +DECLARE @cursor_handle2 int; +EXEC sp_cursorprepare @stmt_handle OUTPUT, N'', 'select i, d, c, u from babel_cursor_t1', 0, 2, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle OUTPUT, 2, 1; +EXEC sp_cursorfetch @cursor_handle, 2, 0, 1; +EXEC sp_cursorexecute @stmt_handle, @cursor_handle2 OUTPUT, 2, 1; +EXEC sp_cursorclose @cursor_handle; +EXEC sp_cursorclose @cursor_handle2; +EXEC sp_cursorunprepare @stmt_handle; +GO + +-- check the status of the cursor +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +go +-- check the status of the cursor should give 1073741825 +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +exec sys.sp_reset_connection +GO + +exec sys.sp_reset_connection +GO + +-- Check cursor being cleaned up +DECLARE @Report CURSOR; +EXEC sys.sp_cursor_list @cursor_return = @Report OUTPUT,@cursor_scope = 2; +FETCH NEXT from @Report; WHILE (@@FETCH_STATUS <> -1) BEGIN FETCH NEXT from @Report; END; +SELECT sys.babelfish_pltsql_get_last_stmt_handle(); +SELECT sys.babelfish_pltsql_get_last_cursor_handle() +SELECT @@cursor_rows; +GO + +drop table babel_cursor_t1; GO \ No newline at end of file diff --git a/test/python/expected/sql_validation_framework/expected_create.out b/test/python/expected/sql_validation_framework/expected_create.out index 5e2238df1e..933bf058c1 100644 --- a/test/python/expected/sql_validation_framework/expected_create.out +++ b/test/python/expected/sql_validation_framework/expected_create.out @@ -71,7 +71,6 @@ Could not find tests for procedure sys.babel_drop_all_dbs Could not find tests for procedure sys.babel_drop_all_logins Could not find tests for procedure sys.babel_initialize_logins Could not find tests for procedure sys.printarg -Could not find tests for procedure sys.sp_cursor_list Could not find tests for procedure sys.sp_describe_cursor Could not find tests for table sys.babelfish_helpcollation Could not find tests for table sys.babelfish_syslanguages