From b70c9b6843e15ee5764b8457acea930af0d2b285 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 14 Sep 2007 12:25:54 +0000 Subject: Commits are now committed to the database. --- server/src/Makefile.am | 2 + server/src/database.cc | 165 +++++++++++++++++++++++++++++++++++++++++++++++ server/src/database.h | 47 ++++++++++++++ server/src/server.cc | 17 +++-- server/src/transaction.h | 6 +- server/src/xmlparser.cc | 8 +-- 6 files changed, 234 insertions(+), 11 deletions(-) create mode 100644 server/src/database.cc create mode 100644 server/src/database.h (limited to 'server/src') diff --git a/server/src/Makefile.am b/server/src/Makefile.am index 93c521c..12b3a21 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -7,6 +7,7 @@ pracrod_CXXFLAGS = $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) pracrod_SOURCES = \ pracrod.cc \ + database.cc \ configuration.cc \ exception.cc \ log.cc \ @@ -17,6 +18,7 @@ pracrod_SOURCES = \ EXTRA_DIST = \ configuration.h \ + database.h \ debug.h \ exception.h \ log.h \ diff --git a/server/src/database.cc b/server/src/database.cc new file mode 100644 index 0000000..4bf9c61 --- /dev/null +++ b/server/src/database.cc @@ -0,0 +1,165 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * database.cc + * + * Thu Sep 6 10:59:07 CEST 2007 + * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "database.h" + +#include "tostring.h" +#include +#include +#include + +Database::Database(std::string hostname, std::string user, std::string password) + : c("host=" + hostname + + " user=" + user + + " password=" + password + + " dbname=pracro") +{ + /* + try { + char port_string[32]; + sprintf(port_string, "%d", port); + std::string hoststring = "host=" + host + " port=" + port_string + + " user=" + user + " password=" + password + " dbname=" + database; + c = new pqxx::connection(hoststring); + + } catch(const std::exception &e) { + //throw PostgreSQLException(e.what()); + } + */ +} + +Database::~Database() +{ +} + +int Database::post(Transaction &transaction) +{ + time_t now = time(NULL); + std::string transidbase = toString((unsigned int)now) + "-" + + toString((unsigned int)getpid()) + "-"; // Here we put the commit index + + try { + pqxx::work W(c); + + Commits::iterator i = transaction.commits.begin(); + unsigned int idx = 0; + while(i != transaction.commits.end()) { + Commit &commit = *i; + std::string transid = transidbase + toString(idx); + + // Insert transaction entry + std::string sql = "INSERT INTO transactions VALUES('" + + transaction.cpr + "', '" + + transid + "', '" + + commit.macro + "', '" + + commit.version + "', '" + + toString((unsigned int)now) + "', '" + + commit.user + "')"; + W.exec(sql); + + // Insert field entries + Fields::iterator j = commit.fields.begin(); + while(j != commit.fields.end()) { + Field &field = *j; + + sql = "INSERT INTO fields VALUES('" + + transid + "', '" + + field.name + "', '" + + field.value + "')"; + W.exec(sql); + + j++; + } + + idx++; + i++; + } + + W.commit(); + } catch(const std::exception &e) { + // throw PostgreSQLException(e.what()); + } + + return 0; +} + +// som root +// # createuser -P -h localhost -U postgres +// # createdb -U postgres -h localhost pracro + +/* +CREATE DATABASE pracro + WITH OWNER = pracro + ENCODING = 'UNICODE' + TABLESPACE = pg_default; + +CREATE TABLE transactions +( + "cpr" varchar(255), + "transaction" varchar(255), + "makro" varchar(255), + "version" varchar(255), + "timestamp" varchar(255), + "user" varchar(255) +) +WITH OIDS; +ALTER TABLE transactions OWNER TO pracro; + +CREATE TABLE fields +( + "transaction" varchar(255), + "name" varchar(255), + "value" varchar(255) +) +WITH OIDS; +ALTER TABLE fields OWNER TO pracro; + +// Get all matching fields +SELECT transactions.timestamp, transactions.transaction, fields.name, fields.value + FROM transactions, fields + WHERE transactions.cpr='2003791613' + AND transactions.transaction=fields.transaction + AND fields.name='fisk'; +*/ + +/* + Employees: + Employee_ID Name + 01 Hansen, Ola + 02 Svendson, Tove + 03 Svendson, Stephen + 04 Pettersen, Kari + + Orders: + Prod_ID Product Employee_ID + 234 Printer 01 + 657 Table 03 + 865 Chair 03 + + SELECT Employees.Name, Orders.Product + FROM Employees, Orders + WHERE Employees.Employee_ID=Orders.Employee_ID +*/ diff --git a/server/src/database.h b/server/src/database.h new file mode 100644 index 0000000..9659eda --- /dev/null +++ b/server/src/database.h @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * database.h + * + * Thu Sep 6 10:59:07 CEST 2007 + * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_DATABASE_H__ +#define __PRACRO_DATABASE_H__ + +#include +#include +#include "transaction.h" + +class Database { +public: + Database(std::string hostname = "localhost", + std::string user = "pracro", + std::string password = "pracro"); + ~Database(); + + int post(Transaction &transaction); + +private: + pqxx::connection c; +}; + +#endif/*__PRACRO_DATABASE_H__*/ diff --git a/server/src/server.cc b/server/src/server.cc index 58b54b9..24f55ef 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -49,6 +49,8 @@ #include "transaction.h" #include "xmlparser.h" +#include "database.h" + /** \section{Data transmission} En transmission består af en række deltransmissioner som afhænger af @@ -114,20 +116,27 @@ static void connection(TCPSocket &socket) } // Handle commits + if(transaction.commits.size() > 0) { + Database db; + db.post(transaction); + } + + /* Commits::iterator j = transaction.commits.begin(); while(j != transaction.commits.end()) { - Commit commit = *j; + Commit &commit = *j; printf("Commit %s\n", commit.macro.c_str()); - CommitValues::iterator k = commit.values.begin(); - while(k != commit.values.end()) { - CommitValue val = *k; + Fields::iterator k = commit.fields.begin(); + while(k != commit.fields.end()) { + Field &val = *k; printf("\t%s=%s\n", val.name.c_str(), val.value.c_str()); k++; } j++; } + */ socket.write("\n"); diff --git a/server/src/transaction.h b/server/src/transaction.h index d898ef7..04d83b3 100644 --- a/server/src/transaction.h +++ b/server/src/transaction.h @@ -38,12 +38,12 @@ public: typedef std::vector< Request > Requests; -class CommitValue { +class Field { public: std::string name; std::string value; }; -typedef std::vector< CommitValue > CommitValues; +typedef std::vector< Field > Fields; class Commit { @@ -51,7 +51,7 @@ public: std::string user; std::string macro; std::string version; - CommitValues values; + Fields fields; }; typedef std::vector< Commit > Commits; diff --git a/server/src/xmlparser.cc b/server/src/xmlparser.cc index 7efdc83..d4a6bd8 100644 --- a/server/src/xmlparser.cc +++ b/server/src/xmlparser.cc @@ -82,10 +82,10 @@ void start_hndl(void *p, const char *el, const char **attr) } if(name == "field") { - CommitValue v; - v.name = attributes["name"]; - v.value = attributes["value"]; - transaction->commits.back().values.push_back(v); + Field f; + f.name = attributes["name"]; + f.value = attributes["value"]; + transaction->commits.back().fields.push_back(f); } } -- cgit v1.2.3