Abstract the session default database

The default database can now be manipulated with a set of functions
exposed by the maxscale/protocol/mysql.h header. This removes the need to
handle the structures themselves in the modules and is a step towards
moving the dcb->data contents inside the session.
This commit is contained in:
Markus Mäkelä
2017-06-13 12:33:50 +03:00
parent 93660c19ed
commit bd7a26d830
11 changed files with 66 additions and 45 deletions

View File

@ -380,8 +380,7 @@ bool execute_sescmd_in_backend(backend_ref_t *backend_ref)
MYSQL_session* data;
unsigned int qlen;
data = (MYSQL_session*)dcb->session->client_dcb->data;
*data->db = 0;
mxs_mysql_set_current_db(dcb->session, "");
tmpbuf = scur->scmd_cur_cmd->my_sescmd_buf;
qlen = MYSQL_GET_PAYLOAD_LEN((unsigned char *) GWBUF_DATA(tmpbuf));
if (qlen)
@ -394,8 +393,10 @@ bool execute_sescmd_in_backend(backend_ref_t *backend_ref)
qlen = MYSQL_DATABASE_MAXLEN;
}
memcpy(data->db, (char*)GWBUF_DATA(tmpbuf) + 5, qlen);
data->db[qlen] = 0;
char db[qlen + 1];
memcpy(db, (char*)GWBUF_DATA(tmpbuf) + 5, qlen);
db[qlen] = 0;
mxs_mysql_set_current_db(dcb->session, db);
}
}
/** Fallthrough */

View File

@ -54,8 +54,8 @@ void check_drop_tmp_table(ROUTER_CLIENT_SES *router_cli_ses, GWBUF *querybuf)
for (size_t i = 0; i < n_infos; i++)
{
MYSQL_session* data = static_cast<MYSQL_session*>(router_cli_ses->client_dcb->data);
std::string table = info[i].database ? info[i].database : data->db;
const char* db = mxs_mysql_get_current_db(router_cli_ses->client_dcb->session);
std::string table = info[i].database ? info[i].database : db;
table += ".";
if (info[i].table)
@ -95,8 +95,8 @@ bool is_read_tmp_table(ROUTER_CLIENT_SES *router_cli_ses,
for (size_t i = 0; i < n_infos; i++)
{
MYSQL_session* data = static_cast<MYSQL_session*>(router_cli_ses->client_dcb->data);
std::string table = info[i].database ? info[i].database : data->db;
const char* db = mxs_mysql_get_current_db(router_cli_ses->client_dcb->session);
std::string table = info[i].database ? info[i].database : db;
table += ".";
if (info[i].table)
@ -142,8 +142,8 @@ void check_create_tmp_table(ROUTER_CLIENT_SES *router_cli_ses,
if (tblname && *tblname)
{
MYSQL_session* data = static_cast<MYSQL_session*>(router_cli_ses->client_dcb->data);
table += data->db;
const char* db = mxs_mysql_get_current_db(router_cli_ses->client_dcb->session);
table += db;
table += ".";
table += tblname;
}

View File

@ -1,5 +1,5 @@
add_library(schemarouter SHARED schemarouter.cc schemarouterinstance.cc schemaroutersession.cc shard_map.cc session_command.cc)
target_link_libraries(schemarouter maxscale-common)
target_link_libraries(schemarouter maxscale-common MySQLCommon)
add_dependencies(schemarouter pcre2)
set_target_properties(schemarouter PROPERTIES VERSION "1.0.0")
install_module(schemarouter core)

View File

@ -46,28 +46,23 @@ SchemaRouterSession::SchemaRouterSession(MXS_SESSION* session, SchemaRouter* rou
{
char db[MYSQL_DATABASE_MAXLEN + 1] = "";
MySQLProtocol* protocol = (MySQLProtocol*)session->client_dcb->protocol;
MYSQL_session* data = (MYSQL_session*)session->client_dcb->data;
bool using_db = false;
bool have_db = false;
const char* current_db = mxs_mysql_get_current_db(session);
/* To enable connecting directly to a sharded database we first need
* to disable it for the client DCB's protocol so that we can connect to them*/
if (protocol->client_capabilities & GW_MYSQL_CAPABILITIES_CONNECT_WITH_DB &&
(have_db = strnlen(data->db, MYSQL_DATABASE_MAXLEN) > 0))
(have_db = *current_db))
{
protocol->client_capabilities &= ~GW_MYSQL_CAPABILITIES_CONNECT_WITH_DB;
strcpy(db, data->db);
*data->db = 0;
strcpy(db, current_db);
mxs_mysql_set_current_db(session, "");
using_db = true;
MXS_INFO("Client logging in directly to a database '%s', "
"postponing until databases have been mapped.", db);
}
if (!have_db)
{
MXS_INFO("Client'%s' connecting with empty database.", data->user);
}
if (using_db)
{
m_state |= INIT_USE_DB;