I added a small db_bridge to the repository
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:05 +0100
changeset 12 9f0ce4eaa1ce
parent 11 0a8be4eac87e
child 13 2640a7fd96ca
I added a small db_bridge to the repository committer: Markus Bröker <mbroeker@largo.homelinux.org>
db_bridge/Makefile
db_bridge/include/abstract_db.h
db_bridge/include/mysql_db.h
db_bridge/include/postgresql_db.h
db_bridge/main.cpp
db_bridge/mysql_db.cpp
db_bridge/postgresql_db.cpp
new file mode 100644
--- /dev/null
+++ b/db_bridge/Makefile
@@ -0,0 +1,28 @@
+CC=g++
+CFLAGS=-Wall -O2 -ansi -Iinclude
+TARGET=\
+	mysql_db \
+	postgresql_db
+
+.SUFFIXES: .cpp
+
+.cpp.o:
+	$(CC) -c $(CFLAGS) $<
+
+all: $(TARGET)
+
+mysql_db: mysql_db.o main.o
+	$(CC) $< main.o -lmysqlclient -o $@
+	rm -f main.o
+
+postgresql_db: postgresql_db.o main.cpp
+	$(CC) -c $(CFLAGS) -DWITH_POSTGRESQL main.cpp
+	$(CC) $< main.o -lpq -o $@
+	rm -f main.o
+
+.PHONY: clean
+
+clean:
+	rm -f *.o;
+	rm -f *~
+	rm -f $(TARGET)
new file mode 100644
--- /dev/null
+++ b/db_bridge/include/abstract_db.h
@@ -0,0 +1,25 @@
+/**
+ * db_bridge/include/abstract_db.h
+ * Copyright 2008 (C) Markus Broeker
+ */
+
+#ifndef ABSTRACT_DB_H
+#define ABSTRACT_DB_H
+
+#include <vector>
+#include <string>
+
+class Abstract_DB {
+  protected:
+    std::string user;
+    std::string database;
+    std::string password;
+    std::string server;
+
+  public:
+    virtual ~ Abstract_DB () {
+    };
+    virtual bool connect () = 0;
+    virtual std::vector < std::vector<std::string> >query (std::string) = 0;
+};
+#endif
new file mode 100644
--- /dev/null
+++ b/db_bridge/include/mysql_db.h
@@ -0,0 +1,25 @@
+/**
+ * db_bridge/include/mysql_db.h
+ * Copyright 2008 (C) Markus Broeker
+ */
+
+#ifndef MYSQL_DB_H
+#define MYSQL_DB_H
+
+#include <iostream>
+#include <mysql/mysql.h>
+
+#include <abstract_db.h>
+
+class MySQL_DB:public Abstract_DB {
+  private:
+    MYSQL * conn;
+    MYSQL_RES *res;
+
+  public:
+      MySQL_DB (std::string, std::string, std::string, std::string);
+      virtual ~ MySQL_DB ();
+      bool connect ();
+      std::vector < std::vector <std::string> >query (std::string);
+};
+#endif
new file mode 100644
--- /dev/null
+++ b/db_bridge/include/postgresql_db.h
@@ -0,0 +1,27 @@
+/**
+ * db_bridge/include/postgresql_db.h
+ * Copyright 2008 (C) Markus Broeker
+ */
+
+#ifndef POSTGRESQL_DB_H
+#define POSTGRESQL_DB_H
+
+#include <cstdlib>
+#include <iostream>
+#include <abstract_db.h>
+
+#include <postgresql/libpq-fe.h>
+
+class PostgreSQL_DB:public Abstract_DB {
+  private:
+    PGconn * conn;
+    PGresult *res;
+      std::string conninfo;
+
+  public:
+      PostgreSQL_DB (std::string, std::string, std::string, std::string);
+      virtual ~ PostgreSQL_DB ();
+      bool connect ();
+      std::vector < std::vector <std::string> >query (std::string);
+};
+#endif
new file mode 100644
--- /dev/null
+++ b/db_bridge/main.cpp
@@ -0,0 +1,44 @@
+/**
+ * db_bridge/main.cpp
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#ifdef WITH_POSTGRESQL
+#include <postgresql_db.h>
+#else
+#include <mysql_db.h>
+#endif
+
+int main (int argc, char **argv)
+{
+    Abstract_DB *db;
+
+    std::vector <std::string> vec;
+    std::vector <std::vector <std::string> >result;
+    unsigned int i, j;
+
+#ifdef WITH_POSTGRESQL
+    db = new PostgreSQL_DB ("", "largo", "", "clubstatistik");
+#else
+    db = new MySQL_DB ("localhost", "dbuser", "dbpasswd", "datenbank");
+#endif
+
+    if (db->connect () == false) {
+        std::cerr << "Kann mich nicht zur Datenbank verbinden. Passwoerter falsch?" << std::endl;
+        return EXIT_FAILURE;
+    }
+
+    result = db->query ("select * from euroleague");
+
+    for (i = 0; i < result.size (); i++) {
+        vec = result[i];
+        for (j = 0; j < vec.size (); j++) {
+            std::cout << vec[j] << " ";
+        }
+        std::cout << std::endl;
+    }
+
+    delete db;
+
+    return EXIT_SUCCESS;
+}
new file mode 100644
--- /dev/null
+++ b/db_bridge/mysql_db.cpp
@@ -0,0 +1,69 @@
+/**
+ * db_bridge/mysql_db.cpp
+ * Copyright 2008 (C) Markus Broeker
+ */
+
+#include <mysql_db.h>
+
+MySQL_DB::MySQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
+{
+    server = srv;
+    user = usr;
+    password = pwd;
+    database = db;
+    conn = mysql_init (NULL);
+}
+
+MySQL_DB::~MySQL_DB ()
+{
+    if (conn != NULL) {
+        mysql_close (conn);
+        std::cerr << "Datenbank wird geschlossen" << std::endl;
+    }
+}
+
+bool MySQL_DB::connect ()
+{
+    if (!mysql_real_connect (conn, server.c_str (), user.c_str (), password.c_str (), database.c_str (), 0, NULL, 0)) {
+        std::cerr << server << ": " << mysql_error (conn) << std::endl;
+        return false;
+    }
+
+    return true;
+}
+
+std::vector < std::vector<std::string> >MySQL_DB::query (std::string sql_string)
+{
+    std::vector <std::string> vec;
+    std::vector < std::vector <std::string> >rows;
+    MYSQL_ROW row;
+    int i;
+
+    /*
+     * send SQL query
+     */
+    if (mysql_query (conn, sql_string.c_str ())) {
+        std::cerr << server << ": " << mysql_error (conn) << std::endl;
+        return rows;
+    }
+
+    res = mysql_use_result (conn);
+
+    /*
+     * push everything into a vector< vector<string> >
+     */
+    while ((row = mysql_fetch_row (res)) != NULL) {
+        i = 0;
+        while (row[i] != NULL) {
+            vec.push_back (row[i++]);
+        }
+        rows.push_back (vec);
+        vec.clear ();
+    }
+
+    if (res != NULL) {
+        mysql_free_result (res);
+    }
+
+    return rows;
+}
new file mode 100644
--- /dev/null
+++ b/db_bridge/postgresql_db.cpp
@@ -0,0 +1,73 @@
+/**
+ * db_bridge/postgresql_db.cpp
+ * Copyright 2008 (C) Markus Broeker
+ */
+
+#include <postgresql_db.h>
+
+PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
+{
+    server = (srv != "") ? "host=" + srv : "";
+    user = (usr != "") ? "user=" + usr : "user=" + std::string (getenv ("USER"));
+    password = (pwd != "") ? "password=" + pwd : "";
+    database = "dbname=" + db;
+
+    if (server != "" && pwd == "") {
+        std::cerr << "HBA funktioniert nur mit Passwoertern..." << std::endl;
+    }
+
+    conninfo = server + " " + user + " " + password + " " + database;
+}
+
+PostgreSQL_DB::~PostgreSQL_DB ()
+{
+    if (conn != NULL) {
+        PQfinish (conn);
+        std::cerr << "Datenbank wird geschlossen" << std::endl;
+    }
+}
+
+bool PostgreSQL_DB::connect ()
+{
+    if ((conn = PQconnectdb (conninfo.c_str ())) == NULL) {
+        std::cerr << server << ": " << "ERROR " << std::endl;
+        return false;
+    }
+
+    std::cerr << "Verbindungsparameter: " << conninfo << std::endl;
+    return true;
+}
+
+std::vector < std::vector<std::string> >PostgreSQL_DB::query (std::string sql_string)
+{
+    std::vector <std::string> vec;
+    std::vector < std::vector <std::string> >rows;
+    PGresult *res;
+    int i, j, nFields;
+
+    /*
+     * send SQL query
+     */
+    res = PQexec (conn, sql_string.c_str ());
+
+    nFields = PQnfields (res);
+
+    std::cout << "Die Abfrage ergab " << nFields << " Zeilen." << std::endl;
+
+    /*
+     * push everything into a vector< vector<string> >
+     */
+    for (i = 0; i < PQntuples (res); i++) {
+        for (j = 0; j < nFields; j++) {
+            vec.push_back (PQgetvalue (res, i, j));
+        }
+        rows.push_back (vec);
+        vec.clear ();
+    }
+
+    if (res != NULL) {
+        PQclear (res);
+    }
+
+    return rows;
+}