diff options
author | Brian Cully <bjc@kublai.com> | 2008-03-02 19:09:52 -0500 |
---|---|---|
committer | Brian Cully <github.20.shmit@spamgourmet.com> | 2008-03-02 19:09:52 -0500 |
commit | 3846bcdd45003c16786d99500b5bb84d3badac24 (patch) | |
tree | e5c1f8a57f2d4b48a581c307d07d4edca8744498 /src | |
parent | 9b172108c0facc0e480d5e4e534e3d600d11df12 (diff) | |
download | mysqlerl-3846bcdd45003c16786d99500b5bb84d3badac24.tar.gz mysqlerl-3846bcdd45003c16786d99500b5bb84d3badac24.zip |
Get next cursor working.
Diffstat (limited to 'src')
-rw-r--r-- | src/mysqlerl.c | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 5c2063f..1bf3475 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -15,10 +15,11 @@ const char *QUERY_MSG = "sql_query"; const char *PARAM_QUERY_MSG = "sql_param_query"; const char *SELECT_COUNT_MSG = "sql_select_count"; +const char *FIRST_MSG = "sql_first"; const char *NEXT_MSG = "sql_next"; MYSQL_RES *results = NULL; -my_ulonglong resultoffset = 0; +my_ulonglong resultoffset = 0, numrows = 0; void usage() @@ -34,6 +35,7 @@ set_mysql_results(MYSQL_RES *res) mysql_free_result(results); results = res; resultoffset = 0; + numrows = mysql_num_rows(results); } ETERM * @@ -113,6 +115,7 @@ make_rows(unsigned int num_rows, unsigned int num_fields) MYSQL_ROW row; row = mysql_fetch_row(results); + resultoffset++; lengths = mysql_fetch_lengths(results); rt = make_row(row, lengths, num_fields); @@ -134,14 +137,13 @@ handle_mysql_result() { MYSQL_FIELD *fields; ETERM *ecols, *erows, *resp; - unsigned int num_fields, num_rows; + unsigned int num_fields; num_fields = mysql_num_fields(results); fields = mysql_fetch_fields(results); - num_rows = mysql_num_rows(results); ecols = make_cols(fields, num_fields); - erows = make_rows(num_rows, num_fields); + erows = make_rows(numrows, num_fields); resp = erl_format("{selected, ~w, ~w}", ecols, erows); @@ -218,7 +220,7 @@ handle_select_count(MYSQL *dbh, ETERM *msg) } else { set_mysql_results(mysql_store_result(dbh)); if (results) { - resp = erl_format("{ok, ~i}", mysql_num_rows(results)); + resp = erl_format("{ok, ~i}", numrows); } else { if (mysql_field_count(dbh) == 0) resp = erl_format("{ok, ~i}", mysql_affected_rows(dbh)); @@ -234,6 +236,34 @@ handle_select_count(MYSQL *dbh, ETERM *msg) } void +handle_first(MYSQL *dbh, ETERM *msg) +{ + MYSQL_FIELD *fields; + ETERM *ecols, *erows, *resp; + unsigned int num_fields; + + logmsg("DEBUG: got first msg."); + if (results == NULL) { + logmsg("ERROR: got first message w/o cursor."); + exit(2); + } + + num_fields = mysql_num_fields(results); + fields = mysql_fetch_fields(results); + mysql_data_seek(results, 0); + resultoffset = 0; + + ecols = make_cols(fields, num_fields); + erows = make_rows(1, num_fields); + resp = erl_format("{selected, ~w, ~w}", ecols, erows); + erl_free_term(erows); + + erl_free_term(ecols); + write_msg(resp); + erl_free_term(resp); +} + +void handle_next(MYSQL *dbh, ETERM *msg) { MYSQL_FIELD *fields; @@ -250,13 +280,13 @@ handle_next(MYSQL *dbh, ETERM *msg) fields = mysql_fetch_fields(results); ecols = make_cols(fields, num_fields); - if (resultoffset == mysql_num_rows(results)) { + logmsg("resultoffset: %d, num_rows: %d", resultoffset, numrows); + if (resultoffset == numrows) { resp = erl_format("{selected, ~w, []}", ecols); } else { erows = make_rows(1, num_fields); resp = erl_format("{selected, ~w, ~w}", ecols, erows); erl_free_term(erows); - resultoffset++; } erl_free_term(ecols); @@ -280,6 +310,9 @@ dispatch_db_cmd(MYSQL *dbh, ETERM *msg) SELECT_COUNT_MSG, strlen(SELECT_COUNT_MSG)) == 0) { handle_select_count(dbh, msg); } else if (strncmp((char *)ERL_ATOM_PTR(tag), + FIRST_MSG, strlen(FIRST_MSG)) == 0) { + handle_first(dbh, msg); + } else if (strncmp((char *)ERL_ATOM_PTR(tag), NEXT_MSG, strlen(NEXT_MSG)) == 0) { handle_next(dbh, msg); } else { |