diff options
author | Brian Cully <bjc@kublai.com> | 2008-03-02 12:19:56 -0500 |
---|---|---|
committer | Brian Cully <github.20.shmit@spamgourmet.com> | 2008-03-02 12:19:56 -0500 |
commit | eb2a542f72900cd4426b5a788723959d590313a3 (patch) | |
tree | db618a45356e32818a4e04f44a45bf2cbcbcd238 /src | |
parent | 40547b6702940c68cec48be580d80a6d95614c29 (diff) | |
download | mysqlerl-eb2a542f72900cd4426b5a788723959d590313a3.tar.gz mysqlerl-eb2a542f72900cd4426b5a788723959d590313a3.zip |
Move SQL result processing to its own function. Clear up a memory leak.
Diffstat (limited to 'src')
-rw-r--r-- | src/mysqlerl.c | 137 |
1 files changed, 73 insertions, 64 deletions
diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 08ade42..1e36b86 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -13,6 +13,77 @@ const char *QUERY_MSG = "sql_query"; +ETERM * +handle_mysql_result(MYSQL_RES *result) +{ + MYSQL_FIELD *fields; + unsigned int i, num_fields, num_rows; + ETERM **cols, *ecols, **rows, *erows, *resp; + + num_fields = mysql_num_fields(result); + fields = mysql_fetch_fields(result); + cols = (ETERM **)malloc(num_fields * sizeof(ETERM *)); + for (i = 0; i < num_fields; i++) { + logmsg("DEBUG: cols[%d]: %s", i, fields[i].name); + cols[i] = erl_mk_string(fields[i].name); + } + ecols = erl_mk_list(cols, num_fields); + + num_rows = mysql_num_rows(result); + rows = (ETERM **)malloc(num_rows * sizeof(ETERM *)); + for (i = 0; i < num_rows; i++) { + ETERM **rowtup, *rt; + unsigned long *lengths; + MYSQL_ROW row; + unsigned int j; + + row = mysql_fetch_row(result); + lengths = mysql_fetch_lengths(result); + + rowtup = (ETERM **)malloc(num_fields * sizeof(ETERM *)); + for (j = 0; j < num_fields; j++) { + logmsg("DEBUG: rows[%d][%d] (%d): '%s'", i, j, lengths[j], row[j]); + if (row[j]) + rowtup[j] = erl_mk_estring(row[j], lengths[j]); + else + rowtup[j] = erl_mk_atom("NULL"); + logmsg("DEBUG: rowtup[%d]: %d", j, rowtup[j]); + } + logmsg("DEBUG: making tuple of %d", num_fields); + rt = erl_mk_tuple(rowtup, num_fields); + if (rt == NULL) { + logmsg("ERROR: couldn't allocate %d-tuple", num_fields); + exit(3); + } + logmsg("DEBUG: copying rt"); + rows[i] = erl_format("~w", rt); + + logmsg("DEBUG: freeing row fields"); + for (j = 0; j < num_fields; j++) + erl_free_term(rowtup[j]); + free(rowtup); + erl_free_term(rt); + } + logmsg("DEBUG: making row list"); + erows = erl_mk_list(rows, num_rows); + + logmsg("DEBUG: preparing response"); + resp = erl_format("{selected, ~w, ~w}", + ecols, erows); + + for (i = 0; i < num_fields; i++) + erl_free_term(cols[i]); + free(cols); + erl_free_term(ecols); + + for (i = 0; i < num_rows; i++) + erl_free_term(rows[i]); + free(rows); + erl_free_term(erows); + + return resp; +} + void handle_sql_query(MYSQL *dbh, ETERM *cmd) { @@ -32,70 +103,8 @@ handle_sql_query(MYSQL *dbh, ETERM *cmd) result = mysql_store_result(dbh); if (result) { - MYSQL_FIELD *fields; - unsigned int i, num_fields, num_rows; - ETERM **cols, *ecols, **rows, *erows; - - num_fields = mysql_num_fields(result); - fields = mysql_fetch_fields(result); - cols = (ETERM **)malloc(num_fields * sizeof(ETERM *)); - for (i = 0; i < num_fields; i++) { - logmsg("DEBUG: cols[%d]: %s", i, fields[i].name); - cols[i] = erl_mk_string(fields[i].name); - } - ecols = erl_mk_list(cols, num_fields); - - num_rows = mysql_num_rows(result); - rows = (ETERM **)malloc(num_rows * sizeof(ETERM *)); - for (i = 0; i < num_rows; i++) { - ETERM **rowtup, *rt; - unsigned long *lengths; - MYSQL_ROW row; - unsigned int j; - - row = mysql_fetch_row(result); - lengths = mysql_fetch_lengths(result); - - rowtup = (ETERM **)malloc(num_fields * sizeof(ETERM *)); - for (j = 0; j < num_fields; j++) { - logmsg("DEBUG: rows[%d][%d] (%d): '%s'", i, j, lengths[j], row[j]); - if (row[j]) - rowtup[j] = erl_mk_estring(row[j], lengths[j]); - else - rowtup[j] = erl_mk_atom("NULL"); - logmsg("DEBUG: rowtup[%d]: %d", j, rowtup[j]); - } - logmsg("DEBUG: making tuple of %d", num_fields); - rt = erl_mk_tuple(rowtup, num_fields); - if (rt == NULL) { - logmsg("ERROR: couldn't allocate %d-tuple", num_fields); - exit(3); - } - logmsg("DEBUG: copying rt"); - rows[i] = erl_format("~w", rt); - - logmsg("DEBUG: freeing row fields"); - for (j = 0; j < num_fields; j++) - erl_free_term(rowtup[j]); - free(rowtup); - erl_free_term(rt); - } - logmsg("DEBUG: making row list"); - erows = erl_mk_list(rows, num_rows); - - logmsg("DEBUG: preparing response"); - resp = erl_format("{selected, ~w, ~w}", - ecols, erows); - - for (i = 0; i < num_fields; i++) - erl_free_term(cols[i]); - free(cols); - erl_free_term(ecols); - - for (i = 0; i < num_rows; i++) - erl_free_term(rows[i]); - free(rows); - erl_free_term(erows); + resp = handle_mysql_result(result); + mysql_free_result(result); } else { if (mysql_field_count(dbh) == 0) resp = erl_format("{num_rows, ~i}", mysql_affected_rows(dbh)); |