MySQL Pluginの作成

bool

MySQL8.0以降

MySQLでは、自分でプラグインを作成して、機能拡張する事が可能です。

詳細は

  • ここでは、Integer TimeStampを作成しています。
  • MySQL のヘッダファイルをincludeしてコードを作成
  • コンパイルして作成された、*.soをpluginディレクトリーにコピー
  • 作成したモジュールを指定してcreate functionで関数を作成
[root@ip-192-168-2-30 src]# vim inttime.c 
[root@ip-192-168-2-30 src]$ cat inttime.c 
#include <mysql.h>
#include <sys/time.h>

bool inttime_init(UDF_INIT *initid,UDF_ARGS *args, char *message) {
  return 0;
}

void inttime_deinit(UDF_INIT *initid) {};

unsigned long int inttime(UDF_INIT *initid, UDF_ARGS *args, char *result,
                          unsigned long  *length, char *is_null, char *error)
{
  struct timeval tv;
  gettimeofday(&tv,(void *)0);
  return ((double)tv.tv_usec)+tv.tv_sec*1000000;   /*** 時刻取得 - マイクロ秒(tv_usec), 秒(tv_sec)***/
}

[root@ip-192-168-2-30 src]# gcc -I/usr/local/mysql/include/ -fPIC -shared -o inttime.so inttime.c

[root@ip-192-168-2-30 src]# ls -l
total 12
-rw-r--r-- 1 root root  489 Nov 18 12:40 inttime.c
-rwxr-xr-x 1 root root 6688 Nov 18 12:40 inttime.so

[root@ip-192-168-2-30 src]# cp inttime.so /usr/local/mysql/lib/plugin/
  • 上記で登録したプラグインを利用してFunctionを作成
root@localhost [mysql]> create function inttime RETURNS REAL SONAME 'inttime.so';
Query OK, 0 rows affected (0.03 sec)


root@localhost [mysql]> select inttime();select inttime();
+------------------+
| inttime()        |
+------------------+
| 1637239424114029 |
+------------------+
1 row in set (0.00 sec)

+------------------+
| inttime()        |
+------------------+
| 1637239424114734 |
+------------------+
1 row in set (0.00 sec)

root@localhost [mysql]> 


root@localhost [mysql]> select now(),sysdate(),
    -> inttime(),ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
    -> UNIX_TIMESTAMP(now())\G
*************************** 1. row ***************************
                                   now(): 2021-11-20 22:57:56
                               sysdate(): 2021-11-20 22:57:56
                               inttime(): 1637449076602391
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000): 1637449076602
                   UNIX_TIMESTAMP(now()): 1637449076
1 row in set (0.00 sec)

MySQL5.7迄

my_boolを指定してコードを作成して下さい。これまで、my_boolを指定して作成していたコードは8.0ではエラーになるので留意して下さい。

#include <mysql.h>
#include <sys/time.h>

my_bool inttime_init(UDF_INIT *initid,UDF_ARGS *args, char *message) {
  return 0;
}

void inttime_deinit(UDF_INIT *initid) {};

unsigned long int inttime(UDF_INIT *initid, UDF_ARGS *args, char *result,
                          unsigned long  *length, char *is_null, char *error)
{
  struct timeval tv;
  gettimeofday(&tv,(void *)0);
  return ((double)tv.tv_usec)+tv.tv_sec*1000000;
}

Compilation Notes

  • Incompatible Change: The my_bool type is no longer used in MySQL source code. Any third-party code that used this type to represent C boolean variables should use the bool or int C type instead.NoteThe change from my_bool to bool means that the mysql.h header file now requires a C++ or C99 compiler to compile.

Changes in MySQL 8.0.1 (2017-04-10, Development Milestone)

カテゴリー:

最近のコメント

表示できるコメントはありません。