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@0
|
7 int i, count;
|
jbe@0
|
8 char *conninfo;
|
jbe@0
|
9 PGconn *db;
|
jbe@0
|
10 PGresult *list;
|
jbe@0
|
11 PGresult *status;
|
jbe@0
|
12 if (argc == 0) return 1;
|
jbe@0
|
13 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
|
jbe@0
|
14 FILE *out;
|
jbe@0
|
15 out = argc == 1 ? stderr : stdout;
|
jbe@0
|
16 fprintf(stdout, "\n");
|
jbe@0
|
17 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
|
jbe@0
|
18 fprintf(stdout, "\n");
|
jbe@0
|
19 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
|
jbe@0
|
20 fprintf(stdout, "see http://www.postgresql.org/docs/8.4/static/libpq-connect.html\n");
|
jbe@0
|
21 fprintf(stdout, "\n");
|
jbe@0
|
22 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
|
jbe@0
|
23 fprintf(stdout, "\n");
|
jbe@0
|
24 return argc == 1 ? 1 : 0;
|
jbe@0
|
25 }
|
jbe@0
|
26 {
|
jbe@0
|
27 size_t len = 0;
|
jbe@0
|
28 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
|
jbe@0
|
29 conninfo = malloc(len * sizeof(char));
|
jbe@0
|
30 if (!conninfo) {
|
jbe@0
|
31 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
|
jbe@0
|
32 return 1;
|
jbe@0
|
33 }
|
jbe@0
|
34 conninfo[0] = 0;
|
jbe@0
|
35 for (i=1; i<argc; i++) {
|
jbe@0
|
36 if (i>1) strcat(conninfo, " ");
|
jbe@0
|
37 strcat(conninfo, argv[i]);
|
jbe@0
|
38 }
|
jbe@0
|
39 }
|
jbe@0
|
40 db = PQconnectdb(conninfo);
|
jbe@0
|
41 if (!db) {
|
jbe@0
|
42 fprintf(stderr, "Error: Could not create database handle\n");
|
jbe@0
|
43 return 1;
|
jbe@0
|
44 }
|
jbe@0
|
45 if (PQstatus(db) != CONNECTION_OK) {
|
jbe@0
|
46 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
|
jbe@0
|
47 return 1;
|
jbe@0
|
48 }
|
jbe@0
|
49 list = PQexec(db, "SELECT \"id\" FROM \"open_issue\"");
|
jbe@0
|
50 if (!list) {
|
jbe@0
|
51 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
|
jbe@0
|
52 return 1;
|
jbe@0
|
53 }
|
jbe@0
|
54 if (PQresultStatus(list) != PGRES_TUPLES_OK) {
|
jbe@0
|
55 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(list));
|
jbe@0
|
56 return 1;
|
jbe@0
|
57 }
|
jbe@0
|
58 count = PQntuples(list);
|
jbe@0
|
59 for (i=0; i<count; i++) {
|
jbe@0
|
60 const char *params[1];
|
jbe@0
|
61 params[0] = PQgetvalue(list, i, 0);
|
jbe@0
|
62 status = PQexecParams(
|
jbe@0
|
63 db, "SELECT \"check_issue\"($1)", 1, NULL, params, NULL, NULL, 0
|
jbe@0
|
64 );
|
jbe@0
|
65 if (
|
jbe@0
|
66 PQresultStatus(status) != PGRES_COMMAND_OK &&
|
jbe@0
|
67 PQresultStatus(status) != PGRES_TUPLES_OK
|
jbe@0
|
68 ) {
|
jbe@0
|
69 fprintf(stderr, "Error while calling SQL function \"check_issue\"(...):\n%s", PQresultErrorMessage(status));
|
jbe@0
|
70 return 1;
|
jbe@0
|
71 }
|
jbe@0
|
72 PQclear(status);
|
jbe@0
|
73 }
|
jbe@0
|
74 PQclear(list);
|
jbe@0
|
75 list = PQexec(db, "SELECT \"id\" FROM \"issue_with_ranks_missing\"");
|
jbe@0
|
76 if (!list) {
|
jbe@0
|
77 fprintf(stderr, "Error in pqlib while sending SQL command selecting issues where ranks are missing\n");
|
jbe@0
|
78 return 1;
|
jbe@0
|
79 }
|
jbe@0
|
80 if (PQresultStatus(list) != PGRES_TUPLES_OK) {
|
jbe@0
|
81 fprintf(stderr, "Error while executing SQL command selecting issues where ranks are missing:\n%s", PQresultErrorMessage(list));
|
jbe@0
|
82 return 1;
|
jbe@0
|
83 }
|
jbe@0
|
84 count = PQntuples(list);
|
jbe@0
|
85 for (i=0; i<count; i++) {
|
jbe@0
|
86 const char *params[1];
|
jbe@0
|
87 params[0] = PQgetvalue(list, i, 0);
|
jbe@0
|
88 status = PQexecParams(
|
jbe@0
|
89 db, "SELECT \"calculate_ranks\"($1)", 1, NULL, params, NULL, NULL, 0
|
jbe@0
|
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 \"calculate_ranks\"(...):\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@0
|
101 PQfinish(db);
|
jbe@0
|
102 return 0;
|
jbe@0
|
103 }
|