Skip to content

Commit 0a0dfd6

Browse files
Alexey Botchkovsanja-byelkin
authored andcommitted
MDEV-19275 Provide SQL service to plugins.
SQL service added. It provides the limited set of client library functions to be used by plugin.
1 parent 401ff69 commit 0a0dfd6

25 files changed

+698
-250
lines changed

include/mysql/plugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ typedef struct st_mysql_xid MYSQL_XID;
7777
#define MYSQL_PLUGIN_INTERFACE_VERSION 0x0104
7878

7979
/* MariaDB plugin interface version */
80-
#define MARIA_PLUGIN_INTERFACE_VERSION 0x010e
80+
#define MARIA_PLUGIN_INTERFACE_VERSION 0x010f
8181

8282
/*
8383
The allowable types of plugins

include/mysql/service_sql.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* Copyright (C) 2021 MariaDB Corporation
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation; version 2 of the License.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
15+
16+
#if defined(MYSQL_SERVER) && !defined MYSQL_SERVICE_SQL
17+
#define MYSQL_SERVICE_SQL
18+
19+
#include <mysql.h>
20+
21+
/**
22+
@file
23+
SQL service
24+
25+
Interface for plugins to execute SQL queries on the local server.
26+
27+
Functions of the service are the 'server-limited' client library:
28+
mysql_init
29+
mysql_real_connect_local
30+
mysql_real_connect
31+
mysql_errno
32+
mysql_error
33+
mysql_real_query
34+
mysql_affected_rows
35+
mysql_num_rows
36+
mysql_store_result
37+
mysql_free_result
38+
mysql_fetch_row
39+
mysql_close
40+
*/
41+
42+
43+
#ifdef __cplusplus
44+
extern "C" {
45+
#endif
46+
47+
extern struct sql_service_st {
48+
MYSQL *(STDCALL *mysql_init)(MYSQL *mysql);
49+
MYSQL *(*mysql_real_connect_local)(MYSQL *mysql,
50+
const char *host, const char *user, const char *db,
51+
unsigned long clientflag);
52+
MYSQL *(STDCALL *mysql_real_connect)(MYSQL *mysql, const char *host,
53+
const char *user, const char *passwd, const char *db, unsigned int port,
54+
const char *unix_socket, unsigned long clientflag);
55+
unsigned int(STDCALL *mysql_errno)(MYSQL *mysql);
56+
const char *(STDCALL *mysql_error)(MYSQL *mysql);
57+
int (STDCALL *mysql_real_query)(MYSQL *mysql, const char *q,
58+
unsigned long length);
59+
my_ulonglong (STDCALL *mysql_affected_rows)(MYSQL *mysql);
60+
my_ulonglong (STDCALL *mysql_num_rows)(MYSQL_RES *res);
61+
MYSQL_RES *(STDCALL *mysql_store_result)(MYSQL *mysql);
62+
void (STDCALL *mysql_free_result)(MYSQL_RES *result);
63+
MYSQL_ROW (STDCALL *mysql_fetch_row)(MYSQL_RES *result);
64+
void (STDCALL *mysql_close)(MYSQL *sock);
65+
} *sql_service;
66+
67+
#ifdef MYSQL_DYNAMIC_PLUGIN
68+
69+
#define mysql_init sql_service->mysql_init
70+
#define mysql_real_connect_local sql_service->mysql_real_connect_local
71+
#define mysql_real_connect sql_service->mysql_real_connect
72+
#define mysql_errno(M) sql_service->mysql_errno(M)
73+
#define mysql_error(M) sql_service->mysql_error(M)
74+
#define mysql_real_query sql_service->mysql_real_query
75+
#define mysql_affected_rows sql_service->mysql_affected_rows
76+
#define mysql_num_rows sql_service->mysql_num_rows
77+
#define mysql_store_result sql_service->mysql_store_result
78+
#define mysql_free_result sql_service->mysql_free_result
79+
#define mysql_fetch_row sql_service->mysql_fetch_row
80+
#define mysql_close sql_service->mysql_close
81+
82+
#else
83+
84+
MYSQL *mysql_real_connect_local(MYSQL *mysql,
85+
const char *host, const char *user, const char *db,
86+
unsigned long clientflag);
87+
88+
/* The rest of the function declarations mest be taken from the mysql.h */
89+
90+
#endif /*MYSQL_DYNAMIC_PLUGIN*/
91+
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif
96+
97+
#endif /*MYSQL_SERVICE_SQL */
98+
99+

include/mysql/services.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern "C" {
4141
#include <mysql/service_thd_wait.h>
4242
#include <mysql/service_json.h>
4343
/*#include <mysql/service_wsrep.h>*/
44+
#include <mysql/service_sql.h>
4445

4546
#ifdef __cplusplus
4647
}

include/service_versions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444
#define VERSION_wsrep 0x0500
4545
#define VERSION_json 0x0100
4646
#define VERSION_thd_mdl 0x0100
47+
#define VERSION_sql_service 0x0100

include/sql_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ typedef struct st_mysql_methods
6161
MYSQL_ROW column, unsigned int field_count);
6262
void (*flush_use_result)(MYSQL *mysql, my_bool flush_all_results);
6363
int (*read_change_user_result)(MYSQL *mysql);
64+
void (*on_close_free)(MYSQL *mysql);
6465
#if !defined(MYSQL_SERVER) || defined(EMBEDDED_LIBRARY)
6566
MYSQL_FIELD * (*list_fields)(MYSQL *mysql);
6667
my_bool (*read_prepare_result)(MYSQL *mysql, MYSQL_STMT *stmt);
6768
int (*stmt_execute)(MYSQL_STMT *stmt);
6869
int (*read_binary_rows)(MYSQL_STMT *stmt);
6970
int (*unbuffered_fetch)(MYSQL *mysql, char **row);
70-
void (*free_embedded_thd)(MYSQL *mysql);
7171
const char *(*read_statistics)(MYSQL *mysql);
7272
my_bool (*next_result)(MYSQL *mysql);
7373
int (*read_rows_from_cursor)(MYSQL_STMT *stmt);

