SQL Service for Plugins
The SQL service lets a plugin run SQL statements through a subset of the MariaDB C API, on the server that loaded it or over a client connection to another server.
Overview
The SQL service is a plugin service that lets a plugin execute SQL statements on the server that loaded it. The plugin uses the familiar MariaDB C API functions (mysql_init(), mysql_real_query(), mysql_store_result(), and so on). It can open two kinds of connection:
A local connection, opened with
mysql_real_connect_local(). Its statements run inside the server that loaded the plugin, not over a network connection. See Connecting to the Local Server.A regular client connection to any server, with
mysql_real_connect(). See Connecting to Another Server.
A typical use is a plugin that stores its own data or settings in tables. See Plugins That Use the SQL Service.
The service is declared in include/mysql/service_sql.h, which <mysql/plugin.h> includes. In a dynamically loaded plugin, the C API function names are macros that call the server through the service, so the plugin does not link against a client library.
Functions
The service provides these functions. They behave like the C API functions of the same name, except for mysql_real_connect_local(), which only the SQL service provides.
mysql_init()mysql_real_connect_local()mysql_real_connect()mysql_close()mysql_options()mysql_ssl_set()mysql_set_character_set()mysql_select_db()mysql_real_query()mysql_real_escape_string()mysql_errno()mysql_error()mysql_affected_rows()mysql_store_result()mysql_use_result()mysql_free_result()mysql_num_rows()mysql_num_fields()mysql_fetch_row()mysql_fetch_lengths()mysql_fetch_fields()
The set of functions grew over time; the first releases of the service provided only 12 of them. Check include/mysql/service_sql.h in the source of the server version you build the plugin for.
Connecting to the Local Server
mysql_real_connect_local() connects to the server that loaded the plugin. Use it in place of mysql_real_connect(), which opens a regular client connection to a server.
A local connection behaves differently from a client connection:
It has no user or host, and statements run without privilege checks.
It has no default database. Qualify table names with the database name, or call
mysql_select_db()first.If the plugin connects while no connection thread is active (for example, while the server starts and loads plugins), or while the calling thread has tables locked (for example, when
INSTALL PLUGINinitializes the plugin), the local connection gets its own internal thread. Galera replication (wsrep_on) is switched off for that thread.Otherwise, statements run in the thread of the connection that invoked the plugin.
Session variables that the plugin sets through the connection keep their values for later statements on the same connection.
Connecting to Another Server
mysql_real_connect() opens a regular client connection to a server, as it does in the client library. The other functions of the service, such as mysql_real_query() and mysql_store_result(), then work on that connection as they do in a client application. Use mysql_options() and mysql_ssl_set() to configure the connection before connecting.
The server already contains the client library code (it uses it for replication, for example), and the SQL service makes those functions available to plugins. A plugin can therefore act as a client of another server without linking against a client library.
Binary Logging
Statements that a plugin runs through a local connection are not written to the binary log. To have them logged, run SET SQL_LOG_BIN=1 through the connection first. The setting stays in effect for that connection until the plugin changes it.
Example
This function opens a local connection, inserts a row, and reads it back:
The INSERT is not written to the binary log. The server source has a complete test plugin in plugin/test_sql_service/.
Plugins That Use the SQL Service
The password_reuse_check plugin reads and writes its password history table.
The Spider storage engine creates and upgrades its system tables when it is loaded.
MariaDB Enterprise Audit reads its audit filters from the
mysql.server_audit_filtersandmysql.server_audit_userstables.
See Also
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

