liquid_feedback_core

view lf_update.c @ 0:8d021cb5eaf4

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

Impressum / About Us