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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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-02-21 17:37:05 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* DB storage of Stored PROCEDUREs and FUNCTIONs
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
db_find_routine_aux(THD *thd, int type, char *name, uint namelen,
|
|
|
|
enum thr_lock_type ltype, TABLE **tablep)
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_ENTER("db_find_routine_aux");
|
|
|
|
DBUG_PRINT("enter", ("type: %d name: %*s", type, namelen, name));
|
2002-12-12 13:14:23 +01:00
|
|
|
TABLE *table;
|
|
|
|
TABLE_LIST tables;
|
2003-02-21 17:37:05 +01:00
|
|
|
byte key[65]; // We know name is 64 and the enum is 1 byte
|
|
|
|
uint keylen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// Put the key together
|
|
|
|
keylen= namelen;
|
|
|
|
if (keylen > sizeof(key)-1)
|
|
|
|
keylen= sizeof(key)-1;
|
|
|
|
memcpy(key, name, keylen);
|
|
|
|
memset(key+keylen, (int)' ', sizeof(key)-1 - keylen); // Pad with space
|
|
|
|
key[sizeof(key)-1]= type;
|
|
|
|
keylen= sizeof(key);
|
2002-12-12 13:14:23 +01:00
|
|
|
|
|
|
|
memset(&tables, 0, sizeof(tables));
|
|
|
|
tables.db= (char*)"mysql";
|
|
|
|
tables.real_name= tables.alias= (char*)"proc";
|
2003-02-21 17:37:05 +01:00
|
|
|
if (! (table= open_ltable(thd, &tables, ltype)))
|
|
|
|
DBUG_RETURN(SP_OPEN_TABLE_FAILED);
|
2002-12-12 13:14:23 +01:00
|
|
|
|
|
|
|
if (table->file->index_read_idx(table->record[0], 0,
|
2003-02-21 17:37:05 +01:00
|
|
|
key, keylen,
|
2002-12-12 13:14:23 +01:00
|
|
|
HA_READ_KEY_EXACT))
|
2003-02-21 17:37:05 +01:00
|
|
|
{
|
|
|
|
close_thread_tables(thd);
|
|
|
|
DBUG_RETURN(SP_KEY_NOT_FOUND);
|
|
|
|
}
|
|
|
|
*tablep= table;
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_RETURN(SP_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
db_find_routine(THD *thd, int type, char *name, uint namelen, sp_head **sphp)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("db_find_routine");
|
|
|
|
DBUG_PRINT("enter", ("type: %d name: %*s", type, namelen, name));
|
|
|
|
extern int yyparse(void *thd);
|
|
|
|
LEX *tmplex;
|
|
|
|
TABLE *table;
|
|
|
|
const char *defstr;
|
|
|
|
int ret;
|
2002-12-12 13:14:23 +01:00
|
|
|
|
|
|
|
// QQ Set up our own mem_root here???
|
2003-02-21 17:37:05 +01:00
|
|
|
ret= db_find_routine_aux(thd, type, name, namelen, TL_READ, &table);
|
|
|
|
if (ret != SP_OK)
|
|
|
|
goto done;
|
|
|
|
if ((defstr= get_field(&thd->mem_root, table->field[2])) == NULL)
|
|
|
|
{
|
|
|
|
ret= SP_GET_FIELD_FAILED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-12-12 13:14:23 +01:00
|
|
|
tmplex= lex_start(thd, (uchar*)defstr, strlen(defstr));
|
2003-02-18 19:58:03 +01:00
|
|
|
if (yyparse(thd) || thd->is_fatal_error || tmplex->sphead == NULL)
|
2003-02-21 17:37:05 +01:00
|
|
|
ret= SP_PARSE_ERROR;
|
2002-12-12 13:14:23 +01:00
|
|
|
else
|
2003-02-21 17:37:05 +01:00
|
|
|
*sphp= tmplex->sphead;
|
2002-12-12 13:14:23 +01:00
|
|
|
|
|
|
|
done:
|
2003-02-21 17:37:05 +01:00
|
|
|
if (ret == SP_OK && table)
|
2002-12-12 13:14:23 +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-02-21 17:37:05 +01:00
|
|
|
static int
|
|
|
|
db_create_routine(THD *thd, int type,
|
|
|
|
char *name, uint namelen, char *def, uint deflen)
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2003-02-21 17:37:05 +01:00
|
|
|
DBUG_ENTER("db_create_routine");
|
|
|
|
DBUG_PRINT("enter", ("type: %d name: %*s def: %*s", type, namelen, name, deflen, def));
|
|
|
|
int ret;
|
2002-12-12 13:14:23 +01:00
|
|
|
TABLE *table;
|
|
|
|
TABLE_LIST tables;
|
|
|
|
|
|
|
|
memset(&tables, 0, sizeof(tables));
|
|
|
|
tables.db= (char*)"mysql";
|
|
|
|
tables.real_name= tables.alias= (char*)"proc";
|
2003-02-21 17:37:05 +01:00
|
|
|
|
2002-12-12 13:14:23 +01:00
|
|
|
if (! (table= open_ltable(thd, &tables, TL_WRITE)))
|
2003-02-21 17:37:05 +01:00
|
|
|
ret= SP_OPEN_TABLE_FAILED;
|
|
|
|
else
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2003-02-21 17:37:05 +01:00
|
|
|
restore_record(table, 2); // Get default values for fields
|
|
|
|
|
|
|
|
table->field[0]->store(name, namelen, default_charset_info);
|
|
|
|
table->field[1]->store((longlong)type);
|
|
|
|
table->field[2]->store(def, deflen, default_charset_info);
|
|
|
|
|
|
|
|
if (table->file->write_row(table->record[0]))
|
|
|
|
ret= SP_WRITE_ROW_FAILED;
|
|
|
|
else
|
|
|
|
ret= SP_OK;
|
2002-12-12 13:14:23 +01:00
|
|
|
}
|
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
if (ret == SP_OK && table)
|
|
|
|
close_thread_tables(thd);
|
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
static int
|
|
|
|
db_drop_routine(THD *thd, int type, char *name, uint namelen)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("db_drop_routine");
|
|
|
|
DBUG_PRINT("enter", ("type: %d name: %*s", type, namelen, name));
|
|
|
|
TABLE *table;
|
|
|
|
int ret;
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
ret= db_find_routine_aux(thd, type, name, namelen, TL_WRITE, &table);
|
|
|
|
if (ret == SP_OK)
|
|
|
|
{
|
|
|
|
if (table->file->delete_row(table->record[0]))
|
|
|
|
ret= SP_DELETE_ROW_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == SP_OK && table)
|
|
|
|
close_thread_tables(thd);
|
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* PROCEDURE
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
sp_head *
|
|
|
|
sp_find_procedure(THD *thd, LEX_STRING *name)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("sp_find_procedure");
|
|
|
|
sp_head *sp;
|
|
|
|
|
|
|
|
DBUG_PRINT("enter", ("name: %*s", name->length, name->str));
|
|
|
|
|
|
|
|
if (db_find_routine(thd, TYPE_ENUM_PROCEDURE,
|
|
|
|
name->str, name->length, &sp) != SP_OK)
|
|
|
|
sp= NULL;
|
|
|
|
|
|
|
|
DBUG_RETURN(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sp_create_procedure(THD *thd, char *name, uint namelen, char *def, uint deflen)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("sp_create_procedure");
|
|
|
|
DBUG_PRINT("enter", ("name: %*s def: %*s", namelen, name, deflen, def));
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret= db_create_routine(thd, TYPE_ENUM_PROCEDURE, name, namelen, def, deflen);
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-02-12 16:17:03 +01:00
|
|
|
DBUG_RETURN(ret);
|
2002-12-12 13:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-02-04 17:40:18 +01:00
|
|
|
sp_drop_procedure(THD *thd, char *name, uint namelen)
|
2002-12-12 13:14:23 +01:00
|
|
|
{
|
2003-02-12 16:17:03 +01:00
|
|
|
DBUG_ENTER("sp_drop_procedure");
|
|
|
|
DBUG_PRINT("enter", ("name: %*s", namelen, name));
|
2003-02-21 17:37:05 +01:00
|
|
|
int ret;
|
2002-12-12 13:14:23 +01:00
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
ret= db_drop_routine(thd, TYPE_ENUM_PROCEDURE, name, namelen);
|
|
|
|
|
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
2002-12-12 13:14:23 +01:00
|
|
|
|
|
|
|
|
2003-02-21 17:37:05 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* FUNCTION
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
sp_head *
|
|
|
|
sp_find_function(THD *thd, LEX_STRING *name)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("sp_find_function_i");
|
|
|
|
sp_head *sp;
|
|
|
|
|
|
|
|
DBUG_PRINT("enter", ("name: %*s", name->length, name->str));
|
|
|
|
|
|
|
|
if (db_find_routine(thd, TYPE_ENUM_FUNCTION,
|
|
|
|
name->str, name->length, &sp) != SP_OK)
|
|
|
|
sp= NULL;
|
|
|
|
|
|
|
|
DBUG_RETURN(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sp_create_function(THD *thd, char *name, uint namelen, char *def, uint deflen)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("sp_create_function");
|
|
|
|
DBUG_PRINT("enter", ("name: %*s def: %*s", namelen, name, deflen, def));
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret= db_create_routine(thd, TYPE_ENUM_FUNCTION, name, namelen, def, deflen);
|
|
|
|
|
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sp_drop_function(THD *thd, char *name, uint namelen)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("sp_drop_function");
|
|
|
|
DBUG_PRINT("enter", ("name: %*s", namelen, name));
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret= db_drop_routine(thd, TYPE_ENUM_FUNCTION, name, namelen);
|
|
|
|
|
|
|
|
DBUG_RETURN(ret);
|
2002-12-12 13:14:23 +01:00
|
|
|
}
|