/* Copyright (C) 2008 Roland Bouman * http://rpbouman.blogspot.com/ * roland.bouman@gmail.com This file is part of the MYSQL_SAVEPOINTS information_schema plugin. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** BUILD and (UN)INSTALLATION ========================== This MySQL information_schema plugin can be built on GNU/Linux as follows: g++ -Wall -shared -DMYSQL_DYNAMIC_PLUGIN -I/home/user/mysql-5.1.22-rc/include -I/home/user/mysql-5.1.22-rc/regex -I/home/user/mysql-5.1.22-rc/sql -o mysql_is_savepoints.so mysql_is_savepoints.cc where: - the current working directory is the directory of this source file - /home/user/mysql-5.1.22-rc is the directory with an unpacked mysql-5.1.22-rc source distribution The output will be a shared library: mysql_is_savepoints.so Note that currently, plugins are not suppored on Microsoft Windows. In order to install the plugin, copy mysql_is_savepoints.so to the plugin directory. Use the following method to discover the location of the plugin directory: mysql> SHOW VARIABLES LIKE 'plugin_dir'; +---------------+-----------------------------------------+ | Variable_name | Value | +---------------+-----------------------------------------+ | plugin_dir | /home/roland/mysql-5.1.22-dev/lib/mysql | +---------------+-----------------------------------------+ 1 row in set (0.00 sec) After copying mysql_is_savepoints.so to the plugin directory, you can install the plugin using the INSTALL PLUGIN syntax: mysql> INSTALL PLUGIN MYSQL_SAVEPOINTS SONAME 'mysql_is_savepoints.so'; Query OK, 0 rows affected (0.00 sec) Note: you need to have the privilege to insert into the mysql.plugin table in order to be able to install a plugin like this. Note: there is currently a bug (http://bugs.mysql.com/bug.php?id=33731) that requires you to use a *case sensitive name* when installing and de-installing plugins. For queries, information_schema plugin identifiers are case-insensitive as usual. You can verify the installation like this: mysql> SELECT * FROM information_schema.PLUGINS -> WHERE PLUGIN_NAME = 'MYSQL_SAVEPOINTS'\G *************************** 1. row *************************** PLUGIN_NAME: MYSQL_SAVEPOINTS PLUGIN_VERSION: 0.16 PLUGIN_STATUS: ACTIVE PLUGIN_TYPE: INFORMATION SCHEMA PLUGIN_TYPE_VERSION: 50122.0 PLUGIN_LIBRARY: mysql_is_savepoints.so PLUGIN_LIBRARY_VERSION: 1.0 PLUGIN_AUTHOR: Roland Bouman (http://rpbouman.blogspot.com/) PLUGIN_DESCRIPTION: Show the savepoints for the current transaction PLUGIN_LICENSE: GPL 1 row in set (0.01 sec) To use the plugin, do this: mysql> SET autocommit = OFF; Query OK, 0 rows affected (0.00 sec) mysql> SAVEPOINT A; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM information_schema.MYSQL_SAVEPOINTS; +--------------+----------------+ | SAVEPOINT_ID | SAVEPOINT_NAME | +--------------+----------------+ | 1 | A | +--------------+----------------+ 1 row in set (0.00 sec) mysql> SAVEPOINT B; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM information_schema.MYSQL_SAVEPOINTS; +--------------+----------------+ | SAVEPOINT_ID | SAVEPOINT_NAME | +--------------+----------------+ | 1 | B | | 2 | A | +--------------+----------------+ 2 rows in set (0.00 sec) mysql> ROLLBACK TO SAVEPOINT A; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM information_schema.MYSQL_SAVEPOINTS; +--------------+----------------+ | SAVEPOINT_ID | SAVEPOINT_NAME | +--------------+----------------+ | 1 | A | +--------------+----------------+ 1 row in set (0.00 sec) To uninstall the plugin, use the UNINSTALL PLUGIN syntax. Again, please use the exact same case as when you installed the plugin. mysql> UNINSTALL PLUGIN MYSQL_SAVEPOINTS; Query OK, 0 rows affected (0.00 sec) */ #ifndef MYSQL_SERVER #define MYSQL_SERVER #endif #include #include #include #include #include #include #include #define COLUMN_SAVEPOINT_ID 0 #define COLUMN_SAVEPOINT_NAME 1 static ST_FIELD_INFO mysql_is_savepoints_field_info[]= { {"SAVEPOINT_ID", 0, MYSQL_TYPE_LONGLONG, 0, 0, "Savepoint Id"}, {"SAVEPOINT_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Savepoint Name"}, {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, 0} }; //int schema_table_store_record(THD *thd, TABLE *table); int mysql_is_savepoints_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) { int status = 0; /* return value for this func, 0=success, 1=error*/ CHARSET_INFO *scs= system_charset_info; /* need this to store field into table */ TABLE *table= (TABLE *)tables->table; /* handle to the I_S table. class declared in table.h */ uint savepoint_id = 0; SAVEPOINT *sv= thd->transaction.savepoints; while(sv && !status) { /* store the savepoint sequence into the table column */ table->field[COLUMN_SAVEPOINT_ID]->store(++savepoint_id, 0); /* store the savepoint name into the table column */ table->field[COLUMN_SAVEPOINT_NAME]->store(sv->name, strlen(sv->name), scs); status= schema_table_store_record(thd, table); sv= sv->prev; } return status; } static int mysql_is_savepoints_plugin_init(void *p) { ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p; schema->fields_info= mysql_is_savepoints_field_info; schema->fill_table= mysql_is_savepoints_fill_table; return 0; } static int mysql_is_savepoints_plugin_deinit(void *p) { return 0; } struct st_mysql_information_schema mysql_is_savepoints_plugin= { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION }; mysql_declare_plugin(mysql_is_savepoints) { MYSQL_INFORMATION_SCHEMA_PLUGIN, /* type constant */ &mysql_is_savepoints_plugin, /* type descriptor */ "MYSQL_SAVEPOINTS", /* Name */ "Roland Bouman (http://rpbouman.blogspot.com/)", /* Author */ "Lists all savepoints in the current session.", /* Description */ PLUGIN_LICENSE_GPL, /* License */ mysql_is_savepoints_plugin_init, /* Init function */ mysql_is_savepoints_plugin_deinit, /* Deinit function */ 0x0010, /* Version (1.0) */ NULL, /* status variables */ NULL, /* system variables */ NULL /* config options */ } mysql_declare_plugin_end;