Skip to content

Commit 96b3dd0

Browse files
committed
MDEV-39546 Unclear error message on OPEN strict_cursor FOR 'stmt'
This statement: OPEN strict_cursor_variable FOR 'SELECT ...'; caused an unclear error message: ERROR 4078: Illegal parameter data types row<2> and row<0> for operation 'OPEN..FOR' Fixing the error to be clearer: ERROR HY000: Incorrect usage of c0 and OPEN..FOR <dynamic string> Note, this statement is not supported: OPEN strict_cursor_variable FOR 'SELECT ...'; It can be rewritten: - either to use a SELECT statement instead of the dynamic string: OPEN strict_cursor_variable FOR SELECT ...; - or to make c0 a weak cursor variable (i.e.without the RETURN clause) OPEN weak_cursor_variable FOR 'SELECT ...';
1 parent c8bfb4d commit 96b3dd0

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

plugin/type_cursor/mysql-test/type_cursor/sp-ref_cursor.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ $$
5656
CALL p1;
5757
DROP PROCEDURE p1;
5858
#
59+
# Strict cursors cannot be opened from dynamic strings
60+
#
61+
CREATE PROCEDURE p1 AS
62+
TYPE rec0_t IS RECORD (a INT, b INT);
63+
TYPE cur0_t IS REF CURSOR RETURN rec0_t;
64+
c0 cur0_t;
65+
BEGIN
66+
OPEN c0 FOR 'SELECT 1,2';
67+
END;
68+
$$
69+
ERROR HY000: Incorrect usage of c0 and OPEN..FOR <dynamic string>
70+
#
5971
# Cover the deep_copy() call in LEX::sp_add_fetch_cursor().
6072
# Make sure that every FETCH statement uses
6173
# Column_definition::create_length_to_internal_length_string()

plugin/type_cursor/mysql-test/type_cursor/sp-ref_cursor.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ DELIMITER ;$$
7171
CALL p1;
7272
DROP PROCEDURE p1;
7373

74+
75+
--echo #
76+
--echo # Strict cursors cannot be opened from dynamic strings
77+
--echo #
78+
79+
DELIMITER $$;
80+
--error ER_WRONG_USAGE
81+
CREATE PROCEDURE p1 AS
82+
TYPE rec0_t IS RECORD (a INT, b INT);
83+
TYPE cur0_t IS REF CURSOR RETURN rec0_t;
84+
c0 cur0_t;
85+
BEGIN
86+
OPEN c0 FOR 'SELECT 1,2';
87+
END;
88+
$$
89+
DELIMITER ;$$
90+
91+
7492
--echo #
7593
--echo # Cover the deep_copy() call in LEX::sp_add_fetch_cursor().
7694
--echo # Make sure that every FETCH statement uses

sql/sql_lex.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7853,15 +7853,30 @@ bool LEX::sp_open_cursor_for_stmt(THD *thd, const LEX_CSTRING *name,
78537853
stmt->sql_command= SQLCOM_EXECUTE;
78547854
}
78557855

7856+
/*
7857+
This statement is not supported:
7858+
OPEN strict_cursor_variable FOR 'SELECT ...';
7859+
It can be rewritten:
7860+
- either to use a SELECT statement instead of the dynamic string:
7861+
OPEN strict_cursor_variable FOR SELECT ...;
7862+
- or to make c0 a weak cursor variable (i.e.without the RETURN clause)
7863+
OPEN weak_cursor_variable FOR 'SELECT ...';
7864+
*/
7865+
return_type_def= dynamic_cast<const sp_type_def_ref*>(spv[0].field_def.
7866+
get_attr_const_generic_ptr(0));
7867+
if (return_type_def && stmt->prepared_stmt.code())
7868+
{
7869+
my_error(ER_WRONG_USAGE, MYF(0), name->str, "OPEN..FOR <dynamic string>");
7870+
goto error;
7871+
}
7872+
78567873
/*
78577874
If the REF CURSOR declaration has the RETURN clause and
78587875
the query select list does not have asterisks, check
78597876
that the row sizes are equal.
78607877
A more thorough test (field-by-field assignability) is done
78617878
later, after the cursor has been opened.
78627879
*/
7863-
return_type_def= dynamic_cast<const sp_type_def_ref*>(spv[0].field_def.
7864-
get_attr_const_generic_ptr(0));
78657880
row_def_list=
78667881
return_type_def && return_type_def->def().is_row() ?
78677882
return_type_def->def().row_field_definitions() : nullptr;

0 commit comments

Comments
 (0)