db_bridge acts as an interactive sql monitor
committer: Markus Bröker <mbroeker@largo.homelinux.org>
--- a/db_bridge/include/abstract_db.h
+++ b/db_bridge/include/abstract_db.h
@@ -17,10 +17,9 @@
std::string server;
public:
- typedef std::vector < std::vector<std::string> > DB_RESULT;
- typedef std::vector<std::string> DB_ROW;
+ typedef std::vector < std::vector <std::string> >DB_RESULT;
+ typedef std::vector <std::string> DB_ROW;
- virtual ~ Abstract_DB () {}
virtual bool connect () = 0;
virtual DB_RESULT query (std::string) = 0;
};
--- a/db_bridge/main.cpp
+++ b/db_bridge/main.cpp
@@ -9,6 +9,16 @@
#include <mysql_db.h>
#endif
+#include <cstdlib>
+
+void usage (std::string progname)
+{
+ std::cout << "Benutzung: " << progname << " [-r <remotehost>] [-u <user>] [-p <password>] [-h] DATABASE\n" << std::
+ endl;
+ std::cout << "Berichten Sie Fehler oder Anregungen an mbroeker@largo.homelinux.org" << std::endl;
+ exit (0);
+}
+
int main (int argc, char **argv)
{
Abstract_DB *db;
@@ -16,11 +26,36 @@
Abstract_DB::DB_ROW vec;
Abstract_DB::DB_RESULT result;
unsigned int i, j;
+ char c;
+
+ std::string host, user, pass, database;
+ std::string sql_statement;
+
+ while ((c = getopt (argc, argv, "r:u:p:d:h")) >= 0) {
+ switch (c) {
+ case 'r':
+ host = optarg;
+ break;
+ case 'u':
+ user = optarg;
+ break;
+ case 'p':
+ pass = optarg;
+ break;
+ case 'h':
+ usage (argv[0]);
+ break;
+ }
+ }
+ if (optind < argc)
+ database = argv[optind];
+ else
+ usage (argv[0]);
#ifdef WITH_POSTGRESQL
- db = new PostgreSQL_DB ("", "largo", "", "clubstatistik");
+ db = new PostgreSQL_DB (host, user, pass, database);
#else
- db = new MySQL_DB ("localhost", "dbuser", "dbpasswd", "datenbank");
+ db = new MySQL_DB (host, user, pass, database);
#endif
if (db->connect () == false) {
@@ -28,14 +63,24 @@
return EXIT_FAILURE;
}
- result = db->query ("select * from euroleague");
+ for (;;) {
+ // std::getline(std::cin, sql_statement);
+ std::cout << "DB_BRIDGE # ";
+ std::getline (std::cin, sql_statement);
- for (i = 0; i < result.size (); i++) {
- vec = result[i];
- for (j = 0; j < vec.size (); j++) {
- std::cout << vec[j] << " ";
+ if (sql_statement == "quit" || sql_statement == "exit" || sql_statement == "") {
+ std::cout << std::endl;
+ break;
}
- std::cout << std::endl;
+ result = db->query (sql_statement);
+
+ 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;
--- a/db_bridge/mysql_db.cpp
+++ b/db_bridge/mysql_db.cpp
@@ -37,7 +37,7 @@
Abstract_DB::DB_ROW vec;
Abstract_DB::DB_RESULT rows;
MYSQL_ROW row;
- int i;
+ int i = 0;
/*
* send SQL query
@@ -61,6 +61,8 @@
vec.clear ();
}
+ std::cerr << "Die Abfrage hat " << i << " Spalte(n) und " << rows.size () << " Reihe(n)." << std::endl;
+
if (res != NULL) {
mysql_free_result (res);
}
--- a/db_bridge/postgresql_db.cpp
+++ b/db_bridge/postgresql_db.cpp
@@ -4,6 +4,7 @@
*/
#include <postgresql_db.h>
+#include <exception>
PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
{
@@ -43,7 +44,8 @@
Abstract_DB::DB_ROW vec;
Abstract_DB::DB_RESULT rows;
PGresult *res;
- int i, j, nFields;
+ int i, j;
+ int nFields, nRows;
/*
* send SQL query
@@ -51,13 +53,14 @@
res = PQexec (conn, sql_string.c_str ());
nFields = PQnfields (res);
+ nRows = PQntuples (res);
- std::cout << "Die Abfrage ergab " << nFields << " Zeilen." << std::endl;
+ std::cout << "Die Abfrage hat " << nFields << " Spalte(n) und " << nRows << " Reihe(n)." << std::endl;
/*
* push everything into a vector< vector<string> >
*/
- for (i = 0; i < PQntuples (res); i++) {
+ for (i = 0; i < nRows; i++) {
for (j = 0; j < nFields; j++) {
vec.push_back (PQgetvalue (res, i, j));
}