Commit e3132d9a authored by pem@mysql.comhem.se's avatar pem@mysql.comhem.se

Fixed proper restoring of current db on errors.

This fixes part of BUG#3229: Stored procedure comment causes packets out of order.
parent f87f2791
...@@ -219,7 +219,7 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp) ...@@ -219,7 +219,7 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp)
ulong deflen; ulong deflen;
LEX *oldlex= thd->lex; LEX *oldlex= thd->lex;
char olddb[128]; char olddb[128];
char *olddbptr; bool dbchanged;
enum enum_sql_command oldcmd= thd->lex->sql_command; enum enum_sql_command oldcmd= thd->lex->sql_command;
ulong old_sql_mode= thd->variables.sql_mode; ulong old_sql_mode= thd->variables.sql_mode;
ha_rows select_limit= thd->variables.select_limit; ha_rows select_limit= thd->variables.select_limit;
...@@ -239,8 +239,9 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp) ...@@ -239,8 +239,9 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp)
goto done; goto done;
} }
olddbptr= thd->db; dbchanged= FALSE;
if ((ret= sp_use_new_db(thd, name->m_db.str, olddb, sizeof(olddb), 1))) if ((ret= sp_use_new_db(thd, name->m_db.str, olddb, sizeof(olddb),
1, &dbchanged)))
goto done; goto done;
{ {
...@@ -262,8 +263,7 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp) ...@@ -262,8 +263,7 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp)
LEX *newlex= thd->lex; LEX *newlex= thd->lex;
sp_head *sp= newlex->sphead; sp_head *sp= newlex->sphead;
if (olddbptr != thd->db && if (dbchanged && (ret= sp_change_db(thd, olddb, 1)))
(ret= sp_change_db(thd, olddb, 1)))
goto done; goto done;
if (sp) if (sp)
{ {
...@@ -276,8 +276,7 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp) ...@@ -276,8 +276,7 @@ db_find_routine(THD *thd, int type, sp_name *name, sp_head **sphp)
} }
else else
{ {
if (olddbptr != thd->db && if (dbchanged && (ret= sp_change_db(thd, olddb, 1)))
(ret= sp_change_db(thd, olddb, 1)))
goto done; goto done;
*sphp= thd->lex->sphead; *sphp= thd->lex->sphead;
(*sphp)->set_info((char *)definer, (uint)strlen(definer), (*sphp)->set_info((char *)definer, (uint)strlen(definer),
...@@ -990,7 +989,7 @@ create_string(THD *thd, ulong *lenp, ...@@ -990,7 +989,7 @@ create_string(THD *thd, ulong *lenp,
int int
sp_use_new_db(THD *thd, char *newdb, char *olddb, uint olddblen, sp_use_new_db(THD *thd, char *newdb, char *olddb, uint olddblen,
bool no_access_check) bool no_access_check, bool *dbchangedp)
{ {
bool changeit; bool changeit;
DBUG_ENTER("sp_use_new_db"); DBUG_ENTER("sp_use_new_db");
...@@ -1016,12 +1015,15 @@ sp_use_new_db(THD *thd, char *newdb, char *olddb, uint olddblen, ...@@ -1016,12 +1015,15 @@ sp_use_new_db(THD *thd, char *newdb, char *olddb, uint olddblen,
} }
if (!changeit) if (!changeit)
{ {
*dbchangedp= FALSE;
DBUG_RETURN(0); DBUG_RETURN(0);
} }
else else
{ {
int ret= sp_change_db(thd, newdb, no_access_check); int ret= sp_change_db(thd, newdb, no_access_check);
if (! ret)
*dbchangedp= TRUE;
DBUG_RETURN(ret); DBUG_RETURN(ret);
} }
} }
......
...@@ -93,9 +93,10 @@ sp_cache_functions(THD *thd, LEX *lex); ...@@ -93,9 +93,10 @@ sp_cache_functions(THD *thd, LEX *lex);
// Do a "use newdb". The current db is stored at olddb. // Do a "use newdb". The current db is stored at olddb.
// If newdb is the same as the current one, nothing is changed. // If newdb is the same as the current one, nothing is changed.
// dbchangedp is set to true if the db was actually changed.
int int
sp_use_new_db(THD *thd, char *newdb, char *olddb, uint olddbmax, sp_use_new_db(THD *thd, char *newdb, char *olddb, uint olddbmax,
bool no_access_check); bool no_access_check, bool *dbchangedp);
// Like mysql_change_db() but handles empty db name and the send_ok() problem. // Like mysql_change_db() but handles empty db name and the send_ok() problem.
int int
......
...@@ -376,7 +376,7 @@ sp_head::execute(THD *thd) ...@@ -376,7 +376,7 @@ sp_head::execute(THD *thd)
{ {
DBUG_ENTER("sp_head::execute"); DBUG_ENTER("sp_head::execute");
char olddb[128]; char olddb[128];
char *olddbptr; bool dbchanged;
sp_rcontext *ctx= thd->spcont; sp_rcontext *ctx= thd->spcont;
int ret= 0; int ret= 0;
uint ip= 0; uint ip= 0;
...@@ -388,8 +388,8 @@ sp_head::execute(THD *thd) ...@@ -388,8 +388,8 @@ sp_head::execute(THD *thd)
} }
#endif #endif
olddbptr= thd->db; dbchanged= FALSE;
if ((ret= sp_use_new_db(thd, m_db.str, olddb, sizeof(olddb), 0))) if ((ret= sp_use_new_db(thd, m_db.str, olddb, sizeof(olddb), 0, &dbchanged)))
goto done; goto done;
if (ctx) if (ctx)
...@@ -445,7 +445,7 @@ sp_head::execute(THD *thd) ...@@ -445,7 +445,7 @@ sp_head::execute(THD *thd)
ret= -1; ret= -1;
/* If the DB has changed, the pointer has changed too, but the /* If the DB has changed, the pointer has changed too, but the
original thd->db will then have been freed */ original thd->db will then have been freed */
if (olddbptr != thd->db) if (dbchanged)
{ {
if (! thd->killed) if (! thd->killed)
ret= sp_change_db(thd, olddb, 0); ret= sp_change_db(thd, olddb, 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment