1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-07 00:04:31 +03:00

MDEV-19275 Provide SQL service to plugins.

SQL service added.
It provides the limited set of client library functions
to be used by plugin.
This commit is contained in:
Alexey Botchkov
2021-09-06 22:34:35 +04:00
committed by Oleksandr Byelkin
parent 401ff6994d
commit 0a0dfd63d9
25 changed files with 701 additions and 253 deletions

View File

@@ -14,71 +14,113 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
#define PLUGIN_VERSION 0x100
#define PLUGIN_STR_VERSION "1.0.0"
#define _my_thread_var loc_thread_var
#define PLUGIN_VERSION 0x20000
#define PLUGIN_STR_VERSION "2.0"
#include <my_config.h>
#include <assert.h>
#include <my_global.h>
#include <my_base.h>
#include <typelib.h>
//#include <mysql_com.h> /* for enum enum_server_command */
#include <mysql/plugin.h>
#include <mysql/plugin_audit.h>
//#include <string.h>
#include <mysql.h>
LEX_STRING * thd_query_string (MYSQL_THD thd);
unsigned long long thd_query_id(const MYSQL_THD thd);
size_t thd_query_safe(MYSQL_THD thd, char *buf, size_t buflen);
const char *thd_user_name(MYSQL_THD thd);
const char *thd_client_host(MYSQL_THD thd);
const char *thd_client_ip(MYSQL_THD thd);
LEX_CSTRING *thd_current_db(MYSQL_THD thd);
int thd_current_status(MYSQL_THD thd);
enum enum_server_command thd_current_command(MYSQL_THD thd);
int maria_compare_hostname(const char *wild_host, long wild_ip, long ip_mask,
const char *host, const char *ip);
void maria_update_hostname(const char **wild_host, long *wild_ip, long *ip_mask,
const char *host);
/* Status variables for SHOW STATUS */
static long test_passed= 0;
static char *sql_text_local, *sql_text_global;
static char qwe_res[1024]= "";
static struct st_mysql_show_var test_sql_status[]=
{
{"test_sql_service_passed", (char *)&test_passed, SHOW_LONG},
{"test_sql_query_result", qwe_res, SHOW_CHAR},
{0,0,0}
};
static my_bool do_test= TRUE;
static void run_test(MYSQL_THD thd, struct st_mysql_sys_var *var,
void *var_ptr, const void *save);
static MYSQL_SYSVAR_BOOL(run_test, do_test, PLUGIN_VAR_OPCMDARG,
"Perform the test now.", NULL, run_test, FALSE);
static int run_test(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
struct st_mysql_value *value);
static int run_sql_local(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
struct st_mysql_value *value);
static int run_sql_global(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
struct st_mysql_value *value);
static void noop_update(MYSQL_THD thd, struct st_mysql_sys_var *var,
void *var_ptr, const void *save);
static MYSQL_SYSVAR_BOOL(run_test, do_test,
PLUGIN_VAR_OPCMDARG,
"Perform the test now.",
run_test, NULL, FALSE);
static MYSQL_SYSVAR_STR(execute_sql_local, sql_text_local,
PLUGIN_VAR_OPCMDARG,
"Create the new local connection, execute SQL statement with it.",
run_sql_local, noop_update, FALSE);
static MYSQL_SYSVAR_STR(execute_sql_global, sql_text_global,
PLUGIN_VAR_OPCMDARG,
"Execute SQL statement using the global connection.",
run_sql_global, noop_update, FALSE);
static struct st_mysql_sys_var* test_sql_vars[]=
{
MYSQL_SYSVAR(run_test),
MYSQL_SYSVAR(execute_sql_local),
MYSQL_SYSVAR(execute_sql_global),
NULL
};
static MYSQL *global_mysql;
extern int execute_sql_command(const char *command,
char *hosts, char *names, char *filters);
static int run_queries(MYSQL *mysql)
{
MYSQL_RES *res;
if (mysql_real_query(mysql,
STRING_WITH_LEN("CREATE TABLE test.ts_table"
" ( hash varbinary(512),"
" time timestamp default current_time,"
" primary key (hash), index tm (time) )")))
return 1;
if (mysql_real_query(mysql,
STRING_WITH_LEN("INSERT INTO test.ts_table VALUES('1234567890', NULL)")))
return 1;
if (mysql_real_query(mysql, STRING_WITH_LEN("select * from test.ts_table")))
return 1;
if (!(res= mysql_store_result(mysql)))
return 1;
mysql_free_result(res);
if (mysql_real_query(mysql, STRING_WITH_LEN("DROP TABLE test.ts_table")))
return 1;
return 0;
}
static int do_tests()
{
char plugins[1024];
char names[1024];
char dl[2048];
int result;
MYSQL *mysql;
int result= 1;
result= execute_sql_command("select 'plugin', name, dl from mysql.plugin",
plugins, names, dl);
mysql= mysql_init(NULL);
if (mysql_real_connect_local(mysql, NULL, NULL, NULL, 0) == NULL)
return 1;
if (run_queries(mysql))
goto exit;
if (run_queries(global_mysql))
goto exit;
result= 0;
exit:
mysql_close(mysql);
return result;
}
@@ -89,12 +131,87 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
}
static void run_test(MYSQL_THD thd __attribute__((unused)),
struct st_mysql_sys_var *var __attribute__((unused)),
void *var_ptr __attribute__((unused)),
const void *save __attribute__((unused)))
static int run_test(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
struct st_mysql_value *value)
{
test_passed= do_tests();
return (test_passed= (do_tests() == 0)) == 0;
}
static int run_sql(MYSQL *mysql, void *save, struct st_mysql_value *value)
{
const char *str;
int len= 0;
MYSQL_RES *res;
str= value->val_str(value, NULL, &len);
if (mysql_real_query(mysql, str, len))
{
if (mysql_error(mysql)[0])
{
my_snprintf(qwe_res, sizeof(qwe_res), "Error %d returned. %s",
mysql_errno(mysql), mysql_error(mysql));
return 0;
}
return 1;
}
if ((res= mysql_store_result(mysql)))
{
my_snprintf(qwe_res, sizeof(qwe_res), "Query returned %lld rows.",
mysql_num_rows(res));
mysql_free_result(res);
}
else
{
if (mysql_error(mysql)[0])
{
my_snprintf(qwe_res, sizeof(qwe_res), "Error %d returned. %s",
mysql_errno(mysql), mysql_error(mysql));
}
else
my_snprintf(qwe_res, sizeof(qwe_res), "Query affected %lld rows.",
mysql_affected_rows(mysql));
}
return 0;
}
static void noop_update(MYSQL_THD thd, struct st_mysql_sys_var *var,
void *var_ptr, const void *save)
{
sql_text_local= sql_text_global= qwe_res;
}
static int run_sql_local(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
struct st_mysql_value *value)
{
MYSQL *mysql;
int result= 1;
mysql= mysql_init(NULL);
if (mysql_real_connect_local(mysql, NULL, NULL, NULL, 0) == NULL)
return 1;
if (run_sql(mysql, save, value))
goto exit;
result= 0;
exit:
mysql_close(mysql);
return result;
}
static int run_sql_global(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
struct st_mysql_value *value)
{
return run_sql(global_mysql, save, value);
}
@@ -102,7 +219,16 @@ static int init_done= 0;
static int test_sql_service_plugin_init(void *p __attribute__((unused)))
{
global_mysql= mysql_init(NULL);
if (!global_mysql ||
mysql_real_connect_local(global_mysql, NULL, NULL, NULL, 0) == NULL)
return 1;
init_done= 1;
test_passed= (do_tests() == 0);
return 0;
}
@@ -112,6 +238,8 @@ static int test_sql_service_plugin_deinit(void *p __attribute__((unused)))
if (!init_done)
return 0;
mysql_close(global_mysql);
return 0;
}