Line | |
---|
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+"'.");
|
---|
24 |
|
---|
25 | if (what.size()!=10)
|
---|
26 | throw std::runtime_error("Error parsing '"+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
|
---|
Note:
See
TracBrowser
for help on using the repository browser.