1 | /* |
---|
2 | Copyright (C) 2012 |
---|
3 | Alejandro Mujica (amujica@cenditel.gob.ve) |
---|
4 | Erwin Paredes (eparedes@cenditel.gob.ve) |
---|
5 | José Ruiz (jruiz@cenditel.gob.ve) |
---|
6 | Rodolfo Rangel (rrangel@cenditel.gob.ve) |
---|
7 | Julie Vera (jvera@cenditel.gob.ve) |
---|
8 | |
---|
9 | CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en |
---|
10 | Tecnologías Libres |
---|
11 | |
---|
12 | Este programa es software libre; Usted puede usarlo bajo los términos de la |
---|
13 | licencia de software GPL versión 2.0 de la Free Software Foundation. |
---|
14 | |
---|
15 | Este programa se distribuye con la esperanza de que sea útil, pero SIN |
---|
16 | NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o |
---|
17 | ADECUACIÓN A UN PROPÓSITO PARTICULAR. |
---|
18 | Consulte la licencia GPL para más detalles. Usted debe recibir una copia |
---|
19 | de la GPL junto con este programa; si no, escriba a la Free Software |
---|
20 | Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | Autor: Alejandro J. Mujica |
---|
25 | Fecha de creación: 25/07/2013 |
---|
26 | Este archivo contiene la implementación de la clase DBProperties. |
---|
27 | */ |
---|
28 | |
---|
29 | # include <stdexcept> |
---|
30 | # include <sstream> |
---|
31 | # include <fstream> |
---|
32 | |
---|
33 | # include <dbProperties.H> |
---|
34 | |
---|
35 | std::unique_ptr <DBProperties> DBProperties::instance; |
---|
36 | |
---|
37 | DBProperties::DBProperties() : host(), port(), user(), pass(), dbname() { |
---|
38 | // Empty |
---|
39 | } |
---|
40 | |
---|
41 | DBProperties & DBProperties::getInstance() { |
---|
42 | if (instance.get() == NULL) |
---|
43 | instance = std::unique_ptr <DBProperties> (new DBProperties); |
---|
44 | |
---|
45 | return *instance; |
---|
46 | } |
---|
47 | |
---|
48 | const std::string & DBProperties::getHost() const { |
---|
49 | return host; |
---|
50 | } |
---|
51 | |
---|
52 | void DBProperties::setHost(const std::string & host) { |
---|
53 | this->host = host; |
---|
54 | } |
---|
55 | |
---|
56 | void DBProperties::setHost(std::string && host) |
---|
57 | { |
---|
58 | this->host = std::move(host); |
---|
59 | } |
---|
60 | |
---|
61 | const std::string & DBProperties::getPort() const { |
---|
62 | return port; |
---|
63 | } |
---|
64 | |
---|
65 | void DBProperties::setPort(const std::string & port) { |
---|
66 | this->port = port; |
---|
67 | } |
---|
68 | |
---|
69 | void DBProperties::setPort(std::string && port) { |
---|
70 | this->port = std::move(port); |
---|
71 | } |
---|
72 | |
---|
73 | const std::string & DBProperties::getUser() const { |
---|
74 | return user; |
---|
75 | } |
---|
76 | |
---|
77 | void DBProperties::setUser(const std::string & user) { |
---|
78 | this->user = user; |
---|
79 | } |
---|
80 | |
---|
81 | void DBProperties::setUser(std::string && user) { |
---|
82 | this->user = std::move(user); |
---|
83 | } |
---|
84 | |
---|
85 | const std::string & DBProperties::getPassword() const { |
---|
86 | return pass; |
---|
87 | } |
---|
88 | |
---|
89 | void DBProperties::setPassword(const std::string & pass) { |
---|
90 | this->pass = pass; |
---|
91 | } |
---|
92 | |
---|
93 | void DBProperties::setPassword(std::string && pass) { |
---|
94 | this->pass = std::move(pass); |
---|
95 | } |
---|
96 | |
---|
97 | const std::string & DBProperties::getDbname() const { |
---|
98 | return dbname; |
---|
99 | } |
---|
100 | |
---|
101 | void DBProperties::setDbname(const std::string & dbname) { |
---|
102 | this->dbname = dbname; |
---|
103 | } |
---|
104 | |
---|
105 | void DBProperties::setDbname(std::string && dbname) { |
---|
106 | this->dbname = std::move(dbname); |
---|
107 | } |
---|
108 | |
---|
109 | std::string DBProperties::getConnectionInfo() { |
---|
110 | |
---|
111 | std::stringstream sstr; |
---|
112 | |
---|
113 | sstr << "host=" << host << " port=" << port << " user=" << user |
---|
114 | << " password=" << pass << " dbname=" << dbname; |
---|
115 | |
---|
116 | return sstr.str(); |
---|
117 | } |
---|
118 | |
---|
119 | void DBProperties::readFile(const std::string & fileName) { |
---|
120 | if(fileName.empty()) |
---|
121 | throw std::logic_error("file name is empty"); |
---|
122 | |
---|
123 | std::ifstream file(fileName.c_str()); |
---|
124 | |
---|
125 | if (not file) |
---|
126 | throw std::logic_error("file does not exist"); |
---|
127 | |
---|
128 | std::string host; |
---|
129 | |
---|
130 | std::string port; |
---|
131 | |
---|
132 | std::string dbname; |
---|
133 | |
---|
134 | std::string user; |
---|
135 | |
---|
136 | std::string pass; |
---|
137 | |
---|
138 | getline(file, host); |
---|
139 | getline(file, port); |
---|
140 | getline(file, dbname); |
---|
141 | getline(file, user); |
---|
142 | getline(file, pass); |
---|
143 | |
---|
144 | if(not host.empty()) |
---|
145 | std::swap(this->host, host); |
---|
146 | |
---|
147 | if(not port.empty()) |
---|
148 | std::swap(this->port, port); |
---|
149 | |
---|
150 | if(not dbname.empty()) |
---|
151 | std::swap(this->dbname, dbname); |
---|
152 | |
---|
153 | if(not user.empty()) |
---|
154 | std::swap(this->user, user); |
---|
155 | |
---|
156 | if(not pass.empty()) |
---|
157 | std::swap(this->pass, pass); |
---|
158 | |
---|
159 | file.close(); |
---|
160 | } |
---|
161 | |
---|