author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Tue, 01 Jun 2010 14:50:45 +0200 | |
changeset 129 | d82830a449a9 |
parent 126 | 52722ac7693f |
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/mysql_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 <mysql_db.hpp> |
76514757b0d6
GNU Indent cannot handle C++ Source Files...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
40
diff
changeset
|
8 |
#include <console.hpp> |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
9 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
10 |
MySQL_DB::MySQL_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
|
11 |
{ |
22
0db9235d68d9
mysql_db is untested because we have no sponsor with a mysql database
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
12 |
server = (srv != "") ? srv : "localhost"; |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
13 |
user = (usr != "") ? usr : getenv ("USER"); |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
14 |
password = pwd; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
15 |
database = db; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
16 |
conn = mysql_init (NULL); |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
17 |
|
22
0db9235d68d9
mysql_db is untested because we have no sponsor with a mysql database
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
18 |
if (pwd == "") { |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
19 |
std::cout << "Passwort von " << user << "@" << server << ": "; |
20
5fec678f931b
getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
17
diff
changeset
|
20 |
Console::getpass (password); |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
21 |
} |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
22 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
23 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
24 |
MySQL_DB::~MySQL_DB () |
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 |
if (conn != NULL) { |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
27 |
mysql_close (conn); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
28 |
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
|
29 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
30 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
bool MySQL_DB::connect () |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
{ |
22
0db9235d68d9
mysql_db is untested because we have no sponsor with a mysql database
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
34 |
std::cerr << "Verbindungsparameter:" << " host=" << server |
17
b3731a25b9ec
I have improved the connection info and cleaned something up.
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
15
diff
changeset
|
35 |
<< " 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
|
36 |
|
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
37 |
if (!mysql_real_connect (conn, server.c_str (), user.c_str (), password.c_str (), database.c_str (), 0, NULL, 0)) { |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
38 |
std::cerr << server << ": " << mysql_error (conn) << std::endl; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
39 |
return false; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
40 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
41 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
42 |
return true; |
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 |
|
14
862d63715611
the long vector needs a typedef
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
12
diff
changeset
|
45 |
Abstract_DB::DB_RESULT MySQL_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
|
46 |
{ |
14
862d63715611
the long vector needs a typedef
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
12
diff
changeset
|
47 |
Abstract_DB::DB_ROW vec; |
862d63715611
the long vector needs a typedef
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
12
diff
changeset
|
48 |
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
|
49 |
MYSQL_ROW row; |
126
52722ac7693f
mysql_db: get not more than num_fields fields from a row
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
50 |
int i = 0, num_fields = 0; |
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
|
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
/* |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
* send SQL query |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
54 |
*/ |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
55 |
if (mysql_query (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
|
56 |
std::cerr << server << ": " << mysql_error (conn) << std::endl; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
57 |
return rows; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
58 |
} |
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 |
res = mysql_use_result (conn); |
126
52722ac7693f
mysql_db: get not more than num_fields fields from a row
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
61 |
num_fields = mysql_num_fields (res); |
12
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 |
/* |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
64 |
* 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
|
65 |
*/ |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
66 |
while ((row = mysql_fetch_row (res)) != NULL) { |
126
52722ac7693f
mysql_db: get not more than num_fields fields from a row
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
67 |
for (i = 0; i < num_fields; i++) { |
52722ac7693f
mysql_db: get not more than num_fields fields from a row
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
65
diff
changeset
|
68 |
vec.push_back (row[i]); |
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 |
rows.push_back (vec); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
71 |
vec.clear (); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
72 |
} |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
73 |
|
15
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
74 |
std::cerr << "Die Abfrage hat " << i << " Spalte(n) und " << rows.size () << " Reihe(n)." << std::endl; |
5a0ca1f9a2f1
db_bridge acts as an interactive sql monitor
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
14
diff
changeset
|
75 |
|
12
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
76 |
if (res != NULL) { |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
77 |
mysql_free_result (res); |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
78 |
} |
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 |
return rows; |
9f0ce4eaa1ce
I added a small db_bridge to the repository
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
81 |
} |