/* Copyright (C) 2008 Roland Bouman
 * http://rpbouman.blogspot.com/
 * roland.bouman@gmail.com
 
    This file is part of the MYSQL_HELLO 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 build on GNU/Linux as follows:
    
    g++ -Wall -shared -DMYSQL_DYNAMIC_PLUGIN -I/home/roland/mysql-5.1.22-rc/include -I/home/roland/mysql-5.1.22-rc/regex -I/home/roland/mysql-5.1.22-rc/sql -o mysql_is_hello.so mysql_is_hello.cc
    
    where: 
    - the current working directory is the directory of this source file
    - /home/roland/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_hello.so
    Note that currently, plugins are not suppored on Microsoft Windows.

    In order to install the plugin, copy mysql_is_hello.so to the plugin directory.
    Use the following method to discover 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_hello.so to the plugin directory, 
    you can install the plugin using the INSTALL PLUGIN syntax:
    
    mysql> install plugin MYSQL_HELLO soname 'mysql_is_hello.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_HELLO'\G
        *************************** 1. row ***************************
                   PLUGIN_NAME: MYSQL_HELLO
                PLUGIN_VERSION: 0.16
                 PLUGIN_STATUS: ACTIVE
                   PLUGIN_TYPE: INFORMATION SCHEMA
           PLUGIN_TYPE_VERSION: 50122.0
                PLUGIN_LIBRARY: mysql_is_hello.so
        PLUGIN_LIBRARY_VERSION: 1.0
                 PLUGIN_AUTHOR: Roland Bouman (http://rpbouman.blogspot.com/)
            PLUGIN_DESCRIPTION: Says hello.
                PLUGIN_LICENSE: GPL
        1 row in set (0.01 sec)
    
    Note: due to a missing feature, the plugin is marked as GPL.
    (http://bugs.mysql.com/bug.php?id=34209) In reality this software is LGPL.
    
    To use the plugin, do this:
    
    mysql> select * from information_schema.MYSQL_HELLO;
    +--------------------------------------+
    | HELLO                                |
    +--------------------------------------+
    | plugin: hello, information_schema!!! |
    +--------------------------------------+
    
    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_HELLO;
    Query OK, 0 rows affected (0.00 sec)

**/
#include <mysql_priv.h>
#include <stdlib.h>
#include <ctype.h>
#include <mysql_version.h>
#include <mysql/plugin.h>
#include <my_global.h>
#include <my_dir.h>

static ST_FIELD_INFO mysql_is_hello_field_info[]=
{
  {"HELLO", 64, MYSQL_TYPE_VARCHAR, 0, 0, "Hello", SKIP_OPEN_TABLE},				
  {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, 0}
};

int schema_table_store_record(THD *thd, TABLE *table);

int mysql_is_hello_fill_table(
  THD *thd
, TABLE_LIST *tables
, COND *cond
)
{
  int status;                               /* 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 */
  const char *str = "plugin: hello, information_schema!!!";  
  table->field[0]->store(
    str
  , strlen(str)
  , scs
  );
  status = schema_table_store_record(             /* finally, we can store the current row */
    thd
  , table
  );  
  return status;
}

static int mysql_is_hello_plugin_init(void *p)
{
  ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;

  schema->fields_info= mysql_is_hello_field_info;
  schema->fill_table= mysql_is_hello_fill_table;

  return 0;
}


static int mysql_is_hello_plugin_deinit(void *p)
{
	return 0;
}

struct st_mysql_information_schema mysql_is_hello_plugin=
{ MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };

mysql_declare_plugin(mysql_is_hello)
{
  MYSQL_INFORMATION_SCHEMA_PLUGIN,                 /* type constant    */
  &mysql_is_hello_plugin,                          /* type descriptor  */
  "MYSQL_HELLO",                                   /* Name             */
  "Roland Bouman (http://rpbouman.blogspot.com/)", /* Author           */
  "Says hello.",                                   /* Description      */
  PLUGIN_LICENSE_GPL,                              /* License          */
  mysql_is_hello_plugin_init,                      /* Init function    */
  mysql_is_hello_plugin_deinit,                    /* Deinit function  */
  0x0010,                                          /* Version (1.0)    */
  NULL,                                            /* status variables */
  NULL,                                            /* system variables */
  NULL                                             /* config options   */
}
mysql_declare_plugin_end;

