WL# 3031

Backport of functionality in private 5.2 tree. 

Added new language to parser, new mysql.servers table and associated code
to be used by the federated storage engine to allow central connection information
per WL entry.
parent 082de8df
......@@ -72,7 +72,8 @@ sqlsources = derror.cc field.cc field_conv.cc strfunc.cc filesort.cc \
event_queue.cc event_db_repository.cc \
rpl_filter.cc sql_partition.cc sql_builtin.cc sql_plugin.cc \
sql_tablespace.cc \
rpl_injector.cc my_user.c partition_info.cc
rpl_injector.cc my_user.c partition_info.cc \
sql_servers.cc
libmysqld_int_a_SOURCES= $(libmysqld_sources) $(libmysqlsources) $(sqlsources)
libmysqld_a_SOURCES=
......
......@@ -114,6 +114,20 @@ REPLACE INTO user VALUES ('127.0.0.1' ,'root','','Y','Y','Y','Y','Y','Y','Y','Y
INSERT INTO user (host,user) VALUES ('localhost','');
INSERT INTO user (host,user) VALUES ('@HOSTNAME@%','');
CREATE TABLE servers (
Server_name char(64) NOT NULL DEFAULT '',
Host char(64) NOT NULL DEFAULT '',
Db char(64) NOT NULL DEFAULT '',
Username char(64) NOT NULL DEFAULT '',
Password char(64) NOT NULL DEFAULT '',
Port INT(4) NOT NULL DEFAULT '0',
Socket char(64) NOT NULL DEFAULT '',
Wrapper char(64) NOT NULL DEFAULT '',
Owner char(64) NOT NULL DEFAULT '',
PRIMARY KEY (Server_name))
comment='MySQL Foreign Servers table';
INSERT INTO servers VALUES ('test','localhost','test','root','', 0,'','mysql','root');
CREATE TABLE func (
name char(64) binary DEFAULT '' NOT NULL,
......
stop slave;
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
reset master;
reset slave;
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
start slave;
stop slave;
DROP DATABASE IF EXISTS federated;
CREATE DATABASE federated;
DROP DATABASE IF EXISTS federated;
CREATE DATABASE federated;
create database first_db;
create database second_db;
use first_db;
DROP TABLE IF EXISTS first_db.t1;
Warnings:
Note 1051 Unknown table 't1'
CREATE TABLE first_db.t1 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
DEFAULT CHARSET=latin1;
use second_db;
DROP TABLE IF EXISTS second_db.t1;
Warnings:
Note 1051 Unknown table 't1'
CREATE TABLE second_db.t1 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
DEFAULT CHARSET=latin1;
drop server if exists 'server_one';
create server 'server_one' foreign data wrapper 'mysql' options
(HOST '127.0.0.1',
DATABASE 'first_db',
USER 'root',
PASSWORD '',
PORT 9308,
SOCKET '',
OWNER 'root');
drop server if exists 'server_two';
create server 'server_two' foreign data wrapper 'mysql' options
(HOST '127.0.0.1',
DATABASE 'second_db',
USER 'root',
PASSWORD '',
PORT 9308,
SOCKET '',
OWNER 'root');
select * from mysql.servers;
Server_name Host Db Username Password Port Socket Wrapper Owner
test localhost test root 0 mysql root
server_one 127.0.0.1 first_db root 9308 mysql root
server_two 127.0.0.1 second_db root 9308 mysql root
DROP TABLE IF EXISTS federated.old;
Warnings:
Note 1051 Unknown table 'old'
CREATE TABLE federated.old (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
ENGINE="FEDERATED" DEFAULT CHARSET=latin1
CONNECTION='mysql://root@127.0.0.1:9308/first_db/t1';
INSERT INTO federated.old (id, name) values (1, 'federated.old url');
SELECT * FROM federated.old;
id name
1 federated.old url
DROP TABLE IF EXISTS federated.old2;
Warnings:
Note 1051 Unknown table 'old2'
CREATE TABLE federated.old2 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
ENGINE="FEDERATED" DEFAULT CHARSET=latin1
CONNECTION='mysql://root@127.0.0.1:9308/second_db/t1';
INSERT INTO federated.old2 (id, name) values (1, 'federated.old2 url');
DROP TABLE IF EXISTS federated.t1;
Warnings:
Note 1051 Unknown table 't1'
CREATE TABLE federated.t1 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
ENGINE="FEDERATED" DEFAULT CHARSET=latin1
CONNECTION='server_one';
INSERT INTO federated.t1 (id, name) values (1, 'server_one, new scheme');
SELECT * FROM federated.t1;
id name
1 federated.old url
1 server_one, new scheme
ALTER SERVER 'server_one' options(DATABASE 'second_db');
flush tables;
INSERT INTO federated.t1 (id, name) values (1, 'server_two, new scheme');
SELECT * FROM federated.t1;
id name
1 federated.old2 url
1 server_two, new scheme
drop table federated.t1;
drop server 'server_one';
drop server 'server_two';
select * from mysql.servers;
Server_name Host Db Username Password Port Socket Wrapper Owner
test localhost test root 0 mysql root
drop table first_db.t1;
drop table second_db.t1;
drop database first_db;
drop database second_db;
DROP TABLE IF EXISTS federated.t1;
DROP DATABASE IF EXISTS federated;
DROP TABLE IF EXISTS federated.t1;
DROP DATABASE IF EXISTS federated;
# should work with embedded server after mysqltest is fixed
-- source include/not_embedded.inc
source include/federated.inc;
connection slave;
create database first_db;
create database second_db;
use first_db;
DROP TABLE IF EXISTS first_db.t1;
CREATE TABLE first_db.t1 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
DEFAULT CHARSET=latin1;
use second_db;
DROP TABLE IF EXISTS second_db.t1;
CREATE TABLE second_db.t1 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
DEFAULT CHARSET=latin1;
connection master;
drop server if exists 'server_one';
eval create server 'server_one' foreign data wrapper 'mysql' options
(HOST '127.0.0.1',
DATABASE 'first_db',
USER 'root',
PASSWORD '',
PORT $SLAVE_MYPORT,
SOCKET '',
OWNER 'root');
drop server if exists 'server_two';
eval create server 'server_two' foreign data wrapper 'mysql' options
(HOST '127.0.0.1',
DATABASE 'second_db',
USER 'root',
PASSWORD '',
PORT $SLAVE_MYPORT,
SOCKET '',
OWNER 'root');
select * from mysql.servers;
DROP TABLE IF EXISTS federated.old;
eval CREATE TABLE federated.old (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
ENGINE="FEDERATED" DEFAULT CHARSET=latin1
CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/first_db/t1';
INSERT INTO federated.old (id, name) values (1, 'federated.old url');
SELECT * FROM federated.old;
DROP TABLE IF EXISTS federated.old2;
eval CREATE TABLE federated.old2 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
ENGINE="FEDERATED" DEFAULT CHARSET=latin1
CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/second_db/t1';
INSERT INTO federated.old2 (id, name) values (1, 'federated.old2 url');
DROP TABLE IF EXISTS federated.t1;
CREATE TABLE federated.t1 (
`id` int(20) NOT NULL,
`name` varchar(64) NOT NULL default ''
)
ENGINE="FEDERATED" DEFAULT CHARSET=latin1
CONNECTION='server_one';
INSERT INTO federated.t1 (id, name) values (1, 'server_one, new scheme');
SELECT * FROM federated.t1;
ALTER SERVER 'server_one' options(DATABASE 'second_db');
flush tables;
INSERT INTO federated.t1 (id, name) values (1, 'server_two, new scheme');
SELECT * FROM federated.t1;
drop table federated.t1;
drop server 'server_one';
drop server 'server_two';
select * from mysql.servers;
connection slave;
drop table first_db.t1;
drop table second_db.t1;
drop database first_db;
drop database second_db;
source include/federated_cleanup.inc;
......@@ -192,6 +192,33 @@ then
fi
fi
# Check for old tables
if test ! -f $mdata/servers.frm
then
if test "$1" = "verbose" ; then
echo "Preparing servers table" 1>&2;
fi
c_d="$c_d
CREATE TABLE servers ("
c_d="$c_d Server_name char(64) NOT NULL,"
c_d="$c_d Hostname char(64) NOT NULL,"
c_d="$c_d Db char(64) NOT NULL,"
c_d="$c_d Username char(64) NOT NULL,"
c_d="$c_d Passwd char(64) NOT NULL,"
c_d="$c_d Portnum INT(4),"
c_d="$c_d Sock char(64),"
c_d="$c_d Scheme char(64) NOT NULL,"
c_d="$c_d Owner char(64) NOT NULL,"
c_d="$c_d PRIMARY KEY (Server_name));"
i_d="INSERT INTO servers VALUES
('test','localhost','test','root','', 0,
'','mysql','root');
"
fi
if test ! -f $mdata/func.frm
then
if test "$1" = "verbose" ; then
......
......@@ -68,7 +68,7 @@ noinst_HEADERS = item.h item_func.h item_sum.h item_cmpfunc.h \
event_db_repository.h event_queue.h \
sql_plugin.h authors.h sql_partition.h event_data_objects.h \
partition_info.h partition_element.h event_scheduler.h \
contributors.h
contributors.h sql_servers.h
mysqld_SOURCES = sql_lex.cc sql_handler.cc sql_partition.cc \
item.cc item_sum.cc item_buff.cc item_func.cc \
item_cmpfunc.cc item_strfunc.cc item_timefunc.cc \
......@@ -106,7 +106,8 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc sql_partition.cc \
event_scheduler.cc event_data_objects.cc \
event_queue.cc event_db_repository.cc events.cc \
sql_plugin.cc sql_binlog.cc \
sql_builtin.cc sql_tablespace.cc partition_info.cc
sql_builtin.cc sql_tablespace.cc partition_info.cc \
sql_servers.cc
gen_lex_hash_SOURCES = gen_lex_hash.cc
......
......@@ -229,6 +229,7 @@ static SYMBOL symbols[] = {
{ "HAVING", SYM(HAVING)},
{ "HELP", SYM(HELP_SYM)},
{ "HIGH_PRIORITY", SYM(HIGH_PRIORITY)},
{ "HOST", SYM(HOST_SYM)},
{ "HOSTS", SYM(HOSTS_SYM)},
{ "HOUR", SYM(HOUR_SYM)},
{ "HOUR_MICROSECOND", SYM(HOUR_MICROSECOND_SYM)},
......@@ -368,6 +369,7 @@ static SYMBOL symbols[] = {
{ "ONE_SHOT", SYM(ONE_SHOT_SYM)},
{ "OPEN", SYM(OPEN_SYM)},
{ "OPTIMIZE", SYM(OPTIMIZE)},
{ "OPTIONS", SYM(OPTIONS_SYM)},
{ "OPTION", SYM(OPTION)},
{ "OPTIONALLY", SYM(OPTIONALLY)},
{ "OR", SYM(OR_SYM)},
......@@ -375,6 +377,7 @@ static SYMBOL symbols[] = {
{ "OUT", SYM(OUT_SYM)},
{ "OUTER", SYM(OUTER)},
{ "OUTFILE", SYM(OUTFILE)},
{ "OWNER", SYM(OWNER_SYM)},
{ "PACK_KEYS", SYM(PACK_KEYS_SYM)},
{ "PARSER", SYM(PARSER_SYM)},
{ "PARTIAL", SYM(PARTIAL)},
......@@ -387,6 +390,7 @@ static SYMBOL symbols[] = {
{ "PLUGINS", SYM(PLUGINS_SYM)},
{ "POINT", SYM(POINT_SYM)},
{ "POLYGON", SYM(POLYGON)},
{ "PORT", SYM(PORT_SYM)},
{ "PRECISION", SYM(PRECISION)},
{ "PREPARE", SYM(PREPARE_SYM)},
{ "PRESERVE", SYM(PRESERVE_SYM)},
......@@ -456,6 +460,7 @@ static SYMBOL symbols[] = {
{ "SERIAL", SYM(SERIAL_SYM)},
{ "SERIALIZABLE", SYM(SERIALIZABLE_SYM)},
{ "SESSION", SYM(SESSION_SYM)},
{ "SERVER", SYM(SERVER_SYM)},
{ "SET", SYM(SET)},
{ "SHARE", SYM(SHARE_SYM)},
{ "SHOW", SYM(SHOW)},
......@@ -465,6 +470,7 @@ static SYMBOL symbols[] = {
{ "SLAVE", SYM(SLAVE)},
{ "SNAPSHOT", SYM(SNAPSHOT_SYM)},
{ "SMALLINT", SYM(SMALLINT)},
{ "SOCKET", SYM(SOCKET_SYM)},
{ "SOME", SYM(ANY_SYM)},
{ "SONAME", SYM(SONAME_SYM)},
{ "SOUNDS", SYM(SOUNDS_SYM)},
......@@ -569,6 +575,7 @@ static SYMBOL symbols[] = {
{ "VIEW", SYM(VIEW_SYM)},
{ "WITH", SYM(WITH)},
{ "WORK", SYM(WORK_SYM)},
{ "WRAPPER", SYM(WRAPPER_SYM)},
{ "WRITE", SYM(WRITE_SYM)},
{ "X509", SYM(X509_SYM)},
{ "XOR", SYM(XOR)},
......
......@@ -624,6 +624,7 @@ Item *negate_expression(THD *thd, Item *expr);
#include "sql_acl.h"
#include "tztime.h"
#ifdef MYSQL_SERVER
#include "sql_servers.h"
#include "opt_range.h"
#ifdef HAVE_QUERY_CACHE
......
......@@ -1174,6 +1174,7 @@ void clean_up(bool print_message)
my_tz_free();
my_database_names_free();
#ifndef NO_EMBEDDED_ACCESS_CHECKS
servers_free(1);
acl_free(1);
grant_free();
#endif
......@@ -3638,6 +3639,9 @@ we force server id to 2, but this MySQL server will not act as a slave.");
if (!opt_noacl)
(void) grant_init();
if (!opt_bootstrap)
servers_init(0);
if (!opt_noacl)
{
#ifdef HAVE_DLOPEN
......
......@@ -5607,6 +5607,8 @@ ER_SP_RECURSION_LIMIT
ER_SP_PROC_TABLE_CORRUPT
eng "Failed to load routine %-.64s. The table mysql.proc is missing, corrupt, or contains bad data (internal code %d)"
ger "Routine %-64s konnte nicht geladen werden. Die Tabelle mysql.proc fehlt, ist beschdigt, oder enthlt fehlerhaften Daten (interner Code: %d)"
ER_FOREIGN_SERVER_EXISTS
eng "The foreign server, %s, you are trying to create already exists."
ER_SP_WRONG_NAME 42000
eng "Incorrect routine name '%-.64s'"
ger "Ungltiger Routinenname '%-.64s'"
......@@ -5847,6 +5849,9 @@ ER_BINLOG_ROW_WRONG_TABLE_DEF
ER_BINLOG_ROW_RBR_TO_SBR
eng "Slave running with --log-slave-updates must use row-based binary logging to be able to replicate row-based binary log events"
ger "Slave, die mit --log-slave-updates laufen, mssen zeilenbasiertes Loggen verwenden, um zeilenbasierte Binrlog-Ereignisse loggen zu knnen"
ER_FOREIGN_SERVER_DOESNT_EXIST
eng "The foreign server name you are trying to reference does not exist. Data source error: %-.64s"
ger "Die externe Verbindung, auf die Sie zugreifen wollen, existiert nicht. Datenquellenfehlermeldung: %-.64s"
ER_EVENT_ALREADY_EXISTS
eng "Event '%-.64s' already exists"
ger "Event '%-.64s' existiert bereits"
......
......@@ -184,6 +184,21 @@ void lex_start(THD *thd, const uchar *buf, uint length)
lex->nest_level=0 ;
lex->allow_sum_func= 0;
lex->in_sum_func= NULL;
/*
ok, there must be a better solution for this, long-term
I tried "bzero" in the sql_yacc.yy code, but that for
some reason made the values zero, even if they were set
*/
lex->server_options.server_name= 0;
lex->server_options.server_name_length= 0;
lex->server_options.host= 0;
lex->server_options.db= 0;
lex->server_options.username= 0;
lex->server_options.password= 0;
lex->server_options.scheme= 0;
lex->server_options.socket= 0;
lex->server_options.owner= 0;
lex->server_options.port= -1;
DBUG_VOID_RETURN;
}
......
......@@ -111,6 +111,7 @@ enum enum_sql_command {
SQLCOM_SHOW_AUTHORS, SQLCOM_BINLOG_BASE64_EVENT,
SQLCOM_SHOW_PLUGINS,
SQLCOM_SHOW_CONTRIBUTORS,
SQLCOM_CREATE_SERVER, SQLCOM_DROP_SERVER, SQLCOM_ALTER_SERVER,
SQLCOM_CREATE_EVENT, SQLCOM_ALTER_EVENT, SQLCOM_DROP_EVENT,
SQLCOM_SHOW_CREATE_EVENT, SQLCOM_SHOW_EVENTS,
......@@ -174,6 +175,14 @@ enum enum_drop_mode
typedef List<Item> List_item;
/* SERVERS CACHE CHANGES */
typedef struct st_lex_server_options
{
long port;
uint server_name_length;
char *server_name, *host, *db, *username, *password, *scheme, *socket, *owner;
} LEX_SERVER_OPTIONS;
typedef struct st_lex_master_info
{
char *host, *user, *password, *log_file_name;
......@@ -979,6 +988,7 @@ typedef struct st_lex : public Query_tables_list
HA_CREATE_INFO create_info;
KEY_CREATE_INFO key_create_info;
LEX_MASTER_INFO mi; // used by CHANGE MASTER
LEX_SERVER_OPTIONS server_options;
USER_RESOURCES mqh;
ulong type;
/*
......
......@@ -5183,6 +5183,58 @@ mysql_execute_command(THD *thd)
#endif /* EMBEDDED_LIBRARY */
break;
}
case SQLCOM_CREATE_SERVER:
{
int error;
LEX *lex= thd->lex;
DBUG_PRINT("info", ("case SQLCOM_CREATE_SERVER"));
if ((error= create_server(thd, &lex->server_options)))
{
DBUG_PRINT("info", ("problem creating server",
lex->server_options.server_name));
my_error(error, MYF(0), lex->server_options.server_name);
break;
}
send_ok(thd, 1);
break;
}
case SQLCOM_ALTER_SERVER:
{
int error;
LEX *lex= thd->lex;
DBUG_PRINT("info", ("case SQLCOM_ALTER_SERVER"));
if ((error= alter_server(thd, &lex->server_options)))
{
DBUG_PRINT("info", ("problem altering server",
lex->server_options.server_name));
my_error(error, MYF(0), lex->server_options.server_name);
break;
}
send_ok(thd, 1);
break;
}
case SQLCOM_DROP_SERVER:
{
int err_code;
LEX *lex= thd->lex;
DBUG_PRINT("info", ("case SQLCOM_DROP_SERVER"));
if ((err_code= drop_server(thd, &lex->server_options)))
{
if (! lex->drop_if_exists && err_code == ER_FOREIGN_SERVER_EXISTS)
{
DBUG_PRINT("info", ("problem dropping server %s",
lex->server_options.server_name));
my_error(err_code, MYF(0), lex->server_options.server_name);
}
else
{
send_ok(thd, 0);
}
break;
}
send_ok(thd, 1);
break;
}
default:
#ifndef EMBEDDED_LIBRARY
DBUG_ASSERT(0); /* Impossible */
......
/* Copyright (C) 2000-2003 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/*
The servers are saved in the system table "servers"
*/
#include "mysql_priv.h"
#include "hash_filo.h"
#include <m_ctype.h>
#include <stdarg.h>
#include "sp_head.h"
#include "sp.h"
HASH servers_cache;
pthread_mutex_t servers_cache_mutex; // To init the hash
uint servers_cache_initialised=FALSE;
/* Version of server table. incremented by servers_load */
static uint servers_version=0;
static MEM_ROOT mem;
static rw_lock_t THR_LOCK_servers;
static bool initialized=0;
static byte *servers_cache_get_key(FOREIGN_SERVER *server, uint *length,
my_bool not_used __attribute__((unused)))
{
DBUG_ENTER("servers_cache_get_key");
DBUG_PRINT("info", ("server_name_length %d server_name %s",
server->server_name_length,
server->server_name));
*length= (uint) server->server_name_length;
DBUG_RETURN((byte*) server->server_name);
}
/*
Initialize structures responsible for servers used in federated
server scheme information for them from the server
table in the 'mysql' database.
SYNOPSIS
servers_init()
dont_read_server_table TRUE if we want to skip loading data from
server table and disable privilege checking.
NOTES
This function is mostly responsible for preparatory steps, main work
on initialization and grants loading is done in servers_reload().
RETURN VALUES
0 ok
1 Could not initialize servers
*/
my_bool servers_init(bool dont_read_servers_table)
{
THD *thd;
my_bool return_val= 0;
DBUG_ENTER("servers_init");
/* init the mutex */
if (pthread_mutex_init(&servers_cache_mutex, MY_MUTEX_INIT_FAST))
DBUG_RETURN(1);
/* initialise our servers cache */
if (hash_init(&servers_cache, system_charset_info, 32, 0, 0,
(hash_get_key) servers_cache_get_key, 0, 0))
{
return_val= 1; /* we failed, out of memory? */
goto end;
}
/* Initialize the mem root for data */
init_alloc_root(&mem, ACL_ALLOC_BLOCK_SIZE, 0);
/*
at this point, the cache is initialised, let it be known
*/
servers_cache_initialised= TRUE;
if (dont_read_servers_table)
goto end;
/*
To be able to run this from boot, we allocate a temporary THD
*/
if (!(thd=new THD))
DBUG_RETURN(1);
thd->thread_stack= (char*) &thd;
thd->store_globals();
/*
It is safe to call servers_reload() since servers_* arrays and hashes which
will be freed there are global static objects and thus are initialized
by zeros at startup.
*/
return_val= servers_reload(thd);
delete thd;
/* Remember that we don't have a THD */
my_pthread_setspecific_ptr(THR_THD, 0);
end:
DBUG_RETURN(return_val);
}
/*
Initialize server structures
SYNOPSIS
servers_load()
thd Current thread
tables List containing open "mysql.servers"
RETURN VALUES
FALSE Success
TRUE Error
*/
static my_bool servers_load(THD *thd, TABLE_LIST *tables)
{
TABLE *table;
READ_RECORD read_record_info;
my_bool return_val= TRUE;
DBUG_ENTER("servers_load");
if (!servers_cache_initialised)
DBUG_RETURN(0);
/* need to figure out how to utilise this variable */
servers_version++; /* servers updated */
/* first, send all cached rows to sleep with the fishes, oblivion!
I expect this crappy comment replaced */
free_root(&mem, MYF(MY_MARK_BLOCKS_FREE));
my_hash_reset(&servers_cache);
init_read_record(&read_record_info,thd,table=tables[0].table,NULL,1,0);
while (!(read_record_info.read_record(&read_record_info)))
{
/* return_val is already TRUE, so no need to set */
if ((get_server_from_table_to_cache(table)))
goto end;
}
return_val=0;
end:
end_read_record(&read_record_info);
DBUG_RETURN(return_val);
}
/*
Forget current servers cache and read new servers
from the conneciton table.
SYNOPSIS
servers_reload()
thd Current thread
NOTE
All tables of calling thread which were open and locked by LOCK TABLES
statement will be unlocked and closed.
This function is also used for initialization of structures responsible
for user/db-level privilege checking.
RETURN VALUE
FALSE Success
TRUE Failure
*/
my_bool servers_reload(THD *thd)
{
TABLE_LIST tables[1];
my_bool return_val= 1;
DBUG_ENTER("servers_reload");
if (thd->locked_tables)
{ // Can't have locked tables here
thd->lock=thd->locked_tables;
thd->locked_tables=0;
close_thread_tables(thd);
}
/*
To avoid deadlocks we should obtain table locks before
obtaining servers_cache->lock mutex.
*/
bzero((char*) tables, sizeof(tables));
tables[0].alias= tables[0].table_name= (char*) "servers";
tables[0].db= (char*) "mysql";
tables[0].lock_type= TL_READ;
if (simple_open_n_lock_tables(thd, tables))
{
sql_print_error("Fatal error: Can't open and lock privilege tables: %s",
thd->net.last_error);
goto end;
}
DBUG_PRINT("info", ("locking servers_cache"));
VOID(pthread_mutex_lock(&servers_cache_mutex));
//old_servers_cache= servers_cache;
//old_mem=mem;
if ((return_val= servers_load(thd, tables)))
{ // Error. Revert to old list
/* blast, for now, we have no servers, discuss later way to preserve */
DBUG_PRINT("error",("Reverting to old privileges"));
servers_free();
}
DBUG_PRINT("info", ("unlocking servers_cache"));
VOID(pthread_mutex_unlock(&servers_cache_mutex));
end:
close_thread_tables(thd);
DBUG_RETURN(return_val);
}
/*
Initialize structures responsible for servers used in federated
server scheme information for them from the server
table in the 'mysql' database.
SYNOPSIS
get_server_from_table_to_cache()
TABLE *table open table pointer
NOTES
This function takes a TABLE pointer (pointing to an opened
table). With this open table, a FOREIGN_SERVER struct pointer
is allocated into root memory, then each member of the FOREIGN_SERVER
struct is populated. A char pointer takes the return value of get_field
for each column we're interested in obtaining, and if that pointer
isn't 0x0, the FOREIGN_SERVER member is set to that value, otherwise,
is set to the value of an empty string, since get_field would set it to
0x0 if the column's value is empty, even if the default value for that
column is NOT NULL.
RETURN VALUES
0 ok
1 could not insert server struct into global servers cache
*/
my_bool get_server_from_table_to_cache(TABLE *table)
{
/* alloc a server struct */
char *ptr;
char *blank= (char*)"";
FOREIGN_SERVER *server= (FOREIGN_SERVER *)alloc_root(&mem,
sizeof(FOREIGN_SERVER));
DBUG_ENTER("get_server_from_table_to_cache");
table->use_all_columns();
/* get each field into the server struct ptr */
server->server_name= get_field(&mem, table->field[0]);
server->server_name_length= strlen(server->server_name);
ptr= get_field(&mem, table->field[1]);
server->host= ptr ? ptr : blank;
ptr= get_field(&mem, table->field[2]);
server->db= ptr ? ptr : blank;
ptr= get_field(&mem, table->field[3]);
server->username= ptr ? ptr : blank;
ptr= get_field(&mem, table->field[4]);
server->password= ptr ? ptr : blank;
ptr= get_field(&mem, table->field[5]);
server->sport= ptr ? ptr : blank;
server->port= server->sport ? atoi(server->sport) : 0;
ptr= get_field(&mem, table->field[6]);
server->socket= ptr && strlen(ptr) ? ptr : NULL;
ptr= get_field(&mem, table->field[7]);
server->scheme= ptr ? ptr : blank;
ptr= get_field(&mem, table->field[8]);
server->owner= ptr ? ptr : blank;
DBUG_PRINT("info", ("server->server_name %s", server->server_name));
DBUG_PRINT("info", ("server->host %s", server->host));
DBUG_PRINT("info", ("server->db %s", server->db));
DBUG_PRINT("info", ("server->username %s", server->username));
DBUG_PRINT("info", ("server->password %s", server->password));
DBUG_PRINT("info", ("server->socket %s", server->socket));
if (my_hash_insert(&servers_cache, (byte*) server))
{
DBUG_PRINT("info", ("had a problem inserting server %s at %lx",
server->server_name, server));
// error handling needed here
DBUG_RETURN(TRUE);
}
DBUG_RETURN(FALSE);
}
/*
SYNOPSIS
server_exists_in_table()
THD *thd - thread pointer
LEX_SERVER_OPTIONS *server_options - pointer to Lex->server_options
NOTES
This function takes a LEX_SERVER_OPTIONS struct, which is very much the
same type of structure as a FOREIGN_SERVER, it contains the values parsed
in any one of the [CREATE|DELETE|DROP] SERVER statements. Using the
member "server_name", index_read_idx either founds the record and returns
1, or doesn't find the record, and returns 0
RETURN VALUES
0 record not found
1 record found
*/
my_bool server_exists_in_table(THD *thd, LEX_SERVER_OPTIONS *server_options)
{
byte server_key[MAX_KEY_LENGTH];
int result= 1;
int error;
TABLE_LIST tables;
TABLE *table;
DBUG_ENTER("server_exists");
bzero((char*) &tables, sizeof(tables));
tables.db= (char*) "mysql";
tables.alias= tables.table_name= (char*) "servers";
table->use_all_columns();
/* need to open before acquiring THR_LOCK_plugin or it will deadlock */
if (! (table= open_ltable(thd, &tables, TL_WRITE)))
DBUG_RETURN(TRUE);
rw_wrlock(&THR_LOCK_servers);
VOID(pthread_mutex_lock(&servers_cache_mutex));
/* set the field that's the PK to the value we're looking for */
table->field[0]->store(server_options->server_name,
server_options->server_name_length,
system_charset_info);
if ((error= table->file->index_read_idx(table->record[0], 0,
(byte *)table->field[0]->ptr,
table->key_info[0].key_length,
HA_READ_KEY_EXACT)))
{
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
{
table->file->print_error(error, MYF(0));
result= -1;
}
result= 0;
DBUG_PRINT("info",("record for server '%s' not found!",
server_options->server_name));
}
VOID(pthread_mutex_unlock(&servers_cache_mutex));
rw_unlock(&THR_LOCK_servers);
DBUG_RETURN(result);
}
/*
SYNOPSIS
insert_server()
THD *thd - thread pointer
FOREIGN_SERVER *server - pointer to prepared FOREIGN_SERVER struct
NOTES
This function takes a server object that is has all members properly
prepared, ready to be inserted both into the mysql.servers table and
the servers cache.
RETURN VALUES
0 - no error
other - error code
*/
int insert_server(THD *thd, FOREIGN_SERVER *server)
{
byte server_key[MAX_KEY_LENGTH];
int error;
TABLE_LIST tables;
TABLE *table;
DBUG_ENTER("insert_server");
bzero((char*) &tables, sizeof(tables));
tables.db= (char*) "mysql";
tables.alias= tables.table_name= (char*) "servers";
/* need to open before acquiring THR_LOCK_plugin or it will deadlock */
if (! (table= open_ltable(thd, &tables, TL_WRITE)))
DBUG_RETURN(TRUE);
/* lock mutex to make sure no changes happen */
VOID(pthread_mutex_lock(&servers_cache_mutex));
/* lock table */
rw_wrlock(&THR_LOCK_servers);
/* insert the server into the table */
if ((error= insert_server_record(table, server)))
goto end;
/* insert the server into the cache */
if ((error= insert_server_record_into_cache(server)))
goto end;
end:
/* unlock the table */
rw_unlock(&THR_LOCK_servers);
VOID(pthread_mutex_unlock(&servers_cache_mutex));
DBUG_RETURN(error);
}
/*
SYNOPSIS
int insert_server_record_into_cache()
FOREIGN_SERVER *server
NOTES
This function takes a FOREIGN_SERVER pointer to an allocated (root mem)
and inserts it into the global servers cache
RETURN VALUE
0 - no error
>0 - error code
*/
int insert_server_record_into_cache(FOREIGN_SERVER *server)
{
int error=0;
DBUG_ENTER("insert_server_record_into_cache");
/*
We succeded in insertion of the server to the table, now insert
the server to the cache
*/
DBUG_PRINT("info", ("inserting server %s at %lx, length %d",
server->server_name, server,
server->server_name_length));
if (my_hash_insert(&servers_cache, (byte*) server))
{
DBUG_PRINT("info", ("had a problem inserting server %s at %lx",
server->server_name, server));
// error handling needed here
error= 1;
}
DBUG_RETURN(error);
}
/*
SYNOPSIS
store_server_fields()
TABLE *table
FOREIGN_SERVER *server
NOTES
This function takes an opened table object, and a pointer to an
allocated FOREIGN_SERVER struct, and then stores each member of
the FOREIGN_SERVER to the appropriate fields in the table, in
advance of insertion into the mysql.servers table
RETURN VALUE
VOID
*/
void store_server_fields(TABLE *table, FOREIGN_SERVER *server)
{
table->use_all_columns();
/*
"server" has already been prepped by prepare_server_struct_for_<>
so, all we need to do is check if the value is set (> -1 for port)
If this happens to be an update, only the server members that
have changed will be set. If an insert, then all will be set,
even if with empty strings
*/
if (server->host)
table->field[1]->store(server->host,
(uint) strlen(server->host), system_charset_info);
if (server->db)
table->field[2]->store(server->db,
(uint) strlen(server->db), system_charset_info);
if (server->username)
table->field[3]->store(server->username,
(uint) strlen(server->username), system_charset_info);
if (server->password)
table->field[4]->store(server->password,
(uint) strlen(server->password), system_charset_info);
if (server->port > -1)
table->field[5]->store(server->port);
if (server->socket)
table->field[6]->store(server->socket,
(uint) strlen(server->socket), system_charset_info);
if (server->scheme)
table->field[7]->store(server->scheme,
(uint) strlen(server->scheme), system_charset_info);
if (server->owner)
table->field[8]->store(server->owner,
(uint) strlen(server->owner), system_charset_info);
}
/*
SYNOPSIS
insert_server_record()
TABLE *table
FOREIGN_SERVER *server
NOTES
This function takes the arguments of an open table object and a pointer
to an allocated FOREIGN_SERVER struct. It stores the server_name into
the first field of the table (the primary key, server_name column). With
this, index_read_idx is called, if the record is found, an error is set
to ER_FOREIGN_SERVER_EXISTS (the server with that server name exists in the
table), if not, then store_server_fields stores all fields of the
FOREIGN_SERVER to the table, then ha_write_row is inserted. If an error
is encountered in either index_read_idx or ha_write_row, then that error
is returned
RETURN VALUE
0 - no errors
>0 - error code
*/
int insert_server_record(TABLE *table, FOREIGN_SERVER *server)
{
int error;
DBUG_ENTER("insert_server_record");
table->use_all_columns();
/* set the field that's the PK to the value we're looking for */
table->field[0]->store(server->server_name,
server->server_name_length,
system_charset_info);
/* read index until record is that specified in server_name */
if ((error= table->file->index_read_idx(table->record[0], 0,
(byte *)table->field[0]->ptr,
table->key_info[0].key_length,
HA_READ_KEY_EXACT)))
{
/* if not found, err */
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
{
table->file->print_error(error, MYF(0));
error= 1;
}
/* store each field to be inserted */
store_server_fields(table, server);
DBUG_PRINT("info",("record for server '%s' not found!",
server->server_name));
/* write/insert the new server */
if ((error=table->file->ha_write_row(table->record[0])))
{
table->file->print_error(error, MYF(0));
}
else
error= 0;
}
else
error= ER_FOREIGN_SERVER_EXISTS;
DBUG_RETURN(error);
}
/*
SYNOPSIS
drop_server()
THD *thd
LEX_SERVER_OPTIONS *server_options
NOTES
This function takes as its arguments a THD object pointer and a pointer
to a LEX_SERVER_OPTIONS struct from the parser. The member 'server_name'
of this LEX_SERVER_OPTIONS struct contains the value of the server to be
deleted. The mysql.servers table is opened via open_ltable, a table object
returned, the servers cache mutex locked, then delete_server_record is
called with this table object and LEX_SERVER_OPTIONS server_name and
server_name_length passed, containing the name of the server to be
dropped/deleted, then delete_server_record_in_cache is called to delete
the server from the servers cache.
RETURN VALUE
0 - no error
> 0 - error code
*/
int drop_server(THD *thd, LEX_SERVER_OPTIONS *server_options)
{
byte server_key[MAX_KEY_LENGTH];
int error;
TABLE_LIST tables;
TABLE *table;
DBUG_ENTER("drop_server");
DBUG_PRINT("info", ("server name server->server_name %s",
server_options->server_name));
bzero((char*) &tables, sizeof(tables));
tables.db= (char*) "mysql";
tables.alias= tables.table_name= (char*) "servers";
/* need to open before acquiring THR_LOCK_plugin or it will deadlock */
if (! (table= open_ltable(thd, &tables, TL_WRITE)))
DBUG_RETURN(TRUE);
rw_wrlock(&THR_LOCK_servers);
VOID(pthread_mutex_lock(&servers_cache_mutex));
if ((error= delete_server_record(table,
server_options->server_name,
server_options->server_name_length)))
goto end;
if ((error= delete_server_record_in_cache(server_options)))
goto end;
end:
VOID(pthread_mutex_unlock(&servers_cache_mutex));
rw_unlock(&THR_LOCK_servers);
DBUG_RETURN(error);
}
/*
SYNOPSIS
delete_server_record_in_cache()
LEX_SERVER_OPTIONS *server_options
NOTES
This function's argument is a LEX_SERVER_OPTIONS struct pointer. This
function uses the "server_name" and "server_name_length" members of the
lex->server_options to search for the server in the servers_cache. Upon
returned the server (pointer to a FOREIGN_SERVER struct), it then deletes
that server from the servers_cache hash.
RETURN VALUE
0 - no error
*/
int delete_server_record_in_cache(LEX_SERVER_OPTIONS *server_options)
{
int error= 0;
FOREIGN_SERVER *server;
DBUG_ENTER("delete_server_record_in_cache");
DBUG_PRINT("info",("trying to obtain server name %s length %d",
server_options->server_name,
server_options->server_name_length));
if (!(server= (FOREIGN_SERVER *) hash_search(&servers_cache,
(byte*) server_options->server_name,
server_options->server_name_length)))
{
DBUG_PRINT("info", ("server_name %s length %d not found!",
server_options->server_name,
server_options->server_name_length));
// what should be done if not found in the cache?
}
/*
We succeded in deletion of the server to the table, now delete
the server from the cache
*/
DBUG_PRINT("info",("deleting server %s length %d",
server->server_name,
server->server_name_length));
if (server)
VOID(hash_delete(&servers_cache, (byte*) server));
servers_version++; /* servers updated */
DBUG_RETURN(error);
}
/*
SYNOPSIS
update_server()
THD *thd
FOREIGN_SERVER *existing
FOREIGN_SERVER *altered
NOTES
This function takes as arguments a THD object pointer, and two pointers,
one pointing to the existing FOREIGN_SERVER struct "existing" (which is
the current record as it is) and another pointer pointing to the
FOREIGN_SERVER struct with the members containing the modified/altered
values that need to be updated in both the mysql.servers table and the
servers_cache. It opens a table, passes the table and the altered
FOREIGN_SERVER pointer, which will be used to update the mysql.servers
table for the particular server via the call to update_server_record,
and in the servers_cache via update_server_record_in_cache.
RETURN VALUE
0 - no error
>0 - error code
*/
int update_server(THD *thd, FOREIGN_SERVER *existing, FOREIGN_SERVER *altered)
{
int error= 0;
TABLE *table;
TABLE_LIST tables;
DBUG_ENTER("update_server");
bzero((char*) &tables, sizeof(tables));
tables.db= (char*)"mysql";
tables.alias= tables.table_name= (char*)"servers";
if (!(table= open_ltable(thd, &tables, TL_WRITE)))
DBUG_RETURN(1);
rw_wrlock(&THR_LOCK_servers);
if ((error= update_server_record(table, altered)))
goto end;
update_server_record_in_cache(existing, altered);
end:
rw_unlock(&THR_LOCK_servers);
DBUG_RETURN(error);
}
/*
SYNOPSIS
update_server_record_in_cache()
FOREIGN_SERVER *existing
FOREIGN_SERVER *altered
NOTES
This function takes as an argument the FOREIGN_SERVER structi pointer
for the existing server and the FOREIGN_SERVER struct populated with only
the members which have been updated. It then "merges" the "altered" struct
members to the existing server, the existing server then represents an
updated server. Then, the existing record is deleted from the servers_cache
HASH, then the updated record inserted, in essence replacing the old
record.
RETURN VALUE
0 - no error
1 - error
*/
int update_server_record_in_cache(FOREIGN_SERVER *existing,
FOREIGN_SERVER *altered)
{
int error= 0;
DBUG_ENTER("update_server_record_in_cache");
/*
update the members that haven't been change in the altered server struct
with the values of the existing server struct
*/
merge_server_struct(existing, altered);
/*
delete the existing server struct from the server cache
*/
VOID(hash_delete(&servers_cache, (byte*)existing));
/*
Insert the altered server struct into the server cache
*/
if (my_hash_insert(&servers_cache, (byte*)altered))
{
DBUG_PRINT("info", ("had a problem inserting server %s at %lx",
altered->server_name, altered));
error= 1;
}
servers_version++; /* servers updated */
DBUG_RETURN(error);
}
/*
SYNOPSIS
merge_server_struct()
FOREIGN_SERVER *from
FOREIGN_SERVER *to
NOTES
This function takes as its arguments two pointers each to an allocated
FOREIGN_SERVER struct. The first FOREIGN_SERVER struct represents the struct
that we will obtain values from (hence the name "from"), the second
FOREIGN_SERVER struct represents which FOREIGN_SERVER struct we will be
"copying" any members that have a value to (hence the name "to")
RETURN VALUE
VOID
*/
void merge_server_struct(FOREIGN_SERVER *from, FOREIGN_SERVER *to)
{
DBUG_ENTER("merge_server_struct");
if (!to->host)
to->host= strdup_root(&mem, from->host);
if (!to->db)
to->db= strdup_root(&mem, from->db);
if (!to->username)
to->username= strdup_root(&mem, from->username);
if (!to->password)
to->password= strdup_root(&mem, from->password);
if (to->port == -1)
to->port= from->port;
if (!to->socket)
to->socket= strdup_root(&mem, from->socket);
if (!to->scheme)
to->scheme= strdup_root(&mem, from->scheme);
if (!to->owner)
to->owner= strdup_root(&mem, from->owner);
DBUG_VOID_RETURN;
}
/*
SYNOPSIS
update_server_record()
TABLE *table
FOREIGN_SERVER *server
NOTES
This function takes as its arguments an open TABLE pointer, and a pointer
to an allocated FOREIGN_SERVER structure representing an updated record
which needs to be inserted. The primary key, server_name is stored to field
0, then index_read_idx is called to read the index to that record, the
record then being ready to be updated, if found. If not found an error is
set and error message printed. If the record is found, store_record is
called, then store_server_fields stores each field from the the members of
the updated FOREIGN_SERVER struct.
RETURN VALUE
0 - no error
*/
int update_server_record(TABLE *table, FOREIGN_SERVER *server)
{
int error=0;
DBUG_ENTER("update_server_record");
table->use_all_columns();
/* set the field that's the PK to the value we're looking for */
table->field[0]->store(server->server_name,
server->server_name_length,
system_charset_info);
if ((error= table->file->index_read_idx(table->record[0], 0,
(byte *)table->field[0]->ptr,
table->key_info[0].key_length,
HA_READ_KEY_EXACT)))
{
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
{
table->file->print_error(error, MYF(0));
error= 1;
}
DBUG_PRINT("info",("server not found!"));
error= ER_FOREIGN_SERVER_DOESNT_EXIST;
}
else
{
/* ok, so we can update since the record exists in the table */
store_record(table,record[1]);
store_server_fields(table, server);
if ((error=table->file->ha_update_row(table->record[1],table->record[0])))
{
DBUG_PRINT("info",("problems with ha_update_row %d", error));
goto end;
}
}
end:
DBUG_RETURN(error);
}
/*
SYNOPSIS
delete_server_record()
TABLE *table
char *server_name
int server_name_length
NOTES
RETURN VALUE
0 - no error
*/
int delete_server_record(TABLE *table,
char *server_name,
int server_name_length)
{
int error= 0;
DBUG_ENTER("delete_server_record");
table->use_all_columns();
/* set the field that's the PK to the value we're looking for */
table->field[0]->store(server_name, server_name_length, system_charset_info);
if ((error= table->file->index_read_idx(table->record[0], 0,
(byte *)table->field[0]->ptr,
table->key_info[0].key_length,
HA_READ_KEY_EXACT)))
{
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
{
table->file->print_error(error, MYF(0));
error= 1;
}
DBUG_PRINT("info",("server not found!"));
error= ER_FOREIGN_SERVER_DOESNT_EXIST;
}
else
{
if ((error= table->file->ha_delete_row(table->record[0])))
table->file->print_error(error, MYF(0));
}
end:
DBUG_RETURN(error);
}
/*
SYNOPSIS
create_server()
THD *thd
LEX_SERVER_OPTIONS *server_options
NOTES
RETURN VALUE
0 - no error
*/
int create_server(THD *thd, LEX_SERVER_OPTIONS *server_options)
{
int error;
FOREIGN_SERVER *server;
DBUG_ENTER("create_server");
DBUG_PRINT("info", ("server_options->server_name %s",
server_options->server_name));
server= (FOREIGN_SERVER *)alloc_root(&mem,
sizeof(FOREIGN_SERVER));
if ((error= prepare_server_struct_for_insert(server_options, server)))
goto end;
if ((error= insert_server(thd, server)))
goto end;
DBUG_PRINT("info", ("error returned %d", error));
end:
DBUG_RETURN(error);
}
/*
SYNOPSIS
alter_server()
THD *thd
LEX_SERVER_OPTIONS *server_options
NOTES
RETURN VALUE
0 - no error
*/
int alter_server(THD *thd, LEX_SERVER_OPTIONS *server_options)
{
int error= 0;
FOREIGN_SERVER *altered, *existing;
DBUG_ENTER("alter_server");
DBUG_PRINT("info", ("server_options->server_name %s",
server_options->server_name));
altered= (FOREIGN_SERVER *)alloc_root(&mem,
sizeof(FOREIGN_SERVER));
VOID(pthread_mutex_lock(&servers_cache_mutex));
if (!(existing= (FOREIGN_SERVER *) hash_search(&servers_cache,
(byte*) server_options->server_name,
server_options->server_name_length)))
{
error= ER_FOREIGN_SERVER_DOESNT_EXIST;
goto end;
}
if ((error= prepare_server_struct_for_update(server_options, existing, altered)))
goto end;
if ((error= update_server(thd, existing, altered)))
goto end;
end:
DBUG_PRINT("info", ("error returned %d", error));
VOID(pthread_mutex_unlock(&servers_cache_mutex));
DBUG_RETURN(error);
}
/*
SYNOPSIS
prepare_server_struct_for_insert()
LEX_SERVER_OPTIONS *server_options
FOREIGN_SERVER *server
NOTES
RETURN VALUE
0 - no error
*/
int prepare_server_struct_for_insert(LEX_SERVER_OPTIONS *server_options,
FOREIGN_SERVER *server)
{
int error;
char *unset_ptr= (char*)"";
DBUG_ENTER("prepare_server_struct");
error= 0;
/* these two MUST be set */
server->server_name= strdup_root(&mem, server_options->server_name);
server->server_name_length= server_options->server_name_length;
server->host= server_options->host ?
strdup_root(&mem, server_options->host) : unset_ptr;
server->db= server_options->db ?
strdup_root(&mem, server_options->db) : unset_ptr;
server->username= server_options->username ?
strdup_root(&mem, server_options->username) : unset_ptr;
server->password= server_options->password ?
strdup_root(&mem, server_options->password) : unset_ptr;
/* set to 0 if not specified */
server->port= server_options->port > -1 ?
server_options->port : 0;
server->socket= server_options->socket ?
strdup_root(&mem, server_options->socket) : unset_ptr;
server->scheme= server_options->scheme ?
strdup_root(&mem, server_options->scheme) : unset_ptr;
server->owner= server_options->owner ?
strdup_root(&mem, server_options->owner) : unset_ptr;
DBUG_RETURN(error);
}
/*
SYNOPSIS
prepare_server_struct_for_update()
LEX_SERVER_OPTIONS *server_options
NOTES
RETURN VALUE
0 - no error
*/
int prepare_server_struct_for_update(LEX_SERVER_OPTIONS *server_options,
FOREIGN_SERVER *existing,
FOREIGN_SERVER *altered)
{
int error;
DBUG_ENTER("prepare_server_struct_for_update");
error= 0;
altered->server_name= strdup_root(&mem, server_options->server_name);
altered->server_name_length= server_options->server_name_length;
DBUG_PRINT("info", ("existing name %s altered name %s",
existing->server_name, altered->server_name));
/*
The logic here is this: is this value set AND is it different
than the existing value?
*/
altered->host=
(server_options->host && (strcmp(server_options->host, existing->host))) ?
strdup_root(&mem, server_options->host) : 0;
altered->db=
(server_options->db && (strcmp(server_options->db, existing->db))) ?
strdup_root(&mem, server_options->db) : 0;
altered->username=
(server_options->username &&
(strcmp(server_options->username, existing->username))) ?
strdup_root(&mem, server_options->username) : 0;
altered->password=
(server_options->password &&
(strcmp(server_options->password, existing->password))) ?
strdup_root(&mem, server_options->password) : 0;
/*
port is initialised to -1, so if unset, it will be -1
*/
altered->port= (server_options->port > -1 &&
server_options->port != existing->port) ?
server_options->port : -1;
altered->socket=
(server_options->socket &&
(strcmp(server_options->socket, existing->socket))) ?
strdup_root(&mem, server_options->socket) : 0;
altered->scheme=
(server_options->scheme &&
(strcmp(server_options->scheme, existing->scheme))) ?
strdup_root(&mem, server_options->scheme) : 0;
altered->owner=
(server_options->owner &&
(strcmp(server_options->owner, existing->owner))) ?
strdup_root(&mem, server_options->owner) : 0;
DBUG_RETURN(error);
}
/*
SYNOPSIS
servers_free()
bool end
NOTES
RETURN VALUE
void
*/
void servers_free(bool end)
{
DBUG_ENTER("servers_free");
if (!servers_cache_initialised)
DBUG_VOID_RETURN;
VOID(pthread_mutex_destroy(&servers_cache_mutex));
servers_cache_initialised=0;
free_root(&mem,MYF(0));
hash_free(&servers_cache);
DBUG_VOID_RETURN;
}
/*
SYNOPSIS
get_server_by_name()
const char *server_name
NOTES
RETURN VALUE
FOREIGN_SERVER *
*/
FOREIGN_SERVER *get_server_by_name(const char *server_name)
{
ulong error_num=0;
uint i, server_name_length;
FOREIGN_SERVER *server;
DBUG_ENTER("get_server_by_name");
DBUG_PRINT("info", ("server_name %s", server_name));
server_name_length= strlen(server_name);
if (! server_name || !strlen(server_name))
{
DBUG_PRINT("info", ("server_name not defined!"));
error_num= 1;
DBUG_RETURN((FOREIGN_SERVER *)NULL);
}
DBUG_PRINT("info", ("locking servers_cache"));
VOID(pthread_mutex_lock(&servers_cache_mutex));
if (!(server= (FOREIGN_SERVER *) hash_search(&servers_cache,
(byte*) server_name,
server_name_length)))
{
DBUG_PRINT("info", ("server_name %s length %d not found!",
server_name, server_name_length));
server= (FOREIGN_SERVER *) NULL;
}
DBUG_PRINT("info", ("unlocking servers_cache"));
VOID(pthread_mutex_unlock(&servers_cache_mutex));
DBUG_RETURN(server);
}
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "slave.h" // for tables_ok(), rpl_filter
/* structs */
typedef struct st_federated_server
{
char *server_name;
long port;
uint server_name_length;
char *db, *scheme, *username, *password, *socket, *owner, *host, *sport;
} FOREIGN_SERVER;
/* cache handlers */
my_bool servers_init(bool dont_read_server_table);
static my_bool servers_load(THD *thd, TABLE_LIST *tables);
my_bool servers_reload(THD *thd);
my_bool get_server_from_table_to_cache(TABLE *table);
void servers_free(bool end=0);
/* insert functions */
int create_server(THD *thd, LEX_SERVER_OPTIONS *server_options);
int insert_server(THD *thd, FOREIGN_SERVER *server_options);
int insert_server_record(TABLE *table, FOREIGN_SERVER *server);
int insert_server_record_into_cache(FOREIGN_SERVER *server);
void store_server_fields_for_insert(TABLE *table, FOREIGN_SERVER *server);
void store_server_fields_for_insert(TABLE *table,
FOREIGN_SERVER *existing,
FOREIGN_SERVER *altered);
int prepare_server_struct_for_insert(LEX_SERVER_OPTIONS *server_options,
FOREIGN_SERVER *server);
/* drop functions */
int drop_server(THD *thd, LEX_SERVER_OPTIONS *server_options);
int delete_server_record(TABLE *table,
char *server_name,
int server_name_length);
int delete_server_record_in_cache(LEX_SERVER_OPTIONS *server_options);
/* update functions */
int alter_server(THD *thd, LEX_SERVER_OPTIONS *server_options);
int prepare_server_struct_for_update(LEX_SERVER_OPTIONS *server_options,
FOREIGN_SERVER *existing,
FOREIGN_SERVER *altered);
int update_server(THD *thd, FOREIGN_SERVER *existing, FOREIGN_SERVER *altered);
int update_server_record(TABLE *table, FOREIGN_SERVER *server);
int update_server_record_in_cache(FOREIGN_SERVER *existing,
FOREIGN_SERVER *altered);
/* utility functions */
void merge_server_struct(FOREIGN_SERVER *from, FOREIGN_SERVER *to);
FOREIGN_SERVER *get_server_by_name(const char *server_name);
my_bool server_exists_in_table(THD *thd, char *server_name);
......@@ -364,6 +364,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%token HELP_SYM
%token HEX_NUM
%token HIGH_PRIORITY
%token HOST_SYM
%token HOSTS_SYM
%token HOUR_MICROSECOND_SYM
%token HOUR_MINUTE_SYM
......@@ -506,6 +507,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%token ONE_SYM
%token OPEN_SYM /* SQL-2003-R */
%token OPTIMIZE
%token OPTIONS_SYM
%token OPTION /* SQL-2003-N */
%token OPTIONALLY
%token OR2_SYM
......@@ -515,6 +517,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%token OUTER
%token OUTFILE
%token OUT_SYM /* SQL-2003-R */
%token OWNER_SYM
%token PACK_KEYS_SYM
%token PARAM_MARKER
%token PARSER_SYM
......@@ -528,6 +531,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%token PLUGIN_SYM
%token POINT_SYM
%token POLYGON
%token PORT_SYM
%token POSITION_SYM /* SQL-2003-N */
%token PRECISION /* SQL-2003-R */
%token PREPARE_SYM /* SQL-2003-R */
......@@ -596,6 +600,8 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%token SERIALIZABLE_SYM /* SQL-2003-N */
%token SERIAL_SYM
%token SESSION_SYM /* SQL-2003-N */
%token SERVER_SYM
%token SERVER_OPTIONS
%token SET /* SQL-2003-R */
%token SET_VAR
%token SHARE_SYM
......@@ -608,6 +614,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%token SLAVE
%token SMALLINT /* SQL-2003-R */
%token SNAPSHOT_SYM
%token SOCKET_SYM
%token SONAME_SYM
%token SOUNDS_SYM
%token SPATIAL_SYM
......@@ -715,6 +722,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%token WHILE_SYM
%token WITH /* SQL-2003-R */
%token WORK_SYM /* SQL-2003-N */
%token WRAPPER_SYM
%token WRITE_SYM /* SQL-2003-N */
%token X509_SYM
%token XA_SYM
......@@ -910,6 +918,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
view_check_option trigger_tail sp_tail
install uninstall partition_entry binlog_base64_event
init_key_options key_options key_opts key_opt key_using_alg
server_def server_options_list server_option
END_OF_INPUT
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
......@@ -1303,8 +1312,61 @@ create:
{
Lex->alter_tablespace_info->ts_cmd_type= CREATE_TABLESPACE;
}
| CREATE server_def
{
Lex->sql_command= SQLCOM_CREATE_SERVER;
}
;
server_def:
SERVER_SYM ident_or_text FOREIGN DATA_SYM WRAPPER_SYM ident_or_text OPTIONS_SYM '(' server_options_list ')'
{
Lex->server_options.server_name= $2.str;
Lex->server_options.server_name_length= $2.length;
Lex->server_options.scheme= $6.str;
}
;
server_options_list:
server_option
| server_options_list ',' server_option
;
server_option:
USER TEXT_STRING_sys
{
Lex->server_options.username= $2.str;
}
|
HOST_SYM TEXT_STRING_sys
{
Lex->server_options.host= $2.str;
}
|
DATABASE TEXT_STRING_sys
{
Lex->server_options.db= $2.str;
}
|
OWNER_SYM TEXT_STRING_sys
{
Lex->server_options.owner= $2.str;
}
|
PASSWORD TEXT_STRING_sys
{
Lex->server_options.password= $2.str;
}
|
SOCKET_SYM TEXT_STRING_sys
{
Lex->server_options.socket= $2.str;
}
|
PORT_SYM ulong_num
{
Lex->server_options.port= $2;
}
;
event_tail:
EVENT_SYM opt_if_not_exists sp_name
......@@ -4832,6 +4894,13 @@ alter:
LEX *lex= Lex;
lex->alter_tablespace_info->ts_cmd_type= ALTER_ACCESS_MODE_TABLESPACE;
}
| ALTER SERVER_SYM ident_or_text OPTIONS_SYM '(' server_options_list ')'
{
LEX *lex= Lex;
Lex->sql_command= SQLCOM_ALTER_SERVER;
Lex->server_options.server_name= $3.str;
Lex->server_options.server_name_length= $3.length;
}
;
ev_alter_on_schedule_completion: /* empty */ { $$= 0;}
......@@ -7758,6 +7827,13 @@ drop:
LEX *lex= Lex;
lex->alter_tablespace_info->ts_cmd_type= DROP_LOGFILE_GROUP;
}
| DROP SERVER_SYM if_exists ident_or_text
{
Lex->sql_command = SQLCOM_DROP_SERVER;
Lex->drop_if_exists= $3;
Lex->server_options.server_name= $4.str;
Lex->server_options.server_name_length= $4.length;
}
;
table_list:
......@@ -9324,12 +9400,15 @@ keyword:
| FLUSH_SYM {}
| HANDLER_SYM {}
| HELP_SYM {}
| HOST_SYM {}
| INSTALL_SYM {}
| LANGUAGE_SYM {}
| NO_SYM {}
| OPEN_SYM {}
| OWNER_SYM {}
| PARSER_SYM {}
| PARTITION_SYM {}
| PORT_SYM {}
| PREPARE_SYM {}
| REMOVE_SYM {}
| REPAIR {}
......@@ -9338,7 +9417,9 @@ keyword:
| ROLLBACK_SYM {}
| SAVEPOINT_SYM {}
| SECURITY_SYM {}
| SERVER_SYM {}
| SIGNED_SYM {}
| SOCKET_SYM {}
| SLAVE {}
| SONAME_SYM {}
| START_SYM {}
......@@ -9346,6 +9427,8 @@ keyword:
| TRUNCATE_SYM {}
| UNICODE_SYM {}
| UNINSTALL_SYM {}
| USER {}
| WRAPPER_SYM {}
| XA_SYM {}
| UPGRADE_SYM {}
;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -381,8 +381,8 @@ static handler *federated_create_handler(handlerton *hton,
static byte *federated_get_key(FEDERATED_SHARE *share, uint *length,
my_bool not_used __attribute__ ((unused)))
{
*length= share->connect_string_length;
return (byte*) share->scheme;
*length= share->share_key_length;
return (byte*) share->share_key;
}
/*
......@@ -549,13 +549,14 @@ static int parse_url_error(FEDERATED_SHARE *share, TABLE *table, int error_num)
int buf_len;
DBUG_ENTER("ha_federated parse_url_error");
if (share->scheme)
if (share->connection_string)
{
DBUG_PRINT("info",
("error: parse_url. Returning error code %d freeing share->scheme 0x%lx",
error_num, (long) share->scheme));
my_free((gptr) share->scheme, MYF(0));
share->scheme= 0;
("error: parse_url. Returning error code %d \
freeing share->connection_string %lx",
error_num, share->connection_string));
my_free((gptr) share->connection_string, MYF(0));
share->connection_string= 0;
}
buf_len= min(table->s->connect_string.length,
FEDERATED_QUERY_BUFFER_SIZE-1);
......@@ -563,6 +564,75 @@ static int parse_url_error(FEDERATED_SHARE *share, TABLE *table, int error_num)
my_error(error_num, MYF(0), buf);
DBUG_RETURN(error_num);
}
/*
retrieve server object which contains server meta-data
from the system table given a server's name, set share
connection parameter members
*/
int get_connection(FEDERATED_SHARE *share)
{
int error_num= ER_FOREIGN_SERVER_DOESNT_EXIST;
char error_buffer[FEDERATED_QUERY_BUFFER_SIZE];
FOREIGN_SERVER *server;
MYSQL *mysql_conn;
MYSQL_RES *result= 0;
MYSQL_ROW row;
DBUG_ENTER("ha_federated::get_connection");
if (!(server=
get_server_by_name(share->connection_string)))
{
DBUG_PRINT("info", ("get_server_by_name returned > 0 error condition!"));
/* need to come up with error handling */
error_num=1;
goto error;
}
DBUG_PRINT("info", ("get_server_by_name returned server at %lx", server));
/*
Most of these should never be empty strings, error handling will
need to be implemented. Also, is this the best way to set the share
members? Is there some allocation needed? In running this code, it works
except there are errors in the trace file of the share being overrun
at the address of the share.
*/
if (server->server_name)
share->server_name= server->server_name;
share->server_name_length= server->server_name_length ?
server->server_name_length : 0;
if (server->username)
share->username= server->username;
if (server->password)
share->password= server->password;
if (server->db)
share->database= server->db;
if (server->sport)
share->sport= server->sport;
share->port= server->port ? server->port : 0;
if (server->host)
share->hostname= server->host;
if (server->socket)
share->socket= server->socket;
if (server->scheme)
share->scheme= server->scheme;
else
share->scheme= NULL;
DBUG_PRINT("info", ("share->username %s", share->username));
DBUG_PRINT("info", ("share->password %s", share->password));
DBUG_PRINT("info", ("share->hostname %s", share->hostname));
DBUG_PRINT("info", ("share->database %s", share->database));
DBUG_PRINT("info", ("share->port %d", share->port));
DBUG_PRINT("info", ("share->socket %d", share->socket));
DBUG_RETURN(0);
error:
my_sprintf(error_buffer,
(error_buffer, "server name: '%s' doesn't exist!",
share->connection_string));
my_error(error_num, MYF(0), error_buffer);
DBUG_RETURN(error_num);
}
/*
Parse connection info from table->s->connect_string
......@@ -576,22 +646,39 @@ static int parse_url_error(FEDERATED_SHARE *share, TABLE *table, int error_num)
DESCRIPTION
Populates the share with information about the connection
to the foreign database that will serve as the data source.
This string must be specified (currently) in the "comment" field,
This string must be specified (currently) in the "CONNECTION" field,
listed in the CREATE TABLE statement.
This string MUST be in the format of any of these:
scheme://username:password@hostname:port/database/table
scheme://username@hostname/database/table
scheme://username@hostname:port/database/table
scheme://username:password@hostname/database/table
CONNECTION="scheme://username:password@hostname:port/database/table"
CONNECTION="scheme://username@hostname/database/table"
CONNECTION="scheme://username@hostname:port/database/table"
CONNECTION="scheme://username:password@hostname/database/table"
_OR_
CONNECTION="connection name"
An Example:
mysql://joe:joespass@192.168.1.111:9308/federated/testtable
CREATE TABLE t1 (id int(32))
ENGINE="FEDERATED"
CONNECTION="mysql://joe:joespass@192.168.1.111:9308/federated/testtable";
CREATE TABLE t2 (
id int(4) NOT NULL auto_increment,
name varchar(32) NOT NULL,
PRIMARY KEY(id)
) ENGINE="FEDERATED" CONNECTION="my_conn";
***IMPORTANT***
Currently, only "mysql://" is supported.
Currently, the Federated Storage Engine only supports connecting to another
MySQL Database ("scheme" of "mysql"). Connections using JDBC as well as
other connectors are in the planning stage.
'password' and 'port' are both optional.
......@@ -611,15 +698,51 @@ static int parse_url(FEDERATED_SHARE *share, TABLE *table,
share->port= 0;
share->socket= 0;
DBUG_PRINT("info", ("share at %lx", share));
DBUG_PRINT("info", ("Length: %d", table->s->connect_string.length));
DBUG_PRINT("info", ("String: '%.*s'", table->s->connect_string.length,
table->s->connect_string.str));
share->scheme= my_strndup(table->s->connect_string.str,
share->connection_string= my_strndup((const byte*)table->s->
connect_string.str,
table->s->connect_string.length,
MYF(0));
share->connect_string_length= table->s->connect_string.length;
DBUG_PRINT("info",("parse_url alloced share->scheme 0x%lx", (long) share->scheme));
// Add a null for later termination of table name
share->connection_string[table->s->connect_string.length]= 0;
DBUG_PRINT("info",("parse_url alloced share->scheme %lx",
share->connection_string));
DBUG_PRINT("info",("share->connection_string %s",share->connection_string));
/* No delimiters, must be a straight connection name */
if ( (!strchr(share->connection_string, '/')) &&
(!strchr(share->connection_string, '@')) &&
(!strchr(share->connection_string, ';')))
{
DBUG_PRINT("info",
("share->connection_string %s internal format \
share->connection_string",
share->connection_string, share->connection_string));
share->parsed= FALSE;
if ((error_num= get_connection(share)))
goto error;
/*
connection specifies everything but, resort to
expecting remote and foreign table names to match
*/
share->table_name= table->s->table_name.str;
share->table_name_length= table->s->table_name.length;
share->table_name[share->table_name_length]= '\0';
}
else
{
share->parsed= TRUE;
// Add a null for later termination of table name
share->connection_string[table->s->connect_string.length]= 0;
share->scheme= share->connection_string;
DBUG_PRINT("info",("parse_url alloced share->scheme %lx", share->scheme));
/*
remove addition of null terminator and store length
......@@ -650,8 +773,8 @@ static int parse_url(FEDERATED_SHARE *share, TABLE *table,
goto error;
/*
Found that if the string is:
user:@hostname:port/database/table
Then password is a null string, so set to NULL
user:@hostname:port/db/table
Then password is a null string, so set to NULL
*/
if ((share->password[0] == '\0'))
share->password= NULL;
......@@ -692,6 +815,7 @@ static int parse_url(FEDERATED_SHARE *share, TABLE *table,
if (share->hostname[0] == '\0')
share->hostname= NULL;
}
if (!share->port)
{
if (strcmp(share->hostname, my_localhost) == 0)
......@@ -702,7 +826,7 @@ static int parse_url(FEDERATED_SHARE *share, TABLE *table,
DBUG_PRINT("info",
("scheme: %s username: %s password: %s \
hostname: %s port: %d database: %s tablename: %s",
hostname: %s port: %d db: %s tablename: %s",
share->scheme, share->username, share->password,
share->hostname, share->port, share->database,
share->table_name));
......@@ -713,7 +837,6 @@ static int parse_url(FEDERATED_SHARE *share, TABLE *table,
DBUG_RETURN(parse_url_error(share, table, error_num));
}
/*****************************************************************************
** FEDERATED tables
*****************************************************************************/
......@@ -1313,14 +1436,16 @@ static FEDERATED_SHARE *get_share(const char *table_name, TABLE *table)
pthread_mutex_lock(&federated_mutex);
tmp_share.share_key= table_name;
tmp_share.share_key_length= strlen(table_name);
if (parse_url(&tmp_share, table, 0))
goto error;
/* TODO: change tmp_share.scheme to LEX_STRING object */
if (!(share= (FEDERATED_SHARE *) hash_search(&federated_open_tables,
(byte*) tmp_share.scheme,
(byte*) tmp_share.share_key,
tmp_share.
connect_string_length)))
share_key_length)))
{
query.set_charset(system_charset_info);
query.append(STRING_WITH_LEN("SELECT "));
......@@ -1387,8 +1512,13 @@ static int free_share(FEDERATED_SHARE *share)
if (!--share->use_count)
{
hash_delete(&federated_open_tables, (byte*) share);
my_free((gptr) share->scheme, MYF(MY_ALLOW_ZERO_PTR));
if (share->parsed)
my_free((gptr) share->socket, MYF(MY_ALLOW_ZERO_PTR));
if (share->connection_string)
{
my_free((gptr) share->connection_string, MYF(MY_ALLOW_ZERO_PTR));
share->connection_string= 0;
}
thr_lock_delete(&share->lock);
VOID(pthread_mutex_destroy(&share->mutex));
my_free((gptr) share, MYF(0));
......@@ -2697,7 +2827,7 @@ int ha_federated::create(const char *name, TABLE *table_arg,
if (!(retval= parse_url(&tmp_share, table_arg, 1)))
retval= check_foreign_data_source(&tmp_share, 1);
my_free((gptr) tmp_share.scheme, MYF(MY_ALLOW_ZERO_PTR));
//my_free((gptr) tmp_share.connection_string, MYF(MY_ALLOW_ZERO_PTR));
DBUG_RETURN(retval);
}
......
......@@ -43,6 +43,9 @@
The example implements the minimum of what you will probably need.
*/
typedef struct st_federated_share {
bool parsed;
/* this key is unique db/tablename */
const char *share_key;
/*
the primary select query to be used in rnd_init
*/
......@@ -50,6 +53,8 @@ typedef struct st_federated_share {
/*
remote host info, parse_url supplies
*/
char *server_name;
char *connection_string;
char *scheme;
char *connect_string;
char *hostname;
......@@ -60,8 +65,9 @@ typedef struct st_federated_share {
char *table;
char *socket;
char *sport;
int share_key_length;
ushort port;
uint table_name_length, connect_string_length, use_count;
uint table_name_length, server_name_length, connect_string_length, use_count;
pthread_mutex_t mutex;
THR_LOCK lock;
} FEDERATED_SHARE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment