liquid_feedback_core
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/lf_update.c Tue Oct 27 12:00:00 2009 +0100 1.3 @@ -0,0 +1,103 @@ 1.4 +#include <stdlib.h> 1.5 +#include <stdio.h> 1.6 +#include <string.h> 1.7 +#include <libpq-fe.h> 1.8 + 1.9 +int main(int argc, char **argv) { 1.10 + int i, count; 1.11 + char *conninfo; 1.12 + PGconn *db; 1.13 + PGresult *list; 1.14 + PGresult *status; 1.15 + if (argc == 0) return 1; 1.16 + if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { 1.17 + FILE *out; 1.18 + out = argc == 1 ? stderr : stdout; 1.19 + fprintf(stdout, "\n"); 1.20 + fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]); 1.21 + fprintf(stdout, "\n"); 1.22 + fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n"); 1.23 + fprintf(stdout, "see http://www.postgresql.org/docs/8.4/static/libpq-connect.html\n"); 1.24 + fprintf(stdout, "\n"); 1.25 + fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]); 1.26 + fprintf(stdout, "\n"); 1.27 + return argc == 1 ? 1 : 0; 1.28 + } 1.29 + { 1.30 + size_t len = 0; 1.31 + for (i=1; i<argc; i++) len += strlen(argv[i]) + 1; 1.32 + conninfo = malloc(len * sizeof(char)); 1.33 + if (!conninfo) { 1.34 + fprintf(stderr, "Error: Could not allocate memory for conninfo string\n"); 1.35 + return 1; 1.36 + } 1.37 + conninfo[0] = 0; 1.38 + for (i=1; i<argc; i++) { 1.39 + if (i>1) strcat(conninfo, " "); 1.40 + strcat(conninfo, argv[i]); 1.41 + } 1.42 + } 1.43 + db = PQconnectdb(conninfo); 1.44 + if (!db) { 1.45 + fprintf(stderr, "Error: Could not create database handle\n"); 1.46 + return 1; 1.47 + } 1.48 + if (PQstatus(db) != CONNECTION_OK) { 1.49 + fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db)); 1.50 + return 1; 1.51 + } 1.52 + list = PQexec(db, "SELECT \"id\" FROM \"open_issue\""); 1.53 + if (!list) { 1.54 + fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n"); 1.55 + return 1; 1.56 + } 1.57 + if (PQresultStatus(list) != PGRES_TUPLES_OK) { 1.58 + fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(list)); 1.59 + return 1; 1.60 + } 1.61 + count = PQntuples(list); 1.62 + for (i=0; i<count; i++) { 1.63 + const char *params[1]; 1.64 + params[0] = PQgetvalue(list, i, 0); 1.65 + status = PQexecParams( 1.66 + db, "SELECT \"check_issue\"($1)", 1, NULL, params, NULL, NULL, 0 1.67 + ); 1.68 + if ( 1.69 + PQresultStatus(status) != PGRES_COMMAND_OK && 1.70 + PQresultStatus(status) != PGRES_TUPLES_OK 1.71 + ) { 1.72 + fprintf(stderr, "Error while calling SQL function \"check_issue\"(...):\n%s", PQresultErrorMessage(status)); 1.73 + return 1; 1.74 + } 1.75 + PQclear(status); 1.76 + } 1.77 + PQclear(list); 1.78 + list = PQexec(db, "SELECT \"id\" FROM \"issue_with_ranks_missing\""); 1.79 + if (!list) { 1.80 + fprintf(stderr, "Error in pqlib while sending SQL command selecting issues where ranks are missing\n"); 1.81 + return 1; 1.82 + } 1.83 + if (PQresultStatus(list) != PGRES_TUPLES_OK) { 1.84 + fprintf(stderr, "Error while executing SQL command selecting issues where ranks are missing:\n%s", PQresultErrorMessage(list)); 1.85 + return 1; 1.86 + } 1.87 + count = PQntuples(list); 1.88 + for (i=0; i<count; i++) { 1.89 + const char *params[1]; 1.90 + params[0] = PQgetvalue(list, i, 0); 1.91 + status = PQexecParams( 1.92 + db, "SELECT \"calculate_ranks\"($1)", 1, NULL, params, NULL, NULL, 0 1.93 + ); 1.94 + if ( 1.95 + PQresultStatus(status) != PGRES_COMMAND_OK && 1.96 + PQresultStatus(status) != PGRES_TUPLES_OK 1.97 + ) { 1.98 + fprintf(stderr, "Error while calling SQL function \"calculate_ranks\"(...):\n%s", PQresultErrorMessage(status)); 1.99 + return 1; 1.100 + } 1.101 + PQclear(status); 1.102 + } 1.103 + PQclear(list); 1.104 + PQfinish(db); 1.105 + return 0; 1.106 +}