From 81a351208d5707a949d06b560f397446fe2b60bf Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 1 Mar 2008 22:56:48 -0500 Subject: Split handlers out. --- src/mysqlerl.c | 98 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 40 deletions(-) (limited to 'src/mysqlerl.c') diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 6a3a2e3..cc684a3 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -170,6 +170,61 @@ write_cmd(const char *cmd, msglen_t len) return 0; } +void +handle_sql_query(MYSQL *dbh, ETERM *cmd) +{ + ETERM *query, *resp; + char *q, *buf; + int buflen; + + query = erl_element(2, cmd); + q = erl_iolist_to_string(query); + erl_free_term(query); + + logmsg("DEBUG: got query msg: %s.", q); + resp = erl_format("{query, 1}", q); + erl_free(q); + + buflen = erl_term_len(resp); + buf = (char *)malloc(buflen); + erl_encode(resp, (unsigned char *)buf); + erl_free_term(resp); + write_cmd(buf, buflen); + free(buf); +} + +void +handle_sql_commit(MYSQL *dbh, ETERM *cmd) +{ + ETERM *resp; + char *buf; + int buflen; + + resp = erl_format("{ok, commit}"); + buflen = erl_term_len(resp); + buf = (char *)malloc(buflen); + erl_encode(resp, (unsigned char *)buf); + erl_free_term(resp); + write_cmd(buf, buflen); + free(buf); +} + +void +handle_sql_rollback(MYSQL *dbh, ETERM *cmd) +{ + ETERM *resp; + char *buf; + int buflen; + + resp = erl_format("{ok, rollback}"); + buflen = erl_term_len(resp); + buf = (char *)malloc(buflen); + erl_encode(resp, (unsigned char *)buf); + erl_free_term(resp); + write_cmd(buf, buflen); + free(buf); +} + void dispatch_db_cmd(MYSQL *dbh, msg_t *msg) { @@ -178,50 +233,13 @@ dispatch_db_cmd(MYSQL *dbh, msg_t *msg) tag = erl_element(1, msg->cmd); if (strncmp((char *)ERL_ATOM_PTR(tag), QUERY_MSG, strlen(QUERY_MSG)) == 0) { - ETERM *query, *resp; - char *q, *buf; - int buflen; - - query = erl_element(2, msg->cmd); - q = erl_iolist_to_string(query); - erl_free_term(query); - - logmsg("DEBUG: got query msg: %s.", q); - resp = erl_format("{query, ~s}", q); - erl_free(q); - - buflen = erl_term_len(resp); - buf = (char *)malloc(buflen); - erl_encode(resp, (unsigned char *)buf); - erl_free_term(resp); - write_cmd(buf, buflen); - free(buf); + handle_sql_query(dbh, msg->cmd); } else if (strncmp((char *)ERL_ATOM_PTR(tag), COMMIT_MSG, strlen(COMMIT_MSG)) == 0) { - ETERM *resp; - char *buf; - int buflen; - - resp = erl_format("{ok, commit}"); - buflen = erl_term_len(resp); - buf = (char *)malloc(buflen); - erl_encode(resp, (unsigned char *)buf); - erl_free_term(resp); - write_cmd(buf, buflen); - free(buf); + handle_sql_commit(dbh, msg->cmd); } else if (strncmp((char *)ERL_ATOM_PTR(tag), ROLLBACK_MSG, strlen(ROLLBACK_MSG)) == 0) { - ETERM *resp; - char *buf; - int buflen; - - resp = erl_format("{ok, rollback}"); - buflen = erl_term_len(resp); - buf = (char *)malloc(buflen); - erl_encode(resp, (unsigned char *)buf); - erl_free_term(resp); - write_cmd(buf, buflen); - free(buf); + handle_sql_rollback(dbh, msg->cmd); } else { logmsg("WARNING: message type %s unknown.", (char *)ERL_ATOM_PTR(tag)); erl_free_term(tag); -- cgit v1.2.3