1
0
mirror of https://github.com/MariaDB/server.git synced 2026-10-23 18:49:29 +03:00
Files
mariadb/sql/sp_cursor.cc
Alexander Barkov 90f8178f33 MDEV-10152 Add support for TYPE .. IS REF CURSOR
Adding support for the strict cursor data types:

Example 1a:
  TYPE rec0_t IS RECORD (a INT, VARCHAR(10));
  TYPE cur0_t IS REF CURSOR RETURN rec0_t;

Example 1b:
  TYPE rec0_t IS RECORD (a t1.a%TYPE, b t1.b%TYPE);
  TYPE cur0_t IS REF CURSOR RETURN rec0_t;

Example 1c:
  TYPE rec0_t IS RECORD (a INT, VARCHAR(10));
  r0 rec0_t;
  TYPE cur0_t IS REF CURSOR RETURN r0%TYPE;

Example 1d:
  TYPE rec0_t IS RECORD (a t1.a%TYPE, b t1.b%TYPE);
  r0 rec0_t;
  TYPE cur0_t IS REF CURSOR RETURN r0%TYPE;

Example2a:
  TYPE cur0_t IS REF CURSOR RETURN t1%ROWTYPE; -- t1 is a table

Example 2b:
  r0 t1%ROWTYPE;
  TYPE cur0_t IS REF CURSOR RETURN r0%TYPE;

Example3a:
  CURSOR cursor_sample IS SELECT a,b FROM t1;
  TYPE cur0_t IS REF CURSOR RETURN cursor_sample%ROWTYPE;

Example3b:
  CURSOR cursor_sample IS SELECT a,b FROM t1;
  r0 cursor_sample%ROWTYPE;
  TYPE cur0_t IS REF CURSOR RETURN r0%TYPE;

If a cursor variable is declared with a RETURN clause then:
1. At OPEN type the data type of the SELECT list row is compared
  for compatibility with the cursor RETURN data type.
  The SELECT list row must be assignable to the RETURN type row.
  If case if assignability is not meet, an error is raised
  Assignability means:
  - The arity of the SELECT list must be equal to the arity
    of the RETURN clause
  - Every n-th field of the SELECT list must be assignable to the
    n-th field of the RETURN Clause

2. At FETCH time, the data is fetched in two steps:
  a. On the first step the data is fetched into a virtual table
     with the row type described in the RETURN clause
  b. On the second step the data is copied from the virtual table
     to the target fetch list. Data type conversion can happen
     on this step.

Change details:

Adding new methods:
- sp_cursor::check_assignability_to
- Virtual_tmp_table::check_assignability_from
- Virtual_tmp_table::sp_set_from_select_list
- Virtual_tmp_table::sp_save_in_vtable
- Virtual_tmp_table::sp_save_in_target_list
- LEX::check_ref_cursor_components
- LEX::make_sp_instr_copy_struct_for_last_context_variables
- LEX::declare_type_ref_cursor
- sp_cursor::Select_fetch_into_spvars::send_data_with_return_type

Adding new members:
- sp_instr_copen_by_ref::m_cursor_name
- Select_fetch_into_spvars::m_return_type
- Select_materialize::m_cursor_name
- Select_materialize::m_return_type

Adding new virtual methods:
- Item::resolve_spvar_cursor_rowtype
- Type_handler::Spvar_definition_resolve_type_refs
- Server_side_cursor::check_assignability_to
- Overriding Select_materialize::prepare to raise an error when the cursor
  returned data type is not compatible with the RETURN clause

Making these methods virtual:
- Field::check_assignability_from

Adding new classes:
- sp_type_def_ref
- RowTypeBuffer

Adding new constructors to:
- Spvar_definition

Adding new helper methods (e.g. to reuse the code)
- Field::store_field_maybe_null
- ChanBuffer::append_ulonglong
- sp_pcontext::set_type_for_last_context_variables

Minor changes:
- Making TABLE::export_structure const
- Overriding Item_splocal::type_extra_attributes. It was forgotten in earlier changes.

Adding new error messages
- ER_CANNOT_CAST_ON_IDENT1_ASSIGNMENT_FOR_OPERATION
- ER_CANNOT_CAST_ON_IDENT2_ASSIGNMENT_FOR_OPERATION
2026-04-06 15:16:38 +04:00

105 lines
3.3 KiB
C++

/*
Copyright (c) 2025, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
#ifdef MYSQL_SERVER
#include "mariadb.h"
#include "sql_class.h"
#include "sql_cursor.h"
bool sp_cursor::check_assignability_to(const Virtual_tmp_table *table,
const char *spvar_name,
const char *op) const
{
DBUG_ASSERT(server_side_cursor);
return server_side_cursor->check_assignability_to(table, spvar_name, op);
}
bool sp_cursor::check_for_open(THD *thd, bool check_open_cursor_counter) const
{
if (server_side_cursor)
{
my_message(ER_SP_CURSOR_ALREADY_OPEN,
ER_THD(thd, ER_SP_CURSOR_ALREADY_OPEN),
MYF(0));
return true;
}
if (check_open_cursor_counter &&
thd->open_cursors_counter() >= thd->variables.max_open_cursors)
{
my_error(ER_TOO_MANY_OPEN_CURSORS, MYF(0),
thd->variables.max_open_cursors);
return true;
}
return false;
}
/*
Append a new element into the array.
@return NULL Type_ref_null (with is_null()==true) on error (EOM).
@return not-NULL Type_ref_null on success
*/
Type_ref_null sp_cursor_array::append(THD *thd)
{
if (Dynamic_array::append(sp_cursor_array_element()))
{
DBUG_ASSERT(thd->is_error());
return Type_ref_null();
}
return Type_ref_null((ulonglong) size() - 1);
}
/*
Find a cursor by reference.
@param thd - the current THD
@param ref - the field behind a SYS_REFCURSOR SP variable
@param for_open - if the cursor is needed for OPEN rather than FETCH/CLOSE,
so a new cursor is appended if not found.
*/
sp_cursor_array_element *sp_cursor_array::get_cursor_by_ref(THD *thd,
Field *ref_field,
bool for_open)
{
Type_ref_null ref= ref_field->val_ref(thd);
if (ref < (ulonglong) elements())
return &at((size_t) ref.value());// "ref" points to an initialized sp_cursor
if (!for_open)
return nullptr;
/*
We are here when:
- The reference ref.is_null() is true, meaning that the
ref_field's SP variable is not linked to any cursors in "this" array:
* it is the very first "OPEN .. FOR STMT" command for ref_field.
* or the ref_field's SP variable was set to NULL explicitly.
- Or ref_field for some reasons returned a cursor offset outside
or the range [0..elements()-1].
*/
if (((ref= find_unused()).is_null() && (ref= append(thd)).is_null()) ||
ref_field->store_ref(ref, true/*no_conversions*/))
return nullptr;
at((size_t) ref.value()).reset(thd, 1/*ref count*/);
return &at((size_t) ref.value());
}
#endif // MYSQL_SERVER