FACT++
1.0
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
Database.h
Go to the documentation of this file.
1
#ifndef FACT_Database
2
#define FACT_Database
3
4
#include <exception>
5
#include <boost/regex.hpp>
6
7
#include <mysql++/mysql++.h>
8
9
struct
DatabaseName
10
{
11
std::string
user
;
12
std::string
passwd
;
13
std::string
server
;
14
std::string
db
;
15
int
port
;
16
17
DatabaseName
(
const
std::string &database)
18
{
19
static
const
boost::regex expr(
"(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))"
);
20
21
boost::smatch what;
22
if
(!boost::regex_match(database, what, expr, boost::match_extra))
23
throw
std::runtime_error(
"Couldn't parse database URI '"
+database+
"'."
);
24
25
if
(what.size()!=10)
26
throw
std::runtime_error(
"Error parsing database URI '"
+database+
"'."
);
27
28
user = what[2];
29
passwd = what[4];
30
server = what[5];
31
db = what[9];
32
33
try
34
{
35
port = stoi(std::string(what[7]));
36
}
37
catch
(...)
38
{
39
port = 0;
40
}
41
}
42
43
std::string
uri
()
const
44
{
45
std::string rc;
46
if
(!user.empty())
47
rc += user+
"@"
;
48
rc +=
server
;
49
if
(port)
50
rc +=
":"
+std::to_string(port);
51
if
(!db.empty())
52
rc +=
"/"
+db;
53
return
rc;
54
}
55
};
56
57
class
Database
:
public
DatabaseName
,
public
mysqlpp::Connection
58
{
59
public
:
60
Database
(
const
std::string &desc) :
DatabaseName
(desc),
61
mysqlpp::
Connection
(
db
.c_str(),
server
.c_str(),
user
.c_str(),
passwd
.c_str(),
port
)
62
{
63
}
64
};
65
66
#endif
DatabaseName::port
int port
Definition:
Database.h:15
DatabaseName
Definition:
Database.h:9
Database
Definition:
Database.h:57
Database::Database
Database(const std::string &desc)
Definition:
Database.h:60
DatabaseName::user
std::string user
Definition:
Database.h:11
DatabaseName::db
std::string db
Definition:
Database.h:14
DatabaseName::DatabaseName
DatabaseName(const std::string &database)
Definition:
Database.h:17
DatabaseName::uri
std::string uri() const
Definition:
Database.h:43
DatabaseName::passwd
std::string passwd
Definition:
Database.h:12
Connection
Maintains an ansynchronous TCP/IP client connection.
Definition:
Connection.h:15
DatabaseName::server
std::string server
Definition:
Database.h:13
src
Database.h
Generated on Sun Sep 18 2016 20:50:24 for FACT++ by
1.8.11