liquid_feedback_core

annotate lf_update_suggestion_order.c @ 375:3f7a89ad996d

Added colon in comment in lf_update.c and lf_update_suggestion_order.c
author jbe
date Sun Mar 17 20:35:42 2013 +0100 (2013-03-17)
parents fadfe4f267e2
children 4a18576a359f
rev   line source
jbe@352 1 #include <stdlib.h>
jbe@352 2 #include <stdio.h>
jbe@352 3 #include <string.h>
jbe@352 4 #include <libpq-fe.h>
jbe@352 5 #include <search.h>
jbe@352 6
jbe@374 7 static int logging = 0;
jbe@374 8
jbe@352 9 static char *escapeLiteral(PGconn *conn, const char *str, size_t len) {
jbe@352 10 // provides compatibility for PostgreSQL versions prior 9.0
jbe@352 11 // in future: return PQescapeLiteral(conn, str, len);
jbe@352 12 char *res;
jbe@352 13 size_t res_len;
jbe@352 14 res = malloc(2*len+3);
jbe@358 15 if (!res) return NULL;
jbe@352 16 res[0] = '\'';
jbe@352 17 res_len = PQescapeStringConn(conn, res+1, str, len, NULL);
jbe@352 18 res[res_len+1] = '\'';
jbe@352 19 res[res_len+2] = 0;
jbe@352 20 return res;
jbe@352 21 }
jbe@352 22
jbe@352 23 static void freemem(void *ptr) {
jbe@352 24 // to be used for "escapeLiteral" function
jbe@352 25 // provides compatibility for PostgreSQL versions prior 9.0
jbe@352 26 // in future: PQfreemem(ptr);
jbe@352 27 free(ptr);
jbe@352 28 }
jbe@352 29
jbe@372 30 // column numbers when querying "individual_suggestion_ranking" view in function main():
jbe@352 31 #define COL_MEMBER_ID 0
jbe@352 32 #define COL_WEIGHT 1
jbe@352 33 #define COL_PREFERENCE 2
jbe@352 34 #define COL_SUGGESTION_ID 3
jbe@352 35
jbe@372 36 // data structure for a candidate (in this case a suggestion) to the proportional runoff system:
jbe@353 37 struct candidate {
jbe@372 38 char *key; // identifier of the candidate, which is the "suggestion_id" string
jbe@372 39 double score_per_step; // added score per step
jbe@372 40 double score; // current score of candidate; a score of 1.0 is needed to survive a round
jbe@372 41 int seat; // equals 0 for unseated candidates, or contains rank number
jbe@353 42 };
jbe@353 43
jbe@372 44 // compare two integers stored as strings (invocation like strcmp):
jbe@369 45 static int compare_id(char *id1, char *id2) {
jbe@369 46 int ldiff;
jbe@369 47 ldiff = strlen(id1) - strlen(id2);
jbe@369 48 if (ldiff) return ldiff;
jbe@369 49 else return strcmp(id1, id2);
jbe@369 50 }
jbe@369 51
jbe@372 52 // compare two candidates by their key (invocation like strcmp):
jbe@353 53 static int compare_candidate(struct candidate *c1, struct candidate *c2) {
jbe@369 54 return compare_id(c1->key, c2->key);
jbe@353 55 }
jbe@352 56
jbe@372 57 // candidates are stored as global variables due to the constrained twalk() interface:
jbe@353 58 static int candidate_count;
jbe@353 59 static struct candidate *candidates;
jbe@353 60
jbe@372 61 // function to be passed to twalk() to store candidates ordered in candidates[] array:
jbe@353 62 static void register_candidate(char **candidate_key, VISIT visit, int level) {
jbe@352 63 if (visit == postorder || visit == leaf) {
jbe@353 64 struct candidate *candidate;
jbe@353 65 candidate = candidates + (candidate_count++);
jbe@353 66 candidate->key = *candidate_key;
jbe@353 67 candidate->seat = 0;
jbe@352 68 }
jbe@352 69 }
jbe@352 70
jbe@372 71 // performs a binary search in candidates[] array to lookup a candidate by its key (which is the suggestion_id):
jbe@353 72 static struct candidate *candidate_by_key(char *candidate_key) {
jbe@353 73 struct candidate *candidate;
jbe@353 74 struct candidate compare;
jbe@353 75 compare.key = candidate_key;
jbe@353 76 candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate);
jbe@353 77 if (!candidate) {
jbe@356 78 fprintf(stderr, "Candidate not found (should not happen).\n");
jbe@352 79 abort();
jbe@352 80 }
jbe@353 81 return candidate;
jbe@352 82 }
jbe@352 83
jbe@372 84 // section of a ballot with equally ranked candidates:
jbe@352 85 struct ballot_section {
jbe@352 86 int count;
jbe@353 87 struct candidate **candidates;
jbe@352 88 };
jbe@352 89
jbe@372 90 // ballot of the proportional runoff system:
jbe@352 91 struct ballot {
jbe@372 92 int weight; // if weight is greater than 1, then the ballot is counted multiple times
jbe@372 93 struct ballot_section sections[4]; // 4 sections, most preferred candidates first
jbe@352 94 };
jbe@352 95
jbe@372 96 // determine candidate, which is assigned the next seat (starting with the worst rank):
jbe@356 97 static struct candidate *loser(int round_number, struct ballot *ballots, int ballot_count) {
jbe@372 98 int i, j, k; // index variables for loops
jbe@372 99 int remaining; // remaining candidates to be seated
jbe@372 100 // reset scores of all candidates:
jbe@356 101 for (i=0; i<candidate_count; i++) {
jbe@356 102 candidates[i].score = 0.0;
jbe@356 103 }
jbe@372 104 // calculate remaining candidates to be seated:
jbe@356 105 remaining = candidate_count - round_number;
jbe@373 106 // repeat following loop, as long as there is more than one remaining candidate:
jbe@373 107 while (remaining > 1) {
jbe@372 108 double scale; // factor to be later multiplied with score_per_step:
jbe@372 109 // reset score_per_step for all candidates:
jbe@356 110 for (i=0; i<candidate_count; i++) {
jbe@356 111 candidates[i].score_per_step = 0.0;
jbe@356 112 }
jbe@372 113 // calculate score_per_step for all candidates:
jbe@356 114 for (i=0; i<ballot_count; i++) {
jbe@356 115 for (j=0; j<4; j++) {
jbe@356 116 int matches = 0;
jbe@356 117 for (k=0; k<ballots[i].sections[j].count; k++) {
jbe@356 118 struct candidate *candidate;
jbe@356 119 candidate = ballots[i].sections[j].candidates[k];
jbe@356 120 if (candidate->score < 1.0 && !candidate->seat) matches++;
jbe@356 121 }
jbe@356 122 if (matches) {
jbe@356 123 double score_inc;
jbe@368 124 score_inc = (double)ballots[i].weight / (double)matches;
jbe@356 125 for (k=0; k<ballots[i].sections[j].count; k++) {
jbe@356 126 struct candidate *candidate;
jbe@356 127 candidate = ballots[i].sections[j].candidates[k];
jbe@356 128 if (candidate->score < 1.0 && !candidate->seat) {
jbe@356 129 candidate->score_per_step += score_inc;
jbe@356 130 }
jbe@356 131 }
jbe@356 132 break;
jbe@356 133 }
jbe@356 134 }
jbe@356 135 }
jbe@372 136 // calculate scale factor:
jbe@372 137 scale = (double)0.0; // 0.0 is used to indicate that there is no value yet
jbe@356 138 for (i=0; i<candidate_count; i++) {
jbe@356 139 double max_scale;
jbe@356 140 if (candidates[i].score_per_step > 0.0) {
jbe@356 141 max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step;
jbe@368 142 if (scale == 0.0 || max_scale <= scale) {
jbe@356 143 scale = max_scale;
jbe@356 144 }
jbe@356 145 }
jbe@356 146 }
jbe@372 147 // add scale*score_per_step to each candidates score:
jbe@356 148 for (i=0; i<candidate_count; i++) {
jbe@356 149 if (candidates[i].score_per_step > 0.0) {
jbe@370 150 double max_scale;
jbe@370 151 max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step;
jbe@370 152 if (max_scale == scale) {
jbe@372 153 // score of 1.0 should be reached, so we set score directly to avoid floating point errors:
jbe@356 154 candidates[i].score = 1.0;
jbe@356 155 remaining--;
jbe@356 156 } else {
jbe@356 157 candidates[i].score += scale * candidates[i].score_per_step;
jbe@356 158 if (candidates[i].score >= 1.0) remaining--;
jbe@356 159 }
jbe@373 160 // when there is only one candidate remaining, then break inner (and thus outer) loop:
jbe@356 161 if (remaining <= 1) break;
jbe@356 162 }
jbe@356 163 }
jbe@356 164 }
jbe@372 165 // return remaining candidate:
jbe@372 166 for (i=0; i<candidate_count; i++) {
jbe@356 167 if (candidates[i].score < 1.0 && !candidates[i].seat) return candidates+i;
jbe@356 168 }
jbe@372 169 // if there is no remaining candidate, then something went wrong:
jbe@356 170 fprintf(stderr, "No remaining candidate (should not happen).");
jbe@356 171 abort();
jbe@356 172 }
jbe@356 173
jbe@372 174 // write results to database:
jbe@359 175 static int write_ranks(PGconn *db, char *escaped_initiative_id, int final) {
jbe@358 176 PGresult *res;
jbe@358 177 char *cmd;
jbe@358 178 int i;
jbe@359 179 if (final) {
jbe@371 180 if (asprintf(&cmd, "BEGIN; UPDATE \"initiative\" SET \"final_suggestion_order_calculated\" = TRUE WHERE \"id\" = %s; UPDATE \"suggestion\" SET \"proportional_order\" = 0 WHERE \"initiative_id\" = %s", escaped_initiative_id, escaped_initiative_id) < 0) {
jbe@359 181 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@359 182 abort();
jbe@359 183 }
jbe@359 184 } else {
jbe@371 185 if (asprintf(&cmd, "BEGIN; UPDATE \"suggestion\" SET \"proportional_order\" = 0 WHERE \"initiative_id\" = %s", escaped_initiative_id) < 0) {
jbe@359 186 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@359 187 abort();
jbe@359 188 }
jbe@358 189 }
jbe@358 190 res = PQexec(db, cmd);
jbe@358 191 free(cmd);
jbe@358 192 if (!res) {
jbe@358 193 fprintf(stderr, "Error in pqlib while sending SQL command to initiate suggestion update.\n");
jbe@358 194 return 1;
jbe@358 195 } else if (
jbe@358 196 PQresultStatus(res) != PGRES_COMMAND_OK &&
jbe@358 197 PQresultStatus(res) != PGRES_TUPLES_OK
jbe@358 198 ) {
jbe@358 199 fprintf(stderr, "Error while executing SQL command to initiate suggestion update:\n%s", PQresultErrorMessage(res));
jbe@358 200 PQclear(res);
jbe@358 201 return 1;
jbe@358 202 } else {
jbe@358 203 PQclear(res);
jbe@358 204 }
jbe@358 205 for (i=0; i<candidate_count; i++) {
jbe@358 206 char *escaped_suggestion_id;
jbe@358 207 escaped_suggestion_id = escapeLiteral(db, candidates[i].key, strlen(candidates[i].key));
jbe@358 208 if (!escaped_suggestion_id) {
jbe@358 209 fprintf(stderr, "Could not escape literal in memory.\n");
jbe@358 210 abort();
jbe@358 211 }
jbe@358 212 if (asprintf(&cmd, "UPDATE \"suggestion\" SET \"proportional_order\" = %i WHERE \"id\" = %s", candidates[i].seat, escaped_suggestion_id) < 0) {
jbe@358 213 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@358 214 abort();
jbe@358 215 }
jbe@358 216 freemem(escaped_suggestion_id);
jbe@358 217 res = PQexec(db, cmd);
jbe@358 218 free(cmd);
jbe@358 219 if (!res) {
jbe@358 220 fprintf(stderr, "Error in pqlib while sending SQL command to update suggestion order.\n");
jbe@358 221 } else if (
jbe@358 222 PQresultStatus(res) != PGRES_COMMAND_OK &&
jbe@358 223 PQresultStatus(res) != PGRES_TUPLES_OK
jbe@358 224 ) {
jbe@358 225 fprintf(stderr, "Error while executing SQL command to update suggestion order:\n%s", PQresultErrorMessage(res));
jbe@358 226 PQclear(res);
jbe@358 227 } else {
jbe@358 228 PQclear(res);
jbe@358 229 continue;
jbe@358 230 }
jbe@358 231 res = PQexec(db, "ROLLBACK");
jbe@358 232 if (res) PQclear(res);
jbe@358 233 return 1;
jbe@358 234 }
jbe@358 235 res = PQexec(db, "COMMIT");
jbe@358 236 if (!res) {
jbe@358 237 fprintf(stderr, "Error in pqlib while sending SQL command to commit transaction.\n");
jbe@358 238 return 1;
jbe@358 239 } else if (
jbe@358 240 PQresultStatus(res) != PGRES_COMMAND_OK &&
jbe@358 241 PQresultStatus(res) != PGRES_TUPLES_OK
jbe@358 242 ) {
jbe@358 243 fprintf(stderr, "Error while executing SQL command to commit transaction:\n%s", PQresultErrorMessage(res));
jbe@358 244 PQclear(res);
jbe@358 245 return 1;
jbe@358 246 } else {
jbe@358 247 PQclear(res);
jbe@358 248 return 0;
jbe@358 249 }
jbe@358 250 }
jbe@358 251
jbe@372 252 // calculate ordering of suggestions for an initiative and call write_ranks() to write it to database:
jbe@359 253 static int process_initiative(PGconn *db, PGresult *res, char *escaped_initiative_id, int final) {
jbe@372 254 int err; // variable to store an error condition (0 = success)
jbe@372 255 int ballot_count = 1; // number of ballots, must be initiatized to 1, due to loop below
jbe@372 256 struct ballot *ballots; // data structure containing the ballots
jbe@372 257 int i; // index variable for loops
jbe@372 258 // create candidates[] and ballots[] arrays:
jbe@355 259 {
jbe@372 260 void *candidate_tree = NULL; // temporary structure to create a sorted unique list of all candidate keys
jbe@372 261 int tuple_count; // number of tuples returned from the database
jbe@372 262 char *old_member_id = NULL; // old member_id to be able to detect a new ballot in loops
jbe@372 263 struct ballot *ballot; // pointer to current ballot
jbe@372 264 int candidates_in_sections[4] = {0, }; // number of candidates that have been placed in each section
jbe@372 265 // reset candidate count:
jbe@367 266 candidate_count = 0;
jbe@372 267 // determine number of tuples:
jbe@359 268 tuple_count = PQntuples(res);
jbe@372 269 // trivial case, when there are no tuples:
jbe@359 270 if (!tuple_count) {
jbe@359 271 if (final) {
jbe@374 272 if (logging) printf("No suggestions found, but marking initiative as finally calculated.\n");
jbe@359 273 err = write_ranks(db, escaped_initiative_id, final);
jbe@374 274 if (logging) printf("Done.\n");
jbe@359 275 return err;
jbe@359 276 } else {
jbe@374 277 if (logging) printf("Nothing to do.\n");
jbe@359 278 return 0;
jbe@359 279 }
jbe@359 280 }
jbe@372 281 // calculate ballot_count and generate set of candidate keys (suggestion_id is used as key):
jbe@366 282 for (i=0; i<tuple_count; i++) {
jbe@355 283 char *member_id, *suggestion_id;
jbe@366 284 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@366 285 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
jbe@369 286 if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)compare_id)) {
jbe@366 287 candidate_count++;
jbe@369 288 if (!tsearch(suggestion_id, &candidate_tree, (void *)compare_id)) {
jbe@366 289 fprintf(stderr, "Insufficient memory while inserting into candidate tree.\n");
jbe@366 290 abort();
jbe@352 291 }
jbe@352 292 }
jbe@366 293 if (old_member_id && strcmp(old_member_id, member_id)) ballot_count++;
jbe@355 294 old_member_id = member_id;
jbe@352 295 }
jbe@372 296 // print candidate count:
jbe@374 297 if (logging) printf("Candidate count: %i\n", candidate_count);
jbe@372 298 // allocate memory for candidates[] array:
jbe@355 299 candidates = malloc(candidate_count * sizeof(struct candidate));
jbe@355 300 if (!candidates) {
jbe@356 301 fprintf(stderr, "Insufficient memory while creating candidate list.\n");
jbe@353 302 abort();
jbe@352 303 }
jbe@372 304 // transform tree of candidate keys into sorted array:
jbe@372 305 candidate_count = 0; // needed by register_candidate()
jbe@355 306 twalk(candidate_tree, (void *)register_candidate);
jbe@372 307 // free memory of tree structure (tdestroy() is not available on all platforms):
jbe@369 308 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)compare_id);
jbe@372 309 // print ballot count:
jbe@374 310 if (logging) printf("Ballot count: %i\n", ballot_count);
jbe@372 311 // allocate memory for ballots[] array:
jbe@355 312 ballots = calloc(ballot_count, sizeof(struct ballot));
jbe@355 313 if (!ballots) {
jbe@356 314 fprintf(stderr, "Insufficient memory while creating ballot list.\n");
jbe@353 315 abort();
jbe@352 316 }
jbe@372 317 // set ballot weights, determine ballot section sizes, and verify preference values:
jbe@355 318 ballot = ballots;
jbe@365 319 old_member_id = NULL;
jbe@355 320 for (i=0; i<tuple_count; i++) {
jbe@363 321 char *member_id;
jbe@355 322 int weight, preference;
jbe@352 323 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@355 324 weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
jbe@355 325 if (weight <= 0) {
jbe@356 326 fprintf(stderr, "Unexpected weight value.\n");
jbe@358 327 free(ballots);
jbe@358 328 free(candidates);
jbe@358 329 return 1;
jbe@355 330 }
jbe@352 331 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
jbe@352 332 if (preference < 1 || preference > 4) {
jbe@356 333 fprintf(stderr, "Unexpected preference value.\n");
jbe@358 334 free(ballots);
jbe@358 335 free(candidates);
jbe@358 336 return 1;
jbe@352 337 }
jbe@352 338 preference--;
jbe@366 339 if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
jbe@355 340 ballot->weight = weight;
jbe@355 341 ballot->sections[preference].count++;
jbe@355 342 old_member_id = member_id;
jbe@355 343 }
jbe@372 344 // allocate memory for ballot sections:
jbe@355 345 for (i=0; i<ballot_count; i++) {
jbe@355 346 int j;
jbe@355 347 for (j=0; j<4; j++) {
jbe@355 348 if (ballots[i].sections[j].count) {
jbe@355 349 ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
jbe@355 350 if (!ballots[i].sections[j].candidates) {
jbe@356 351 fprintf(stderr, "Insufficient memory while creating ballot section.\n");
jbe@355 352 abort();
jbe@355 353 }
jbe@355 354 }
jbe@355 355 }
jbe@352 356 }
jbe@372 357 // fill ballot sections with candidate references:
jbe@365 358 old_member_id = NULL;
jbe@355 359 ballot = ballots;
jbe@365 360 for (i=0; i<tuple_count; i++) {
jbe@355 361 char *member_id, *suggestion_id;
jbe@355 362 int preference;
jbe@366 363 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@366 364 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
jbe@366 365 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
jbe@366 366 preference--;
jbe@366 367 if (old_member_id && strcmp(old_member_id, member_id)) {
jbe@366 368 ballot++;
jbe@366 369 candidates_in_sections[0] = 0;
jbe@366 370 candidates_in_sections[1] = 0;
jbe@366 371 candidates_in_sections[2] = 0;
jbe@366 372 candidates_in_sections[3] = 0;
jbe@355 373 }
jbe@366 374 ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
jbe@355 375 old_member_id = member_id;
jbe@352 376 }
jbe@352 377 }
jbe@355 378
jbe@372 379 // calculate ranks based on constructed data structures:
jbe@356 380 for (i=0; i<candidate_count; i++) {
jbe@356 381 struct candidate *candidate = loser(i, ballots, ballot_count);
jbe@356 382 candidate->seat = candidate_count - i;
jbe@374 383 if (logging) printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key);
jbe@356 384 }
jbe@356 385
jbe@372 386 // free ballots[] array:
jbe@354 387 for (i=0; i<ballot_count; i++) {
jbe@354 388 int j;
jbe@354 389 for (j=0; j<4; j++) {
jbe@354 390 if (ballots[i].sections[j].count) {
jbe@354 391 free(ballots[i].sections[j].candidates);
jbe@354 392 }
jbe@354 393 }
jbe@354 394 }
jbe@354 395 free(ballots);
jbe@358 396
jbe@372 397 // write results to database:
jbe@361 398 if (final) {
jbe@374 399 if (logging) printf("Writing final ranks to database.\n");
jbe@361 400 } else {
jbe@374 401 if (logging) printf("Writing ranks to database.\n");
jbe@361 402 }
jbe@359 403 err = write_ranks(db, escaped_initiative_id, final);
jbe@374 404 if (logging) printf("Done.\n");
jbe@358 405
jbe@372 406 // free candidates[] array:
jbe@358 407 free(candidates);
jbe@358 408
jbe@372 409 // return error code of write_ranks() call
jbe@358 410 return err;
jbe@352 411 }
jbe@352 412
jbe@352 413 int main(int argc, char **argv) {
jbe@352 414
jbe@352 415 // variable declarations:
jbe@352 416 int err = 0;
jbe@352 417 int i, count;
jbe@352 418 char *conninfo;
jbe@352 419 PGconn *db;
jbe@352 420 PGresult *res;
jbe@352 421
jbe@352 422 // parse command line:
jbe@352 423 if (argc == 0) return 1;
jbe@352 424 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
jbe@352 425 FILE *out;
jbe@352 426 out = argc == 1 ? stderr : stdout;
jbe@362 427 fprintf(out, "\n");
jbe@374 428 fprintf(out, "Usage: %s [-v|--verbose] <conninfo>\n", argv[0]);
jbe@362 429 fprintf(out, "\n");
jbe@362 430 fprintf(out, "<conninfo> is specified by PostgreSQL's libpq,\n");
jbe@362 431 fprintf(out, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n");
jbe@362 432 fprintf(out, "\n");
jbe@362 433 fprintf(out, "Example: %s dbname=liquid_feedback\n", argv[0]);
jbe@362 434 fprintf(out, "\n");
jbe@352 435 return argc == 1 ? 1 : 0;
jbe@352 436 }
jbe@352 437 {
jbe@352 438 size_t len = 0;
jbe@374 439 int argb = 1;
jbe@374 440 if (
jbe@374 441 argc >= 2 &&
jbe@374 442 (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--verbose"))
jbe@374 443 ) {
jbe@374 444 argb = 2;
jbe@374 445 logging = 1;
jbe@374 446 }
jbe@374 447 for (i=argb; i<argc; i++) len += strlen(argv[i]) + 1;
jbe@352 448 conninfo = malloc(len * sizeof(char));
jbe@352 449 if (!conninfo) {
jbe@358 450 fprintf(stderr, "Error: Could not allocate memory for conninfo string.\n");
jbe@358 451 abort();
jbe@352 452 }
jbe@352 453 conninfo[0] = 0;
jbe@374 454 for (i=argb; i<argc; i++) {
jbe@374 455 if (i>argb) strcat(conninfo, " ");
jbe@352 456 strcat(conninfo, argv[i]);
jbe@352 457 }
jbe@352 458 }
jbe@352 459
jbe@352 460 // connect to database:
jbe@352 461 db = PQconnectdb(conninfo);
jbe@352 462 if (!db) {
jbe@358 463 fprintf(stderr, "Error: Could not create database handle.\n");
jbe@352 464 return 1;
jbe@352 465 }
jbe@352 466 if (PQstatus(db) != CONNECTION_OK) {
jbe@352 467 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
jbe@352 468 return 1;
jbe@352 469 }
jbe@352 470
jbe@352 471 // check initiatives:
jbe@352 472 res = PQexec(db, "SELECT \"initiative_id\", \"final\" FROM \"initiative_suggestion_order_calculation\"");
jbe@352 473 if (!res) {
jbe@358 474 fprintf(stderr, "Error in pqlib while sending SQL command selecting initiatives to process.\n");
jbe@352 475 err = 1;
jbe@352 476 } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
jbe@358 477 fprintf(stderr, "Error while executing SQL command selecting initiatives to process:\n%s", PQresultErrorMessage(res));
jbe@352 478 err = 1;
jbe@352 479 PQclear(res);
jbe@359 480 } else if (PQnfields(res) < 2) {
jbe@359 481 fprintf(stderr, "Too few columns returned by SQL command selecting initiatives to process.\n");
jbe@359 482 err = 1;
jbe@359 483 PQclear(res);
jbe@352 484 } else {
jbe@352 485 count = PQntuples(res);
jbe@374 486 if (logging) printf("Number of initiatives to process: %i\n", count);
jbe@352 487 for (i=0; i<count; i++) {
jbe@352 488 char *initiative_id, *escaped_initiative_id;
jbe@359 489 int final;
jbe@352 490 char *cmd;
jbe@352 491 PGresult *res2;
jbe@352 492 initiative_id = PQgetvalue(res, i, 0);
jbe@359 493 final = (PQgetvalue(res, i, 1)[0] == 't') ? 1 : 0;
jbe@374 494 if (logging) printf("Processing initiative_id: %s\n", initiative_id);
jbe@352 495 escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
jbe@358 496 if (!escaped_initiative_id) {
jbe@358 497 fprintf(stderr, "Could not escape literal in memory.\n");
jbe@358 498 abort();
jbe@358 499 }
jbe@352 500 if (asprintf(&cmd, "SELECT \"member_id\", \"weight\", \"preference\", \"suggestion_id\" FROM \"individual_suggestion_ranking\" WHERE \"initiative_id\" = %s ORDER BY \"member_id\", \"preference\"", escaped_initiative_id) < 0) {
jbe@352 501 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@355 502 abort();
jbe@352 503 }
jbe@352 504 res2 = PQexec(db, cmd);
jbe@352 505 free(cmd);
jbe@352 506 if (!res2) {
jbe@358 507 fprintf(stderr, "Error in pqlib while sending SQL command selecting individual suggestion rankings.\n");
jbe@352 508 err = 1;
jbe@352 509 } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
jbe@358 510 fprintf(stderr, "Error while executing SQL command selecting individual suggestion rankings:\n%s", PQresultErrorMessage(res));
jbe@352 511 err = 1;
jbe@352 512 PQclear(res2);
jbe@356 513 } else if (PQnfields(res2) < 4) {
jbe@358 514 fprintf(stderr, "Too few columns returned by SQL command selecting individual suggestion rankings.\n");
jbe@356 515 err = 1;
jbe@356 516 PQclear(res2);
jbe@352 517 } else {
jbe@359 518 if (process_initiative(db, res2, escaped_initiative_id, final)) err = 1;
jbe@352 519 PQclear(res2);
jbe@352 520 }
jbe@352 521 freemem(escaped_initiative_id);
jbe@352 522 }
jbe@352 523 PQclear(res);
jbe@352 524 }
jbe@352 525
jbe@375 526 // cleanup and exit:
jbe@352 527 PQfinish(db);
jbe@374 528 if (!err) {
jbe@374 529 if (logging) printf("Successfully terminated.\n");
jbe@374 530 } else {
jbe@374 531 fprintf(stderr, "Exiting with error code %i.\n", err);
jbe@374 532 }
jbe@352 533 return err;
jbe@352 534
jbe@352 535 }

Impressum / About Us