From 76c6dd8f62ad737bc17e59f29904efe8c31ca7ea Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 1 Mar 2008 18:02:49 -0500 Subject: Normalize more code flow. --- src/mysqlerl.c | 32 ++++++++++++++++++++++++++++++-- src/mysqlerl.erl | 4 ++-- src/mysqlerl.hrl | 2 ++ src/mysqlerl_connection.erl | 13 ++++--------- 4 files changed, 38 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 0ce2703..6a3a2e3 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -20,6 +20,8 @@ const char *LOGPATH = "/tmp/mysqlerl.log"; static FILE *logfile = NULL; const char *QUERY_MSG = "sql_query"; +const char *COMMIT_MSG = "sql_commit"; +const char *ROLLBACK_MSG = "sql_rollback"; typedef u_int32_t msglen_t; @@ -174,7 +176,8 @@ dispatch_db_cmd(MYSQL *dbh, msg_t *msg) ETERM *tag; tag = erl_element(1, msg->cmd); - if (strncmp((char *)ERL_ATOM_PTR(tag), QUERY_MSG, sizeof(QUERY_MSG)) == 0) { + if (strncmp((char *)ERL_ATOM_PTR(tag), + QUERY_MSG, strlen(QUERY_MSG)) == 0) { ETERM *query, *resp; char *q, *buf; int buflen; @@ -184,14 +187,39 @@ dispatch_db_cmd(MYSQL *dbh, msg_t *msg) erl_free_term(query); logmsg("DEBUG: got query msg: %s.", q); - resp = erl_format("{ok, ~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); + } 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); + } 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); } else { diff --git a/src/mysqlerl.erl b/src/mysqlerl.erl index aae7fda..0bb46eb 100644 --- a/src/mysqlerl.erl +++ b/src/mysqlerl.erl @@ -56,9 +56,9 @@ commit(Ref, CommitMode) -> %% common_reason() %% ok | {error, Reason} commit(Ref, commit, Timeout) -> - mysqlerl_connection:sql_query(Ref, "COMMIT", Timeout); + gen_server:call(Ref, #sql_commit{}, Timeout); commit(Ref, rollback, Timeout) -> - mysqlerl_connection:sql_query(Ref, "ROLLBACK", Timeout). + gen_server:call(Ref, #sql_rollback{}, Timeout). %% Arguments: %% Host = string() diff --git a/src/mysqlerl.hrl b/src/mysqlerl.hrl index aabd384..ab789ec 100644 --- a/src/mysqlerl.hrl +++ b/src/mysqlerl.hrl @@ -1 +1,3 @@ -record(sql_query, {q}). +-record(sql_commit, {}). +-record(sql_rollback, {}). diff --git a/src/mysqlerl_connection.erl b/src/mysqlerl_connection.erl index cdb19c8..babb109 100644 --- a/src/mysqlerl_connection.erl +++ b/src/mysqlerl_connection.erl @@ -40,20 +40,14 @@ terminate(Reason, State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. -handle_call(#sql_query{q = Query}, _From, State) -> - {reply, make_request(State#state.ref, {sql_query, Query}), State}; -handle_call(Request, From, State) -> - io:format("DEBUG: got unknown call from ~p: ~p~n", [From, Request]), - {noreply, State}. +handle_call(Request, _From, State) -> + {reply, make_request(State#state.ref, Request), State}. handle_cast(stop, State) -> {stop, normal, State}. handle_info({'EXIT', _Ref, Reason}, State) -> - {stop, #port_closed{reason = Reason}, State}; -handle_info(Info, State) -> - io:format("DEBUG: got unknown info: ~p~n", [Info]), - {noreply, State}. + {stop, #port_closed{reason = Reason}, State}. helper() -> case code:priv_dir(mysqlerl) of @@ -63,6 +57,7 @@ helper() -> filename:join([PrivDir, "mysqlerl"]). make_request(Ref, Req) -> + io:format("DEBUG: Sending request: ~p~n", [Req]), port_command(Ref, term_to_binary(Req)), receive {Ref, {data, Res}} -> binary_to_term(Res); -- cgit v1.2.3