1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

MDEV-36047 Package body variables are not allowed as FETCH targets

It was not possible to use a package body variable as a
fetch target:

CREATE PACKAGE BODY pkg AS
  vc INT := 0;
  FUNCTION f1 RETURN INT AS
    CURSOR cur IS SELECT 1 AS c FROM DUAL;
  BEGIN
    OPEN cur;
    FETCH cur INTO vc; -- this returned "Undeclared variable: vc" error.
    CLOSE cur;
    RETURN vc;
  END;
END;

FETCH assumed that all fetch targets reside of the same sp_rcontext
instance with the cursor. This patch fixes the problem.
Now a cursor and its fetch target can reside in different sp_rcontext
instances.

Details:

- Adding a helper class sp_rcontext_addr
  (a combination of Sp_rcontext_handler pointer and an offset in the rcontext)

- Adding a new class sp_fetch_target deriving from sp_rcontext_addr.
  Fetch targets in "FETCH cur INTO target1, target2 ..." are now collected
  into this structure instead of sp_variable.
  sp_variable cannot be used any more to store fetch targets,
  because it does not have a pointer to Sp_rcontext_handler
  (it only has the current rcontext offset).

- Removing members sp_instr_set members m_rcontext_handler and m_offset.
  Deriving sp_instr_set from sp_rcontext_addr instead.

- Renaming sp_instr_cfetch member  "List<sp_variable> m_varlist"
  to "List<sp_fetch_target> m_fetch_target_list".

- Fixing LEX::sp_add_cfetch() to return the pointer to the
  created sp_fetch_target instance (instead of returning bool).
  This helps to make the grammar in sql_yacc.c simpler

- Renaming LEX::sp_add_cfetch() to LEX::sp_add_instr_cfetch(),
  as `if(sp_add_cfetch())` changed its meaning to the opposite,
  to avoid automatic wrong merge from earlier versions.

- Chaning the "List<sp_variable> *vars" parameter to sp_cursor::fetch
  to have the data type "List<sp_fetch_target> *".

- Changing the data type of "List<sp_variable> &vars" in
  sp_cursor::Select_fetch_into_spvars::send_data_to_variable_list()
  to "List<sp_fetch_target> &".

- Adding THD helper methods get_rcontext() and get_variable().

- Moving the code from sql_yacc.yy into a new LEX method
  LEX::make_fetch_target().

- Simplifying the grammar in sql_yacc.yy using the new LEX method.
  Changing the data type of the bison rule sp_fetch_list from "void"
  to "List<sp_fetch_target> *".
This commit is contained in:
Alexander Barkov
2025-02-08 17:39:27 +04:00
parent 6be0940f10
commit b7d67ceb5f
25 changed files with 731 additions and 72 deletions

View File

@@ -6556,6 +6556,21 @@ LEX::find_variable(const LEX_CSTRING *name,
}
sp_fetch_target *LEX::make_fetch_target(THD *thd, const Lex_ident_sys_st &name)
{
sp_pcontext *spc;
const Sp_rcontext_handler *rha;
sp_variable *spv= find_variable(&name, &spc, &rha);
if (unlikely(!spv))
{
my_error(ER_SP_UNDECLARED_VAR, MYF(0), name.str);
return nullptr;
}
return new (thd->mem_root) sp_fetch_target(name,
sp_rcontext_addr(rha, spv->offset));
}
static bool is_new(const char *str)
{
return (str[0] == 'n' || str[0] == 'N') &&
@@ -7234,8 +7249,11 @@ bool LEX::sp_for_loop_cursor_iterate(THD *thd, const Lex_for_loop_st &loop)
spcont, loop.m_cursor_offset, false);
if (unlikely(instr == NULL) || unlikely(sphead->add_instr(instr)))
return true;
instr->add_to_varlist(loop.m_index);
return false;
const sp_rcontext_addr raddr(&sp_rcontext_handler_local,
loop.m_index->offset);
sp_fetch_target *trg=
new (thd->mem_root) sp_fetch_target(loop.m_index->name, raddr);
return !trg || instr->add_to_fetch_target_list(trg);
}
@@ -9321,7 +9339,7 @@ int set_statement_var_if_exists(THD *thd, const char *var_name,
}
bool LEX::sp_add_cfetch(THD *thd, const LEX_CSTRING *name)
sp_instr_cfetch *LEX::sp_add_instr_cfetch(THD *thd, const LEX_CSTRING *name)
{
uint offset;
sp_instr_cfetch *i;
@@ -9329,14 +9347,14 @@ bool LEX::sp_add_cfetch(THD *thd, const LEX_CSTRING *name)
if (!spcont->find_cursor(name, &offset, false))
{
my_error(ER_SP_CURSOR_MISMATCH, MYF(0), name->str);
return true;
return nullptr;
}
i= new (thd->mem_root)
sp_instr_cfetch(sphead->instructions(), spcont, offset,
!(thd->variables.sql_mode & MODE_ORACLE));
if (unlikely(i == NULL) || unlikely(sphead->add_instr(i)))
return true;
return false;
return nullptr;
return i;
}