Commit 585d88a2 authored by Alexey Botchkov's avatar Alexey Botchkov Committed by Oleksandr Byelkin

MDEV-19275 SQL service for plugins.

review fixes.
parent e1f9a809
......@@ -322,6 +322,14 @@ typedef struct st_mysql_res {
} MYSQL_RES;
/*
We should not define MYSQL_CLIENT when the mysql.h is included
by the server or server plugins.
Now it is important only for the SQL service to work so we rely on
the MYSQL_SERVICE_SQL to check we're compiling the server/plugin
related file.
*/
#if !defined(MYSQL_SERVICE_SQL) && !defined(MYSQL_CLIENT)
#define MYSQL_CLIENT
#endif
......
......@@ -46,6 +46,25 @@ set global test_sql_service_execute_sql_global= 'drop table test.t1';
show status like 'test_sql_query_result';
Variable_name Value
Test_sql_query_result Error 1051 returned. Unknown table 'test.t1'
create table t1 (id int, time timestamp);
insert into t1 values (1, NULL), (2, NULL), (3, NULL), (4, NULL), (5, NULL);
set global test_sql_service_execute_sql_global= 'select * FROM test.t1 WHERE time < DATE_SUB(NOW(), interval 5 minute)';
show status like 'test_sql_query_result';
Variable_name Value
Test_sql_query_result Query returned 0 rows.
set global test_sql_service_execute_sql_global= 'select * FROM test.t1 WHERE time <= NOW()';
show status like 'test_sql_query_result';
Variable_name Value
Test_sql_query_result Query returned 5 rows.
set global test_sql_service_execute_sql_local= 'select * FROM test.t1 WHERE time < DATE_SUB(NOW(), interval 5 minute)';
show status like 'test_sql_query_result';
Variable_name Value
Test_sql_query_result Query returned 0 rows.
set global test_sql_service_execute_sql_local= 'select * FROM test.t1 WHERE time <= NOW()';
show status like 'test_sql_query_result';
Variable_name Value
Test_sql_query_result Query returned 5 rows.
drop table t1;
uninstall plugin test_sql_service;
Warnings:
Warning 1620 Plugin is busy and will be uninstalled on shutdown
......@@ -44,5 +44,17 @@ show status like 'test_sql_query_result';
set global test_sql_service_execute_sql_global= 'drop table test.t1';
show status like 'test_sql_query_result';
create table t1 (id int, time timestamp);
insert into t1 values (1, NULL), (2, NULL), (3, NULL), (4, NULL), (5, NULL);
set global test_sql_service_execute_sql_global= 'select * FROM test.t1 WHERE time < DATE_SUB(NOW(), interval 5 minute)';
show status like 'test_sql_query_result';
set global test_sql_service_execute_sql_global= 'select * FROM test.t1 WHERE time <= NOW()';
show status like 'test_sql_query_result';
set global test_sql_service_execute_sql_local= 'select * FROM test.t1 WHERE time < DATE_SUB(NOW(), interval 5 minute)';
show status like 'test_sql_query_result';
set global test_sql_service_execute_sql_local= 'select * FROM test.t1 WHERE time <= NOW()';
show status like 'test_sql_query_result';
drop table t1;
uninstall plugin test_sql_service;
......@@ -212,8 +212,9 @@ static int run_sql_global(MYSQL_THD thd, struct st_mysql_sys_var *var, void *sav
static int init_done= 0;
static int test_sql_service_plugin_init(void *p __attribute__((unused)))
static int test_sql_service_plugin_init(void *p)
{
(void) p;
global_mysql= mysql_init(NULL);
if (!global_mysql ||
......@@ -228,8 +229,9 @@ static int test_sql_service_plugin_init(void *p __attribute__((unused)))
}
static int test_sql_service_plugin_deinit(void *p __attribute__((unused)))
static int test_sql_service_plugin_deinit(void *p)
{
(void) p;
if (!init_done)
return 0;
......
......@@ -4860,8 +4860,6 @@ Prepared_statement::execute_server_runnable(Server_runnable *server_runnable)
Statement stmt_backup;
bool error;
Query_arena *save_stmt_arena= thd->stmt_arena;
my_time_t save_query_start= thd->query_start();
ulong save_query_sec= thd->start_time_sec_part;
Item_change_list save_change_list;
thd->Item_change_list::move_elements_to(&save_change_list);
......@@ -4871,7 +4869,6 @@ Prepared_statement::execute_server_runnable(Server_runnable *server_runnable)
if (!(lex= new (mem_root) st_lex_local))
return TRUE;
thd->set_time();
thd->set_n_backup_statement(this, &stmt_backup);
thd->set_n_backup_active_arena(this, &stmt_backup);
thd->stmt_arena= this;
......@@ -4885,7 +4882,6 @@ Prepared_statement::execute_server_runnable(Server_runnable *server_runnable)
thd->stmt_arena= save_stmt_arena;
save_change_list.move_elements_to(thd);
thd->force_set_time(save_query_start, save_query_sec);
/* Items and memory will freed in destructor */
......@@ -5613,6 +5609,7 @@ class Protocol_local : public Protocol_text
MYSQL_FIELD *next_mysql_field;
MEM_ROOT *alloc;
THD *new_thd;
Security_context empty_ctx;
Protocol_local(THD *thd_arg, THD *new_thd_arg, ulong prealloc) :
Protocol_text(thd_arg, prealloc),
......@@ -6225,11 +6222,14 @@ loc_advanced_command(MYSQL *mysql, enum enum_server_command command,
else
{
Ed_connection con(p->thd);
Security_context *ctx_orig= p->thd->security_ctx;
MYSQL_LEX_STRING sql_text;
DBUG_ASSERT(current_thd == p->thd);
sql_text.str= (char *) arg;
sql_text.length= arg_length;
p->thd->security_ctx= &p->empty_ctx;
result= con.execute_direct(p, sql_text);
p->thd->security_ctx= ctx_orig;
}
if (skip_check)
result= 0;
......@@ -6397,6 +6397,11 @@ extern "C" MYSQL *mysql_real_connect_local(MYSQL *mysql)
p= new Protocol_local(thd_orig, new_thd, 0);
if (new_thd)
new_thd->protocol= p;
else
{
p->empty_ctx.init();
p->empty_ctx.skip_grants();
}
mysql->thd= p;
mysql->server_status= SERVER_STATUS_AUTOCOMMIT;
......
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