4998
Item_func_sp::Item_func_sp(Name_resolution_context *context_arg,
5000
:Item_func(), context(context_arg), m_name(name_arg), m_sp(NULL),
5004
m_name->init_qname(current_thd);
5005
dummy_table= (TABLE*) sql_calloc(sizeof(TABLE));
5009
Item_func_sp::Item_func_sp(Name_resolution_context *context_arg,
5010
sp_name *name_arg, List<Item> &list)
5011
:Item_func(list), context(context_arg), m_name(name_arg), m_sp(NULL),
5015
m_name->init_qname(current_thd);
5016
dummy_table= (TABLE*) sql_calloc(sizeof(TABLE));
5003
Item_func_sp::Item_func_sp(Name_resolution_context *context_arg, sp_name *name)
5004
:Item_func(), context(context_arg), m_name(name), m_sp(NULL), sp_result_field(NULL)
5007
m_name->init_qname(current_thd);
5008
dummy_table= (TABLE*) sql_calloc(sizeof(TABLE));
5012
Item_func_sp::Item_func_sp(Name_resolution_context *context_arg,
5013
sp_name *name, List<Item> &list)
5014
:Item_func(list), context(context_arg), m_name(name), m_sp(NULL),sp_result_field(NULL)
5017
m_name->init_qname(current_thd);
5018
dummy_table= (TABLE*) sql_calloc(sizeof(TABLE));
5020
5023
Item_func_sp::cleanup()
5025
if (sp_result_field)
5024
delete result_field;
5027
delete sp_result_field;
5028
sp_result_field= NULL;
5028
5031
dummy_table->s= NULL;
5044
5047
system_charset_info);
5046
5049
qname.length(0);
5047
append_identifier(thd, &qname, m_name->m_db.str, m_name->m_db.length);
5050
if (m_name->m_explicit_name)
5052
append_identifier(thd, &qname, m_name->m_db.str, m_name->m_db.length);
5049
5055
append_identifier(thd, &qname, m_name->m_name.str, m_name->m_name.length);
5050
5056
return qname.ptr();
5055
Item_func_sp::sp_result_field(void) const
5058
DBUG_ENTER("Item_func_sp::sp_result_field");
5059
DBUG_PRINT("info", ("sp: %s, flags: %x, level: %lu",
5060
(m_sp ? "YES" : "NO"),
5061
(m_sp ? m_sp->m_flags : (uint)0),
5062
(m_sp ? m_sp->m_recursion_level : (ulong)0)));
5066
THD *thd= current_thd;
5067
if (!(m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name,
5068
&thd->sp_func_cache, TRUE)))
5070
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", m_name->m_qname.str);
5074
if (!dummy_table->s)
5076
char *empty_name= (char *) "";
5078
dummy_table->s= share= &dummy_table->share_not_to_be_used;
5079
dummy_table->alias = empty_name;
5080
dummy_table->maybe_null = maybe_null;
5081
dummy_table->in_use= current_thd;
5082
dummy_table->copy_blobs= TRUE;
5083
share->table_cache_key = empty_name;
5084
share->table_name = empty_name;
5086
if (!(field= m_sp->create_result_field(max_length, name, dummy_table)))
5087
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
5094
Execute function & store value in field
5098
1 value = NULL or error
5102
Item_func_sp::execute(Field **flp)
5062
@brief Initialize the result field by creating a temporary dummy table
5063
and assign it to a newly created field object. Meta data used to
5064
create the field is fetched from the sp_head belonging to the stored
5065
proceedure found in the stored procedure functon cache.
5067
@note This function should be called from fix_fields to init the result
5068
field. It is some what related to Item_field.
5072
@param thd A pointer to the session and thread context.
5074
@return Function return error status.
5075
@retval TRUE is returned on an error
5076
@retval FALSE is returned on success.
5079
Item_func_sp::init_result_field(THD *thd)
5081
DBUG_ENTER("Item_func_sp::init_result_field");
5083
char *empty_name= (char *) "";
5086
DBUG_ASSERT(m_sp == NULL);
5087
DBUG_ASSERT(sp_result_field == NULL);
5088
DBUG_ASSERT(dummy_table->s == NULL);
5090
if (!(m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name,
5091
&thd->sp_func_cache, TRUE)))
5093
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", m_name->m_qname.str);
5094
context->process_error(thd);
5099
A Field need to be attached to a Table.
5100
Below we "create" a dummy table by initializing
5101
the needed pointers.
5103
dummy_table->s= share= &dummy_table->share_not_to_be_used;
5104
dummy_table->alias = empty_name;
5105
dummy_table->maybe_null = maybe_null;
5106
dummy_table->in_use= thd;
5107
dummy_table->copy_blobs= TRUE;
5108
share->table_cache_key = empty_name;
5109
share->table_name = empty_name;
5111
if (!(sp_result_field= m_sp->create_result_field(max_length, name, dummy_table)))
5116
if (sp_result_field->pack_length() > sizeof(result_buf))
5118
sp_result_field->move_field(sql_alloc(sp_result_field->pack_length()));
5120
sp_result_field->move_field(result_buf);
5123
sp_result_field->null_ptr= (uchar *) &null_value;
5124
sp_result_field->null_bit= 1;
5131
@brief Initialize local members with values from the Field interface.
5133
@note called from Item::fix_fields.
5135
void Item_func_sp::fix_length_and_dec()
5137
DBUG_ENTER("Item_func_sp::fix_length_and_dec");
5139
DBUG_ASSERT(sp_result_field);
5140
decimals= sp_result_field->decimals();
5141
max_length= sp_result_field->field_length;
5142
collation.set(sp_result_field->charset());
5144
unsigned_flag= test(sp_result_field->flags & UNSIGNED_FLAG);
5150
@brief Execute function & store value in field.
5152
@return Function returns error status.
5153
@retval FALSE on success.
5154
@retval TRUE if an error occurred.
5158
Item_func_sp::execute()
5104
5160
THD *thd= current_thd;
5108
5163
Get field in virtual tmp table to store result. Create the field if
5109
5164
invoked first time.
5114
if (!(*flp= f= sp_result_field()))
5116
/* Error set by sp_result_field() */
5121
f->move_field((f->pack_length() > sizeof(result_buf)) ?
5122
sql_alloc(f->pack_length()) : result_buf);
5123
f->null_ptr= (uchar *)&null_value;
5127
5168
/* Execute function and store the return value in the field. */
5129
if (execute_impl(thd, f))
5170
if (execute_impl(thd))
5132
5173
context->process_error(thd);
5199
5244
enum enum_field_types
5200
5245
Item_func_sp::field_type() const
5203
5247
DBUG_ENTER("Item_func_sp::field_type");
5206
DBUG_RETURN(result_field->type());
5207
if ((field= sp_result_field()))
5209
enum_field_types result= field->type();
5211
DBUG_RETURN(result);
5213
DBUG_RETURN(MYSQL_TYPE_VARCHAR);
5248
DBUG_ASSERT(sp_result_field);
5249
DBUG_RETURN(sp_result_field->type());
5218
5253
Item_func_sp::result_type() const
5221
5255
DBUG_ENTER("Item_func_sp::result_type");
5222
5256
DBUG_PRINT("info", ("m_sp = %p", m_sp));
5225
DBUG_RETURN(result_field->result_type());
5226
if ((field= sp_result_field()))
5228
Item_result result= field->result_type();
5230
DBUG_RETURN(result);
5232
DBUG_RETURN(STRING_RESULT);
5236
Item_func_sp::fix_length_and_dec()
5239
DBUG_ENTER("Item_func_sp::fix_length_and_dec");
5243
decimals= result_field->decimals();
5244
max_length= result_field->field_length;
5245
collation.set(result_field->charset());
5249
if (!(field= sp_result_field()))
5251
context->process_error(current_thd);
5254
decimals= field->decimals();
5255
max_length= field->field_length;
5256
collation.set(field->charset());
5257
DBUG_ASSERT(sp_result_field);
5258
DBUG_RETURN(sp_result_field->result_type());
5263
5261
longlong Item_func_found_rows::val_int()
5271
5269
Item_func_sp::tmp_table_field(TABLE *t_arg)
5274
5271
DBUG_ENTER("Item_func_sp::tmp_table_field");
5277
field= m_sp->create_result_field(max_length, (const char*) name, t_arg);
5280
field= Item_func::tmp_table_field(t_arg);
5283
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
5273
DBUG_ASSERT(sp_result_field);
5274
DBUG_RETURN(sp_result_field);
5290
Find the function and check access rights to the function
5293
find_and_check_access()
5297
FALSE Access granted
5298
TRUE Requested access can't be granted or function doesn't exists
5301
Checks if requested access to function can be granted to user.
5279
@brief Checks if requested access to function can be granted to user.
5302
5280
If function isn't found yet, it searches function first.
5303
5281
If function can't be found or user don't have requested access
5304
5282
error is raised.
5284
@param thd thread handler
5286
@return Indication if the access was granted or not.
5287
@retval FALSE Access is granted.
5288
@retval TRUE Requested access can't be granted or function doesn't exists.
5308
Item_func_sp::find_and_check_access(THD *thd)
5293
Item_func_sp::sp_check_access(THD *thd)
5310
if (! m_sp && ! (m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name,
5311
&thd->sp_func_cache, TRUE)))
5313
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", m_name->m_qname.str);
5295
DBUG_ENTER("Item_func_sp::sp_check_access");
5317
5297
#ifndef NO_EMBEDDED_ACCESS_CHECKS
5318
5298
if (check_routine_access(thd, EXECUTE_ACL,
5319
5299
m_sp->m_db.str, m_sp->m_name.str, 0, FALSE))