Skip to content

Commit 6bd6943

Browse files
committed
convert 'use database' to COM_INIT_DB packet, for the compatibility with GUI tools
1 parent de78e7b commit 6bd6943

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

plugins/proxy/proxy-plugin.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,33 @@ gboolean sql_is_write(GPtrArray *tokens) {
13081308
return TRUE;
13091309
}
13101310

1311+
/**
1312+
* for GUI tools compatibility, who send 'use xxxx' than com_init_db to change database
1313+
* return COM_INIT_DB packets or origin packets
1314+
*/
1315+
GString* convert_use_database2com_init_db(char type, GString *origin_packets, GPtrArray *tokens) {
1316+
if (type == COM_QUERY) {
1317+
sql_token **ts = (sql_token**)(tokens->pdata);
1318+
guint tokens_len = tokens->len;
1319+
if (tokens_len > 1) {
1320+
guint i = 1;
1321+
sql_token_id token_id = ts[i]->token_id;
1322+
1323+
while (token_id == TK_COMMENT && ++i < tokens_len) {
1324+
token_id = ts[i]->token_id;
1325+
}
1326+
1327+
if (token_id == TK_SQL_USE && (i+1) < tokens_len && ts[i+1]->token_id == TK_LITERAL) {
1328+
g_string_truncate(origin_packets, 0);
1329+
g_string_append_c(origin_packets, COM_INIT_DB);
1330+
g_string_append_printf(origin_packets, "%s", ts[i+1]->text->str);
1331+
}
1332+
}
1333+
}
1334+
1335+
return origin_packets;
1336+
}
1337+
13111338
/**
13121339
* gets called after a query has been read
13131340
*
@@ -1357,6 +1384,7 @@ NETWORK_MYSQLD_PLUGIN_PROTO(proxy_read_query) {
13571384
sqls = sql_parse(con, tokens);
13581385
}
13591386

1387+
packets = convert_use_database2com_init_db(type, packets, tokens);
13601388
gboolean is_write = sql_is_write(tokens);
13611389

13621390
ret = PROXY_SEND_INJECTION;
@@ -1802,6 +1830,7 @@ NETWORK_MYSQLD_PLUGIN_PROTO(proxy_read_query_result) {
18021830
} else if (inj->id == 7) {
18031831
log_sql(con, inj);
18041832

1833+
18051834
merge_res_t* merge_res = con->merge_res;
18061835
if (inj->qstat.query_status == MYSQLD_PACKET_OK && merge_res->rows->len < merge_res->limit) merge_rows(con, inj);
18071836

0 commit comments

Comments
 (0)