# HG changeset patch # User jbe # Date 1363513200 -3600 # Node ID e22a4d2aea2d470a35e45ccb7b1dc30bfc2d43f1 # Parent d420e7a5a75f6e6620b2ed6a2e500def613c0b58 Write ranks to database in "lf_update_suggestion_order" diff -r d420e7a5a75f -r e22a4d2aea2d lf_update_suggestion_order.c --- a/lf_update_suggestion_order.c Sun Mar 17 10:02:49 2013 +0100 +++ b/lf_update_suggestion_order.c Sun Mar 17 10:40:00 2013 +0100 @@ -10,6 +10,7 @@ char *res; size_t res_len; res = malloc(2*len+3); + if (!res) return NULL; res[0] = '\''; res_len = PQescapeStringConn(conn, res+1, str, len, NULL); res[res_len+1] = '\''; @@ -142,7 +143,78 @@ abort(); } -static void process_initiative(PGresult *res) { +static int write_ranks(PGconn *db, char *escaped_initiative_id) { + PGresult *res; + char *cmd; + int i; + if (asprintf(&cmd, "BEGIN; UPDATE \"suggestion\" SET \"proportional_order\" = NULL WHERE \"initiative_id\" = %s", escaped_initiative_id) < 0) { + fprintf(stderr, "Could not prepare query string in memory.\n"); + abort(); + } + res = PQexec(db, cmd); + free(cmd); + if (!res) { + fprintf(stderr, "Error in pqlib while sending SQL command to initiate suggestion update.\n"); + return 1; + } else if ( + PQresultStatus(res) != PGRES_COMMAND_OK && + PQresultStatus(res) != PGRES_TUPLES_OK + ) { + fprintf(stderr, "Error while executing SQL command to initiate suggestion update:\n%s", PQresultErrorMessage(res)); + PQclear(res); + return 1; + } else { + PQclear(res); + } + for (i=0; i 4) { fprintf(stderr, "Unexpected preference value.\n"); - abort(); + free(ballots); + free(candidates); + return 1; } preference--; ballot->weight = weight; @@ -246,9 +322,9 @@ for (i=0; iseat = candidate_count - i; + printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key); } - free(candidates); for (i=0; i