OpenTREP Logo  0.08.01
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
DBParams.hpp
Go to the documentation of this file.
1#ifndef __OPENTREP_DBPARAMS_HPP
2#define __OPENTREP_DBPARAMS_HPP
3
4// //////////////////////////////////////////////////////////////////////
5// Import section
6// //////////////////////////////////////////////////////////////////////
7// STL
8#include <sstream>
9#include <string>
10// OpenTrep
13#include <opentrep/DBType.hpp>
14
15namespace OPENTREP {
16
20 typedef std::list<std::string> DBParamsNameList_T;
21
22
26 struct DBParams : public OPENTREP_Abstract {
27 public:
28 // ///////////////////// Getters //////////////////////
32 const DBType& getType() const {
33 return _dbtype;
34 }
35
39 const std::string& getUser() const {
40 return _user;
41 }
42
46 const std::string& getPassword() const {
47 return _passwd;
48 }
49
53 const std::string& getHost() const {
54 return _host;
55 }
56
60 const std::string& getPort() const {
61 return _port;
62 }
63
67 const std::string& getDBName() const {
68 return _dbname;
69 }
70
71
72 public:
73 // /////////////////////// Setters ///////////////////////
77 void setType (const DBType& iType) {
78 _dbtype = iType;
79 }
80
84 void setUser (const std::string& iUser) {
85 _user = iUser;
86 }
87
91 void setPassword (const std::string& iPasswd) {
92 _passwd = iPasswd;
93 }
94
98 void setHost (const std::string& iHost) {
99 _host = iHost;
100 }
101
105 void setPort (const std::string& iPort) {
106 _port = iPort;
107 }
108
112 void setDBName (const std::string& iDBName) {
113 _dbname = iDBName;
114 }
115
116
117 public:
118 // ///////////////////// Busines methods ////////////////////
122 bool checkSQLite () const {
123 if (_dbname.empty() == true) {
124 return false;
125 }
126 return true;
127 }
128
132 bool checkMySQL () const {
133 if (_user.empty() == true || _passwd.empty() == true
134 || _host.empty() == true || _port.empty()
135 || _dbname.empty() == true) {
136 return false;
137 }
138 return true;
139 }
140
144 bool checkPG () const {
145 if (_user.empty() == true || _passwd.empty() == true
146 || _dbname.empty() == true) {
147 return false;
148 }
149 return true;
150 }
151
152 public:
153 // //////////////////// Display methods //////////////////////
159 void toStream (std::ostream& ioOut) const {
160 ioOut << toString();
161 }
162
167 void fromStream (std::istream&) {
168 }
169
173 std::string toShortString() const {
174 std::ostringstream oStr;
175 oStr << _dbname << "." << _user << "@" << _host << ":" << _port;
176 return oStr.str();
177 }
178
182 std::string toString() const {
183 std::ostringstream oStr;
184 oStr << _dbname << "." << _user << "@" << _host << ":" << _port;
185 return oStr.str();
186 }
187
191 std::string toMySQLConnectionString() const {
192 std::ostringstream oStr;
193 oStr << "db=" << _dbname << " user=" << _user << " password=" << _passwd
194 << " port=" << _port << " host=" << _host;
195 return oStr.str();
196 }
197
201 std::string toPGConnectionString() const {
202 std::ostringstream oStr;
203 oStr << "dbname=" << _dbname << " user=" << _user
204 << " password=" << _passwd;
205 if (!_host.empty()) {
206 oStr << " host=" << _host;
207 }
208 if (!_port.empty()) {
209 oStr << " port=" << _port;
210 }
211 return oStr.str();
212 }
213
217 std::string toSQLiteConnectionString() const {
218 std::ostringstream oStr;
219 oStr << "db=" << _dbname;
220 return oStr.str();
221 }
222
223
224 public:
225 // /////////////// Constructors and Destructors ///////////////////
229 DBParams (const DBType& iDBType,
230 const std::string& iDBUser, const std::string& iDBPasswd,
231 const std::string& iDBHost, const std::string& iDBPort,
232 const std::string& iDBName)
233 : _dbtype (iDBType), _user (iDBUser), _passwd (iDBPasswd),
234 _host (iDBHost), _port (iDBPort), _dbname (iDBName) {
235 }
236 DBParams (const DBType& iDBType, const std::string& iDBName)
237 : _dbtype (iDBType), _dbname (iDBName) {
238 }
239
243 // DBParams();
247 // DBParams (const DBParams&);
248
252 virtual ~DBParams() {}
253
254
255 private:
256 // ///////////////////////// Attributes ////////////////////////
260 DBType _dbtype;
261
265 std::string _user;
266
270 std::string _passwd;
271
275 std::string _host;
276
280 std::string _port;
281
285 std::string _dbname;
286 };
287
288}
289#endif // __OPENTREP_DBPARAMS_HPP
std::list< std::string > DBParamsNameList_T
Definition DBParams.hpp:20
const std::string & getUser() const
Definition DBParams.hpp:39
const std::string & getPort() const
Definition DBParams.hpp:60
virtual ~DBParams()
Definition DBParams.hpp:252
const std::string & getDBName() const
Definition DBParams.hpp:67
void setPassword(const std::string &iPasswd)
Definition DBParams.hpp:91
DBParams(const DBType &iDBType, const std::string &iDBUser, const std::string &iDBPasswd, const std::string &iDBHost, const std::string &iDBPort, const std::string &iDBName)
Definition DBParams.hpp:229
bool checkSQLite() const
Definition DBParams.hpp:122
std::string toShortString() const
Definition DBParams.hpp:173
void toStream(std::ostream &ioOut) const
Definition DBParams.hpp:159
void fromStream(std::istream &)
Definition DBParams.hpp:167
const DBType & getType() const
Definition DBParams.hpp:32
void setUser(const std::string &iUser)
Definition DBParams.hpp:84
const std::string & getPassword() const
Definition DBParams.hpp:46
std::string toString() const
Definition DBParams.hpp:182
std::string toMySQLConnectionString() const
Definition DBParams.hpp:191
bool checkMySQL() const
Definition DBParams.hpp:132
void setType(const DBType &iType)
Definition DBParams.hpp:77
void setDBName(const std::string &iDBName)
Definition DBParams.hpp:112
std::string toPGConnectionString() const
Definition DBParams.hpp:201
std::string toSQLiteConnectionString() const
Definition DBParams.hpp:217
void setPort(const std::string &iPort)
Definition DBParams.hpp:105
bool checkPG() const
Definition DBParams.hpp:144
DBParams(const DBType &iDBType, const std::string &iDBName)
Definition DBParams.hpp:236
void setHost(const std::string &iHost)
Definition DBParams.hpp:98
const std::string & getHost() const
Definition DBParams.hpp:53
Enumeration of database types.
Definition DBType.hpp:17