Skip to content

Commit

Permalink
support set autocommit
Browse files Browse the repository at this point in the history
  • Loading branch information
Qihoo360 committed Jul 30, 2013
1 parent 8723a59 commit 0e6d9aa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
38 changes: 26 additions & 12 deletions plugins/proxy/proxy-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ typedef int socklen_t;

#include <mysqld_error.h> /** for ER_UNKNOWN_ERROR */

#include <math.h>
#include <openssl/evp.h>

#include "network-mysqld.h"
Expand Down Expand Up @@ -1301,18 +1302,29 @@ void modify_charset(GPtrArray* tokens, network_mysqld_con* con) {
void check_flags(GPtrArray* tokens, network_mysqld_con* con) {
con->is_in_select_calc_found_rows = con->is_insert_id = FALSE;

if (tokens->len > 2) {
sql_token* first_token = tokens->pdata[1];
sql_token* second_token = tokens->pdata[2];
if (first_token->token_id == TK_SQL_SELECT && strcasecmp(second_token->text->str, "GET_LOCK") == 0) {
gchar* key = ((sql_token*)(tokens->pdata[4]))->text->str;
sql_token** ts = (sql_token**)(tokens->pdata);
guint len = tokens->len;

if (len > 2) {
if (ts[1]->token_id == TK_SQL_SELECT && strcasecmp(ts[2]->text->str, "GET_LOCK") == 0) {
gchar* key = ts[4]->text->str;
if (!g_hash_table_lookup(con->locks, key)) g_hash_table_add(con->locks, g_strdup(key));
}

if (len > 4) { //SET AUTOCOMMIT = {0 | 1}
if (ts[1]->token_id == TK_SQL_SET && ts[3]->token_id == TK_EQ) {
if (strcasecmp(ts[2]->text->str, "AUTOCOMMIT") == 0) {
char* str = ts[4]->text->str;
if (strcmp(str, "0") == 0) con->is_not_autocommit = TRUE;
else if (strcmp(str, "1") == 0) con->is_not_autocommit = FALSE;
}
}
}
}

int i;
for (i = 1; i < tokens->len; ++i) {
sql_token* token = tokens->pdata[i];
guint i;
for (i = 1; i < len; ++i) {
sql_token* token = ts[i];
if (token->token_id == TK_SQL_SQL_CALC_FOUND_ROWS) {
con->is_in_select_calc_found_rows = TRUE;
} else {
Expand All @@ -1334,7 +1346,9 @@ gboolean is_in_blacklist(GPtrArray* tokens) {
if (token->token_id == TK_SQL_WHERE) break;
}
if (i == len) return TRUE;
} else if (token->token_id == TK_SQL_SET) {
}
/*
else if (token->token_id == TK_SQL_SET) {
if (tokens->len >= 5) {
token = tokens->pdata[2];
if (strcasecmp(token->text->str, "AUTOCOMMIT") == 0) {
Expand All @@ -1343,7 +1357,7 @@ gboolean is_in_blacklist(GPtrArray* tokens) {
}
}
}

*/
for (i = 2; i < len; ++i) {
token = tokens->pdata[i];
if (token->token_id == TK_OBRACE) {
Expand Down Expand Up @@ -1457,7 +1471,7 @@ NETWORK_MYSQLD_PLUGIN_PROTO(proxy_read_query) {
if (con->server == NULL) {
int backend_ndx = -1;

if (!con->is_in_transaction && g_hash_table_size(con->locks) == 0) {
if (!con->is_in_transaction && !con->is_not_autocommit && g_hash_table_size(con->locks) == 0) {
if (type == COM_QUERY) {
backend_ndx = rw_split(tokens, con);
//g_mutex_lock(&mutex);
Expand Down Expand Up @@ -1909,7 +1923,7 @@ NETWORK_MYSQLD_PLUGIN_PROTO(proxy_read_query_result) {

gboolean have_last_insert_id = inj->qstat.insert_id > 0;

if (!con->is_in_transaction && !con->is_in_select_calc_found_rows && !have_last_insert_id && g_hash_table_size(con->locks) == 0) network_connection_pool_lua_add_connection(con);
if (!con->is_in_transaction && !con->is_not_autocommit && !con->is_in_select_calc_found_rows && !have_last_insert_id && g_hash_table_size(con->locks) == 0) network_connection_pool_lua_add_connection(con);

++st->injected.sent_resultset;
if (st->injected.sent_resultset == 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/network-mysqld.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ network_mysqld_con *network_mysqld_con_new() {
con = g_new0(network_mysqld_con, 1);
con->parse.command = -1;

con->is_in_transaction = con->is_in_select_calc_found_rows = con->is_insert_id = FALSE;
con->is_in_transaction = con->is_in_select_calc_found_rows = con->is_insert_id = con->is_not_autocommit = FALSE;

con->charset_client = g_string_new(NULL);
con->charset_results = g_string_new(NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/network-mysqld.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ struct network_mysqld_con {

gboolean is_in_transaction;
gboolean is_in_select_calc_found_rows;
gboolean is_in_lock;
gboolean is_insert_id;
gboolean is_not_autocommit;

GString* charset_client;
GString* charset_results;
Expand Down

0 comments on commit 0e6d9aa

Please sign in to comment.