comparison C++/DriverPostgres.cpp @ 189:cc1e5fe1ee2c

Update drivers to style and using instead of typedefs
author David Demelier <markand@malikania.fr>
date Tue, 26 Nov 2013 20:36:59 +0100
parents 4c746050969a
children d263f85f43a4
comparison
equal deleted inserted replaced
188:ce3e1c3d6fed 189:cc1e5fe1ee2c
69 int QueryPostgres::getInt(int row, const std::string &column) 69 int QueryPostgres::getInt(int row, const std::string &column)
70 { 70 {
71 int idx = PQfnumber(m_result.get(), column.c_str()); 71 int idx = PQfnumber(m_result.get(), column.c_str());
72 72
73 return std::stoi(PQgetvalue(m_result.get(), row, idx)); 73 return std::stoi(PQgetvalue(m_result.get(), row, idx));
74
75 } 74 }
76 75
77 std::string QueryPostgres::getString(int row, const std::string &column) 76 std::string QueryPostgres::getString(int row, const std::string &column)
78 { 77 {
79 int idx = PQfnumber(m_result.get(), column.c_str()); 78 int idx = PQfnumber(m_result.get(), column.c_str());
100 ColumnType type; 99 ColumnType type;
101 int pqType, index; 100 int pqType, index;
102 101
103 index = PQfnumber(m_result.get(), column.c_str()); 102 index = PQfnumber(m_result.get(), column.c_str());
104 pqType = PQftype(m_result.get(), index); 103 pqType = PQftype(m_result.get(), index);
105 switch (pqType) 104 switch (pqType) {
106 {
107 case 16: 105 case 16:
108 type = ColumnType::Boolean; 106 type = ColumnType::Boolean;
109 break; 107 break;
110 case 1082: 108 case 1082:
111 case 1083: 109 case 1083:
155 { 153 {
156 std::cout << "Dumping PostgreSQL result, "; 154 std::cout << "Dumping PostgreSQL result, ";
157 std::cout << countRows() << " rows, "; 155 std::cout << countRows() << " rows, ";
158 std::cout << countColumns() << " columns" << std::endl; 156 std::cout << countColumns() << " columns" << std::endl;
159 157
160 for (int r = 0; r < countRows(); ++r) 158 for (int r = 0; r < countRows(); ++r) {
161 {
162 std::cout << "Dumping row " << r << std::endl; 159 std::cout << "Dumping row " << r << std::endl;
163 std::cout << "==============================" << std::endl; 160 std::cout << "==============================" << std::endl;
164 161
165 for (int c = 0; c < countColumns(); ++c) 162 for (int c = 0; c < countColumns(); ++c) {
166 {
167 std::cout << "\t" << PQfname(m_result.get(), c); 163 std::cout << "\t" << PQfname(m_result.get(), c);
168 std::cout << " = " << PQgetvalue(m_result.get(), r, c) << std::endl; 164 std::cout << " = " << PQgetvalue(m_result.get(), r, c) << std::endl;
169 } 165 }
170 } 166 }
171 } 167 }
250 bool DriverPostgres::connect(const Params &params) 246 bool DriverPostgres::connect(const Params &params)
251 { 247 {
252 Params copy = params; 248 Params copy = params;
253 PGconn *conn = PQconnectdb(convert(copy).c_str()); 249 PGconn *conn = PQconnectdb(convert(copy).c_str());
254 250
255 if (conn == nullptr) 251 if (conn == nullptr) {
256 {
257 m_error = strerror(ENOMEM); 252 m_error = strerror(ENOMEM);
258 return false; 253 return false;
259 } 254 }
260 255
261 if (PQstatus(conn) == CONNECTION_BAD) 256 if (PQstatus(conn) == CONNECTION_BAD) {
262 {
263 m_error = PQerrorMessage(conn); 257 m_error = PQerrorMessage(conn);
264 PQfinish(conn); 258 PQfinish(conn);
265 259
266 return false; 260 return false;
267 } 261 }
285 if (info == nullptr) 279 if (info == nullptr)
286 throw Query::Error(strerror(ENOMEM)); 280 throw Query::Error(strerror(ENOMEM));
287 281
288 // If an error occured 282 // If an error occured
289 int errorCode = PQresultStatus(info); 283 int errorCode = PQresultStatus(info);
290 if (errorCode != PGRES_COMMAND_OK && errorCode != PGRES_TUPLES_OK) 284 if (errorCode != PGRES_COMMAND_OK && errorCode != PGRES_TUPLES_OK) {
291 {
292 std::string error = PQresultErrorMessage(info); 285 std::string error = PQresultErrorMessage(info);
293 PQclear(info); 286 PQclear(info);
294 throw Query::Error(error); 287 throw Query::Error(error);
295 } 288 }
296 289