libmysqld/lib_sql.cc

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ C_MODE_START
4343
extern unsigned int mysql_server_last_errno;
4444
extern char mysql_server_last_error[MYSQL_ERRMSG_SIZE];
4545
static my_bool emb_read_query_result(MYSQL *mysql);
46-
static void emb_free_embedded_thd(MYSQL *mysql);
46+
static void free_embedded_thd(MYSQL *mysql);
4747
static bool embedded_print_errors= 0;
4848

4949
extern "C" void unireg_clear(int exit_code)
@@ -121,7 +121,7 @@ emb_advanced_command(MYSQL *mysql, enum enum_server_command command,
121121
thd->killed= NOT_KILLED;
122122
else
123123
{
124-
emb_free_embedded_thd(mysql);
124+
free_embedded_thd(mysql);
125125
thd= 0;
126126
}
127127
}
@@ -430,7 +430,7 @@ int emb_unbuffered_fetch(MYSQL *mysql, char **row)
430430
return 0;
431431
}
432432

433-
static void emb_free_embedded_thd(MYSQL *mysql)
433+
static void free_embedded_thd(MYSQL *mysql)
434434
{
435435
THD *thd= (THD*)mysql->thd;
436436
server_threads.erase(thd);
@@ -453,12 +453,23 @@ static MYSQL_RES * emb_store_result(MYSQL *mysql)
453453
return mysql_store_result(mysql);
454454
}
455455

456-
int emb_read_change_user_result(MYSQL *mysql)
456+
static int emb_read_change_user_result(MYSQL *mysql)
457457
{
458458
mysql->net.read_pos= (uchar*)""; // fake an OK packet
459459
return mysql_errno(mysql) ? (int)packet_error : 1 /* length of the OK packet */;
460460
}
461461

