liquid_feedback_core

annotate lf_update_issue_order.c @ 406:1821e7aee87f

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

Impressum / About Us