From 23b16ab5373c0869cd00eed3aa2b52fd0a04cbad Mon Sep 17 00:00:00 2001 From: Oliver Schinagl Date: Tue, 6 May 2008 12:01:18 +0000 Subject: --- reset/Makefile | 15 +++++++++ reset/reset | Bin 0 -> 13121 bytes reset/reset.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ reset/reset.h | 25 +++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 reset/Makefile create mode 100755 reset/reset create mode 100644 reset/reset.c create mode 100644 reset/reset.h diff --git a/reset/Makefile b/reset/Makefile new file mode 100644 index 0000000..afb19bd --- /dev/null +++ b/reset/Makefile @@ -0,0 +1,15 @@ +# House of mozart statistic reseter (c) 2003 Oliver +# -================================================- + +CC = gcc +CFLAGS += -O2 -I`/usr/local/pgsql/bin/pg_config --includedir` -L`/usr/local/pgsql/bin/pg_config --libdir` -lpq + +DESTDIR = /usr/local/bin +BIN = reset + +all: + $(CC) $(CFLAGS) reset.c -o $(BIN) + + +clean: + rm -rf *~ *.o $(BIN) diff --git a/reset/reset b/reset/reset new file mode 100755 index 0000000..f64b33c Binary files /dev/null and b/reset/reset differ diff --git a/reset/reset.c b/reset/reset.c new file mode 100644 index 0000000..b52f5c4 --- /dev/null +++ b/reset/reset.c @@ -0,0 +1,96 @@ +/* + * reset.c + * House of Mozart statistic database reset app (c) 2003 oliver + * + * GNU public license *FIXME* + */ + +#include "reset.h" +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int i; + int options; + int day; + int month; + int year; + PGconn *conn; + char SQL_query[512]; + PGresult *res; + + day = month = year = 0; + + conn = PQsetdbLogin(PGHOST, PGPORT, PGOPTIONS, PGTTY, DBNAME, LOGIN, PWD); + + for(;-1 != (options = getopt(argc, argv, "hdmya"));) { + if('h' == options) { + printf(LONGHELP); + } + if('d' == options) { + day = 23; + } + if('m' == options) { + month = 31; + } + if('y' == options) { + year = 1; + } + if('a' == options) { + day = 23; + month = 31; + } + } + + if(CONNECTION_BAD == PQstatus(conn)) { + fprintf(stderr, "Connection to database '%s' failed.\n %s", DBNAME, PQerrorMessage(conn)); + PQfinish(conn); + return 1; + } + + if(0 < day) { + for(i = 0; i <= day; i++) { + sprintf(SQL_query, "UPDATE counter_day SET \"%0.2d\" = 0;", i); + res = PQexec(conn, SQL_query); + + if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) { + printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn)); + PQclear(res); + PQfinish(conn); + return 1; + } + PQclear(res); + } + } + if(0 < month) { + for(i = 1; i <= month; i++) { + sprintf(SQL_query, "UPDATE counter_month SET \"%0.2d\" = 0;", i); + res = PQexec(conn, SQL_query); + + if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) { + printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn)); + PQclear(res); + PQfinish(conn); + return 1; + } + PQclear(res); + } + } + if(1 == year) { + sprintf(SQL_query, "INSERT INTO counter_year VALUES (counter_year.year +1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"); + res = PQexec(conn, SQL_query); + + if((NULL == res) || (PGRES_COMMAND_OK != PQresultStatus(res))) { + printf("Unable to execute query: %s\n %s\n", SQL_query, PQerrorMessage(conn)); + PQclear(res); + PQfinish(conn); + return 1; + } + PQclear(res); + } + PQfinish(conn); + + return 0; +} diff --git a/reset/reset.h b/reset/reset.h new file mode 100644 index 0000000..766d74c --- /dev/null +++ b/reset/reset.h @@ -0,0 +1,25 @@ +/* + * reset.h + * House of Mozart statistic database reset app (c) 2003 oliver + * + * GNU public license *FIXME* + */ + +#define VERSION 0.0.1 + +#define PGHOST "127.0.0.1" +#define PGPORT "5432" +#define PGOPTIONS "\0" +#define PGTTY "\0" +#define DBNAME "houseofmozart" +#define LOGIN "houseofmozart" +#define PWD "M0z4rT" + +#define SHORTHELP \ +"Not enough or incorrect arguments try -h for help\n" + +#define LONGHELP \ +" -h\tprint this help message\n \ + -d\tReset daily stats\n \ + -m\tReset monthly stats\n \ + -a\tReset All stats (month day)\n" -- cgit v0.12