liquid_feedback_core

view lf_update.c @ 2:d45919d791ff

Version beta3

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

Impressum / About Us