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/configure.in | 54 ++++++++-------- 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 +-- 7 files changed, 261 insertions(+), 38 deletions(-) create mode 100644 server/src/database.cc create mode 100644 server/src/database.h diff --git a/server/configure.in b/server/configure.in index 553eb16..3bd9e7b 100644 --- a/server/configure.in +++ b/server/configure.in @@ -16,9 +16,9 @@ dnl Create the ETC var i config.h dnl ====================== if echo "$prefix" | grep "NONE" > /dev/null then - MYPREFIX="/usr/local" + MYPREFIX="/usr/local" else - MYPREFIX="${prefix}" + MYPREFIX="${prefix}" fi AC_SUBST(MYPREFIX) AC_DEFINE_UNQUOTED(ETC, "$MYPREFIX/etc", [The path to the config files]) @@ -28,10 +28,10 @@ dnl ====================== dnl Use efence in linking and includes dnl ====================== AC_ARG_ENABLE(efence, - [ --enable-efence enable efence - for debugging only (no)], - [], [ enable_efence=no]) + [ --enable-efence enable efence - for debugging only (no)], + [], [ enable_efence=no]) if test "x$enable_efence" = xno; then - enable_efence=no + enable_efence=no else LD_EFENCE="-lefence" AC_SUBST(LD_EFENCE) @@ -54,7 +54,7 @@ AC_HAVE_HEADERS(getopt.h) dnl ====================== dnl Check for libpqxx dnl ====================== -PKG_CHECK_MODULES(PQXX, libpqxx >= 0.20) +PKG_CHECK_MODULES(PQXX, libpqxx >= 2.6.8) dnl ====================== @@ -115,41 +115,41 @@ AC_SUBST(LDFLAGS) AC_DEFUN([BB_ENABLE_DOXYGEN], [ AC_ARG_ENABLE(doxygen, - [ --enable-doxygen enable documentation generation with doxygen (auto)]) + [ --enable-doxygen enable documentation generation with doxygen (auto)]) AC_ARG_ENABLE(dot, - [ --enable-dot use 'dot' to generate graphs in doxygen (auto)]) + [ --enable-dot use 'dot' to generate graphs in doxygen (auto)]) AC_ARG_ENABLE(html-docs, - [ --enable-html-docs enable HTML generation with doxygen (no)], - [], [ enable_html_docs=no]) + [ --enable-html-docs enable HTML generation with doxygen (no)], + [], [ enable_html_docs=no]) AC_ARG_ENABLE(latex-docs, - [ --enable-latex-docs enable LaTeX documentation generation with doxygen (no)], - [], [ enable_latex_docs=no]) + [ --enable-latex-docs enable LaTeX documentation generation with doxygen (no)], + [], [ enable_latex_docs=no]) if test "x$enable_doxygen" = xno; then - enable_doc=no + enable_doc=no else AC_OUTPUT(doc/Makefile) DOC_DIR=doc AC_SUBST(DOC_DIR) AC_PATH_PROG(DOXYGEN, doxygen, , $PATH) - if test x$DOXYGEN = x; then - if test "x$enable_doxygen" = xyes; then - AC_MSG_ERROR([could not find doxygen]) - fi - enable_doc=no - else - enable_doc=yes - AC_PATH_PROG(DOT, dot, , $PATH) - fi + if test x$DOXYGEN = x; then + if test "x$enable_doxygen" = xyes; then + AC_MSG_ERROR([could not find doxygen]) + fi + enable_doc=no + else + enable_doc=yes + AC_PATH_PROG(DOT, dot, , $PATH) + fi fi AM_CONDITIONAL(DOC, test x$enable_doc = xyes) if test x$DOT = x; then - if test "x$enable_dot" = xyes; then - AC_MSG_ERROR([could not find dot]) - fi - enable_dot=no + if test "x$enable_dot" = xyes; then + AC_MSG_ERROR([could not find dot]) + fi + enable_dot=no else - enable_dot=yes + enable_dot=yes fi AM_CONDITIONAL(ENABLE_DOXYGEN, test x$enable_doc = xtrue) AC_SUBST(enable_dot) 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