liquid_feedback_core
view lf_update.c @ 14:ac7836ac00d9
Version beta15
Bugfix: Revoked initiatives will never be admitted
Possibility to recommend another initiative to support when revoking an initiative
Allow "initiator"."accepted" to be NULL (NULL means "no answer yet", while FALSE means "rejected")
Bugfix: Revoked initiatives will never be admitted
Possibility to recommend another initiative to support when revoking an initiative
Allow "initiator"."accepted" to be NULL (NULL means "no answer yet", while FALSE means "rejected")
author | jbe |
---|---|
date | Sun Jan 10 12:00:00 2010 +0100 (2010-01-10) |
parents | 6133c0a62378 |
children | bdccc56fb705 |
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 // calculate member counts:
71 status = PQexec(db, "SELECT \"calculate_member_counts\"()");
72 if (!status) {
73 fprintf(stderr, "Error in pqlib while sending SQL command calculating member counts\n");
74 return 1;
75 }
76 if (
77 PQresultStatus(status) != PGRES_COMMAND_OK &&
78 PQresultStatus(status) != PGRES_TUPLES_OK
79 ) {
80 fprintf(stderr, "Error while executing SQL command calculating member counts:\n%s", PQresultErrorMessage(status));
81 return 1;
82 }
84 // update open issues:
85 list = PQexec(db, "SELECT \"id\" FROM \"open_issue\"");
86 if (!list) {
87 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
88 return 1;
89 }
90 if (PQresultStatus(list) != PGRES_TUPLES_OK) {
91 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(list));
92 return 1;
93 }
94 count = PQntuples(list);
95 for (i=0; i<count; i++) {
96 const char *params[1];
97 params[0] = PQgetvalue(list, i, 0);
98 status = PQexecParams(
99 db, "SELECT \"check_issue\"($1)", 1, NULL, params, NULL, NULL, 0
100 );
101 if (!status) {
102 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"check_issue\"(...):\n");
103 return 1;
104 }
105 if (
106 PQresultStatus(status) != PGRES_COMMAND_OK &&
107 PQresultStatus(status) != PGRES_TUPLES_OK
108 ) {
109 fprintf(stderr, "Error while calling SQL function \"check_issue\"(...):\n%s", PQresultErrorMessage(status));
110 return 1;
111 }
112 PQclear(status);
113 }
114 PQclear(list);
116 // calculate ranks after voting is finished:
117 // (NOTE: This is a seperate process to avoid long transactions with locking)
118 list = PQexec(db, "SELECT \"id\" FROM \"issue_with_ranks_missing\"");
119 if (!list) {
120 fprintf(stderr, "Error in pqlib while sending SQL command selecting issues where ranks are missing\n");
121 return 1;
122 }
123 if (PQresultStatus(list) != PGRES_TUPLES_OK) {
124 fprintf(stderr, "Error while executing SQL command selecting issues where ranks are missing:\n%s", PQresultErrorMessage(list));
125 return 1;
126 }
127 count = PQntuples(list);
128 for (i=0; i<count; i++) {
129 const char *params[1];
130 params[0] = PQgetvalue(list, i, 0);
131 status = PQexecParams(
132 db, "SELECT \"calculate_ranks\"($1)", 1, NULL, params, NULL, NULL, 0
133 );
134 if (!status) {
135 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"calculate_ranks\"(...):\n");
136 return 1;
137 }
138 if (
139 PQresultStatus(status) != PGRES_COMMAND_OK &&
140 PQresultStatus(status) != PGRES_TUPLES_OK
141 ) {
142 fprintf(stderr, "Error while calling SQL function \"calculate_ranks\"(...):\n%s", PQresultErrorMessage(status));
143 return 1;
144 }
145 PQclear(status);
146 }
147 PQclear(list);
149 // cleanup and exit
150 PQfinish(db);
151 return 0;
153 }