462+
static void emb_on_close_free(MYSQL *mysql)
463+
{
464+
my_free(mysql->info_buffer);
465+
mysql->info_buffer= 0;
466+
if (mysql->thd)
467+
{
468+
free_embedded_thd(mysql);
469+
mysql->thd= 0;
470+
}
471+
}
472+
462473
MYSQL_METHODS embedded_methods=
463474
{
464475
emb_read_query_result,
@@ -468,12 +479,12 @@ MYSQL_METHODS embedded_methods=
468479
emb_fetch_lengths,
469480
emb_flush_use_result,
470481
emb_read_change_user_result,
482+
emb_on_close_free,
471483
emb_list_fields,
472484
emb_read_prepare_result,
473485
emb_stmt_execute,
474486
emb_read_binary_rows,
475487
emb_unbuffered_fetch,
476-
emb_free_embedded_thd,
477488
emb_read_statistics,
478489
emb_read_query_result,
479490
emb_read_rows_from_cursor

libservices/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SET(MYSQLSERVICES_SOURCES
3838
thd_wait_service.c
3939
wsrep_service.c
4040
json_service.c
41+
sql_service.c
4142
)
4243

4344
ADD_CONVENIENCE_LIBRARY(mysqlservices ${MYSQLSERVICES_SOURCES})

libservices/sql_service.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
/* Copyright (c) 2018, Monty Program Ab
3+
4+
This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; version 2 of the License.
7+
8+
This program is distributed in the hope that it will be useful, but
9+
WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*/
17+
18+
#include <service_versions.h>
19+
SERVICE_VERSION sql_service= (void*)VERSION_sql_service;

mysql-test/main/handlersocket.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugin_version 1.0
55
plugin_status ACTIVE
66
plugin_type DAEMON
77
plugin_library handlersocket.so
8-
plugin_library_version 1.14
8+
plugin_library_version 1.15
99
plugin_author higuchi dot akira at dena dot jp
1010
plugin_description Direct access into InnoDB
1111
plugin_license BSD

mysql-test/main/plugin.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PLUGIN_STATUS ACTIVE
1212
PLUGIN_TYPE STORAGE ENGINE
1313
PLUGIN_TYPE_VERSION #
1414
PLUGIN_LIBRARY ha_example.so
15-
PLUGIN_LIBRARY_VERSION 1.14
15+
PLUGIN_LIBRARY_VERSION 1.15
1616
PLUGIN_AUTHOR Brian Aker, MySQL AB
1717
PLUGIN_DESCRIPTION Example storage engine
1818
PLUGIN_LICENSE GPL
@@ -25,7 +25,7 @@ PLUGIN_STATUS ACTIVE
2525
PLUGIN_TYPE DAEMON
2626
PLUGIN_TYPE_VERSION #
2727
PLUGIN_LIBRARY ha_example.so
28-
PLUGIN_LIBRARY_VERSION 1.14
28+
PLUGIN_LIBRARY_VERSION 1.15
2929
PLUGIN_AUTHOR Sergei Golubchik
3030
PLUGIN_DESCRIPTION Unusable Daemon
3131
PLUGIN_LICENSE GPL
@@ -64,7 +64,7 @@ PLUGIN_STATUS DELETED
6464
PLUGIN_TYPE STORAGE ENGINE
6565
PLUGIN_TYPE_VERSION #
6666
PLUGIN_LIBRARY ha_example.so
67-
PLUGIN_LIBRARY_VERSION 1.14
67+
PLUGIN_LIBRARY_VERSION 1.15
6868
PLUGIN_AUTHOR Brian Aker, MySQL AB
6969
PLUGIN_DESCRIPTION Example storage engine
7070
PLUGIN_LICENSE GPL

0 commit comments

Comments
 (0)