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