-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPostgres.h
More file actions
executable file
·227 lines (204 loc) · 7.75 KB
/
Postgres.h
File metadata and controls
executable file
·227 lines (204 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* vim:set noexpandtab tabstop=4 wrap filetype=cpp */
#ifndef Postgres_H
#define Postgres_H
#include <string>
#include <iostream>
#include <sstream>
#include <pqxx/pqxx>
#include <sys/time.h>
#include <typeinfo>
#include <cxxabi.h> // demangle
class Postgres {
public:
Postgres();
~Postgres();
void Init(std::string hostname_in="", std::string hostip_in="", int port_in=-1,
std::string user_in="", std::string password_in="", std::string dbname_in="");
void SetVerbosity(int verb);
// open a connection to the database
pqxx::connection* OpenConnection(std::string* err=nullptr);
// close the connection to the database. returns success, for whatever failure implies.
bool CloseConnection(std::string* err=nullptr);
// wrapper around exec since we handle the transaction and connection.
// nret specifies the expected number of returned rows from the query.
// res and row are outputs. return value is success.
bool Query(std::string query, int nret, pqxx::result* res=nullptr, pqxx::row* row=nullptr, std::string* err=nullptr);
// one that returns the value of many fields from one row as strings in a vector (row_or_col='r')
// or the value of one field from many rows (row_or_col='c')
bool QueryAsStrings(std::string query, std::vector<std::string> *results, char row_or_col, std::string* err=nullptr);
// one that returns rows as json strings representing maps of fieldname:value
// we uhhhhh need to check this works ok with nested strings and stuff
bool QueryAsJsons(std::string query, std::vector<std::string> *results, std::string* err=nullptr);
bool Promote(int wait_seconds=60, std::string* err=nullptr);
bool Demote(int wait_seconds=60, std::string* err=nullptr);
private:
int verbosity=1;
int v_error=0;
int v_warning=1;
int v_message=2;
int v_debug=3;
std::string logmessage;
int get_ok;
pqxx::connection* conn=nullptr;
// default connection details
std::string dbname="";
std::string hostaddr="";
std::string hostname="";
int port=-1;
std::string dbuser="";
std::string dbpasswd="";
public:
// template <typename Tuple>
// bool ExecuteQuery(std::string query_string, std::vector<Tuple>& rets){
// // run an SQL query and try to return the results
// // into a vector of tuples, one entry per row.
// // tuple contents must be compatible with the returned columns.
// pqxx::result local_ret;
// bool success = ExecuteQuery(query_string, 2, &local_ret, nullptr);
// if(not success) return false; // query failed
//
// // XXX
// }
template <typename... Ts>
bool ExecuteQuery(std::string query_string, Ts&&... rets){
// run an SQL query and try to pass the results
// into a parameter pack. the passed arguments
// must be compatible with the returned columns
pqxx::row local_row;
bool success = Query(query_string, 1, nullptr, &local_row);
if(not success) return false; // query failed
success = ExpandRow<sizeof...(Ts), Ts&&...>::expand(local_row, std::forward<Ts>(rets)...);
return success;
}
////////
// helper function to use the contents of a pqxx::row
// to populate a parameter pack
template<std::size_t N, typename T, typename... Ts>
struct ExpandRow {
static bool expand(const pqxx::row& row, T& last, Ts&... out){
//std::cout << __PRETTY_FUNCTION__ << "\n";
bool ok = ExpandRow<N-1, Ts...>::expand(row, std::forward<Ts>(out)...);
if(not ok) return false; // do not attempt further expansion.... i mean, we could...?
try{
// std::cout<<"extracting argument "<<(row.size()-pqxx::row::size_type(N))<<" to variable of type "
// <<type_name<decltype(last)>()<<std::endl;
row[row.size()-pqxx::row::size_type(N)].to(last);
}
catch (const pqxx::sql_error &e){
std::cerr << e.what() << std::endl
<< "When executing query: " << e.query();
if(e.sqlstate()!=""){
std::cerr << ", with SQLSTATE error code: " << e.sqlstate();
}
std::cerr<<std::endl;
std::cerr<<"Postgres::ExpandRow failed to convert sql return field "
<<(row.size()-pqxx::row::size_type(N))<<" to output type "
<<typeid(last).name()<<std::endl;
//<<type_name<decltype(last)>()<<std::endl;
return false;
}
catch (std::exception const &e){
std::cerr << e.what() << std::endl;
std::cerr<<"Postgres::ExpandRow failed to convert sql return field "
<<(row.size()-pqxx::row::size_type(N))<<" to output type "
<<typeid(last).name()<<std::endl;
//<<type_name<decltype(last)>()<<std::endl;
return false;
}
}
};
template<typename T>
struct ExpandRow<1, T> {
static bool expand(const pqxx::row& row, T& out){
//std::cout << __PRETTY_FUNCTION__ << "\n";
try{
//std::cout<<"extracting last argument "<<(row.size()-1)<<" to variable of type "
// <<type_name<decltype(out)>()<<std::endl;
row[row.size()-1].to(out);
}
catch (const pqxx::sql_error &e){
std::cerr << e.what() << std::endl
<< "When executing query: " << e.query();
if(e.sqlstate()!=""){
std::cerr << ", with SQLSTATE error code: " << e.sqlstate();
}
std::cerr<<std::endl;
std::cerr<<"Postgres::ExpandRow failed to convert sql return field "<<(row.size()-1)
<<" to output type "<<typeid(out).name()<<std::endl;
//<<" to output type "<<type_name<decltype(out)>()<<std::endl;
return false;
}
catch(std::exception const &e){
std::cerr << e.what() << std::endl;
std::cerr<<"Postgres::ExpandRow failed to convert sql return field "
<<(row.size()-1)<<" to output type "
<<typeid(out).name()<<std::endl;
//<<type_name<decltype(out)>()<<std::endl;
return false;
}
return true;
}
};
// end helper function
////////
////////
// helper function for insertions
template <typename... Rest>
bool Insert(std::string tablename, std::vector<std::string> &fields, std::string* err, Rest... args){
// maybe this is redundant since OpenConnection will check is_open (against recommendations)
for(int tries=0; tries<2; ++tries){
// ensure we have a connection to work with
if(OpenConnection(err)==nullptr){
// no connection to batabase -> abort
return false;
}
try{
// open a transaction to interact with the database
//pqxx::work(*conn);
pqxx::nontransaction txn(*conn);
// form the query
std::string query_string = "INSERT INTO " + tablename + "( ";
for(auto&& afield : fields) query_string += afield +", ";
query_string = query_string.substr(0, query_string.length()-2); // pop trailing comma
query_string += ") VALUES (";
for(int i=0; i<fields.size(); ++i) query_string += "$" + std::to_string(i+1) + ", ";
query_string = query_string.substr(0, query_string.length()-2); // pop trailing comma
query_string += ")";
// try to run it
txn.exec_params(query_string, args...);
// don't expect (or handle) a return.
// if we haven't thrown an exception, we're done.
return true;
}
catch (const pqxx::broken_connection &e){
// if our connection is broken after all, disconnect, reconnect and retry
if(tries==0){
CloseConnection();
delete conn; conn=nullptr;
continue;
} else {
std::cerr<<"Postgres::Query error - broken connection, failed to re-establish it"<<std::endl;
if(err) *err=e.what();
}
}
catch (const pqxx::sql_error &e){
std::string msg = e.what();
msg += "When executing query: " + e.query();
if(e.sqlstate()!=""){
msg += ", with SQLSTATE error code: " + e.sqlstate();
}
std::cerr<<msg<<std::endl;
if(err) *err = msg;
}
catch (std::exception const &e){
std::cerr << e.what() << std::endl;
if(err) *err = e.what();
}
break; // if not explicitly 'continued', break.
} // end tries
return false;
}
// end helper function
////////
};
#endif