author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sun, 24 Oct 2010 20:55:19 +0200 | |
changeset 148 | 8a5cf98a00b6 |
parent 125 | 9c0fdc119939 |
permissions | -rw-r--r-- |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
1 |
/** |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
2 |
* db_bridge/postgresql_db.cpp |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
3 |
* Copyright 2008 (C) Markus Broeker |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
4 |
*/ |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
5 |
|
40 | 6 |
#include <cstdlib> |
65
76514757b0d6
GNU Indent cannot handle C++ Source Files...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
40
diff
changeset
|
7 |
#include <postgresql_db.hpp> |
15
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
8 |
#include <exception> |
65
76514757b0d6
GNU Indent cannot handle C++ Source Files...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
40
diff
changeset
|
9 |
#include <console.hpp> |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
10 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
11 |
PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db) |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
12 |
{ |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
13 |
server = srv; |
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
14 |
user = (usr != "") ? usr : getenv ("USER"); |
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
15 |
password = pwd; |
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
16 |
database = db; |
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
17 |
|
18
f3657061ec00
conninfo construction error fixed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
17
diff
changeset
|
18 |
conninfo = "user=" + user + " dbname=" + database; |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
19 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
20 |
if (server != "" && pwd == "") { |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
21 |
std::cout << "Passwort von " << user << "@" << server << ": "; |
20
5fec678f931b
getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
18
diff
changeset
|
22 |
Console::getpass (password); |
18
f3657061ec00
conninfo construction error fixed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
17
diff
changeset
|
23 |
conninfo += " host=" + server + " password=" + password; |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
24 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
25 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
26 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
27 |
PostgreSQL_DB::~PostgreSQL_DB () |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
28 |
{ |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
29 |
if (conn != NULL) { |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
30 |
PQfinish (conn); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
std::cerr << "Datenbank wird geschlossen" << std::endl; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
bool PostgreSQL_DB::connect () |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
36 |
{ |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
37 |
std::cerr << "Verbindungsparameter:" << " host=" << ((server != "") ? server : "LOCAL") |
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
38 |
<< " user=" << user << " password=" << password << " database=" << database << std::endl; |
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
39 |
|
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
40 |
if ((conn = PQconnectdb (conninfo.c_str ())) == NULL) { |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
41 |
std::cerr << server << ": " << "ERROR " << std::endl; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
42 |
return false; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
43 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
44 |
|
125
9c0fdc119939
postgresql_db: check connection status
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
45 |
if (PQstatus(conn) != CONNECTION_OK) { |
9c0fdc119939
postgresql_db: check connection status
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
46 |
return false; |
9c0fdc119939
postgresql_db: check connection status
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
47 |
} |
9c0fdc119939
postgresql_db: check connection status
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
48 |
|
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
return true; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
|
14
862d63715611
the long vector needs a typedef
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
12
diff
changeset
|
52 |
Abstract_DB::DB_RESULT PostgreSQL_DB::query (std::string sql_string) |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
{ |
14
862d63715611
the long vector needs a typedef
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
12
diff
changeset
|
54 |
Abstract_DB::DB_ROW vec; |
862d63715611
the long vector needs a typedef
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
12
diff
changeset
|
55 |
Abstract_DB::DB_RESULT rows; |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
56 |
PGresult *res; |
15
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
57 |
int i, j; |
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
58 |
int nFields, nRows; |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
59 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
60 |
/* |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
61 |
* send SQL query |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
62 |
*/ |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
63 |
res = PQexec (conn, sql_string.c_str ()); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
64 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
65 |
nFields = PQnfields (res); |
15
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
66 |
nRows = PQntuples (res); |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
67 |
|
15
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
68 |
std::cout << "Die Abfrage hat " << nFields << " Spalte(n) und " << nRows << " Reihe(n)." << std::endl; |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
69 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
70 |
/* |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
71 |
* push everything into a vector< vector<string> > |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
72 |
*/ |
15
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
73 |
for (i = 0; i < nRows; i++) { |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
74 |
for (j = 0; j < nFields; j++) { |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
75 |
vec.push_back (PQgetvalue (res, i, j)); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
76 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
77 |
rows.push_back (vec); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
78 |
vec.clear (); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
79 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
80 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
81 |
if (res != NULL) { |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
82 |
PQclear (res); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
83 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
84 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
85 |
return rows; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
86 |
} |