liquid_feedback_core

view lf_update.c @ 88:dfa00eaa5081

Added tag v1.2.9 for changeset e588fdf1676e
author jbe
date Tue Oct 26 22:25:47 2010 +0200 (2010-10-26)
parents bdccc56fb705
children bc8aa59b0945
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 err = 0;
10 int i, count;
11 char *conninfo;
12 PGconn *db;
13 PGresult *list;
14 PGresult *status;
16 // parse command line:
17 if (argc == 0) return 1;
18 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
19 FILE *out;
20 out = argc == 1 ? stderr : stdout;
21 fprintf(stdout, "\n");
22 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
23 fprintf(stdout, "\n");
24 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
25 fprintf(stdout, "see http://www.postgresql.org/docs/8.4/static/libpq-connect.html\n");
26 fprintf(stdout, "\n");
27 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
28 fprintf(stdout, "\n");
29 return argc == 1 ? 1 : 0;
30 }
31 {
32 size_t len = 0;
33 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
34 conninfo = malloc(len * sizeof(char));
35 if (!conninfo) {
36 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
37 return 1;
38 }
39 conninfo[0] = 0;
40 for (i=1; i<argc; i++) {
41 if (i>1) strcat(conninfo, " ");
42 strcat(conninfo, argv[i]);
43 }
44 }
46 // connect to database:
47 db = PQconnectdb(conninfo);
48 if (!db) {
49 fprintf(stderr, "Error: Could not create database handle\n");
50 return 1;
51 }
52 if (PQstatus(db) != CONNECTION_OK) {
53 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
54 return 1;
55 }
57 // delete expired sessions:
58 status = PQexec(db, "DELETE FROM \"expired_session\"");
59 if (!status) {
60 fprintf(stderr, "Error in pqlib while sending SQL command deleting expired sessions\n");
61 err = 1;
62 } else 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 err = 1;
68 PQclear(status);
69 } else {
70 PQclear(status);
71 }
73 // calculate member counts:
74 status = PQexec(db, "SELECT \"calculate_member_counts\"()");
75 if (!status) {
76 fprintf(stderr, "Error in pqlib while sending SQL command calculating member counts\n");
77 err = 1;
78 } else if (
79 PQresultStatus(status) != PGRES_COMMAND_OK &&
80 PQresultStatus(status) != PGRES_TUPLES_OK
81 ) {
82 fprintf(stderr, "Error while executing SQL command calculating member counts:\n%s", PQresultErrorMessage(status));
83 err = 1;
84 PQclear(status);
85 } else {
86 PQclear(status);
87 }
89 // update open issues:
90 list = PQexec(db, "SELECT \"id\" FROM \"open_issue\"");
91 if (!list) {
92 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
93 err = 1;
94 } else if (PQresultStatus(list) != PGRES_TUPLES_OK) {
95 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(list));
96 err = 1;
97 PQclear(list);
98 } else {
99 count = PQntuples(list);
100 for (i=0; i<count; i++) {
101 const char *params[1];
102 params[0] = PQgetvalue(list, i, 0);
103 status = PQexecParams(
104 db, "SELECT \"check_issue\"($1)", 1, NULL, params, NULL, NULL, 0
105 );
106 if (!status) {
107 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"check_issue\"(...):\n");
108 err = 1;
109 } else if (
110 PQresultStatus(status) != PGRES_COMMAND_OK &&
111 PQresultStatus(status) != PGRES_TUPLES_OK
112 ) {
113 fprintf(stderr, "Error while calling SQL function \"check_issue\"(...):\n%s", PQresultErrorMessage(status));
114 err = 1;
115 PQclear(status);
116 } else {
117 PQclear(status);
118 }
119 }
120 PQclear(list);
121 }
123 // calculate ranks after voting is finished:
124 // (NOTE: This is a seperate process to avoid long transactions with locking)
125 list = PQexec(db, "SELECT \"id\" FROM \"issue_with_ranks_missing\"");
126 if (!list) {
127 fprintf(stderr, "Error in pqlib while sending SQL command selecting issues where ranks are missing\n");
128 err = 1;
129 } else if (PQresultStatus(list) != PGRES_TUPLES_OK) {
130 fprintf(stderr, "Error while executing SQL command selecting issues where ranks are missing:\n%s", PQresultErrorMessage(list));
131 err = 1;
132 PQclear(list);
133 } else {
134 count = PQntuples(list);
135 for (i=0; i<count; i++) {
136 const char *params[1];
137 params[0] = PQgetvalue(list, i, 0);
138 status = PQexecParams(
139 db, "SELECT \"calculate_ranks\"($1)", 1, NULL, params, NULL, NULL, 0
140 );
141 if (!status) {
142 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"calculate_ranks\"(...):\n");
143 err = 1;
144 } else if (
145 PQresultStatus(status) != PGRES_COMMAND_OK &&
146 PQresultStatus(status) != PGRES_TUPLES_OK
147 ) {
148 fprintf(stderr, "Error while calling SQL function \"calculate_ranks\"(...):\n%s", PQresultErrorMessage(status));
149 err = 1;
150 PQclear(status);
151 } else {
152 PQclear(status);
153 }
154 }
155 PQclear(list);
156 }
158 // cleanup and exit
159 PQfinish(db);
160 return err;
162 }

Impressum / About Us