2002-12-12 13:14:23 +01:00
|
|
|
/* Copyright (C) 2002 MySQL AB
|
|
|
|
|
|
|
|
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
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2002-12-12 13:14:23 +01:00
|
|
|
|
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
|
|
#include "mysql_priv.h"
|
|
|
|
#include "sp.h"
|
|
|
|
#include "sp_head.h"
|
2003-07-03 15:58:37 +02:00
|
|
|
#include "sp_cache.h"
|
2005-07-09 19:51:59 +02:00
|
|
|
#include "sql_trigger.h"
|
2003-07-01 18:14:24 +02:00
|
|
|
|
2006-03-02 13:18:49 +01:00
|
|
|
#include <my_user.h>
|
|
|
|
|
2004-06-09 14:19:43 +02:00
|
|
|
static bool
|
|
|
|
create_string(THD *thd, String *buf,
|
2003-12-12 14:05:29 +01:00
|
|
|
int sp_type,
|
2004-03-11 17:18:59 +01:00
|
|
|
sp_name *name,
|
2003-12-12 14:05:29 +01:00
|
|
|
const char *params, ulong paramslen,
|
|
|
|
const char *returns, ulong returnslen,
|
|
|
|
const char *body, ulong bodylen,
|
2006-03-02 13:18:49 +01:00
|
|
|
st_sp_chistics *chistics,
|
|
|
|
const LEX_STRING *definer_user,
|
|
|
|
const LEX_STRING *definer_host);
|
2005-11-23 00:11:19 +01:00
|
|
|
static int
|
|
|
|
db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp,
|
|
|
|
ulong sql_mode, const char *params, const char *returns,
|
|
|
|
const char *body, st_sp_chistics &chistics,
|
|
|
|
const char *definer, longlong created, longlong modified);
|
2003-12-12 14:05:29 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* DB storage of Stored PROCEDUREs and FUNCTIONs
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-12-12 14:05:29 +01:00
|
|
|
enum
|
|
|
|
{
|
2003-12-16 19:14:10 +01:00
|
|
|
MYSQL_PROC_FIELD_DB = 0,
|
2003-12-12 14:05:29 +01:00
|
|
|
MYSQL_PROC_FIELD_NAME,
|
|
|
|
MYSQL_PROC_FIELD_TYPE,
|
|
|
|
MYSQL_PROC_FIELD_SPECIFIC_NAME,
|
|
|
|
MYSQL_PROC_FIELD_LANGUAGE,
|
|
|
|
MYSQL_PROC_FIELD_ACCESS,
|
|
|
|
MYSQL_PROC_FIELD_DETERMINISTIC,
|
|
|
|
MYSQL_PROC_FIELD_SECURITY_TYPE,
|
|
|
|
MYSQL_PROC_FIELD_PARAM_LIST,
|
|
|
|
MYSQL_PROC_FIELD_RETURNS,
|
|
|
|
MYSQL_PROC_FIELD_BODY,
|
|
|
|
MYSQL_PROC_FIELD_DEFINER,
|
|
|
|
MYSQL_PROC_FIELD_CREATED,
|
|
|
|
MYSQL_PROC_FIELD_MODIFIED,
|
|
|
|
MYSQL_PROC_FIELD_SQL_MODE,
|
|
|
|
MYSQL_PROC_FIELD_COMMENT,
|
|
|
|
MYSQL_PROC_FIELD_COUNT
|
|
|
|
};
|
2003-10-30 10:25:45 +01:00
|
|
|
|
2005-05-05 14:20:53 +02:00
|
|
|
/* Tells what SP_DEFAULT_ACCESS should be mapped to */
|
|
|
|
#define SP_DEFAULT_ACCESS_MAPPING SP_CONTAINS_SQL
|
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Close mysql.proc, opened with open_proc_table_for_read().
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
close_proc_table()
|
2005-08-08 15:46:06 +02:00
|
|
|
thd Thread context
|
|
|
|
backup Pointer to Open_tables_state instance which holds
|
|
|
|
information about tables which were open before we
|
|
|
|
decided to access mysql.proc.
|
2005-07-13 11:48:13 +02:00
|
|
|
*/
|
|
|
|
|
2005-08-08 15:46:06 +02:00
|
|
|
void close_proc_table(THD *thd, Open_tables_state *backup)
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2005-07-13 11:48:13 +02:00
|
|
|
close_thread_tables(thd);
|
2005-08-08 15:46:06 +02:00
|
|
|
thd->restore_backup_open_tables_state(backup);
|
2005-07-13 11:48:13 +02:00
|
|
|
}
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Open the mysql.proc table for read.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
open_proc_table_for_read()
|
2005-08-08 15:46:06 +02:00
|
|
|
thd Thread context
|
|
|
|
backup Pointer to Open_tables_state instance where information about
|
|
|
|
currently open tables will be saved, and from which will be
|
|
|
|
restored when we will end work with mysql.proc.
|
2005-07-13 11:48:13 +02:00
|
|
|
|
|
|
|
NOTES
|
|
|
|
Thanks to restrictions which we put on opening and locking of
|
|
|
|
this table for writing, we can open and lock it for reading
|
|
|
|
even when we already have some other tables open and locked.
|
|
|
|
One must call close_proc_table() to close table opened with
|
|
|
|
this call.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 Error
|
|
|
|
# Pointer to TABLE object of mysql.proc
|
|
|
|
*/
|
|
|
|
|
2005-08-08 15:46:06 +02:00
|
|
|
TABLE *open_proc_table_for_read(THD *thd, Open_tables_state *backup)
|
2005-07-13 11:48:13 +02:00
|
|
|
{
|
|
|
|
TABLE_LIST tables;
|
|
|
|
TABLE *table;
|
2005-09-15 01:56:09 +02:00
|
|
|
bool not_used;
|
2005-07-13 11:48:13 +02:00
|
|
|
DBUG_ENTER("open_proc_table");
|
2005-05-19 03:48:22 +02:00
|
|
|
|
2005-08-08 15:46:06 +02:00
|
|
|
thd->reset_n_backup_open_tables_state(backup);
|
2005-07-13 11:48:13 +02:00
|
|
|
|
|
|
|
bzero((char*) &tables, sizeof(tables));
|
|
|
|
tables.db= (char*) "mysql";
|
|
|
|
tables.table_name= tables.alias= (char*)"proc";
|
2005-09-15 01:56:09 +02:00
|
|
|
if (!(table= open_table(thd, &tables, thd->mem_root, ¬_used,
|
2005-07-13 11:48:13 +02:00
|
|
|
MYSQL_LOCK_IGNORE_FLUSH)))
|
2004-09-01 18:00:41 +02:00
|
|
|
{
|
2005-08-08 15:46:06 +02:00
|
|
|
thd->restore_backup_open_tables_state(backup);
|
2005-07-13 11:48:13 +02:00
|
|
|
DBUG_RETURN(0);
|
2004-09-01 18:00:41 +02:00
|
|
|
}
|
2003-03-02 19:17:41 +01:00
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
DBUG_ASSERT(table->s->system_table);
|
|
|
|
|
|
|
|
table->reginfo.lock_type= TL_READ;
|
|
|
|
/*
|
2005-08-08 15:46:06 +02:00
|
|
|
We have to ensure we are not blocked by a flush tables, as this
|
|
|
|
could lead to a deadlock if we have other tables opened.
|
2005-07-13 11:48:13 +02:00
|
|
|
*/
|
|
|
|
if (!(thd->lock= mysql_lock_tables(thd, &table, 1,
|
2005-09-15 01:56:09 +02:00
|
|
|
MYSQL_LOCK_IGNORE_FLUSH, ¬_used)))
|
2005-07-13 11:48:13 +02:00
|
|
|
{
|
2005-08-08 15:46:06 +02:00
|
|
|
close_proc_table(thd, backup);
|
2005-07-13 11:48:13 +02:00
|
|
|
DBUG_RETURN(0);
|
2003-02-28 15:07:14 +01:00
|
|
|
}
|
2005-07-13 11:48:13 +02:00
|
|
|
DBUG_RETURN(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Open the mysql.proc table for update.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
open_proc_table_for_update()
|
|
|
|
thd Thread context
|
|
|
|
|
|
|
|
NOTES
|
|
|
|
Table opened with this call should closed using close_thread_tables().
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 Error
|
|
|
|
# Pointer to TABLE object of mysql.proc
|
|
|
|
*/
|
|
|
|
|
|
|
|
static TABLE *open_proc_table_for_update(THD *thd)
|
|
|
|
{
|
|
|
|
TABLE_LIST tables;
|
|
|
|
TABLE *table;
|
|
|
|
DBUG_ENTER("open_proc_table");
|
|
|
|
|
|
|
|
bzero((char*) &tables, sizeof(tables));
|
|
|
|
tables.db= (char*) "mysql";
|
|
|
|
tables.table_name= tables.alias= (char*)"proc";
|
|
|
|
tables.lock_type= TL_WRITE;
|
|
|
|
|
|
|
|
table= open_ltable(thd, &tables, TL_WRITE);
|
|
|
|
|
|
|
|
DBUG_RETURN(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Find row in open mysql.proc table representing stored routine.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
db_find_routine_aux()
|
|
|
|
thd Thread context
|
|
|
|
type Type of routine to find (function or procedure)
|
|
|
|
name Name of routine
|
|
|
|
table TABLE object for open mysql.proc table.
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
SP_OK - Routine found
|
|
|
|
SP_KEY_NOT_FOUND- No routine with given name
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
db_find_routine_aux(THD *thd, int type, sp_name *name, TABLE *table)
|
|
|
|
{
|
2005-07-13 13:08:13 +02:00
|
|
|
byte key[MAX_KEY_LENGTH]; // db, name, optional key length type
|
2005-07-13 11:48:13 +02:00
|
|
|
DBUG_ENTER("db_find_routine_aux");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("type: %d name: %.*s",
|
2005-07-13 11:48:13 +02:00
|
|
|
type, name->m_name.length, name->m_name.str));
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2005-04-04 15:43:25 +02:00
|
|
|
/*
|
|
|
|
Create key to find row. We have to use field->store() to be able to
|
|
|
|
handle VARCHAR and CHAR fields.
|
|
|
|
Assumption here is that the three first fields in the table are
|
|
|
|
'db', 'name' and 'type' and the first key is the primary key over the
|
|
|
|
same fields.
|
|
|
|
*/
|
2005-05-31 18:36:32 +02:00
|
|
|
if (name->m_name.length > table->field[1]->field_length)
|
|
|
|
DBUG_RETURN(SP_KEY_NOT_FOUND);
|
2005-04-04 15:43:25 +02:00
|
|
|
table->field[0]->store(name->m_db.str, name->m_db.length, &my_charset_bin);
|
|
|
|
table->field[1]->store(name->m_name.str, name->m_name.length,
|
|
|
|
&my_charset_bin);
|
2005-09-14 00:41:44 +02:00
|
|
|
table->field[2]->store((longlong) type, TRUE);
|
2005-04-04 15:43:25 +02:00
|
|
|
key_copy(key, table->record[0], table->key_info,
|
|
|
|
table->key_info->key_length);
|
|
|
|
|
2002-12-12 13:14:23 +01:00
|
|
|
if (table->file->index_read_idx(table->record[0], 0,
|
2005-04-04 15:43:25 +02:00
|
|
|
key, table->key_info->key_length,
|
2002-12-12 13:14:23 +01:00
|
|
|
HA_READ_KEY_EXACT))
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_RETURN(SP_KEY_NOT_FOUND);
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_RETURN(SP_OK);
|
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
/*
|
|
|
|
Find routine definition in mysql.proc table and create corresponding
|
|
|
|
sp_head object for it.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
db_find_routine()
|
|
|
|
thd Thread context
|
|
|
|
type Type of routine (TYPE_ENUM_PROCEDURE/...)
|
|
|
|
name Name of routine
|
|
|
|
sphp Out parameter in which pointer to created sp_head
|
|
|
|
object is returned (0 in case of error).
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
This function may damage current LEX during execution, so it is good
|
|
|
|
idea to create temporary LEX and make it active before calling it.
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
0 - Success
|
|
|
|
non-0 - Error (may be one of special codes like SP_KEY_NOT_FOUND)
|
|
|
|
*/
|
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
static int
|
2004-02-17 17:36:53 +01:00
|
|
|
db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
|
|
|
TABLE *table;
|
2003-12-12 14:05:29 +01:00
|
|
|
const char *params, *returns, *body;
|
2003-02-21 17:37:05 +01:00
|
|
|
int ret;
|
2003-12-13 16:40:52 +01:00
|
|
|
const char *definer;
|
2003-05-06 18:09:20 +02:00
|
|
|
longlong created;
|
|
|
|
longlong modified;
|
2003-12-13 16:40:52 +01:00
|
|
|
st_sp_chistics chistics;
|
2003-05-06 18:09:20 +02:00
|
|
|
char *ptr;
|
|
|
|
uint length;
|
|
|
|
char buff[65];
|
2003-07-03 15:58:37 +02:00
|
|
|
String str(buff, sizeof(buff), &my_charset_bin);
|
2003-12-11 12:23:50 +01:00
|
|
|
ulong sql_mode;
|
2005-08-08 15:46:06 +02:00
|
|
|
Open_tables_state open_tables_state_backup;
|
2003-12-21 11:48:03 +01:00
|
|
|
DBUG_ENTER("db_find_routine");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("type: %d name: %.*s",
|
2004-02-17 17:36:53 +01:00
|
|
|
type, name->m_name.length, name->m_name.str));
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
*sphp= 0; // In case of errors
|
2005-08-08 15:46:06 +02:00
|
|
|
if (!(table= open_proc_table_for_read(thd, &open_tables_state_backup)))
|
2005-07-13 11:48:13 +02:00
|
|
|
DBUG_RETURN(SP_OPEN_TABLE_FAILED);
|
|
|
|
|
|
|
|
if ((ret= db_find_routine_aux(thd, type, name, table)) != SP_OK)
|
2003-02-21 17:37:05 +01:00
|
|
|
goto done;
|
2003-10-30 10:25:45 +01:00
|
|
|
|
2005-01-06 12:00:13 +01:00
|
|
|
if (table->s->fields != MYSQL_PROC_FIELD_COUNT)
|
2003-10-30 10:25:45 +01:00
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2004-10-14 18:07:09 +02:00
|
|
|
bzero((char *)&chistics, sizeof(chistics));
|
2004-11-09 02:58:44 +01:00
|
|
|
if ((ptr= get_field(thd->mem_root,
|
2004-10-14 18:07:09 +02:00
|
|
|
table->field[MYSQL_PROC_FIELD_ACCESS])) == NULL)
|
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
switch (ptr[0]) {
|
|
|
|
case 'N':
|
|
|
|
chistics.daccess= SP_NO_SQL;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
chistics.daccess= SP_CONTAINS_SQL;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
chistics.daccess= SP_READS_SQL_DATA;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
chistics.daccess= SP_MODIFIES_SQL_DATA;
|
|
|
|
break;
|
|
|
|
default:
|
2005-05-05 14:20:53 +02:00
|
|
|
chistics.daccess= SP_DEFAULT_ACCESS_MAPPING;
|
2004-10-14 18:07:09 +02:00
|
|
|
}
|
|
|
|
|
2004-11-09 02:58:44 +01:00
|
|
|
if ((ptr= get_field(thd->mem_root,
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_DETERMINISTIC])) == NULL)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
2003-12-13 16:40:52 +01:00
|
|
|
chistics.detistic= (ptr[0] == 'N' ? FALSE : TRUE);
|
2003-05-06 18:09:20 +02:00
|
|
|
|
2004-11-09 02:58:44 +01:00
|
|
|
if ((ptr= get_field(thd->mem_root,
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_SECURITY_TYPE])) == NULL)
|
2003-05-06 18:09:20 +02:00
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
2004-10-14 18:07:09 +02:00
|
|
|
chistics.suid= (ptr[0] == 'I' ? SP_IS_NOT_SUID : SP_IS_SUID);
|
2003-05-06 18:09:20 +02:00
|
|
|
|
2004-11-09 02:58:44 +01:00
|
|
|
if ((params= get_field(thd->mem_root,
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_PARAM_LIST])) == NULL)
|
|
|
|
{
|
|
|
|
params= "";
|
|
|
|
}
|
2003-05-06 18:09:20 +02:00
|
|
|
|
2003-12-12 14:05:29 +01:00
|
|
|
if (type == TYPE_ENUM_PROCEDURE)
|
|
|
|
returns= "";
|
2004-11-09 02:58:44 +01:00
|
|
|
else if ((returns= get_field(thd->mem_root,
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_RETURNS])) == NULL)
|
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2004-11-09 02:58:44 +01:00
|
|
|
if ((body= get_field(thd->mem_root,
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_BODY])) == NULL)
|
2003-05-06 18:09:20 +02:00
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
2003-12-12 14:05:29 +01:00
|
|
|
|
|
|
|
// Get additional information
|
2004-11-09 02:58:44 +01:00
|
|
|
if ((definer= get_field(thd->mem_root,
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_DEFINER])) == NULL)
|
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
modified= table->field[MYSQL_PROC_FIELD_MODIFIED]->val_int();
|
|
|
|
created= table->field[MYSQL_PROC_FIELD_CREATED]->val_int();
|
2003-05-06 18:09:20 +02:00
|
|
|
|
2003-12-21 01:07:45 +01:00
|
|
|
sql_mode= (ulong) table->field[MYSQL_PROC_FIELD_SQL_MODE]->val_int();
|
2003-12-11 12:23:50 +01:00
|
|
|
|
2003-10-30 10:25:45 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_COMMENT]->val_str(&str, &str);
|
|
|
|
|
2003-05-06 18:09:20 +02:00
|
|
|
ptr= 0;
|
2003-06-02 11:25:01 +02:00
|
|
|
if ((length= str.length()))
|
2003-12-12 14:05:29 +01:00
|
|
|
ptr= thd->strmake(str.ptr(), length);
|
2003-12-13 16:40:52 +01:00
|
|
|
chistics.comment.str= ptr;
|
|
|
|
chistics.comment.length= length;
|
2003-05-06 18:09:20 +02:00
|
|
|
|
2005-08-08 15:46:06 +02:00
|
|
|
close_proc_table(thd, &open_tables_state_backup);
|
2005-07-13 11:48:13 +02:00
|
|
|
table= 0;
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
ret= db_load_routine(thd, type, name, sphp,
|
|
|
|
sql_mode, params, returns, body, chistics,
|
|
|
|
definer, created, modified);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (table)
|
|
|
|
close_proc_table(thd, &open_tables_state_backup);
|
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2004-03-11 17:18:59 +01:00
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
static int
|
|
|
|
db_load_routine(THD *thd, int type, sp_name *name, sp_head **sphp,
|
|
|
|
ulong sql_mode, const char *params, const char *returns,
|
|
|
|
const char *body, st_sp_chistics &chistics,
|
|
|
|
const char *definer, longlong created, longlong modified)
|
|
|
|
{
|
2006-01-05 23:47:49 +01:00
|
|
|
LEX *old_lex= thd->lex, newlex;
|
2005-11-23 00:11:19 +01:00
|
|
|
String defstr;
|
2006-09-27 16:21:29 +02:00
|
|
|
char old_db_buf[NAME_LEN+1];
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
LEX_STRING old_db= { old_db_buf, sizeof(old_db_buf) };
|
2005-11-23 00:11:19 +01:00
|
|
|
bool dbchanged;
|
|
|
|
ulong old_sql_mode= thd->variables.sql_mode;
|
2006-01-05 23:47:49 +01:00
|
|
|
ha_rows old_select_limit= thd->variables.select_limit;
|
|
|
|
sp_rcontext *old_spcont= thd->spcont;
|
2006-03-02 13:18:49 +01:00
|
|
|
|
2006-09-27 16:21:29 +02:00
|
|
|
char definer_user_name_holder[USERNAME_LENGTH + 1];
|
2006-03-02 13:18:49 +01:00
|
|
|
LEX_STRING_WITH_INIT definer_user_name(definer_user_name_holder,
|
2006-09-27 16:21:29 +02:00
|
|
|
USERNAME_LENGTH);
|
2006-03-02 13:18:49 +01:00
|
|
|
|
|
|
|
char definer_host_name_holder[HOSTNAME_LENGTH + 1];
|
|
|
|
LEX_STRING_WITH_INIT definer_host_name(definer_host_name_holder,
|
|
|
|
HOSTNAME_LENGTH);
|
|
|
|
|
2006-01-05 23:47:49 +01:00
|
|
|
int ret;
|
2005-11-23 00:11:19 +01:00
|
|
|
|
|
|
|
thd->variables.sql_mode= sql_mode;
|
|
|
|
thd->variables.select_limit= HA_POS_ERROR;
|
|
|
|
|
|
|
|
thd->lex= &newlex;
|
|
|
|
newlex.current_select= NULL;
|
|
|
|
|
2006-03-02 13:18:49 +01:00
|
|
|
parse_user(definer, strlen(definer),
|
|
|
|
definer_user_name.str, &definer_user_name.length,
|
|
|
|
definer_host_name.str, &definer_host_name.length);
|
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
defstr.set_charset(system_charset_info);
|
2006-03-02 13:18:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
We have to add DEFINER clause and provide proper routine characterstics in
|
|
|
|
routine definition statement that we build here to be able to use this
|
|
|
|
definition for SHOW CREATE PROCEDURE later.
|
|
|
|
*/
|
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
if (!create_string(thd, &defstr,
|
|
|
|
type,
|
|
|
|
name,
|
|
|
|
params, strlen(params),
|
|
|
|
returns, strlen(returns),
|
|
|
|
body, strlen(body),
|
2006-03-02 13:18:49 +01:00
|
|
|
&chistics, &definer_user_name, &definer_host_name))
|
2006-01-05 23:47:49 +01:00
|
|
|
{
|
|
|
|
ret= SP_INTERNAL_ERROR;
|
2005-11-23 00:11:19 +01:00
|
|
|
goto end;
|
2006-01-05 23:47:49 +01:00
|
|
|
}
|
2004-03-17 12:09:03 +01:00
|
|
|
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
if ((ret= sp_use_new_db(thd, name->m_db, &old_db, 1, &dbchanged)))
|
2005-11-23 00:11:19 +01:00
|
|
|
goto end;
|
2003-07-01 17:19:48 +02:00
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
lex_start(thd, (uchar*)defstr.c_ptr(), defstr.length());
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2005-11-23 00:29:25 +01:00
|
|
|
thd->spcont= 0;
|
2006-03-10 01:44:08 +01:00
|
|
|
if (MYSQLparse(thd) || thd->is_fatal_error || newlex.sphead == NULL)
|
2005-11-23 00:11:19 +01:00
|
|
|
{
|
|
|
|
sp_head *sp= newlex.sphead;
|
|
|
|
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
if (dbchanged && (ret= mysql_change_db(thd, old_db.str, 1)))
|
2005-11-23 00:11:19 +01:00
|
|
|
goto end;
|
|
|
|
delete sp;
|
|
|
|
ret= SP_PARSE_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
if (dbchanged && (ret= mysql_change_db(thd, old_db.str, 1)))
|
2005-11-23 01:49:44 +01:00
|
|
|
goto end;
|
2005-11-23 00:11:19 +01:00
|
|
|
*sphp= newlex.sphead;
|
2006-03-02 13:18:49 +01:00
|
|
|
(*sphp)->set_definer(&definer_user_name, &definer_host_name);
|
2005-11-23 01:49:44 +01:00
|
|
|
(*sphp)->set_info(created, modified, &chistics, sql_mode);
|
2005-11-23 00:11:19 +01:00
|
|
|
(*sphp)->optimize();
|
|
|
|
}
|
2005-11-23 01:49:44 +01:00
|
|
|
end:
|
2006-05-04 14:30:38 +02:00
|
|
|
lex_end(thd->lex);
|
2006-01-05 23:47:49 +01:00
|
|
|
thd->spcont= old_spcont;
|
2005-11-23 00:11:19 +01:00
|
|
|
thd->variables.sql_mode= old_sql_mode;
|
2006-01-05 23:47:49 +01:00
|
|
|
thd->variables.select_limit= old_select_limit;
|
|
|
|
thd->lex= old_lex;
|
2005-11-23 00:11:19 +01:00
|
|
|
return ret;
|
2002-12-12 13:14:23 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2005-03-04 22:14:35 +01:00
|
|
|
static void
|
|
|
|
sp_returns_type(THD *thd, String &result, sp_head *sp)
|
|
|
|
{
|
2005-03-10 20:42:57 +01:00
|
|
|
TABLE table;
|
2005-03-04 22:14:35 +01:00
|
|
|
Field *field;
|
2005-03-10 20:42:57 +01:00
|
|
|
bzero(&table, sizeof(table));
|
|
|
|
table.in_use= thd;
|
|
|
|
table.s = &table.share_not_to_be_used;
|
2005-12-07 15:01:17 +01:00
|
|
|
field= sp->create_result_field(0, 0, &table);
|
2005-03-04 22:14:35 +01:00
|
|
|
field->sql_type(result);
|
2006-07-27 15:57:43 +02:00
|
|
|
|
|
|
|
if (field->has_charset())
|
|
|
|
{
|
|
|
|
result.append(STRING_WITH_LEN(" CHARSET "));
|
|
|
|
result.append(field->charset()->csname);
|
|
|
|
}
|
|
|
|
|
2005-03-04 22:14:35 +01:00
|
|
|
delete field;
|
|
|
|
}
|
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
static int
|
2003-12-12 14:05:29 +01:00
|
|
|
db_create_routine(THD *thd, int type, sp_head *sp)
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2003-02-21 17:37:05 +01:00
|
|
|
int ret;
|
2002-12-12 13:14:23 +01:00
|
|
|
TABLE *table;
|
2006-01-11 00:07:40 +01:00
|
|
|
char definer[USER_HOST_BUFF_SIZE];
|
2003-12-21 11:48:03 +01:00
|
|
|
DBUG_ENTER("db_create_routine");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("type: %d name: %.*s",type,sp->m_name.length,
|
|
|
|
sp->m_name.str));
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
if (!(table= open_proc_table_for_update(thd)))
|
2003-02-21 17:37:05 +01:00
|
|
|
ret= SP_OPEN_TABLE_FAILED;
|
|
|
|
else
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
restore_record(table, s->default_values); // Get default values for fields
|
2006-03-02 13:18:49 +01:00
|
|
|
|
|
|
|
/* NOTE: all needed privilege checks have been already done. */
|
|
|
|
strxmov(definer, thd->lex->definer->user.str, "@",
|
|
|
|
thd->lex->definer->host.str, NullS);
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2005-01-06 12:00:13 +01:00
|
|
|
if (table->s->fields != MYSQL_PROC_FIELD_COUNT)
|
2003-10-30 10:25:45 +01:00
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
2006-02-16 13:40:37 +01:00
|
|
|
|
2006-03-28 15:06:06 +02:00
|
|
|
if (system_charset_info->cset->numchars(system_charset_info,
|
|
|
|
sp->m_name.str,
|
|
|
|
sp->m_name.str+sp->m_name.length) >
|
|
|
|
table->field[MYSQL_PROC_FIELD_NAME]->char_length())
|
2005-05-31 18:36:32 +02:00
|
|
|
{
|
|
|
|
ret= SP_BAD_IDENTIFIER;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-07-27 03:08:49 +02:00
|
|
|
if (sp->m_body.length > table->field[MYSQL_PROC_FIELD_BODY]->field_length)
|
|
|
|
{
|
|
|
|
ret= SP_BODY_TOO_LONG;
|
|
|
|
goto done;
|
|
|
|
}
|
2004-03-11 17:18:59 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_DB]->
|
|
|
|
store(sp->m_db.str, sp->m_db.length, system_charset_info);
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_NAME]->
|
|
|
|
store(sp->m_name.str, sp->m_name.length, system_charset_info);
|
|
|
|
table->field[MYSQL_PROC_FIELD_TYPE]->
|
2006-11-30 17:25:05 +01:00
|
|
|
store((longlong)type, 1);
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_SPECIFIC_NAME]->
|
|
|
|
store(sp->m_name.str, sp->m_name.length, system_charset_info);
|
2004-10-14 18:07:09 +02:00
|
|
|
if (sp->m_chistics->daccess != SP_DEFAULT_ACCESS)
|
|
|
|
table->field[MYSQL_PROC_FIELD_ACCESS]->
|
2006-11-30 17:25:05 +01:00
|
|
|
store((longlong)sp->m_chistics->daccess, 1);
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_DETERMINISTIC]->
|
2006-11-30 17:25:05 +01:00
|
|
|
store((longlong)(sp->m_chistics->detistic ? 1 : 2), 1);
|
2004-10-14 18:07:09 +02:00
|
|
|
if (sp->m_chistics->suid != SP_IS_DEFAULT_SUID)
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_SECURITY_TYPE]->
|
2006-11-30 17:25:05 +01:00
|
|
|
store((longlong)sp->m_chistics->suid, 1);
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_PARAM_LIST]->
|
|
|
|
store(sp->m_params.str, sp->m_params.length, system_charset_info);
|
2005-03-04 22:14:35 +01:00
|
|
|
if (sp->m_type == TYPE_ENUM_FUNCTION)
|
|
|
|
{
|
|
|
|
String retstr(64);
|
|
|
|
sp_returns_type(thd, retstr, sp);
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_RETURNS]->
|
2005-03-04 22:14:35 +01:00
|
|
|
store(retstr.ptr(), retstr.length(), system_charset_info);
|
|
|
|
}
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_BODY]->
|
|
|
|
store(sp->m_body.str, sp->m_body.length, system_charset_info);
|
|
|
|
table->field[MYSQL_PROC_FIELD_DEFINER]->
|
2003-12-13 16:40:52 +01:00
|
|
|
store(definer, (uint)strlen(definer), system_charset_info);
|
2003-10-30 10:25:45 +01:00
|
|
|
((Field_timestamp *)table->field[MYSQL_PROC_FIELD_CREATED])->set_time();
|
2004-09-08 11:42:18 +02:00
|
|
|
((Field_timestamp *)table->field[MYSQL_PROC_FIELD_MODIFIED])->set_time();
|
2003-12-12 14:05:29 +01:00
|
|
|
table->field[MYSQL_PROC_FIELD_SQL_MODE]->
|
2006-11-30 17:25:05 +01:00
|
|
|
store((longlong)thd->variables.sql_mode, 1);
|
2003-12-12 14:05:29 +01:00
|
|
|
if (sp->m_chistics->comment.str)
|
|
|
|
table->field[MYSQL_PROC_FIELD_COMMENT]->
|
|
|
|
store(sp->m_chistics->comment.str, sp->m_chistics->comment.length,
|
|
|
|
system_charset_info);
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2005-11-23 00:29:25 +01:00
|
|
|
if ((sp->m_type == TYPE_ENUM_FUNCTION) &&
|
|
|
|
!trust_function_creators && mysql_bin_log.is_open())
|
2005-05-05 14:20:53 +02:00
|
|
|
{
|
|
|
|
if (!sp->m_chistics->detistic)
|
|
|
|
{
|
|
|
|
/*
|
2005-11-23 00:29:25 +01:00
|
|
|
Note that this test is not perfect; one could use
|
2005-05-05 14:20:53 +02:00
|
|
|
a non-deterministic read-only function in an update statement.
|
|
|
|
*/
|
|
|
|
enum enum_sp_data_access access=
|
|
|
|
(sp->m_chistics->daccess == SP_DEFAULT_ACCESS) ?
|
|
|
|
SP_DEFAULT_ACCESS_MAPPING : sp->m_chistics->daccess;
|
|
|
|
if (access == SP_CONTAINS_SQL ||
|
|
|
|
access == SP_MODIFIES_SQL_DATA)
|
|
|
|
{
|
|
|
|
my_message(ER_BINLOG_UNSAFE_ROUTINE,
|
|
|
|
ER(ER_BINLOG_UNSAFE_ROUTINE), MYF(0));
|
|
|
|
ret= SP_INTERNAL_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2005-09-15 21:29:07 +02:00
|
|
|
if (!(thd->security_ctx->master_access & SUPER_ACL))
|
2005-05-05 14:20:53 +02:00
|
|
|
{
|
|
|
|
my_message(ER_BINLOG_CREATE_ROUTINE_NEED_SUPER,
|
|
|
|
ER(ER_BINLOG_CREATE_ROUTINE_NEED_SUPER), MYF(0));
|
|
|
|
ret= SP_INTERNAL_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
ret= SP_OK;
|
2003-02-21 17:37:05 +01:00
|
|
|
if (table->file->write_row(table->record[0]))
|
|
|
|
ret= SP_WRITE_ROW_FAILED;
|
2005-05-05 14:20:53 +02:00
|
|
|
else if (mysql_bin_log.is_open())
|
|
|
|
{
|
|
|
|
thd->clear_error();
|
2006-03-02 13:18:49 +01:00
|
|
|
|
|
|
|
String log_query;
|
|
|
|
log_query.set_charset(system_charset_info);
|
|
|
|
log_query.append(STRING_WITH_LEN("CREATE "));
|
|
|
|
append_definer(thd, &log_query, &thd->lex->definer->user,
|
|
|
|
&thd->lex->definer->host);
|
2006-07-28 00:49:18 +02:00
|
|
|
log_query.append(thd->lex->stmt_definition_begin,
|
|
|
|
(char *)sp->m_body_begin -
|
|
|
|
thd->lex->stmt_definition_begin +
|
|
|
|
sp->m_body.length);
|
2006-03-02 13:18:49 +01:00
|
|
|
|
2005-05-05 14:20:53 +02:00
|
|
|
/* Such a statement can always go directly to binlog, no trans cache */
|
2006-03-02 13:18:49 +01:00
|
|
|
Query_log_event qinfo(thd, log_query.c_ptr(), log_query.length(), 0,
|
|
|
|
FALSE);
|
2005-05-05 14:20:53 +02:00
|
|
|
mysql_bin_log.write(&qinfo);
|
|
|
|
}
|
|
|
|
|
2002-12-12 13:14:23 +01:00
|
|
|
}
|
|
|
|
|
2003-10-30 10:25:45 +01:00
|
|
|
done:
|
2003-02-28 15:07:14 +01:00
|
|
|
close_thread_tables(thd);
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
static int
|
2004-02-17 17:36:53 +01:00
|
|
|
db_drop_routine(THD *thd, int type, sp_name *name)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
|
|
|
TABLE *table;
|
|
|
|
int ret;
|
2003-12-21 11:48:03 +01:00
|
|
|
DBUG_ENTER("db_drop_routine");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("type: %d name: %.*s",
|
2004-02-17 17:36:53 +01:00
|
|
|
type, name->m_name.length, name->m_name.str));
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
if (!(table= open_proc_table_for_update(thd)))
|
|
|
|
DBUG_RETURN(SP_OPEN_TABLE_FAILED);
|
|
|
|
if ((ret= db_find_routine_aux(thd, type, name, table)) == SP_OK)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
|
|
|
if (table->file->delete_row(table->record[0]))
|
|
|
|
ret= SP_DELETE_ROW_FAILED;
|
|
|
|
}
|
2006-10-03 19:38:25 +02:00
|
|
|
|
|
|
|
if (ret == SP_OK)
|
|
|
|
{
|
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
|
|
|
thd->clear_error();
|
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length, 0, FALSE);
|
|
|
|
mysql_bin_log.write(&qinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
close_thread_tables(thd);
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
static int
|
2004-10-22 19:05:17 +02:00
|
|
|
db_update_routine(THD *thd, int type, sp_name *name, st_sp_chistics *chistics)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
|
|
|
TABLE *table;
|
|
|
|
int ret;
|
2003-12-21 11:48:03 +01:00
|
|
|
DBUG_ENTER("db_update_routine");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("type: %d name: %.*s",
|
2004-02-17 17:36:53 +01:00
|
|
|
type, name->m_name.length, name->m_name.str));
|
2003-11-17 18:21:36 +01:00
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
if (!(table= open_proc_table_for_update(thd)))
|
|
|
|
DBUG_RETURN(SP_OPEN_TABLE_FAILED);
|
|
|
|
if ((ret= db_find_routine_aux(thd, type, name, table)) == SP_OK)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
|
|
|
store_record(table,record[1]);
|
2004-11-03 11:39:38 +01:00
|
|
|
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
|
2003-11-17 18:21:36 +01:00
|
|
|
((Field_timestamp *)table->field[MYSQL_PROC_FIELD_MODIFIED])->set_time();
|
2004-10-14 18:07:09 +02:00
|
|
|
if (chistics->suid != SP_IS_DEFAULT_SUID)
|
|
|
|
table->field[MYSQL_PROC_FIELD_SECURITY_TYPE]->
|
2006-11-30 17:25:05 +01:00
|
|
|
store((longlong)chistics->suid, 1);
|
2004-10-14 18:07:09 +02:00
|
|
|
if (chistics->daccess != SP_DEFAULT_ACCESS)
|
|
|
|
table->field[MYSQL_PROC_FIELD_ACCESS]->
|
2006-11-30 17:25:05 +01:00
|
|
|
store((longlong)chistics->daccess, 1);
|
2003-12-10 19:05:37 +01:00
|
|
|
if (chistics->comment.str)
|
|
|
|
table->field[MYSQL_PROC_FIELD_COMMENT]->store(chistics->comment.str,
|
|
|
|
chistics->comment.length,
|
2003-11-17 18:21:36 +01:00
|
|
|
system_charset_info);
|
|
|
|
if ((table->file->update_row(table->record[1],table->record[0])))
|
|
|
|
ret= SP_WRITE_ROW_FAILED;
|
|
|
|
}
|
2006-10-03 19:38:25 +02:00
|
|
|
|
|
|
|
if (ret == SP_OK)
|
|
|
|
{
|
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
|
|
|
thd->clear_error();
|
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length, 0, FALSE);
|
|
|
|
mysql_bin_log.write(&qinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
close_thread_tables(thd);
|
2003-11-17 18:21:36 +01:00
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
struct st_used_field
|
|
|
|
{
|
|
|
|
const char *field_name;
|
|
|
|
uint field_length;
|
|
|
|
enum enum_field_types field_type;
|
|
|
|
Field *field;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct st_used_field init_fields[]=
|
|
|
|
{
|
2004-03-11 17:18:59 +01:00
|
|
|
{ "Db", NAME_LEN, MYSQL_TYPE_STRING, 0},
|
2003-11-17 18:21:36 +01:00
|
|
|
{ "Name", NAME_LEN, MYSQL_TYPE_STRING, 0},
|
|
|
|
{ "Type", 9, MYSQL_TYPE_STRING, 0},
|
2003-12-10 19:05:37 +01:00
|
|
|
{ "Definer", 77, MYSQL_TYPE_STRING, 0},
|
2003-11-17 18:21:36 +01:00
|
|
|
{ "Modified", 0, MYSQL_TYPE_TIMESTAMP, 0},
|
|
|
|
{ "Created", 0, MYSQL_TYPE_TIMESTAMP, 0},
|
2003-12-10 19:05:37 +01:00
|
|
|
{ "Security_type", 1, MYSQL_TYPE_STRING, 0},
|
2003-11-17 18:21:36 +01:00
|
|
|
{ "Comment", NAME_LEN, MYSQL_TYPE_STRING, 0},
|
|
|
|
{ 0, 0, MYSQL_TYPE_STRING, 0}
|
|
|
|
};
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-20 15:07:22 +01:00
|
|
|
static int
|
|
|
|
print_field_values(THD *thd, TABLE *table,
|
|
|
|
struct st_used_field *used_fields,
|
|
|
|
int type, const char *wild)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
|
|
|
Protocol *protocol= thd->protocol;
|
|
|
|
|
|
|
|
if (table->field[MYSQL_PROC_FIELD_TYPE]->val_int() == type)
|
|
|
|
{
|
2004-03-11 17:18:59 +01:00
|
|
|
String db_string;
|
|
|
|
String name_string;
|
2003-11-17 18:21:36 +01:00
|
|
|
struct st_used_field *used_field= used_fields;
|
2003-11-20 15:07:22 +01:00
|
|
|
|
2004-11-09 02:58:44 +01:00
|
|
|
if (get_field(thd->mem_root, used_field->field, &db_string))
|
2004-03-11 17:18:59 +01:00
|
|
|
db_string.set_ascii("", 0);
|
|
|
|
used_field+= 1;
|
2004-11-09 02:58:44 +01:00
|
|
|
get_field(thd->mem_root, used_field->field, &name_string);
|
2004-03-11 17:18:59 +01:00
|
|
|
|
|
|
|
if (!wild || !wild[0] || !wild_compare(name_string.ptr(), wild, 0))
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
|
|
|
protocol->prepare_for_resend();
|
2004-03-11 17:18:59 +01:00
|
|
|
protocol->store(&db_string);
|
|
|
|
protocol->store(&name_string);
|
2003-11-17 18:21:36 +01:00
|
|
|
for (used_field++;
|
|
|
|
used_field->field_name;
|
|
|
|
used_field++)
|
|
|
|
{
|
|
|
|
switch (used_field->field_type) {
|
|
|
|
case MYSQL_TYPE_TIMESTAMP:
|
2003-11-20 15:07:22 +01:00
|
|
|
{
|
|
|
|
TIME tmp_time;
|
2003-12-10 19:05:37 +01:00
|
|
|
|
|
|
|
bzero((char *)&tmp_time, sizeof(tmp_time));
|
2003-11-20 15:07:22 +01:00
|
|
|
((Field_timestamp *) used_field->field)->get_time(&tmp_time);
|
|
|
|
protocol->store(&tmp_time);
|
|
|
|
}
|
|
|
|
break;
|
2003-11-17 18:21:36 +01:00
|
|
|
default:
|
2003-11-20 15:07:22 +01:00
|
|
|
{
|
2004-03-11 17:18:59 +01:00
|
|
|
String tmp_string;
|
2003-12-10 19:05:37 +01:00
|
|
|
|
2004-11-09 02:58:44 +01:00
|
|
|
get_field(thd->mem_root, used_field->field, &tmp_string);
|
2004-03-11 17:18:59 +01:00
|
|
|
protocol->store(&tmp_string);
|
2003-11-20 15:07:22 +01:00
|
|
|
}
|
|
|
|
break;
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (protocol->write())
|
2003-11-20 15:07:22 +01:00
|
|
|
return SP_INTERNAL_ERROR;
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-03 19:38:25 +02:00
|
|
|
|
2003-11-20 15:07:22 +01:00
|
|
|
return SP_OK;
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-20 15:07:22 +01:00
|
|
|
static int
|
2003-11-17 18:21:36 +01:00
|
|
|
db_show_routine_status(THD *thd, int type, const char *wild)
|
|
|
|
{
|
|
|
|
TABLE *table;
|
|
|
|
TABLE_LIST tables;
|
2003-11-20 15:07:22 +01:00
|
|
|
int res;
|
2003-12-21 11:48:03 +01:00
|
|
|
DBUG_ENTER("db_show_routine_status");
|
2003-11-17 18:21:36 +01:00
|
|
|
|
|
|
|
memset(&tables, 0, sizeof(tables));
|
|
|
|
tables.db= (char*)"mysql";
|
2005-01-06 12:00:13 +01:00
|
|
|
tables.table_name= tables.alias= (char*)"proc";
|
2003-11-17 18:21:36 +01:00
|
|
|
|
|
|
|
if (! (table= open_ltable(thd, &tables, TL_READ)))
|
|
|
|
{
|
2003-11-20 15:07:22 +01:00
|
|
|
res= SP_OPEN_TABLE_FAILED;
|
|
|
|
goto done;
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Item *item;
|
|
|
|
List<Item> field_list;
|
|
|
|
struct st_used_field *used_field;
|
2004-09-14 18:28:29 +02:00
|
|
|
TABLE_LIST *leaves= 0;
|
2003-11-17 18:21:36 +01:00
|
|
|
st_used_field used_fields[array_elements(init_fields)];
|
2003-11-20 15:07:22 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
memcpy((char*) used_fields, (char*) init_fields, sizeof(used_fields));
|
|
|
|
/* Init header */
|
|
|
|
for (used_field= &used_fields[0];
|
|
|
|
used_field->field_name;
|
|
|
|
used_field++)
|
|
|
|
{
|
|
|
|
switch (used_field->field_type) {
|
|
|
|
case MYSQL_TYPE_TIMESTAMP:
|
|
|
|
field_list.push_back(item=new Item_datetime(used_field->field_name));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
field_list.push_back(item=new Item_empty_string(used_field->field_name,
|
|
|
|
used_field->
|
|
|
|
field_length));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Print header */
|
2004-08-03 12:32:21 +02:00
|
|
|
if (thd->protocol->send_fields(&field_list, Protocol::SEND_NUM_ROWS |
|
|
|
|
Protocol::SEND_EOF))
|
2003-11-20 15:07:22 +01:00
|
|
|
{
|
|
|
|
res= SP_INTERNAL_ERROR;
|
2003-11-17 18:21:36 +01:00
|
|
|
goto err_case;
|
2003-11-20 15:07:22 +01:00
|
|
|
}
|
2003-11-17 18:21:36 +01:00
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
/*
|
|
|
|
Init fields
|
|
|
|
|
|
|
|
tables is not VIEW for sure => we can pass 0 as condition
|
|
|
|
*/
|
2005-07-01 06:05:42 +02:00
|
|
|
thd->lex->select_lex.context.resolve_in_table_list_only(&tables);
|
|
|
|
setup_tables(thd, &thd->lex->select_lex.context,
|
2005-08-12 16:57:19 +02:00
|
|
|
&thd->lex->select_lex.top_join_list,
|
2005-07-01 06:05:42 +02:00
|
|
|
&tables, 0, &leaves, FALSE);
|
2003-11-17 18:21:36 +01:00
|
|
|
for (used_field= &used_fields[0];
|
|
|
|
used_field->field_name;
|
|
|
|
used_field++)
|
|
|
|
{
|
2005-07-01 06:05:42 +02:00
|
|
|
Item_field *field= new Item_field(&thd->lex->select_lex.context,
|
|
|
|
"mysql", "proc",
|
2003-11-17 18:21:36 +01:00
|
|
|
used_field->field_name);
|
2005-07-01 06:05:42 +02:00
|
|
|
if (!field ||
|
2005-08-12 16:57:19 +02:00
|
|
|
!(used_field->field= find_field_in_tables(thd, field, &tables, NULL,
|
2005-07-02 23:51:02 +02:00
|
|
|
0, REPORT_ALL_ERRORS, 1,
|
|
|
|
TRUE)))
|
2003-11-20 15:07:22 +01:00
|
|
|
{
|
|
|
|
res= SP_INTERNAL_ERROR;
|
2003-11-17 18:21:36 +01:00
|
|
|
goto err_case1;
|
2003-11-20 15:07:22 +01:00
|
|
|
}
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2004-07-12 06:43:38 +02:00
|
|
|
table->file->ha_index_init(0);
|
2003-11-20 15:07:22 +01:00
|
|
|
if ((res= table->file->index_first(table->record[0])))
|
|
|
|
{
|
2003-12-21 11:48:03 +01:00
|
|
|
res= (res == HA_ERR_END_OF_FILE) ? 0 : SP_INTERNAL_ERROR;
|
2003-11-20 15:07:22 +01:00
|
|
|
goto err_case1;
|
|
|
|
}
|
|
|
|
if ((res= print_field_values(thd, table, used_fields, type, wild)))
|
2003-11-17 18:21:36 +01:00
|
|
|
goto err_case1;
|
|
|
|
while (!table->file->index_next(table->record[0]))
|
|
|
|
{
|
2003-11-20 15:07:22 +01:00
|
|
|
if ((res= print_field_values(thd, table, used_fields, type, wild)))
|
2003-11-17 18:21:36 +01:00
|
|
|
goto err_case1;
|
|
|
|
}
|
2003-11-20 15:07:22 +01:00
|
|
|
res= SP_OK;
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
2003-11-20 15:07:22 +01:00
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
err_case1:
|
2003-11-17 18:21:36 +01:00
|
|
|
send_eof(thd);
|
2003-12-21 11:48:03 +01:00
|
|
|
err_case:
|
2004-07-15 03:19:07 +02:00
|
|
|
table->file->ha_index_end();
|
2003-11-17 18:21:36 +01:00
|
|
|
close_thread_tables(thd);
|
2003-12-21 11:48:03 +01:00
|
|
|
done:
|
2003-11-20 15:07:22 +01:00
|
|
|
DBUG_RETURN(res);
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2004-03-22 14:44:41 +01:00
|
|
|
/* Drop all routines in database 'db' */
|
|
|
|
int
|
|
|
|
sp_drop_db_routines(THD *thd, char *db)
|
|
|
|
{
|
|
|
|
TABLE *table;
|
|
|
|
int ret;
|
2006-04-18 16:01:01 +02:00
|
|
|
uint key_len;
|
2004-03-22 14:44:41 +01:00
|
|
|
DBUG_ENTER("sp_drop_db_routines");
|
|
|
|
DBUG_PRINT("enter", ("db: %s", db));
|
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
ret= SP_OPEN_TABLE_FAILED;
|
|
|
|
if (!(table= open_proc_table_for_update(thd)))
|
|
|
|
goto err;
|
2004-03-22 14:44:41 +01:00
|
|
|
|
2006-04-18 16:01:01 +02:00
|
|
|
table->field[MYSQL_PROC_FIELD_DB]->store(db, strlen(db), system_charset_info);
|
|
|
|
key_len= table->key_info->key_part[0].store_length;
|
|
|
|
|
2004-03-22 14:44:41 +01:00
|
|
|
ret= SP_OK;
|
2004-07-12 06:43:38 +02:00
|
|
|
table->file->ha_index_init(0);
|
2004-03-22 14:44:41 +01:00
|
|
|
if (! table->file->index_read(table->record[0],
|
2006-04-21 14:30:44 +02:00
|
|
|
(byte *)table->field[MYSQL_PROC_FIELD_DB]->ptr,
|
2006-04-18 16:01:01 +02:00
|
|
|
key_len, HA_READ_KEY_EXACT))
|
2004-03-22 14:44:41 +01:00
|
|
|
{
|
|
|
|
int nxtres;
|
|
|
|
bool deleted= FALSE;
|
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
do
|
|
|
|
{
|
2004-03-22 14:44:41 +01:00
|
|
|
if (! table->file->delete_row(table->record[0]))
|
|
|
|
deleted= TRUE; /* We deleted something */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret= SP_DELETE_ROW_FAILED;
|
2004-03-23 12:04:40 +01:00
|
|
|
nxtres= 0;
|
2004-03-22 14:44:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (! (nxtres= table->file->index_next_same(table->record[0],
|
2006-04-21 14:30:44 +02:00
|
|
|
(byte *)table->field[MYSQL_PROC_FIELD_DB]->ptr,
|
2006-04-18 16:01:01 +02:00
|
|
|
key_len)));
|
2004-03-22 14:44:41 +01:00
|
|
|
if (nxtres != HA_ERR_END_OF_FILE)
|
|
|
|
ret= SP_KEY_NOT_FOUND;
|
|
|
|
if (deleted)
|
|
|
|
sp_cache_invalidate();
|
|
|
|
}
|
2004-07-15 03:19:07 +02:00
|
|
|
table->file->ha_index_end();
|
2004-03-22 14:44:41 +01:00
|
|
|
|
|
|
|
close_thread_tables(thd);
|
|
|
|
|
2005-07-13 11:48:13 +02:00
|
|
|
err:
|
2004-03-22 14:44:41 +01:00
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
PROCEDURE
|
|
|
|
******************************************************************************/
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2005-03-04 14:35:28 +01:00
|
|
|
/*
|
2005-11-23 00:11:19 +01:00
|
|
|
Obtain object representing stored procedure/function by its name from
|
2005-03-04 14:35:28 +01:00
|
|
|
stored procedures cache and looking into mysql.proc if needed.
|
|
|
|
|
|
|
|
SYNOPSIS
|
2005-11-23 00:11:19 +01:00
|
|
|
sp_find_routine()
|
2005-03-04 14:35:28 +01:00
|
|
|
thd - thread context
|
2005-11-23 00:11:19 +01:00
|
|
|
type - type of object (TYPE_ENUM_FUNCTION or TYPE_ENUM_PROCEDURE)
|
2005-03-04 14:35:28 +01:00
|
|
|
name - name of procedure
|
2005-11-23 00:11:19 +01:00
|
|
|
cp - hash to look routine in
|
2005-03-04 14:35:28 +01:00
|
|
|
cache_only - if true perform cache-only lookup
|
|
|
|
(Don't look in mysql.proc).
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
Non-0 pointer to sp_head object for the procedure, or
|
|
|
|
0 - in case of error.
|
|
|
|
*/
|
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
sp_head *
|
2005-11-23 00:11:19 +01:00
|
|
|
sp_find_routine(THD *thd, int type, sp_name *name, sp_cache **cp,
|
|
|
|
bool cache_only)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
|
|
|
sp_head *sp;
|
2005-11-23 00:11:19 +01:00
|
|
|
ulong depth= (type == TYPE_ENUM_PROCEDURE ?
|
|
|
|
thd->variables.max_sp_recursion_depth :
|
|
|
|
0);
|
|
|
|
DBUG_ENTER("sp_find_routine");
|
|
|
|
DBUG_PRINT("enter", ("name: %.*s.%.*s, type: %d, cache only %d",
|
|
|
|
name->m_db.length, name->m_db.str,
|
|
|
|
name->m_name.length, name->m_name.str,
|
|
|
|
type, cache_only));
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
if ((sp= sp_cache_lookup(cp, name)))
|
2003-07-01 18:14:24 +02:00
|
|
|
{
|
2005-11-23 00:11:19 +01:00
|
|
|
ulong level;
|
2006-01-05 23:47:49 +01:00
|
|
|
sp_head *new_sp;
|
|
|
|
const char *returns= "";
|
2006-01-11 00:07:40 +01:00
|
|
|
char definer[USER_HOST_BUFF_SIZE];
|
2006-07-27 15:57:43 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
String buffer for RETURNS data type must have system charset;
|
|
|
|
64 -- size of "returns" column of mysql.proc.
|
|
|
|
*/
|
2006-01-05 23:47:49 +01:00
|
|
|
String retstr(64);
|
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
DBUG_PRINT("info", ("found: 0x%lx", (ulong)sp));
|
|
|
|
if (sp->m_first_free_instance)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("info", ("first free: 0x%lx, level: %lu, flags %x",
|
|
|
|
(ulong)sp->m_first_free_instance,
|
|
|
|
sp->m_first_free_instance->m_recursion_level,
|
|
|
|
sp->m_first_free_instance->m_flags));
|
|
|
|
DBUG_ASSERT(!(sp->m_first_free_instance->m_flags & sp_head::IS_INVOKED));
|
|
|
|
if (sp->m_first_free_instance->m_recursion_level > depth)
|
|
|
|
{
|
2006-01-05 23:47:49 +01:00
|
|
|
sp->recursion_level_error(thd);
|
2005-11-23 00:11:19 +01:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
DBUG_RETURN(sp->m_first_free_instance);
|
|
|
|
}
|
2006-08-24 19:36:26 +02:00
|
|
|
/*
|
|
|
|
Actually depth could be +1 than the actual value in case a SP calls
|
|
|
|
SHOW CREATE PROCEDURE. Hence, the linked list could hold up to one more
|
|
|
|
instance.
|
|
|
|
*/
|
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
level= sp->m_last_cached_sp->m_recursion_level + 1;
|
|
|
|
if (level > depth)
|
|
|
|
{
|
2006-01-05 23:47:49 +01:00
|
|
|
sp->recursion_level_error(thd);
|
2005-11-23 00:11:19 +01:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
2006-01-05 23:47:49 +01:00
|
|
|
|
|
|
|
strxmov(definer, sp->m_definer_user.str, "@",
|
|
|
|
sp->m_definer_host.str, NullS);
|
|
|
|
if (type == TYPE_ENUM_FUNCTION)
|
2005-11-23 00:11:19 +01:00
|
|
|
{
|
2006-01-05 23:47:49 +01:00
|
|
|
sp_returns_type(thd, retstr, sp);
|
|
|
|
returns= retstr.ptr();
|
|
|
|
}
|
|
|
|
if (db_load_routine(thd, type, name, &new_sp,
|
|
|
|
sp->m_sql_mode, sp->m_params.str, returns,
|
|
|
|
sp->m_body.str, *sp->m_chistics, definer,
|
|
|
|
sp->m_created, sp->m_modified) == SP_OK)
|
|
|
|
{
|
|
|
|
sp->m_last_cached_sp->m_next_cached_sp= new_sp;
|
|
|
|
new_sp->m_recursion_level= level;
|
|
|
|
new_sp->m_first_instance= sp;
|
|
|
|
sp->m_last_cached_sp= sp->m_first_free_instance= new_sp;
|
|
|
|
DBUG_PRINT("info", ("added level: 0x%lx, level: %lu, flags %x",
|
2005-11-23 00:11:19 +01:00
|
|
|
(ulong)new_sp, new_sp->m_recursion_level,
|
|
|
|
new_sp->m_flags));
|
2006-01-05 23:47:49 +01:00
|
|
|
DBUG_RETURN(new_sp);
|
2005-11-23 00:11:19 +01:00
|
|
|
}
|
2006-01-05 23:47:49 +01:00
|
|
|
DBUG_RETURN(0);
|
2005-11-23 00:11:19 +01:00
|
|
|
}
|
|
|
|
if (!cache_only)
|
|
|
|
{
|
|
|
|
if (db_find_routine(thd, type, name, &sp) == SP_OK)
|
|
|
|
{
|
|
|
|
sp_cache_insert(cp, sp);
|
|
|
|
DBUG_PRINT("info", ("added new: 0x%lx, level: %lu, flags %x",
|
|
|
|
(ulong)sp, sp->m_recursion_level,
|
|
|
|
sp->m_flags));
|
|
|
|
}
|
2003-07-01 18:14:24 +02:00
|
|
|
}
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_RETURN(sp);
|
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2006-01-26 13:29:46 +01:00
|
|
|
/*
|
|
|
|
This is used by sql_acl.cc:mysql_routine_grant() and is used to find
|
|
|
|
the routines in 'routines'.
|
|
|
|
*/
|
2005-11-23 00:11:19 +01:00
|
|
|
|
2004-12-23 11:46:24 +01:00
|
|
|
int
|
2006-01-26 13:29:46 +01:00
|
|
|
sp_exist_routines(THD *thd, TABLE_LIST *routines, bool any, bool no_error)
|
2004-12-23 11:46:24 +01:00
|
|
|
{
|
2006-01-26 13:29:46 +01:00
|
|
|
TABLE_LIST *routine;
|
2004-12-23 11:46:24 +01:00
|
|
|
bool result= 0;
|
2006-02-22 11:44:04 +01:00
|
|
|
bool sp_object_found;
|
2004-12-23 11:46:24 +01:00
|
|
|
DBUG_ENTER("sp_exists_routine");
|
2006-01-26 13:29:46 +01:00
|
|
|
for (routine= routines; routine; routine= routine->next_global)
|
2004-12-23 11:46:24 +01:00
|
|
|
{
|
|
|
|
sp_name *name;
|
|
|
|
LEX_STRING lex_db;
|
|
|
|
LEX_STRING lex_name;
|
2006-01-26 13:29:46 +01:00
|
|
|
lex_db.length= strlen(routine->db);
|
|
|
|
lex_name.length= strlen(routine->table_name);
|
|
|
|
lex_db.str= thd->strmake(routine->db, lex_db.length);
|
|
|
|
lex_name.str= thd->strmake(routine->table_name, lex_name.length);
|
2004-12-23 11:46:24 +01:00
|
|
|
name= new sp_name(lex_db, lex_name);
|
|
|
|
name->init_qname(thd);
|
2006-02-22 11:44:04 +01:00
|
|
|
sp_object_found= sp_find_routine(thd, TYPE_ENUM_PROCEDURE, name,
|
|
|
|
&thd->sp_proc_cache, FALSE) != NULL ||
|
|
|
|
sp_find_routine(thd, TYPE_ENUM_FUNCTION, name,
|
|
|
|
&thd->sp_func_cache, FALSE) != NULL;
|
|
|
|
mysql_reset_errors(thd, TRUE);
|
|
|
|
if (sp_object_found)
|
2004-12-23 11:46:24 +01:00
|
|
|
{
|
|
|
|
if (any)
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
result= 1;
|
|
|
|
}
|
|
|
|
else if (!any)
|
|
|
|
{
|
|
|
|
if (!no_error)
|
|
|
|
{
|
|
|
|
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION or PROCEDURE",
|
2006-01-26 13:29:46 +01:00
|
|
|
routine->table_name);
|
2004-12-23 11:46:24 +01:00
|
|
|
DBUG_RETURN(-1);
|
|
|
|
}
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DBUG_RETURN(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-26 13:29:46 +01:00
|
|
|
/*
|
|
|
|
Check if a routine exists in the mysql.proc table, without actually
|
|
|
|
parsing the definition. (Used for dropping)
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_routine_exists_in_table()
|
|
|
|
thd - thread context
|
|
|
|
name - name of procedure
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
0 - Success
|
|
|
|
non-0 - Error; SP_OPEN_TABLE_FAILED or SP_KEY_NOT_FOUND
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
sp_routine_exists_in_table(THD *thd, int type, sp_name *name)
|
|
|
|
{
|
|
|
|
TABLE *table;
|
|
|
|
int ret;
|
|
|
|
Open_tables_state open_tables_state_backup;
|
|
|
|
|
|
|
|
if (!(table= open_proc_table_for_read(thd, &open_tables_state_backup)))
|
|
|
|
ret= SP_OPEN_TABLE_FAILED;
|
|
|
|
else
|
|
|
|
{
|
2006-02-02 15:37:29 +01:00
|
|
|
if ((ret= db_find_routine_aux(thd, type, name, table)) != SP_OK)
|
2006-01-26 13:29:46 +01:00
|
|
|
ret= SP_KEY_NOT_FOUND;
|
|
|
|
close_proc_table(thd, &open_tables_state_backup);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
int
|
2003-12-12 14:05:29 +01:00
|
|
|
sp_create_procedure(THD *thd, sp_head *sp)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_ENTER("sp_create_procedure");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", sp->m_name.length, sp->m_name.str));
|
2004-01-14 18:18:29 +01:00
|
|
|
|
|
|
|
ret= db_create_routine(thd, TYPE_ENUM_PROCEDURE, sp);
|
|
|
|
DBUG_RETURN(ret);
|
2002-12-12 13:14:23 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2002-12-12 13:14:23 +01:00
|
|
|
int
|
2004-02-17 17:36:53 +01:00
|
|
|
sp_drop_procedure(THD *thd, sp_name *name)
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-02-12 16:17:03 +01:00
|
|
|
DBUG_ENTER("sp_drop_procedure");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", name->m_name.length, name->m_name.str));
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2004-02-17 17:36:53 +01:00
|
|
|
ret= db_drop_routine(thd, TYPE_ENUM_PROCEDURE, name);
|
2005-08-09 01:23:34 +02:00
|
|
|
if (!ret)
|
2004-08-06 18:11:14 +02:00
|
|
|
sp_cache_invalidate();
|
2004-01-14 18:18:29 +01:00
|
|
|
DBUG_RETURN(ret);
|
2003-02-21 17:37:05 +01:00
|
|
|
}
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
int
|
2004-10-22 19:05:17 +02:00
|
|
|
sp_update_procedure(THD *thd, sp_name *name, st_sp_chistics *chistics)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-11-17 18:21:36 +01:00
|
|
|
DBUG_ENTER("sp_update_procedure");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", name->m_name.length, name->m_name.str));
|
2003-11-17 18:21:36 +01:00
|
|
|
|
2004-10-22 19:05:17 +02:00
|
|
|
ret= db_update_routine(thd, TYPE_ENUM_PROCEDURE, name, chistics);
|
2005-08-09 01:23:34 +02:00
|
|
|
if (!ret)
|
2004-08-06 18:11:14 +02:00
|
|
|
sp_cache_invalidate();
|
2004-01-14 18:18:29 +01:00
|
|
|
DBUG_RETURN(ret);
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
int
|
2004-02-17 17:36:53 +01:00
|
|
|
sp_show_create_procedure(THD *thd, sp_name *name)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
2006-08-24 19:36:26 +02:00
|
|
|
int ret= SP_KEY_NOT_FOUND;
|
2003-12-21 11:48:03 +01:00
|
|
|
sp_head *sp;
|
2003-11-17 18:21:36 +01:00
|
|
|
DBUG_ENTER("sp_show_create_procedure");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", name->m_name.length, name->m_name.str));
|
2003-11-17 18:21:36 +01:00
|
|
|
|
2006-08-24 19:36:26 +02:00
|
|
|
/*
|
|
|
|
Increase the recursion limit for this statement. SHOW CREATE PROCEDURE
|
|
|
|
does not do actual recursion.
|
|
|
|
*/
|
|
|
|
thd->variables.max_sp_recursion_depth++;
|
2005-11-23 00:11:19 +01:00
|
|
|
if ((sp= sp_find_routine(thd, TYPE_ENUM_PROCEDURE, name,
|
|
|
|
&thd->sp_proc_cache, FALSE)))
|
2006-08-24 19:36:26 +02:00
|
|
|
ret= sp->show_create_procedure(thd);
|
2003-11-17 18:21:36 +01:00
|
|
|
|
2006-08-24 19:36:26 +02:00
|
|
|
thd->variables.max_sp_recursion_depth--;
|
|
|
|
DBUG_RETURN(ret);
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
int
|
2003-11-20 15:07:22 +01:00
|
|
|
sp_show_status_procedure(THD *thd, const char *wild)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-11-20 15:07:22 +01:00
|
|
|
DBUG_ENTER("sp_show_status_procedure");
|
2004-01-14 18:18:29 +01:00
|
|
|
|
|
|
|
ret= db_show_routine_status(thd, TYPE_ENUM_PROCEDURE, wild);
|
|
|
|
DBUG_RETURN(ret);
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
FUNCTION
|
|
|
|
******************************************************************************/
|
2003-02-21 17:37:05 +01:00
|
|
|
|
|
|
|
int
|
2003-12-12 14:05:29 +01:00
|
|
|
sp_create_function(THD *thd, sp_head *sp)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_ENTER("sp_create_function");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", sp->m_name.length, sp->m_name.str));
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2004-01-14 18:18:29 +01:00
|
|
|
ret= db_create_routine(thd, TYPE_ENUM_FUNCTION, sp);
|
|
|
|
DBUG_RETURN(ret);
|
2003-02-21 17:37:05 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
int
|
2004-02-17 17:36:53 +01:00
|
|
|
sp_drop_function(THD *thd, sp_name *name)
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_ENTER("sp_drop_function");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", name->m_name.length, name->m_name.str));
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2004-02-17 17:36:53 +01:00
|
|
|
ret= db_drop_routine(thd, TYPE_ENUM_FUNCTION, name);
|
2005-08-09 01:23:34 +02:00
|
|
|
if (!ret)
|
2004-08-06 18:11:14 +02:00
|
|
|
sp_cache_invalidate();
|
2004-01-14 18:18:29 +01:00
|
|
|
DBUG_RETURN(ret);
|
2002-12-12 13:14:23 +01:00
|
|
|
}
|
2003-02-26 19:22:29 +01:00
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
int
|
2004-10-22 19:05:17 +02:00
|
|
|
sp_update_function(THD *thd, sp_name *name, st_sp_chistics *chistics)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-11-17 18:21:36 +01:00
|
|
|
DBUG_ENTER("sp_update_procedure");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", name->m_name.length, name->m_name.str));
|
2003-11-17 18:21:36 +01:00
|
|
|
|
2004-10-22 19:05:17 +02:00
|
|
|
ret= db_update_routine(thd, TYPE_ENUM_FUNCTION, name, chistics);
|
2005-08-09 01:23:34 +02:00
|
|
|
if (!ret)
|
2004-08-06 18:11:14 +02:00
|
|
|
sp_cache_invalidate();
|
2004-01-14 18:18:29 +01:00
|
|
|
DBUG_RETURN(ret);
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
int
|
2004-02-17 17:36:53 +01:00
|
|
|
sp_show_create_function(THD *thd, sp_name *name)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
2003-12-21 11:48:03 +01:00
|
|
|
sp_head *sp;
|
2003-11-17 18:21:36 +01:00
|
|
|
DBUG_ENTER("sp_show_create_function");
|
2005-10-07 02:37:24 +02:00
|
|
|
DBUG_PRINT("enter", ("name: %.*s", name->m_name.length, name->m_name.str));
|
2003-11-17 18:21:36 +01:00
|
|
|
|
2005-11-23 00:11:19 +01:00
|
|
|
if ((sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, name,
|
|
|
|
&thd->sp_func_cache, FALSE)))
|
2004-01-14 18:18:29 +01:00
|
|
|
{
|
|
|
|
int ret= sp->show_create_function(thd);
|
|
|
|
|
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
2003-11-20 15:07:22 +01:00
|
|
|
DBUG_RETURN(SP_KEY_NOT_FOUND);
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2003-11-17 18:21:36 +01:00
|
|
|
int
|
2003-11-20 15:07:22 +01:00
|
|
|
sp_show_status_function(THD *thd, const char *wild)
|
2003-11-17 18:21:36 +01:00
|
|
|
{
|
2004-01-14 18:18:29 +01:00
|
|
|
int ret;
|
2003-11-20 15:07:22 +01:00
|
|
|
DBUG_ENTER("sp_show_status_function");
|
2004-01-14 18:18:29 +01:00
|
|
|
ret= db_show_routine_status(thd, TYPE_ENUM_FUNCTION, wild);
|
|
|
|
DBUG_RETURN(ret);
|
2003-11-17 18:21:36 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
/*
|
|
|
|
Structure that represents element in the set of stored routines
|
|
|
|
used by statement or routine.
|
|
|
|
*/
|
|
|
|
struct Sroutine_hash_entry;
|
|
|
|
|
|
|
|
struct Sroutine_hash_entry
|
2003-07-07 14:55:10 +02:00
|
|
|
{
|
2005-07-09 19:51:59 +02:00
|
|
|
/* Set key consisting of one-byte routine type and quoted routine name. */
|
|
|
|
LEX_STRING key;
|
|
|
|
/*
|
|
|
|
Next element in list linking all routines in set. See also comments
|
|
|
|
for LEX::sroutine/sroutine_list and sp_head::m_sroutines.
|
|
|
|
*/
|
|
|
|
Sroutine_hash_entry *next;
|
2005-12-07 10:27:17 +01:00
|
|
|
/*
|
|
|
|
Uppermost view which directly or indirectly uses this routine.
|
|
|
|
0 if routine is not used in view. Note that it also can be 0 if
|
|
|
|
statement uses routine both via view and directly.
|
|
|
|
*/
|
|
|
|
TABLE_LIST *belong_to_view;
|
2005-07-09 19:51:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" byte* sp_sroutine_key(const byte *ptr, uint *plen, my_bool first)
|
|
|
|
{
|
|
|
|
Sroutine_hash_entry *rn= (Sroutine_hash_entry *)ptr;
|
|
|
|
*plen= rn->key.length;
|
|
|
|
return (byte *)rn->key.str;
|
2003-07-07 14:55:10 +02:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2005-07-30 10:19:57 +02:00
|
|
|
/*
|
2005-08-03 05:37:32 +02:00
|
|
|
Check if
|
|
|
|
- current statement (the one in thd->lex) needs table prelocking
|
|
|
|
- first routine in thd->lex->sroutines_list needs to execute its body in
|
|
|
|
prelocked mode.
|
2005-07-30 10:19:57 +02:00
|
|
|
|
|
|
|
SYNOPSIS
|
2005-08-03 05:37:32 +02:00
|
|
|
sp_get_prelocking_info()
|
|
|
|
thd Current thread, thd->lex is the statement to be
|
|
|
|
checked.
|
|
|
|
need_prelocking OUT TRUE - prelocked mode should be activated
|
|
|
|
before executing the statement
|
|
|
|
FALSE - Don't activate prelocking
|
|
|
|
first_no_prelocking OUT TRUE - Tables used by first routine in
|
|
|
|
thd->lex->sroutines_list should be
|
|
|
|
prelocked.
|
|
|
|
FALSE - Otherwise.
|
2005-07-30 10:19:57 +02:00
|
|
|
NOTES
|
|
|
|
This function assumes that for any "CALL proc(...)" statement routines_list
|
|
|
|
will have 'proc' as first element (it may have several, consider e.g.
|
|
|
|
"proc(sp_func(...)))". This property is currently guaranted by the parser.
|
|
|
|
*/
|
|
|
|
|
2005-08-03 05:37:32 +02:00
|
|
|
void sp_get_prelocking_info(THD *thd, bool *need_prelocking,
|
|
|
|
bool *first_no_prelocking)
|
2005-07-30 10:19:57 +02:00
|
|
|
{
|
|
|
|
Sroutine_hash_entry *routine;
|
2005-08-03 05:37:32 +02:00
|
|
|
routine= (Sroutine_hash_entry*)thd->lex->sroutines_list.first;
|
2005-07-30 10:19:57 +02:00
|
|
|
|
2005-08-03 05:37:32 +02:00
|
|
|
DBUG_ASSERT(routine);
|
|
|
|
bool first_is_procedure= (routine->key.str[0] == TYPE_ENUM_PROCEDURE);
|
2005-07-30 10:19:57 +02:00
|
|
|
|
2005-08-03 05:37:32 +02:00
|
|
|
*first_no_prelocking= first_is_procedure;
|
|
|
|
*need_prelocking= !first_is_procedure || test(routine->next);
|
2005-07-30 10:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
/*
|
|
|
|
Auxilary function that adds new element to the set of stored routines
|
|
|
|
used by statement.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
add_used_routine()
|
2005-12-07 10:27:17 +01:00
|
|
|
lex LEX representing statement
|
|
|
|
arena Arena in which memory for new element will be allocated
|
|
|
|
key Key for the hash representing set
|
|
|
|
belong_to_view Uppermost view which uses this routine
|
|
|
|
(0 if routine is not used by view)
|
2005-07-09 19:51:59 +02:00
|
|
|
|
|
|
|
NOTES
|
|
|
|
Will also add element to end of 'LEX::sroutines_list' list.
|
|
|
|
|
|
|
|
In case when statement uses stored routines but does not need
|
|
|
|
prelocking (i.e. it does not use any tables) we will access the
|
|
|
|
elements of LEX::sroutines set on prepared statement re-execution.
|
|
|
|
Because of this we have to allocate memory for both hash element
|
|
|
|
and copy of its key in persistent arena.
|
|
|
|
|
|
|
|
TODO
|
|
|
|
When we will got rid of these accesses on re-executions we will be
|
|
|
|
able to allocate memory for hash elements in non-persitent arena
|
|
|
|
and directly use key values from sp_head::m_sroutines sets instead
|
|
|
|
of making their copies.
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
TRUE - new element was added.
|
|
|
|
FALSE - element was not added (because it is already present in the set).
|
|
|
|
*/
|
|
|
|
|
2005-07-09 21:11:17 +02:00
|
|
|
static bool add_used_routine(LEX *lex, Query_arena *arena,
|
2005-12-07 10:27:17 +01:00
|
|
|
const LEX_STRING *key,
|
|
|
|
TABLE_LIST *belong_to_view)
|
2003-03-02 19:17:41 +01:00
|
|
|
{
|
2006-11-01 13:41:48 +01:00
|
|
|
hash_init_opt(&lex->sroutines, system_charset_info,
|
|
|
|
Query_tables_list::START_SROUTINES_HASH_SIZE,
|
|
|
|
0, 0, sp_sroutine_key, 0, 0);
|
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
if (!hash_search(&lex->sroutines, (byte *)key->str, key->length))
|
2003-03-02 19:17:41 +01:00
|
|
|
{
|
2005-07-09 19:51:59 +02:00
|
|
|
Sroutine_hash_entry *rn=
|
|
|
|
(Sroutine_hash_entry *)arena->alloc(sizeof(Sroutine_hash_entry) +
|
|
|
|
key->length);
|
|
|
|
if (!rn) // OOM. Error will be reported using fatal_error().
|
|
|
|
return FALSE;
|
|
|
|
rn->key.length= key->length;
|
|
|
|
rn->key.str= (char *)rn + sizeof(Sroutine_hash_entry);
|
|
|
|
memcpy(rn->key.str, key->str, key->length);
|
|
|
|
my_hash_insert(&lex->sroutines, (byte *)rn);
|
|
|
|
lex->sroutines_list.link_in_list((byte *)rn, (byte **)&rn->next);
|
2005-12-07 10:27:17 +01:00
|
|
|
rn->belong_to_view= belong_to_view;
|
2005-07-09 19:51:59 +02:00
|
|
|
return TRUE;
|
2003-03-02 19:17:41 +01:00
|
|
|
}
|
2005-07-09 19:51:59 +02:00
|
|
|
return FALSE;
|
2003-03-02 19:17:41 +01:00
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2005-03-04 14:35:28 +01:00
|
|
|
/*
|
2005-09-15 01:56:09 +02:00
|
|
|
Add routine which is explicitly used by statement to the set of stored
|
|
|
|
routines used by this statement.
|
2005-03-04 14:35:28 +01:00
|
|
|
|
|
|
|
SYNOPSIS
|
2005-07-09 19:51:59 +02:00
|
|
|
sp_add_used_routine()
|
|
|
|
lex - LEX representing statement
|
|
|
|
arena - arena in which memory for new element of the set
|
|
|
|
will be allocated
|
|
|
|
rt - routine name
|
|
|
|
rt_type - routine type (one of TYPE_ENUM_PROCEDURE/...)
|
|
|
|
|
|
|
|
NOTES
|
2005-09-15 01:56:09 +02:00
|
|
|
Will also add element to end of 'LEX::sroutines_list' list (and will
|
|
|
|
take into account that this is explicitly used routine).
|
2005-07-09 19:51:59 +02:00
|
|
|
|
|
|
|
To be friendly towards prepared statements one should pass
|
|
|
|
persistent arena as second argument.
|
|
|
|
*/
|
|
|
|
|
2005-07-09 21:11:17 +02:00
|
|
|
void sp_add_used_routine(LEX *lex, Query_arena *arena,
|
2005-07-09 19:51:59 +02:00
|
|
|
sp_name *rt, char rt_type)
|
|
|
|
{
|
|
|
|
rt->set_routine_type(rt_type);
|
2005-12-07 10:27:17 +01:00
|
|
|
(void)add_used_routine(lex, arena, &rt->m_sroutines_key, 0);
|
2005-09-15 01:56:09 +02:00
|
|
|
lex->sroutines_list_own_last= lex->sroutines_list.next;
|
|
|
|
lex->sroutines_list_own_elements= lex->sroutines_list.elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Remove routines which are only indirectly used by statement from
|
|
|
|
the set of routines used by this statement.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_remove_not_own_routines()
|
|
|
|
lex LEX representing statement
|
|
|
|
*/
|
|
|
|
|
|
|
|
void sp_remove_not_own_routines(LEX *lex)
|
|
|
|
{
|
|
|
|
Sroutine_hash_entry *not_own_rt, *next_rt;
|
|
|
|
for (not_own_rt= *(Sroutine_hash_entry **)lex->sroutines_list_own_last;
|
|
|
|
not_own_rt; not_own_rt= next_rt)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
It is safe to obtain not_own_rt->next after calling hash_delete() now
|
|
|
|
but we want to be more future-proof.
|
|
|
|
*/
|
|
|
|
next_rt= not_own_rt->next;
|
|
|
|
hash_delete(&lex->sroutines, (byte *)not_own_rt);
|
|
|
|
}
|
|
|
|
|
|
|
|
*(Sroutine_hash_entry **)lex->sroutines_list_own_last= NULL;
|
|
|
|
lex->sroutines_list.next= lex->sroutines_list_own_last;
|
|
|
|
lex->sroutines_list.elements= lex->sroutines_list_own_elements;
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Merge contents of two hashes representing sets of routines used
|
|
|
|
by statements or by other routines.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_update_sp_used_routines()
|
2005-03-04 14:35:28 +01:00
|
|
|
dst - hash to which elements should be added
|
|
|
|
src - hash from which elements merged
|
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
NOTE
|
|
|
|
This procedure won't create new Sroutine_hash_entry objects,
|
|
|
|
instead it will simply add elements from source to destination
|
|
|
|
hash. Thus time of life of elements in destination hash becomes
|
|
|
|
dependant on time of life of elements from source hash. It also
|
|
|
|
won't touch lists linking elements in source and destination
|
|
|
|
hashes.
|
2005-03-04 14:35:28 +01:00
|
|
|
*/
|
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
void sp_update_sp_used_routines(HASH *dst, HASH *src)
|
2003-03-02 19:17:41 +01:00
|
|
|
{
|
2005-02-08 20:52:50 +01:00
|
|
|
for (uint i=0 ; i < src->records ; i++)
|
2003-03-02 19:17:41 +01:00
|
|
|
{
|
2005-07-09 19:51:59 +02:00
|
|
|
Sroutine_hash_entry *rt= (Sroutine_hash_entry *)hash_element(src, i);
|
|
|
|
if (!hash_search(dst, (byte *)rt->key.str, rt->key.length))
|
|
|
|
my_hash_insert(dst, (byte *)rt);
|
2003-03-02 19:17:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2005-03-04 14:35:28 +01:00
|
|
|
/*
|
2005-07-09 19:51:59 +02:00
|
|
|
Add contents of hash representing set of routines to the set of
|
|
|
|
routines used by statement.
|
2005-03-04 14:35:28 +01:00
|
|
|
|
|
|
|
SYNOPSIS
|
2005-07-09 19:51:59 +02:00
|
|
|
sp_update_stmt_used_routines()
|
2005-12-07 10:27:17 +01:00
|
|
|
thd Thread context
|
|
|
|
lex LEX representing statement
|
|
|
|
src Hash representing set from which routines will be added
|
|
|
|
belong_to_view Uppermost view which uses these routines, 0 if none
|
2005-07-09 19:51:59 +02:00
|
|
|
|
|
|
|
NOTE
|
|
|
|
It will also add elements to end of 'LEX::sroutines_list' list.
|
|
|
|
*/
|
|
|
|
|
2005-12-07 10:27:17 +01:00
|
|
|
static void
|
|
|
|
sp_update_stmt_used_routines(THD *thd, LEX *lex, HASH *src,
|
|
|
|
TABLE_LIST *belong_to_view)
|
2005-07-09 19:51:59 +02:00
|
|
|
{
|
|
|
|
for (uint i=0 ; i < src->records ; i++)
|
|
|
|
{
|
|
|
|
Sroutine_hash_entry *rt= (Sroutine_hash_entry *)hash_element(src, i);
|
2005-12-07 10:27:17 +01:00
|
|
|
(void)add_used_routine(lex, thd->stmt_arena, &rt->key, belong_to_view);
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-15 01:56:09 +02:00
|
|
|
/*
|
|
|
|
Add contents of list representing set of routines to the set of
|
|
|
|
routines used by statement.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_update_stmt_used_routines()
|
2005-12-07 10:27:17 +01:00
|
|
|
thd Thread context
|
|
|
|
lex LEX representing statement
|
|
|
|
src List representing set from which routines will be added
|
|
|
|
belong_to_view Uppermost view which uses these routines, 0 if none
|
2005-09-15 01:56:09 +02:00
|
|
|
|
|
|
|
NOTE
|
|
|
|
It will also add elements to end of 'LEX::sroutines_list' list.
|
|
|
|
*/
|
|
|
|
|
2005-12-07 10:27:17 +01:00
|
|
|
static void sp_update_stmt_used_routines(THD *thd, LEX *lex, SQL_LIST *src,
|
|
|
|
TABLE_LIST *belong_to_view)
|
2005-09-15 01:56:09 +02:00
|
|
|
{
|
|
|
|
for (Sroutine_hash_entry *rt= (Sroutine_hash_entry *)src->first;
|
|
|
|
rt; rt= rt->next)
|
2005-12-07 10:27:17 +01:00
|
|
|
(void)add_used_routine(lex, thd->stmt_arena, &rt->key, belong_to_view);
|
2005-09-15 01:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
/*
|
|
|
|
Cache sub-set of routines used by statement, add tables used by these
|
|
|
|
routines to statement table list. Do the same for all routines used
|
|
|
|
by these routines.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_cache_routines_and_add_tables_aux()
|
2005-07-30 10:19:57 +02:00
|
|
|
thd - thread context
|
|
|
|
lex - LEX representing statement
|
|
|
|
start - first routine from the list of routines to be cached
|
|
|
|
(this list defines mentioned sub-set).
|
|
|
|
first_no_prelock - If true, don't add tables or cache routines used by
|
|
|
|
the body of the first routine (i.e. *start)
|
|
|
|
will be executed in non-prelocked mode.
|
2005-10-26 15:34:57 +02:00
|
|
|
tabs_changed - Set to TRUE some tables were added, FALSE otherwise
|
2005-03-04 14:35:28 +01:00
|
|
|
NOTE
|
|
|
|
If some function is missing this won't be reported here.
|
|
|
|
Instead this fact will be discovered during query execution.
|
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
RETURN VALUE
|
2005-11-25 17:09:26 +01:00
|
|
|
0 - success
|
|
|
|
non-0 - failure
|
2005-03-04 14:35:28 +01:00
|
|
|
*/
|
|
|
|
|
2005-10-26 15:34:57 +02:00
|
|
|
static int
|
2005-07-09 19:51:59 +02:00
|
|
|
sp_cache_routines_and_add_tables_aux(THD *thd, LEX *lex,
|
2005-07-30 10:19:57 +02:00
|
|
|
Sroutine_hash_entry *start,
|
2005-10-26 15:34:57 +02:00
|
|
|
bool first_no_prelock, bool *tabs_changed)
|
2003-03-02 19:17:41 +01:00
|
|
|
{
|
2005-10-26 15:34:57 +02:00
|
|
|
int ret= 0;
|
2005-12-06 14:25:12 +01:00
|
|
|
bool tabschnd= 0; /* Set if tables changed */
|
2005-07-30 10:19:57 +02:00
|
|
|
bool first= TRUE;
|
2005-07-09 19:51:59 +02:00
|
|
|
DBUG_ENTER("sp_cache_routines_and_add_tables_aux");
|
2003-03-02 19:17:41 +01:00
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
for (Sroutine_hash_entry *rt= start; rt; rt= rt->next)
|
2003-03-02 19:17:41 +01:00
|
|
|
{
|
2005-07-09 19:51:59 +02:00
|
|
|
sp_name name(rt->key.str, rt->key.length);
|
|
|
|
int type= rt->key.str[0];
|
|
|
|
sp_head *sp;
|
2003-07-01 18:14:24 +02:00
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
if (!(sp= sp_cache_lookup((type == TYPE_ENUM_FUNCTION ?
|
|
|
|
&thd->sp_func_cache : &thd->sp_proc_cache),
|
|
|
|
&name)))
|
2003-03-02 19:17:41 +01:00
|
|
|
{
|
2005-07-09 19:51:59 +02:00
|
|
|
name.m_name.str= strchr(name.m_qname.str, '.');
|
|
|
|
name.m_db.length= name.m_name.str - name.m_qname.str;
|
|
|
|
name.m_db.str= strmake_root(thd->mem_root, name.m_qname.str,
|
|
|
|
name.m_db.length);
|
|
|
|
name.m_name.str+= 1;
|
|
|
|
name.m_name.length= name.m_qname.length - name.m_db.length - 1;
|
|
|
|
|
2005-10-26 15:34:57 +02:00
|
|
|
switch ((ret= db_find_routine(thd, type, &name, &sp)))
|
2003-07-07 14:55:10 +02:00
|
|
|
{
|
2005-10-26 15:34:57 +02:00
|
|
|
case SP_OK:
|
|
|
|
{
|
|
|
|
if (type == TYPE_ENUM_FUNCTION)
|
|
|
|
sp_cache_insert(&thd->sp_func_cache, sp);
|
|
|
|
else
|
|
|
|
sp_cache_insert(&thd->sp_proc_cache, sp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SP_KEY_NOT_FOUND:
|
|
|
|
ret= SP_OK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
2005-11-25 17:09:26 +01:00
|
|
|
Any error when loading an existing routine is either some problem
|
|
|
|
with the mysql.proc table, or a parse error because the contents
|
|
|
|
has been tampered with (in which case we clear that error).
|
|
|
|
*/
|
|
|
|
if (ret == SP_PARSE_ERROR)
|
|
|
|
thd->clear_error();
|
2005-12-06 14:25:12 +01:00
|
|
|
/*
|
|
|
|
If we cleared the parse error, or when db_find_routine() flagged
|
|
|
|
an error with it's return value without calling my_error(), we
|
|
|
|
set the generic "mysql.proc table corrupt" error here.
|
|
|
|
*/
|
2005-11-25 17:09:26 +01:00
|
|
|
if (!thd->net.report_error)
|
|
|
|
{
|
2006-09-27 21:23:17 +02:00
|
|
|
/*
|
|
|
|
SP allows full NAME_LEN chars thus he have to allocate enough
|
|
|
|
size in bytes. Otherwise there is stack overrun could happen
|
|
|
|
if multibyte sequence is `name`. `db` is still safe because the
|
|
|
|
rest of the server checks agains NAME_LEN bytes and not chars.
|
|
|
|
Hence, the overrun happens only if the name is in length > 32 and
|
|
|
|
uses multibyte (cyrillic, greek, etc.)
|
|
|
|
|
|
|
|
!! Change 3 with SYSTEM_CHARSET_MBMAXLEN when it's defined.
|
|
|
|
*/
|
|
|
|
char n[NAME_LEN*3*2+2];
|
2005-11-25 17:09:26 +01:00
|
|
|
|
|
|
|
/* m_qname.str is not always \0 terminated */
|
|
|
|
memcpy(n, name.m_qname.str, name.m_qname.length);
|
|
|
|
n[name.m_qname.length]= '\0';
|
|
|
|
my_error(ER_SP_PROC_TABLE_CORRUPT, MYF(0), n, ret);
|
|
|
|
}
|
2005-10-26 15:34:57 +02:00
|
|
|
break;
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sp)
|
|
|
|
{
|
2005-07-30 10:19:57 +02:00
|
|
|
if (!(first && first_no_prelock))
|
|
|
|
{
|
2005-12-07 10:27:17 +01:00
|
|
|
sp_update_stmt_used_routines(thd, lex, &sp->m_sroutines,
|
|
|
|
rt->belong_to_view);
|
2005-11-25 17:09:26 +01:00
|
|
|
tabschnd|=
|
2005-12-07 10:47:25 +01:00
|
|
|
sp->add_used_tables_to_table_list(thd, &lex->query_tables_last,
|
|
|
|
rt->belong_to_view);
|
2005-07-30 10:19:57 +02:00
|
|
|
}
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
2005-07-30 10:19:57 +02:00
|
|
|
first= FALSE;
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
2005-11-25 17:09:26 +01:00
|
|
|
if (tabs_changed) /* it can be NULL */
|
|
|
|
*tabs_changed= tabschnd;
|
2005-10-26 15:34:57 +02:00
|
|
|
DBUG_RETURN(ret);
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cache all routines from the set of used by statement, add tables used
|
|
|
|
by those routines to statement table list. Do the same for all routines
|
|
|
|
used by those routines.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_cache_routines_and_add_tables()
|
2005-07-30 10:19:57 +02:00
|
|
|
thd - thread context
|
|
|
|
lex - LEX representing statement
|
|
|
|
first_no_prelock - If true, don't add tables or cache routines used by
|
|
|
|
the body of the first routine (i.e. *start)
|
2005-10-26 15:34:57 +02:00
|
|
|
tabs_changed - Set to TRUE some tables were added, FALSE otherwise
|
2005-07-30 10:19:57 +02:00
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
RETURN VALUE
|
2005-11-25 17:09:26 +01:00
|
|
|
0 - success
|
|
|
|
non-0 - failure
|
2005-07-09 19:51:59 +02:00
|
|
|
*/
|
|
|
|
|
2005-10-26 15:34:57 +02:00
|
|
|
int
|
|
|
|
sp_cache_routines_and_add_tables(THD *thd, LEX *lex, bool first_no_prelock,
|
|
|
|
bool *tabs_changed)
|
2005-07-09 19:51:59 +02:00
|
|
|
{
|
|
|
|
return sp_cache_routines_and_add_tables_aux(thd, lex,
|
2005-07-30 10:19:57 +02:00
|
|
|
(Sroutine_hash_entry *)lex->sroutines_list.first,
|
2005-10-26 15:34:57 +02:00
|
|
|
first_no_prelock, tabs_changed);
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Add all routines used by view to the set of routines used by statement.
|
|
|
|
Add tables used by those routines to statement table list. Do the same
|
|
|
|
for all routines used by these routines.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_cache_routines_and_add_tables_for_view()
|
2005-12-07 10:27:17 +01:00
|
|
|
thd Thread context
|
|
|
|
lex LEX representing statement
|
|
|
|
view Table list element representing view
|
2005-12-07 10:47:25 +01:00
|
|
|
|
2005-10-26 15:34:57 +02:00
|
|
|
RETURN VALUE
|
2005-11-25 17:09:26 +01:00
|
|
|
0 - success
|
|
|
|
non-0 - failure
|
2005-07-09 19:51:59 +02:00
|
|
|
*/
|
|
|
|
|
2005-10-26 15:34:57 +02:00
|
|
|
int
|
2005-12-07 10:27:17 +01:00
|
|
|
sp_cache_routines_and_add_tables_for_view(THD *thd, LEX *lex, TABLE_LIST *view)
|
2005-07-09 19:51:59 +02:00
|
|
|
{
|
|
|
|
Sroutine_hash_entry **last_cached_routine_ptr=
|
|
|
|
(Sroutine_hash_entry **)lex->sroutines_list.next;
|
2005-12-07 10:27:17 +01:00
|
|
|
sp_update_stmt_used_routines(thd, lex, &view->view->sroutines_list,
|
|
|
|
view->top_table());
|
2005-10-26 15:34:57 +02:00
|
|
|
return sp_cache_routines_and_add_tables_aux(thd, lex,
|
|
|
|
*last_cached_routine_ptr, FALSE,
|
|
|
|
NULL);
|
2005-07-09 19:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Add triggers for table to the set of routines used by statement.
|
|
|
|
Add tables used by them to statement table list. Do the same for
|
|
|
|
all implicitly used routines.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_cache_routines_and_add_tables_for_triggers()
|
2005-12-07 10:27:17 +01:00
|
|
|
thd thread context
|
|
|
|
lex LEX respresenting statement
|
|
|
|
table Table list element for table with trigger
|
2005-10-26 15:34:57 +02:00
|
|
|
|
|
|
|
RETURN VALUE
|
2005-11-25 17:09:26 +01:00
|
|
|
0 - success
|
|
|
|
non-0 - failure
|
2005-07-09 19:51:59 +02:00
|
|
|
*/
|
|
|
|
|
2005-10-26 15:34:57 +02:00
|
|
|
int
|
2005-07-09 19:51:59 +02:00
|
|
|
sp_cache_routines_and_add_tables_for_triggers(THD *thd, LEX *lex,
|
2005-12-07 10:27:17 +01:00
|
|
|
TABLE_LIST *table)
|
2005-07-09 19:51:59 +02:00
|
|
|
{
|
2005-10-26 15:34:57 +02:00
|
|
|
int ret= 0;
|
2005-12-07 10:27:17 +01:00
|
|
|
Table_triggers_list *triggers= table->table->triggers;
|
|
|
|
if (add_used_routine(lex, thd->stmt_arena, &triggers->sroutines_key,
|
|
|
|
table->belong_to_view))
|
2005-07-09 19:51:59 +02:00
|
|
|
{
|
|
|
|
Sroutine_hash_entry **last_cached_routine_ptr=
|
|
|
|
(Sroutine_hash_entry **)lex->sroutines_list.next;
|
2005-07-19 18:06:49 +02:00
|
|
|
for (int i= 0; i < (int)TRG_EVENT_MAX; i++)
|
2005-07-28 15:10:14 +02:00
|
|
|
{
|
2005-07-19 18:06:49 +02:00
|
|
|
for (int j= 0; j < (int)TRG_ACTION_MAX; j++)
|
2005-07-28 15:10:14 +02:00
|
|
|
{
|
2005-07-09 19:51:59 +02:00
|
|
|
if (triggers->bodies[i][j])
|
2005-03-04 14:35:28 +01:00
|
|
|
{
|
2005-12-07 10:27:17 +01:00
|
|
|
(void)triggers->bodies[i][j]->
|
|
|
|
add_used_tables_to_table_list(thd, &lex->query_tables_last,
|
|
|
|
table->belong_to_view);
|
2005-07-09 19:51:59 +02:00
|
|
|
sp_update_stmt_used_routines(thd, lex,
|
2005-12-07 10:27:17 +01:00
|
|
|
&triggers->bodies[i][j]->m_sroutines,
|
|
|
|
table->belong_to_view);
|
2005-03-04 14:35:28 +01:00
|
|
|
}
|
2005-07-28 15:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
2005-10-26 15:34:57 +02:00
|
|
|
ret= sp_cache_routines_and_add_tables_aux(thd, lex,
|
|
|
|
*last_cached_routine_ptr,
|
|
|
|
FALSE, NULL);
|
2003-03-02 19:17:41 +01:00
|
|
|
}
|
2005-10-26 15:34:57 +02:00
|
|
|
return ret;
|
2003-02-26 19:22:29 +01:00
|
|
|
}
|
2003-12-12 14:05:29 +01:00
|
|
|
|
2005-07-09 19:51:59 +02:00
|
|
|
|
2004-06-09 14:19:43 +02:00
|
|
|
/*
|
|
|
|
* Generates the CREATE... string from the table information.
|
|
|
|
* Returns TRUE on success, FALSE on (alloc) failure.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
create_string(THD *thd, String *buf,
|
2003-12-12 14:05:29 +01:00
|
|
|
int type,
|
2004-03-11 17:18:59 +01:00
|
|
|
sp_name *name,
|
2003-12-12 14:05:29 +01:00
|
|
|
const char *params, ulong paramslen,
|
|
|
|
const char *returns, ulong returnslen,
|
|
|
|
const char *body, ulong bodylen,
|
2006-03-02 13:18:49 +01:00
|
|
|
st_sp_chistics *chistics,
|
|
|
|
const LEX_STRING *definer_user,
|
|
|
|
const LEX_STRING *definer_host)
|
2003-12-12 14:05:29 +01:00
|
|
|
{
|
2004-06-09 14:19:43 +02:00
|
|
|
/* Make some room to begin with */
|
|
|
|
if (buf->alloc(100 + name->m_qname.length + paramslen + returnslen + bodylen +
|
2006-03-02 13:18:49 +01:00
|
|
|
chistics->comment.length + 10 /* length of " DEFINER= "*/ +
|
|
|
|
USER_HOST_BUFF_SIZE))
|
2004-06-09 14:19:43 +02:00
|
|
|
return FALSE;
|
2003-12-21 11:48:03 +01:00
|
|
|
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN("CREATE "));
|
2006-03-02 13:18:49 +01:00
|
|
|
append_definer(thd, buf, definer_user, definer_host);
|
2003-12-12 14:05:29 +01:00
|
|
|
if (type == TYPE_ENUM_FUNCTION)
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN("FUNCTION "));
|
2004-06-09 14:19:43 +02:00
|
|
|
else
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN("PROCEDURE "));
|
2004-06-09 14:19:43 +02:00
|
|
|
append_identifier(thd, buf, name->m_name.str, name->m_name.length);
|
|
|
|
buf->append('(');
|
|
|
|
buf->append(params, paramslen);
|
|
|
|
buf->append(')');
|
|
|
|
if (type == TYPE_ENUM_FUNCTION)
|
|
|
|
{
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN(" RETURNS "));
|
2004-06-09 14:19:43 +02:00
|
|
|
buf->append(returns, returnslen);
|
|
|
|
}
|
|
|
|
buf->append('\n');
|
2004-10-14 18:07:09 +02:00
|
|
|
switch (chistics->daccess) {
|
|
|
|
case SP_NO_SQL:
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN(" NO SQL\n"));
|
2004-10-14 18:07:09 +02:00
|
|
|
break;
|
|
|
|
case SP_READS_SQL_DATA:
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN(" READS SQL DATA\n"));
|
2004-10-14 18:07:09 +02:00
|
|
|
break;
|
|
|
|
case SP_MODIFIES_SQL_DATA:
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN(" MODIFIES SQL DATA\n"));
|
2004-10-14 18:07:09 +02:00
|
|
|
break;
|
2004-11-03 11:39:38 +01:00
|
|
|
case SP_DEFAULT_ACCESS:
|
|
|
|
case SP_CONTAINS_SQL:
|
|
|
|
/* Do nothing */
|
|
|
|
break;
|
2004-10-14 18:07:09 +02:00
|
|
|
}
|
2003-12-12 14:05:29 +01:00
|
|
|
if (chistics->detistic)
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN(" DETERMINISTIC\n"));
|
2004-10-14 18:07:09 +02:00
|
|
|
if (chistics->suid == SP_IS_NOT_SUID)
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN(" SQL SECURITY INVOKER\n"));
|
2003-12-13 16:40:52 +01:00
|
|
|
if (chistics->comment.length)
|
2003-12-21 01:07:45 +01:00
|
|
|
{
|
2005-11-23 00:29:25 +01:00
|
|
|
buf->append(STRING_WITH_LEN(" COMMENT "));
|
2004-06-09 14:19:43 +02:00
|
|
|
append_unescaped(buf, chistics->comment.str, chistics->comment.length);
|
|
|
|
buf->append('\n');
|
2003-12-21 01:07:45 +01:00
|
|
|
}
|
2004-06-09 14:19:43 +02:00
|
|
|
buf->append(body, bodylen);
|
|
|
|
return TRUE;
|
2003-12-12 14:05:29 +01:00
|
|
|
}
|
2004-03-11 17:18:59 +01:00
|
|
|
|
|
|
|
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Change the current database if needed.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
sp_use_new_db()
|
|
|
|
thd thread handle
|
|
|
|
new_db new database name (a string and its length)
|
|
|
|
old_db [IN] str points to a buffer where to store the old
|
|
|
|
database, length contains the size of the buffer
|
|
|
|
[OUT] if old db was not NULL, its name is copied
|
|
|
|
to the buffer pointed at by str and length is updated
|
|
|
|
accordingly. Otherwise str[0] is set to '\0' and length
|
|
|
|
is set to 0. The out parameter should be used only if
|
|
|
|
the database name has been changed (see dbchangedp).
|
|
|
|
dbchangedp [OUT] is set to TRUE if the current database is changed,
|
|
|
|
FALSE otherwise. A database is not changed if the old
|
|
|
|
name is the same as the new one, both names are empty,
|
|
|
|
or an error has occurred.
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
0 success
|
|
|
|
1 access denied or out of memory (the error message is
|
|
|
|
set in THD)
|
|
|
|
*/
|
2004-03-11 17:18:59 +01:00
|
|
|
|
|
|
|
int
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
sp_use_new_db(THD *thd, LEX_STRING new_db, LEX_STRING *old_db,
|
2004-06-08 18:41:18 +02:00
|
|
|
bool no_access_check, bool *dbchangedp)
|
2004-03-11 17:18:59 +01:00
|
|
|
{
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
int ret;
|
2004-03-11 17:18:59 +01:00
|
|
|
DBUG_ENTER("sp_use_new_db");
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
DBUG_PRINT("enter", ("newdb: %s", new_db.str));
|
2004-03-11 17:18:59 +01:00
|
|
|
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
/*
|
|
|
|
Set new_db to an empty string if it's NULL, because mysql_change_db
|
|
|
|
requires a non-NULL argument.
|
|
|
|
new_db.str can be NULL only if we're restoring the old database after
|
|
|
|
execution of a stored procedure and there were no current database
|
|
|
|
selected. The stored procedure itself must always have its database
|
|
|
|
initialized.
|
|
|
|
*/
|
|
|
|
if (new_db.str == NULL)
|
|
|
|
new_db.str= empty_c_string;
|
|
|
|
|
|
|
|
if (thd->db)
|
2004-03-11 17:18:59 +01:00
|
|
|
{
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
old_db->length= (strmake(old_db->str, thd->db, old_db->length) -
|
|
|
|
old_db->str);
|
2004-03-11 17:18:59 +01:00
|
|
|
}
|
|
|
|
else
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
{
|
|
|
|
old_db->str[0]= '\0';
|
|
|
|
old_db->length= 0;
|
2004-03-11 17:18:59 +01:00
|
|
|
}
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
|
|
|
|
/* Don't change the database if the new name is the same as the old one. */
|
|
|
|
if (my_strcasecmp(system_charset_info, old_db->str, new_db.str) == 0)
|
2004-03-11 17:18:59 +01:00
|
|
|
{
|
2004-06-08 18:41:18 +02:00
|
|
|
*dbchangedp= FALSE;
|
2004-03-11 17:18:59 +01:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
ret= mysql_change_db(thd, new_db.str, no_access_check);
|
|
|
|
|
|
|
|
*dbchangedp= ret == 0;
|
|
|
|
DBUG_RETURN(ret);
|
2004-03-11 17:18:59 +01:00
|
|
|
}
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
|