Skip to content

Commit f11504a

Browse files
committed
MDEV-20034 Add support for the pre-defined weak SYS_REFCURSOR
This patch adds support for SYS_REFCURSOR (a weakly typed cursor) for both sql_mode=ORACLE and sql_mode=DEFAULT. Works as a regular stored routine variable, parameter and return value: - can be passed as an IN parameter to stored functions and procedures - can be passed as an INOUT and OUT parameter to stored procedures - can be returned from a stored function Note, strongly typed REF CURSOR will be added separately. Note, to maintain dependencies easier, some parts of sql_class.h and item.h were moved to new header files: - select_results.h: class select_result_sink class select_result class select_result_interceptor - sp_cursor.h: class sp_cursor_statistics class sp_cursor - sp_rcontext_handler.h class Sp_rcontext_handler and its descendants The implementation consists of the following parts: - A new class sp_cursor_array deriving from Dynamic_array - A new class Statement_rcontext which contains data shared between sub-statements of a compound statement. It has a member m_statement_cursors of the sp_cursor_array data type, as well as open cursor counter. THD inherits from Statement_rcontext. - A new data type handler Type_handler_sys_refcursor in plugins/type_cursor/ It is designed to store uint16 references - positions of the cursor in THD::m_statement_cursors. - Type_handler_sys_refcursor suppresses some derived numeric features. When a SYS_REFCURSOR variable is used as an integer an error is raised. - A new abstract class sp_instr_fetch_cursor. It's needed to share the common code between "OPEN cur" (for static cursors) and "OPER cur FOR stmt" (for SYS_REFCURSORs). - New sp_instr classes: * sp_instr_copen_by_ref - OPEN sys_ref_curor FOR stmt; * sp_instr_cfetch_by_ref - FETCH sys_ref_cursor INTO targets; * sp_instr_cclose_by_ref - CLOSE sys_ref_cursor; * sp_instr_destruct_variable - to destruct SYS_REFCURSOR variables when the execution goes out of the BEGIN..END block where SYS_REFCURSOR variables are declared. - New methods in LEX: * sp_open_cursor_for_stmt - handles "OPEN sys_ref_cursor FOR stmt". * sp_add_instr_fetch_cursor - "FETCH cur INTO targets" for both static cursors and SYS_REFCURSORs. * sp_close - handles "CLOSE cur" both for static cursors and SYS_REFCURSORs. - Changes in cursor functions to handle both static cursors and SYS_REFCURSORs: * Item_func_cursor_isopen * Item_func_cursor_found * Item_func_cursor_notfound * Item_func_cursor_rowcount - A new system variable @@max_open_cursors - to limit the number of cursors (static and SYS_REFCURSORs) opened at the same time. Its allowed range is [0-65536], with 50 by default. - A new virtual method Type_handler::can_return_bool() telling if calling item->val_bool() is allowed for Items of this data type, or if otherwise the "Illegal parameter for operation" error should be raised at fix_fields() time. - New methods in Sp_rcontext_handler: * get_cursor() * get_cursor_by_ref() - A new class Sp_rcontext_handler_statement to handle top level statement wide cursors which are shared by all substatements. - A new virtual method expr_event_handler() in classes Item and Field. It's needed to close (and make available for a new OPEN) unused THD::m_statement_cursors elements which do not have any references any more. It can happen in various moments in time, e.g. * after evaluation parameters of an SQL routine * after assigning a cursor expression into a SYS_REFCURSOR variable * when leaving a BEGIN..END block with SYS_REFCURSOR variables * after setting OUT/INOUT routine actual parameters from formal parameters.
1 parent 1e00b9e commit f11504a

File tree

183 files changed

+16896
-536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+16896
-536
lines changed

libmysqld/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ SET(SQL_EMBEDDED_SOURCES emb_qcache.cc libmysqld.c lib_sql.cc
157157
../sql/json_table.cc
158158
../sql/opt_histogram_json.cc
159159
../sql/sp_instr.cc
160+
../sql/sp_cursor.cc
160161
${GEN_SOURCES}
161162
${MYSYS_LIBWRAP_SOURCE}
162163
)

mysql-test/include/bool_to_char.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SET @sql_mode=@@sql_mode;
2+
3+
SET sql_mode=ORACLE;
4+
5+
DELIMITER /;
6+
CREATE FUNCTION bool_to_char(b BOOLEAN) RETURN VARCHAR AS
7+
BEGIN
8+
RETURN
9+
CASE
10+
WHEN b IS NULL THEN 'NULL'
11+
WHEN b THEN 'true'
12+
WHEN NOT b THEN 'false'
13+
END;
14+
END;
15+
/
16+
DELIMITER ;/
17+
18+
SET sql_mode=@sql_mode;

mysql-test/include/dbms_output.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SET @sql_mode=@@sql_mode;
2+
3+
SET sql_mode=ORACLE;
4+
5+
DELIMITER /;
6+
7+
CREATE PACKAGE DBMS_OUTPUT AS
8+
PROCEDURE PUT_LINE(s VARCHAR);
9+
END;
10+
/
11+
12+
CREATE PACKAGE BODY DBMS_OUTPUT AS
13+
PROCEDURE PUT_LINE(s VARCHAR) AS
14+
BEGIN
15+
SELECT s AS `` FROM DUAL;
16+
END;
17+
END;
18+
/
19+
20+
DELIMITER ;/
21+
22+
SET sql_mode=@sql_mode;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Fetch one value from an open SYS_REFCURSOR
2+
3+
SET @sql_mode=@@sql_mode;
4+
5+
SET sql_mode=ORACLE;
6+
DELIMITER /;
7+
CREATE FUNCTION fetch_one_value(c SYS_REFCURSOR) RETURN VARCHAR AS
8+
v VARCHAR(128) :='none';
9+
BEGIN
10+
IF c%ISOPEN THEN
11+
FETCH c INTO v;
12+
END IF;
13+
RETURN v;
14+
END;
15+
/
16+
DELIMITER ;/
17+
SET sql_mode=@sql_mode;

mysql-test/main/mysqld--help.result

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ The following specify which files/extra groups are read (specified before remain
658658
max_join_size records return an error
659659
--max-length-for-sort-data=#
660660
Max number of bytes in sorted records
661+
--max-open-cursors=#
662+
The maximum number of open cursors allowed per session
661663
--max-password-errors=#
662664
If there is more than this number of failed connect
663665
attempts due to invalid password, user will be blocked
@@ -1817,6 +1819,7 @@ max-error-count 64
18171819
max-heap-table-size 16777216
18181820
max-join-size 18446744073709551615
18191821
max-length-for-sort-data 1024
1822+
max-open-cursors 50
18201823
max-password-errors 18446744073709551615
18211824
max-prepared-stmt-count 16382
18221825
max-recursive-iterations 1000
@@ -1932,7 +1935,7 @@ performance-schema-max-socket-classes 10
19321935
performance-schema-max-socket-instances -1
19331936
performance-schema-max-sql-text-length 1024
19341937
performance-schema-max-stage-classes 160
1935-
performance-schema-max-statement-classes 223
1938+
performance-schema-max-statement-classes 227
19361939
performance-schema-max-statement-stack 10
19371940
performance-schema-max-table-handles -1
19381941
performance-schema-max-table-instances -1

0 commit comments

Comments
 (0)