liquid_feedback_core

annotate lf_update.c @ 1:23092eb00e16

Version beta2

Serious bugfix in SQL function create_snapshot(...), which caused wrong counting of opinions on suggestions

lf_update now deletes expired sessions

Redundancy in SQL function check_everything() removed by using existent views
author jbe
date Tue Nov 03 12:00:00 2009 +0100 (2009-11-03)
parents 8d021cb5eaf4
children 6133c0a62378
rev   line source
jbe@0 1 #include <stdlib.h>
jbe@0 2 #include <stdio.h>
jbe@0 3 #include <string.h>
jbe@0 4 #include <libpq-fe.h>
jbe@0 5
jbe@0 6 int main(int argc, char **argv) {
jbe@1 7
jbe@1 8 // variable declarations:
jbe@0 9 int i, count;
jbe@0 10 char *conninfo;
jbe@0 11 PGconn *db;
jbe@0 12 PGresult *list;
jbe@0 13 PGresult *status;
jbe@1 14
jbe@1 15 // parse command line:
jbe@0 16 if (argc == 0) return 1;
jbe@0 17 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
jbe@0 18 FILE *out;
jbe@0 19 out = argc == 1 ? stderr : stdout;
jbe@0 20 fprintf(stdout, "\n");
jbe@0 21 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
jbe@0 22 fprintf(stdout, "\n");
jbe@0 23 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
jbe@0 24 fprintf(stdout, "see http://www.postgresql.org/docs/8.4/static/libpq-connect.html\n");
jbe@0 25 fprintf(stdout, "\n");
jbe@0 26 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
jbe@0 27 fprintf(stdout, "\n");
jbe@0 28 return argc == 1 ? 1 : 0;
jbe@0 29 }
jbe@0 30 {
jbe@0 31 size_t len = 0;
jbe@0 32 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
jbe@0 33 conninfo = malloc(len * sizeof(char));
jbe@0 34 if (!conninfo) {
jbe@0 35 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
jbe@0 36 return 1;
jbe@0 37 }
jbe@0 38 conninfo[0] = 0;
jbe@0 39 for (i=1; i<argc; i++) {
jbe@0 40 if (i>1) strcat(conninfo, " ");
jbe@0 41 strcat(conninfo, argv[i]);
jbe@0 42 }
jbe@0 43 }
jbe@1 44
jbe@1 45 // connect to database:
jbe@0 46 db = PQconnectdb(conninfo);
jbe@0 47 if (!db) {
jbe@0 48 fprintf(stderr, "Error: Could not create database handle\n");
jbe@0 49 return 1;
jbe@0 50 }
jbe@0 51 if (PQstatus(db) != CONNECTION_OK) {
jbe@0 52 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
jbe@0 53 return 1;
jbe@0 54 }
jbe@1 55
jbe@1 56 // delete expired sessions:
jbe@1 57 status = PQexec(db, "DELETE FROM \"expired_session\"");
jbe@1 58 if (!status) {
jbe@1 59 fprintf(stderr, "Error in pqlib while sending SQL command deleting expired sessions\n");
jbe@1 60 return 1;
jbe@1 61 }
jbe@1 62 if (
jbe@1 63 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@1 64 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@1 65 ) {
jbe@1 66 fprintf(stderr, "Error while executing SQL command deleting expired sessions:\n%s", PQresultErrorMessage(status));
jbe@1 67 return 1;
jbe@1 68 }
jbe@1 69
jbe@1 70 // update open issues:
jbe@0 71 list = PQexec(db, "SELECT \"id\" FROM \"open_issue\"");
jbe@0 72 if (!list) {
jbe@0 73 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
jbe@0 74 return 1;
jbe@0 75 }
jbe@0 76 if (PQresultStatus(list) != PGRES_TUPLES_OK) {
jbe@0 77 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(list));
jbe@0 78 return 1;
jbe@0 79 }
jbe@0 80 count = PQntuples(list);
jbe@0 81 for (i=0; i<count; i++) {
jbe@0 82 const char *params[1];
jbe@0 83 params[0] = PQgetvalue(list, i, 0);
jbe@0 84 status = PQexecParams(
jbe@0 85 db, "SELECT \"check_issue\"($1)", 1, NULL, params, NULL, NULL, 0
jbe@0 86 );
jbe@1 87 if (!status) {
jbe@1 88 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"check_issue\"(...):\n");
jbe@1 89 return 1;
jbe@1 90 }
jbe@0 91 if (
jbe@0 92 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@0 93 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@0 94 ) {
jbe@0 95 fprintf(stderr, "Error while calling SQL function \"check_issue\"(...):\n%s", PQresultErrorMessage(status));
jbe@0 96 return 1;
jbe@0 97 }
jbe@0 98 PQclear(status);
jbe@0 99 }
jbe@0 100 PQclear(list);
jbe@1 101
jbe@1 102 // calculate ranks after voting is finished:
jbe@1 103 // (NOTE: This is a seperate process to avoid long transactions with locking)
jbe@0 104 list = PQexec(db, "SELECT \"id\" FROM \"issue_with_ranks_missing\"");
jbe@0 105 if (!list) {
jbe@0 106 fprintf(stderr, "Error in pqlib while sending SQL command selecting issues where ranks are missing\n");
jbe@0 107 return 1;
jbe@0 108 }
jbe@0 109 if (PQresultStatus(list) != PGRES_TUPLES_OK) {
jbe@0 110 fprintf(stderr, "Error while executing SQL command selecting issues where ranks are missing:\n%s", PQresultErrorMessage(list));
jbe@0 111 return 1;
jbe@0 112 }
jbe@0 113 count = PQntuples(list);
jbe@0 114 for (i=0; i<count; i++) {
jbe@0 115 const char *params[1];
jbe@0 116 params[0] = PQgetvalue(list, i, 0);
jbe@0 117 status = PQexecParams(
jbe@0 118 db, "SELECT \"calculate_ranks\"($1)", 1, NULL, params, NULL, NULL, 0
jbe@0 119 );
jbe@1 120 if (!status) {
jbe@1 121 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"calculate_ranks\"(...):\n");
jbe@1 122 return 1;
jbe@1 123 }
jbe@0 124 if (
jbe@0 125 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@0 126 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@0 127 ) {
jbe@0 128 fprintf(stderr, "Error while calling SQL function \"calculate_ranks\"(...):\n%s", PQresultErrorMessage(status));
jbe@0 129 return 1;
jbe@0 130 }
jbe@0 131 PQclear(status);
jbe@0 132 }
jbe@0 133 PQclear(list);
jbe@1 134
jbe@1 135 // cleanup and exit
jbe@0 136 PQfinish(db);
jbe@0 137 return 0;
jbe@1 138
jbe@0 139 }

Impressum / About Us