このエントリは MySQL Advent Calendar 2022 の17日目の記事です。
TL;DR
昔誰かが「information_schemaプラグインを書きたくなるのはMySQLユーザーの麻疹(はしか)みたいなもん」と言っていて、なるほど誰でも確かに一度は通る道(MySQLのプラグインの中では一番書くのが楽)かなあと思って俺も嗜んだりする。
基本的には↓を写経するだけで骨格は掴める。というかこれだけでやってきた(というほどは書いてないけど)
MySQL :: Extending MySQL 8.0 :: 4.4.6 Writing INFORMATION_SCHEMA Plugins
久々にちょっと(MySQL 8.0になって初めて) 試してみたのが簡単に mysqld自身のPIDを返すだけ
のもの。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MYSQL_ADD_PLUGIN(i_s_pid | |
i_s_pid.cc | |
MODULE_ONLY | |
MODULE_OUTPUT_NAME "i_s_pid" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <unistd.h> | |
#include <mysql/plugin.h> | |
#include <sql/table.h> | |
#include <sql/sql_show.h> | |
static struct st_mysql_information_schema info= | |
{ | |
MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION | |
}; | |
static ST_FIELD_INFO myfields[]= | |
{ | |
{"pid", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, MY_I_S_UNSIGNED, 0, 0}, | |
{0, 0, MYSQL_TYPE_NULL, 0, 0, 0, 0} | |
}; | |
static int fill (THD *thd, TABLE_LIST *tables, Item *cond) | |
{ | |
TABLE *table= tables->table; | |
table->field[0]->store((uint) getpid()); | |
//table->file->ha_write_row(table->record[0]); | |
if (schema_table_store_record(thd, table)) | |
return 1; | |
return 0; | |
} | |
static int init (void *ptr) | |
{ | |
ST_SCHEMA_TABLE *schema_table= (ST_SCHEMA_TABLE*) ptr; | |
schema_table->fields_info= myfields; | |
schema_table->fill_table= fill; | |
return 0; | |
} | |
mysql_declare_plugin (i_s_pid) | |
{ | |
MYSQL_INFORMATION_SCHEMA_PLUGIN, | |
&info, | |
"i_s_pid", | |
"yoku0825", | |
"information_schema plugin example", | |
PLUGIN_LICENSE_GPL, | |
init, | |
NULL, | |
NULL, | |
0x0001, | |
NULL, | |
NULL, | |
NULL, | |
0 | |
} | |
mysql_declare_plugin_end; |
しかしよく考えたら今まで写経しかしてこなかったので、イマイチST_FIELD_INFOのことをよくわかっていない。
という訳でググったら https://dev.mysql.com/doc/dev/mysql-server/latest/structST__FIELD__INFO.html が出てきた。
せじまさんが褒めちぎっていた理由がやっとわかった気がする。これは便利。
0 件のコメント :
コメントを投稿