diff --git a/src/Makefile b/src/Makefile index b86edf5..9c6095c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,5 @@ #This is the makefile -USEPOSTGRESQL=0 USEDBIXX=1 -USESTDOUT=0 BASE_LIBS=-lgcrypt BASE_CFLAGS=-c -g -O0 @@ -10,16 +8,6 @@ O_FILES=build/oastat.o build/oastatstruct.o build/kill2db.o build/init2db.o buil H_FILES=local.h oastatstruct.h oss2db/struct2db.h -ifeq ($(USESTDOUT),1) - O_FILES+= build/db2stdout.o - BASE_CFLAGS+= -DUSESTDOUT=1 -endif - -ifeq ($(USEPOSTGRESQL),1) - BASE_LIBS+= -lpq - O_FILES+= build/db2postgresql.o - BASE_CFLAGS+= -DUSEPOSTGRESQL=1 -endif ifeq ($(USEDBIXX),1) ifeq ($(wildcard /usr/local/lib/libdbixx.so),) @@ -45,16 +33,6 @@ build/oastat: $(O_FILES) build/oastat.o: oastat.cpp g++ $(BASE_CFLAGS) oastat.cpp -o build/oastat.o -ifeq ($(USESTDOUT),1) -build/db2stdout.o: db/db2stdout.cpp db/db2stdout.h db/database.hpp - g++ $(BASE_CFLAGS) db/db2stdout.cpp -o build/db2stdout.o -endif - -ifeq ($(USEPOSTGRESQL),1) -build/db2postgresql.o: db/Db2PostgreSQL.cpp db/Db2PostgreSQL.h db/database.hpp - g++ $(BASE_CFLAGS) db/Db2PostgreSQL.cpp -o build/db2postgresql.o -endif - ifeq ($(USEDBIXX),1) build/db2dbixx.o: db/Db2DbiXX.cpp db/Db2DbiXX.hpp db/database.hpp g++ $(BASE_CFLAGS) db/Db2DbiXX.cpp -o build/db2dbixx.o diff --git a/src/db/Db2PostgreSQL.cpp b/src/db/Db2PostgreSQL.cpp deleted file mode 100644 index a0aec81..0000000 --- a/src/db/Db2PostgreSQL.cpp +++ /dev/null @@ -1,226 +0,0 @@ -/* -=========================================================================== -oastat - a program for parsing log files and write the result to a database -Copyright (C) 2012 Poul Sander - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see http://www.gnu.org/licenses/ - -Source information and contacts persons can be found at -http://code.google.com/p/oastat/ -=========================================================================== -*/ - -#include "Db2PostgreSQL.h" -#include - - -#define STARTGAME "INSERT INTO oastat_games(gametype, mapname, basegame) VALUES (%i,'%s','%s')" -#define PLAYERSINSERT "INSERT INTO oastat_players(guid,nickname,lastseen,isBot, model, headmodel) VALUES ('%s','%s',now(),%s,'%s','%s')" -#define PLAYERSUPDATE "UPDATE oastat_players SET nickname = '%s',lastseen = now(),isBot = %s, model = '%s', headmodel = '%s' WHERE guid = '%s'" -#define USERINFOINSERT "INSERT INTO oastat_userinfo(gamenumber,second,guid,team,model,skill) VALUES (%i,%i,'%s',%i,'%s',%i)" -#define ENDGAME "UPDATE oastat_games SET second=%i,time = now() where gamenumber = %i" -#define KILL "INSERT INTO oastat_kills(gamenumber,second,attacker,target,modtype) VALUES(%i,%i,'%s','%s',%i)" -#define CAPTURE "INSERT INTO oastat_captures(gamenumber,second,player,team) VALUES (%i,%i,'%s',%i)" -#define AWARD "INSERT INTO oastat_awards(gamenumber,second,player,award) VALUES (%i,%i,'%s',%i)" - -#define PREP1 "PREPARE addkill(int,int,text,text,int) AS INSERT INTO oastat_kills(gamenumber,second,attacker,target,modtype) VALUES($1,$2,$3,$4,$5)" -#define PREP2 "PREPARE addgame(int,text,text) AS INSERT INTO oastat_games(gametype, mapname, basegame) VALUES ($1,$2,$3)" -#define PREP3 "PREPARE endgame(int,int) AS UPDATE oastat_games SET second=$2,time = now() where gamenumber = $1" -#define PREP4 "PREPARE addplayer(text,text,boolean,text,text) AS INSERT INTO oastat_players(guid,nickname,lastseen,isBot, model, headmodel) VALUES ($1,$2,now(),$3,$4,$5)" -#define PREP5 "PREPARE updateplayer(text,text,boolean,text,text) AS UPDATE oastat_players SET nickname = $2,lastseen = now(),isBot = $3, model = $4, headmodel = $5 WHERE guid = $6" -#define PREP6 "PREPARE adduserinfo(int,int,text,int,text,int) AS INSERT INTO oastat_userinfo(gamenumber,second,guid,team,model,skill) VALUES ($1,$2,$3,$4,$5,$6)" -#define PREP7 "PREPARE addcapture(int,int,text,int) AS INSERT INTO oastat_captures(gamenumber,second,player,team) VALUES ($1,$2,$3,$4)" -#define PREP8 "PREPARE addaward(int,int,text,int) AS INSERT INTO oastat_awards(gamenumber,second,player,award) VALUES ($1,$2,$3,$4)" - -static char* booltext[2] = {"false","true"}; - -/** - * Escapes special chaecters. The best solution should be to use PQescapeStringConn - * but unfortunatly I had limited success likely because the C++ string class - * already handles encoding. The only char postgreSql can be cheated by is the - * single quete, so that is the only one we removes. - * - * @param unescaped string - * @return escaped string - */ -string Db2PostgreSQL::sqlescape(string sql) -{ -#if 1 - string output = ""; - string::iterator itr; - for(itr = sql.begin(); itr=0 if succes and/or >0 if returned tubles - */ -int Db2PostgreSQL::simpleQuery(const char* query) -{ - res = PQexec(conn, query_string); - int ret; - if (PQresultStatus(res) != PGRES_COMMAND_OK) - { - if (PQresultStatus(res) == PGRES_TUPLES_OK ) - { - //If a SELECT STATEMENT count results - ret = PQntuples(res); - PQclear(res); - return ret; - } - PQclear(res); - cout << "FAIL: " << query << endl; - return -1; - } - PQclear(res); //We don't relly care about the result - cout << "SUCCES: " << query << endl; - return 0; -} - -Db2PostgreSQL::Db2PostgreSQL() -{ - conn = PQconnectdb("dbname=oastat"); //Connect to the database - if (PQstatus(conn) == CONNECTION_BAD) - throw "Failed to open connection to DB!"; - gamenumber = -1; - cout << "CONNECTED" << endl; -} - -Db2PostgreSQL::Db2PostgreSQL(string args) -{ - conn = PQconnectdb(args.c_str()); //Connect to the database - if (PQstatus(conn) == CONNECTION_BAD) - throw "Failed to open connection to DB!"; - cout << "CONNECTED" << endl; -} - -Db2PostgreSQL::Db2PostgreSQL(const Db2PostgreSQL& orig) -{ - throw "May not make copy of Db2PostgreSQL"; -} - -Db2PostgreSQL::~Db2PostgreSQL() -{ - PQfinish(conn); -} - -void Db2PostgreSQL::createTables() -{ - //Nothing to do -} - -void Db2PostgreSQL::startGame(int gametype, string mapname, string basegame) -{ - simpleQuery("ROLLBACK"); //In case we have just completed warmup or - simpleQuery("BEGIN"); - //simpleQuery("LOCK TABLE OASTAT_GAMES IN SHARE ROW EXCLUSIVE"); //Hold a lock until we have selected the just inserted line - gamenumber = -1; //Prevent any following sql commands from completing until this is set! - sprintf(query_string,STARTGAME,gametype,mapname.c_str(),basegame.c_str()); - res = PQexec(conn, query_string); - if (PQresultStatus(res) != PGRES_COMMAND_OK) - { - PQclear(res); - cout << "FAIL: " << query_string << endl; - simpleQuery("ROLLBACK"); - return; - } - PQclear(res); - //res = PQexec(conn, "SELECT MAX(gamenumber) FROM OASTAT_GAMES"); - res = PQexec(conn, "SELECT currval('oastat_games_gamenumber_seq')"); - if (PQresultStatus(res) != PGRES_TUPLES_OK ) - { - PQclear(res); - simpleQuery("ROLLBACK"); - return; - } - gamenumber = atoi(PQgetvalue(res,0,0)); - cout << "GAME: " << PQgetvalue(res,0,0) << ":" << gamenumber << endl; - PQclear(res); - //simpleQuery("COMMIT"); //release lock - //simpleQuery("BEGIN"); //This is just for performace... -} - -int Db2PostgreSQL::getGameNumber() -{ - return gamenumber; //Only to standard out, normally this should be read from db!!! -} - -void Db2PostgreSQL::setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill) -{ - //If team < 0 then it is a disconnect event that need not to be in - if(team>-1) - { - /* - Note: There is a possible race condition here! - * There should be - */ - //simpleQuery("COMMIT"); - sprintf(query_string,"SELECT * FROM oastat_players WHERE GUID = '%s'",sqlescape(guid).c_str()); - if(simpleQuery(query_string)<1) - { - sprintf(query_string,PLAYERSINSERT,sqlescape(guid).c_str(),sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str()); - simpleQuery(query_string); - } - sprintf(query_string,PLAYERSUPDATE,sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str(),guid.c_str()); - simpleQuery(query_string); - //simpleQuery("BEGIN"); - } - sprintf(query_string,USERINFOINSERT,getGameNumber(),second,sqlescape(guid).c_str(),team,sqlescape(model).c_str(),skill); - simpleQuery(query_string); -} - -void Db2PostgreSQL::addKill(int second, string attackerID, string targetID, int type) -{ - sprintf(query_string,KILL,gamenumber,second,sqlescape(attackerID).c_str(),sqlescape(targetID).c_str(),type); - simpleQuery(query_string); -} - -void Db2PostgreSQL::endGame(int second) -{ - sprintf(query_string,ENDGAME,second,gamenumber); - simpleQuery(query_string); - simpleQuery("COMMIT"); //This is just for performace... -} - -void Db2PostgreSQL::addCapture(int second, string player, int team) -{ - sprintf(query_string,CAPTURE,gamenumber,second,sqlescape(player).c_str(),team); - simpleQuery(query_string); -} - -void Db2PostgreSQL::addAward(int second, string player, int award) -{ - sprintf(query_string,AWARD,gamenumber,second,sqlescape(player).c_str(),award); - simpleQuery(query_string); -} \ No newline at end of file diff --git a/src/db/Db2PostgreSQL.h b/src/db/Db2PostgreSQL.h deleted file mode 100644 index d220673..0000000 --- a/src/db/Db2PostgreSQL.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -=========================================================================== -oastat - a program for parsing log files and write the result to a database -Copyright (C) 2012 Poul Sander - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see http://www.gnu.org/licenses/ - -Source information and contacts persons can be found at -http://code.google.com/p/oastat/ -=========================================================================== -*/ - -#ifndef _DB2POSTGRESQL_H -#define _DB2POSTGRESQL_H - -#include "database.hpp" -#include "postgresql/libpq-fe.h" - -using namespace std; - -class Db2PostgreSQL : public Database -{ -public: - Db2PostgreSQL(); - Db2PostgreSQL(string args); - Db2PostgreSQL(const Db2PostgreSQL& orig); - virtual ~Db2PostgreSQL(); - void createTables(); - void startGame(int gametype, string mapname, string basegame); - void endGame(int second); - int getGameNumber(); - void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill); - void addKill(int second, string attackerID, string targetID, int type); - void addCapture(int second, string player, int team); - void addAward(int second, string player, int award); -private: - int simpleQuery(const char *); - string sqlescape(string sql); - PGconn *conn; - PGresult *res; - int gamenumber; - char query_string[4096]; -}; - -#endif /* _DB2POSTGRESQL_H */ - diff --git a/src/db/db2stdout.cpp b/src/db/db2stdout.cpp deleted file mode 100644 index 69acb01..0000000 --- a/src/db/db2stdout.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* -=========================================================================== -oastat - a program for parsing log files and write the result to a database -Copyright (C) 2012 Poul Sander - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see http://www.gnu.org/licenses/ - -Source information and contacts persons can be found at -http://code.google.com/p/oastat/ -=========================================================================== -*/ - -#include "db2stdout.h" -#include - -static int gamecount = 1; //Only to standard out, normally this should be read from db!!! - -/*Note: I use printf instead of cout because that is closest to the format that most databases expect. - Most databases want :1,:2 instead of %s,%s and no quetes but still*/ -#define STARTGAME "INSERT INTO oastat_games(gametype, mapname, basegame) VALUES (%i,'%s','%s');" -#define PLAYERSINSERT "INSERT INTO oastat_players(guid,nickname,lastseen,isBot, model, headmodel) VALUES ('%s','%s',now(),%s,'%s','%s');" -#define PLAYERSUPDATE "UPDATE oastat_players SET nickname = '%s',lastseen = now(),isBot = %s, model = '%s', headmodel = '%s' WHERE guid = '%s';" -#define USERINFOINSERT "INSERT INTO oastat_userinfo(gamenumber,second,guid,team,model,skill) VALUES (%i,%i,'%s',%i,'%s',%i);" -#define ENDGAME "UPDATE oastat_games SET second=%i,time = now() where gamenumber = %i;" -#define KILL "INSERT INTO oastat_kills(gamenumber,second,attacker,target,modtype) VALUES(%i,%i,'%s','%s',%i);" -#define CAPTURE "INSERT INTO oastat_captures(gamenumber,second,player,team) VALUES (%i,%i,'%s',%i);" -#define AWARD "INSERT INTO oastat_awards(gamenumber,second,player,award) VALUES (%i,%i,'%s',%i);" - -static char* booltext[2] = {"false","true"}; - -static string sqlescape(string sql) -{ - string output = ""; - string::iterator itr; - for(itr = sql.begin(); itr-1) - { - printf(PLAYERSINSERT,guid.c_str(),sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str()); - cout << endl; - printf(PLAYERSUPDATE,sqlescape(nickname).c_str(),booltext[isBot],sqlescape(model).c_str(),sqlescape(headmodel).c_str(),guid.c_str()); - cout << endl; - } - printf(USERINFOINSERT,getGameNumber(),second,guid.c_str(),team,sqlescape(model).c_str(),skill); - cout << endl; -} - -void DB2stdout::addKill(int second, string attackerID, string targetID, int type) -{ - printf(KILL,gamenumber,second,attackerID.c_str(),targetID.c_str(),type); - cout << endl; -} - -void DB2stdout::endGame(int second) -{ - printf(ENDGAME,second,gamenumber); - cout << endl; -} - -void DB2stdout::addCapture(int second, string player, int team) -{ - printf(CAPTURE,gamenumber,second,player.c_str(),team); - cout << endl; -} - -void DB2stdout::addAward(int second, string player, int award) -{ - printf(AWARD,gamenumber,second,player.c_str(),award); - cout << endl; -} - -void addCtf(int second, string player, int team, int event) -{ - cout << "missing" << endl; -} \ No newline at end of file diff --git a/src/db/db2stdout.h b/src/db/db2stdout.h deleted file mode 100644 index 640613e..0000000 --- a/src/db/db2stdout.h +++ /dev/null @@ -1,50 +0,0 @@ -/* -=========================================================================== -oastat - a program for parsing log files and write the result to a database -Copyright (C) 2012 Poul Sander - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see http://www.gnu.org/licenses/ - -Source information and contacts persons can be found at -http://code.google.com/p/oastat/ -=========================================================================== -*/ - -#ifndef _DB2STDOUT_H -#define _DB2STDOUT_H - -#include "database.hpp" - -using namespace std; - -class DB2stdout : public Database -{ -public: - DB2stdout(); - ~DB2stdout(); - void createTables(); - void startGame(int gametype, string mapname, string basegame); - void endGame(int second); - int getGameNumber(); - void setPlayerInfo(string guid, string nickname, bool isBot, int second, int team, string model, string headmodel, int skill); - void addKill(int second, string attackerID, string targetID, int type); - void addCapture(int second, string player, int team); - void addAward(int second, string player, int award); - void addCtf(int second, string player, int team, int event); -private: - int gamenumber; -}; - -#endif /* _DB2STDOUT_H */ - diff --git a/src/oastat.cpp b/src/oastat.cpp index 0a4ed5c..493a067 100644 --- a/src/oastat.cpp +++ b/src/oastat.cpp @@ -35,12 +35,6 @@ http://code.google.com/p/oastat/ using namespace std; #include "db/database.hpp" -#ifdef USESTDOUT -#include "db/db2stdout.h" -#endif -#ifdef USEPOSTGRESQL -#include "db/Db2PostgreSQL.h" -#endif #ifdef USEDBIXX #include "db/Db2DbiXX.hpp" #endif @@ -143,18 +137,7 @@ int main (int argc, const char* argv[]) try { db = NULL; -#if USESTDOUT - cout << "INFO: oastat " << VERSION << " ready to process data" << endl; - db = new DB2stdout(); -#endif - /*#if USEPOSTGRESQL - cout << "Using postgreSQL" << endl; - if(dbargs.length()<1) - db = new Db2PostgreSQL(); - else - db = new Db2PostgreSQL(dbargs); - #endif*/ #if USEDBIXX if(backend == "DbiXX")