5034
5040
system_charset_info);
5036
5042
qname.length(0);
5037
append_identifier(thd, &qname, m_name->m_db.str, m_name->m_db.length);
5043
if (m_name->m_explicit_name)
5045
append_identifier(thd, &qname, m_name->m_db.str, m_name->m_db.length);
5039
5048
append_identifier(thd, &qname, m_name->m_name.str, m_name->m_name.length);
5040
5049
return qname.ptr();
5045
Item_func_sp::sp_result_field(void) const
5048
DBUG_ENTER("Item_func_sp::sp_result_field");
5049
DBUG_PRINT("info", ("sp: %s, flags: %x, level: %lu",
5050
(m_sp ? "YES" : "NO"),
5051
(m_sp ? m_sp->m_flags : (uint)0),
5052
(m_sp ? m_sp->m_recursion_level : (ulong)0)));
5056
THD *thd= current_thd;
5057
if (!(m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name,
5058
&thd->sp_func_cache, TRUE)))
5060
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", m_name->m_qname.str);
5064
if (!dummy_table->alias)
5066
char *empty_name= (char *) "";
5067
dummy_table->alias= empty_name;
5068
dummy_table->maybe_null= maybe_null;
5069
dummy_table->in_use= current_thd;
5070
dummy_table->copy_blobs= TRUE;
5071
dummy_table->s->table_cache_key.str = empty_name;
5072
dummy_table->s->table_name.str= empty_name;
5073
dummy_table->s->db.str= empty_name;
5075
if (!(field= m_sp->create_result_field(max_length, name, dummy_table)))
5076
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
5083
Execute function & store value in field
5087
1 value = NULL or error
5091
Item_func_sp::execute(Field **flp)
5055
@brief Initialize the result field by creating a temporary dummy table
5056
and assign it to a newly created field object. Meta data used to
5057
create the field is fetched from the sp_head belonging to the stored
5058
proceedure found in the stored procedure functon cache.
5060
@note This function should be called from fix_fields to init the result
5061
field. It is some what related to Item_field.
5065
@param thd A pointer to the session and thread context.
5067
@return Function return error status.
5068
@retval TRUE is returned on an error
5069
@retval FALSE is returned on success.
5072
Item_func_sp::init_result_field(THD *thd)
5074
DBUG_ENTER("Item_func_sp::init_result_field");
5076
LEX_STRING empty_name= { C_STRING_WITH_LEN("") };
5080
DBUG_ASSERT(m_sp == NULL);
5081
DBUG_ASSERT(sp_result_field == NULL);
5083
if (!(m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name,
5084
&thd->sp_func_cache, TRUE)))
5086
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", m_name->m_qname.str);
5087
context->process_error(thd);
5092
A Field need to be attached to a Table.
5093
Below we "create" a dummy table by initializing
5094
the needed pointers.
5097
share= dummy_table->s;
5098
dummy_table->alias = "";
5099
dummy_table->maybe_null = maybe_null;
5100
dummy_table->in_use= thd;
5101
dummy_table->copy_blobs= TRUE;
5102
share->table_cache_key = empty_name;
5103
share->table_name = empty_name;
5105
if (!(sp_result_field= m_sp->create_result_field(max_length, name, dummy_table)))
5110
if (sp_result_field->pack_length() > sizeof(result_buf))
5112
sp_result_field->move_field(sql_alloc(sp_result_field->pack_length()));
5114
sp_result_field->move_field(result_buf);
5117
sp_result_field->null_ptr= (uchar *) &null_value;
5118
sp_result_field->null_bit= 1;
5125
@brief Initialize local members with values from the Field interface.
5127
@note called from Item::fix_fields.
5129
void Item_func_sp::fix_length_and_dec()
5131
DBUG_ENTER("Item_func_sp::fix_length_and_dec");
5133
DBUG_ASSERT(sp_result_field);
5134
decimals= sp_result_field->decimals();
5135
max_length= sp_result_field->field_length;
5136
collation.set(sp_result_field->charset());
5138
unsigned_flag= test(sp_result_field->flags & UNSIGNED_FLAG);
5144
@brief Execute function & store value in field.
5146
@return Function returns error status.
5147
@retval FALSE on success.
5148
@retval TRUE if an error occurred.
5152
Item_func_sp::execute()
5093
5154
THD *thd= current_thd;
5097
5157
Get field in virtual tmp table to store result. Create the field if
5098
5158
invoked first time.
5103
if (!(*flp= f= sp_result_field()))
5105
/* Error set by sp_result_field() */
5110
f->move_field((f->pack_length() > sizeof(result_buf)) ?
5111
sql_alloc(f->pack_length()) : result_buf);
5112
f->null_ptr= (uchar *)&null_value;
5116
5162
/* Execute function and store the return value in the field. */
5118
if (execute_impl(thd, f))
5164
if (execute_impl(thd))
5121
5167
context->process_error(thd);
5200
5250
enum enum_field_types
5201
5251
Item_func_sp::field_type() const
5204
5253
DBUG_ENTER("Item_func_sp::field_type");
5207
DBUG_RETURN(result_field->type());
5208
if ((field= sp_result_field()))
5210
enum_field_types result= field->type();
5212
DBUG_RETURN(result);
5214
DBUG_RETURN(MYSQL_TYPE_VARCHAR);
5254
DBUG_ASSERT(sp_result_field);
5255
DBUG_RETURN(sp_result_field->type());
5219
5259
Item_func_sp::result_type() const
5222
5261
DBUG_ENTER("Item_func_sp::result_type");
5223
DBUG_PRINT("info", ("m_sp: 0x%lx", (long) m_sp));
5226
DBUG_RETURN(result_field->result_type());
5227
if ((field= sp_result_field()))
5229
Item_result result= field->result_type();
5231
DBUG_RETURN(result);
5233
DBUG_RETURN(STRING_RESULT);
5237
Item_func_sp::fix_length_and_dec()
5240
DBUG_ENTER("Item_func_sp::fix_length_and_dec");
5244
decimals= result_field->decimals();
5245
max_length= result_field->field_length;
5246
collation.set(result_field->charset());
5250
if (!(field= sp_result_field()))
5252
context->process_error(current_thd);
5255
decimals= field->decimals();
5256
max_length= field->field_length;
5257
collation.set(field->charset());
5262
DBUG_PRINT("info", ("m_sp = %p", m_sp));
5263
DBUG_ASSERT(sp_result_field);
5264
DBUG_RETURN(sp_result_field->result_type());
5264
5267
longlong Item_func_found_rows::val_int()
5272
5275
Item_func_sp::tmp_table_field(TABLE *t_arg)
5275
5277
DBUG_ENTER("Item_func_sp::tmp_table_field");
5278
field= m_sp->create_result_field(max_length, (const char*) name, t_arg);
5281
field= Item_func::tmp_table_field(t_arg);
5284
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
5279
DBUG_ASSERT(sp_result_field);
5280
DBUG_RETURN(sp_result_field);
5291
Find the function and check access rights to the function
5294
find_and_check_access()
5298
FALSE Access granted
5299
TRUE Requested access can't be granted or function doesn't exists
5302
Checks if requested access to function can be granted to user.
5285
@brief Checks if requested access to function can be granted to user.
5303
5286
If function isn't found yet, it searches function first.
5304
5287
If function can't be found or user don't have requested access
5305
5288
error is raised.
5290
@param thd thread handler
5292
@return Indication if the access was granted or not.
5293
@retval FALSE Access is granted.
5294
@retval TRUE Requested access can't be granted or function doesn't exists.
5309
Item_func_sp::find_and_check_access(THD *thd)
5299
Item_func_sp::sp_check_access(THD *thd)
5311
if (! m_sp && ! (m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name,
5312
&thd->sp_func_cache, TRUE)))
5314
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", m_name->m_qname.str);
5301
DBUG_ENTER("Item_func_sp::sp_check_access");
5318
5303
#ifndef NO_EMBEDDED_ACCESS_CHECKS
5319
5304
if (check_routine_access(thd, EXECUTE_ACL,
5320
5305
m_sp->m_db.str, m_sp->m_name.str, 0, FALSE))
5343
5344
good idea especially if the view has SQL SECURITY DEFINER and
5344
5345
the used stored procedure has SQL SECURITY DEFINER.
5346
res= find_and_check_access(thd);
5347
res= sp_check_access(thd);
5347
5348
#ifndef NO_EMBEDDED_ACCESS_CHECKS
5350
Try to set and restore the security context to see whether it's valid
5348
5352
Security_context *save_secutiry_ctx;
5349
if (!res && !(res= set_routine_security_ctx(thd, m_sp, false,
5350
&save_secutiry_ctx)))
5352
sp_restore_security_context(thd, save_secutiry_ctx);
5353
res= set_routine_security_ctx(thd, m_sp, false, &save_secutiry_ctx);
5355
m_sp->m_security_ctx.restore_security_context(thd, save_secutiry_ctx);
5354
5357
#endif /* ! NO_EMBEDDED_ACCESS_CHECKS */