summaryrefslogtreecommitdiff
path: root/server/src/database.cc
diff options
context:
space:
mode:
authordeva <deva>2007-09-14 12:25:54 +0000
committerdeva <deva>2007-09-14 12:25:54 +0000
commitb70c9b6843e15ee5764b8457acea930af0d2b285 (patch)
tree677c770b3f0e20c2081661f0a2c5d144a0ce5a24 /server/src/database.cc
parent1a0bd4c03c4045d9cc1b3c0bcec39487fa9c5486 (diff)
Commits are now committed to the database.
Diffstat (limited to 'server/src/database.cc')
-rw-r--r--server/src/database.cc165
1 files changed, 165 insertions, 0 deletions
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 <time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+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
+*/