From 9747eac3e62a5dc1219d4c9982e84935ae35eb4d Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 2 Mar 2008 19:19:10 -0500 Subject: Get previous cursor working. --- src/mysqlerl.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src') diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 3421394..d3eea18 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -324,6 +324,43 @@ handle_next(MYSQL *dbh, ETERM *msg) erl_free_term(resp); } +void +handle_prev(MYSQL *dbh, ETERM *msg) +{ + MYSQL_FIELD *fields; + ETERM *ecols, *erows, *resp; + unsigned int num_fields; + + logmsg("DEBUG: got prev msg."); + if (results == NULL) { + logmsg("ERROR: got prev message w/o cursor."); + exit(2); + } + + num_fields = mysql_num_fields(results); + fields = mysql_fetch_fields(results); + + ecols = make_cols(fields, num_fields); + logmsg("resultoffset: %d, num_rows: %d", resultoffset, numrows); + if (resultoffset == 0) { + resp = erl_format("{selected, ~w, []}", ecols); + } else { + resultoffset = resultoffset - 1; + mysql_data_seek(results, resultoffset); + erows = make_rows(1, num_fields); + + /* Rewind to position at the point we returned. */ + resultoffset = resultoffset - 1; + mysql_data_seek(results, resultoffset); + resp = erl_format("{selected, ~w, ~w}", ecols, erows); + erl_free_term(erows); + } + + erl_free_term(ecols); + write_msg(resp); + erl_free_term(resp); +} + void dispatch_db_cmd(MYSQL *dbh, ETERM *msg) { @@ -348,6 +385,9 @@ dispatch_db_cmd(MYSQL *dbh, ETERM *msg) } else if (strncmp((char *)ERL_ATOM_PTR(tag), NEXT_MSG, strlen(NEXT_MSG)) == 0) { handle_next(dbh, msg); + } else if (strncmp((char *)ERL_ATOM_PTR(tag), + PREV_MSG, strlen(PREV_MSG)) == 0) { + handle_prev(dbh, msg); } else { logmsg("WARNING: message type %s unknown.", (char *)ERL_ATOM_PTR(tag)); erl_free_term(tag); -- cgit v1.2.3