-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbtoolkit.cc
350 lines (334 loc) · 13.4 KB
/
dbtoolkit.cc
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "dbtoolkit.h"
#include <stdlib.h>
#include <iomanip>
#include <iostream>
#include <pqxx/pqxx>
#include "cpptoml.h"
using namespace std;
using namespace pqxx;
DbToolkit::DbToolkit()
: vbox(Gtk::ORIENTATION_VERTICAL, 5),
hbox_extid(Gtk::ORIENTATION_HORIZONTAL, 5),
hbox_fields(Gtk::ORIENTATION_HORIZONTAL, 5),
hbox_output_header(Gtk::ORIENTATION_HORIZONTAL, 5),
output_label("Output:"),
btn_clearoutput("Clear"),
btn_updatedblist("Update Database List"),
btn_setpasswords("Set passwords to admin"),
btn_disablecron("Disable CRONs"),
btn_bak_db("Create _bak suffixed Database"),
btn_drop_db("Drop Database"),
btn_restore_db("Drop, then restore _bak db"),
btn_getextid("Get External ID"),
btn_getfields("Get Fields") {
// High level window settings
set_title("DBToolkit");
set_border_width(10);
scrollwindow_output.set_policy(Gtk::POLICY_AUTOMATIC,
Gtk::POLICY_AUTOMATIC);
// ROADMAP: Expand & contract min content height as content fills / unfills
scrollwindow_output.set_min_content_height(180);
// TOML Configuration parse (~/.dbtoolkit.toml)
// ROADMAP: Secure password storage
homedir = getenv("HOME");
config = cpptoml::parse_file(string(homedir) + "/.dbtoolkit.toml");
pg_user = config->get_qualified_as<string>("postgresql.user")
.value_or("postgres");
pg_pass = config->get_qualified_as<string>("postgresql.password")
.value_or("admin");
pg_host = config->get_qualified_as<string>("postgresql.host")
.value_or("127.0.0.1");
pg_port =
config->get_qualified_as<string>("postgresql.port").value_or("5432");
// Add the main vbox which everything will sit in
add(vbox);
// Add hboxes
add(hbox_extid);
add(hbox_fields);
add(hbox_output_header);
// Connect button click handlers
btn_clearoutput.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_clearoutput_clicked));
btn_updatedblist.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_updatedblist_clicked));
btn_setpasswords.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_setpasswords_clicked));
btn_disablecron.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_disablecron_clicked));
btn_bak_db.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_bak_db_clicked));
btn_drop_db.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_drop_db_clicked));
btn_restore_db.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_restoredb_clicked));
btn_getextid.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_getextid_clicked));
btn_getfields.signal_clicked().connect(
sigc::mem_fun(*this, &DbToolkit::on_btn_getfields_clicked));
// Configure placeholders on the entry boxes so we don't need labels
extid_model_entry.set_placeholder_text("model.name");
extid_id_entry.set_placeholder_text("23");
fields_model_entry.set_placeholder_text("model.name");
// Configure the output box
output.set_editable(false);
output.set_monospace(true);
// Pack the hboxes
hbox_extid.pack_start(extid_model_entry);
hbox_extid.pack_start(extid_id_entry);
hbox_extid.pack_start(btn_getextid);
hbox_fields.pack_start(fields_model_entry);
hbox_fields.pack_start(btn_getfields);
hbox_output_header.pack_start(output_label);
hbox_output_header.pack_start(btn_clearoutput);
// Add everything to the vbox
vbox.pack_start(db_selector);
vbox.pack_start(btn_updatedblist);
vbox.pack_start(btn_setpasswords);
vbox.pack_start(btn_disablecron);
vbox.pack_start(hbox_extid);
vbox.pack_start(hbox_fields);
vbox.pack_start(btn_bak_db);
vbox.pack_start(btn_drop_db);
vbox.pack_start(btn_restore_db);
vbox.pack_start(hbox_output_header);
vbox.pack_start(scrollwindow_output);
scrollwindow_output.add(output);
// vbox.pack_start(output);
// Show everything
show_all_children();
// Populate the database list on init
this->on_btn_updatedblist_clicked();
}
DbToolkit::~DbToolkit() {}
string DbToolkit::format_query_result(result R, bool single_column) {
string result_str;
if (single_column) { // For db list, or extid
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
string line_entry;
for (auto i : c) {
line_entry += i.as<string>();
}
result_str += line_entry + "\r\n";
}
} else {
vector<vector<string>> result_vec;
vector<int> max_col_sizes;
// Push all cells to an array of arrays (which are stored for later use)
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
vector<string> result_line; // Pushed into result_vec
string line_entry;
for (auto i : c) {
result_line.push_back(i.as<string>());
}
result_vec.push_back(result_line);
}
// Find the max column sizes by comparing indexes of lines vs last max
// There must be a nicer way to do this?
for (auto line : result_vec) {
for (int i = 0; i < line.size(); ++i) {
int cell_length = line[i].size();
if (i < max_col_sizes.size()) {
if (max_col_sizes[i] < cell_length) {
max_col_sizes[i] = cell_length;
}
} else { // We don't have one to compare, simply push
max_col_sizes.push_back(cell_length);
}
}
}
// Now build a padded stringstream, taking into account the max sizes
// for each column
stringstream result_str_stream;
const char separator = ' ';
for (auto line : result_vec) {
for (int i = 0; i < line.size(); ++i) {
// + 1 to leave a space gap between largest cells
result_str_stream << left << setw(max_col_sizes[i] + 1)
<< setfill(separator) << line[i];
}
result_str_stream << endl;
}
// Convert back to a string before returning.
result_str = result_str_stream.str();
}
return result_str;
}
auto DbToolkit::execute_query(Glib::ustring db, Glib::ustring query,
bool transactional, bool single_column) {
string result_str;
try {
connection C("dbname = " + db + " user = " + pg_user + " password = " +
pg_pass + " hostaddr = " + pg_host + " port = " + pg_port);
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
}
if (transactional == true) { // UPDATE/CREATE
work W(C);
W.exec(query);
W.commit();
cout << "Executed and committed transactional query: " << query
<< endl;
C.disconnect();
result_str = "Succesfully ran " + query;
return result_str;
} else { // SELECT (or database creation/drop)
nontransaction N(C);
result R(N.exec(query));
cout << "Executed non-transactional query: " << query << endl;
result_str = this->format_query_result(R, single_column);
C.disconnect();
return result_str;
}
} catch (const sql_error& e) {
cerr << "SQL error: " << e.what() << endl;
result_str = e.what();
return result_str;
} catch (const exception& e) {
cerr << e.what() << endl;
result_str = e.what();
return result_str;
}
}
void DbToolkit::kick_sessions(Glib::ustring db) {
this->execute_query(
"postgres",
"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM "
"pg_stat_activity WHERE pg_stat_activity.datname = '" +
db + "' AND pid <> pg_backend_pid();",
false, false);
}
void DbToolkit::clear_output() { output.get_buffer()->set_text(""); }
void DbToolkit::set_output_to(Glib::ustring value) {
this->clear_output();
auto refBuffer = output.get_buffer();
auto iter = refBuffer->get_iter_at_offset(0);
refBuffer->insert(iter, value);
}
void DbToolkit::on_btn_clearoutput_clicked() { this->clear_output(); }
void DbToolkit::on_btn_updatedblist_clicked() {
// Use postgres database for this step, as we can rely on it existing.
string result = this->execute_query(
"postgres", "SELECT datname FROM pg_database ORDER BY datname;", false,
true);
// Clear and re-populate db_selector, and db_array
db_selector.remove_all();
db_array.clear();
istringstream iss(result);
for (string line; getline(iss, line);) {
// Remove newlines.
line.erase(line.length() - 1);
db_array.push_back(line);
db_selector.append(line);
}
}
void DbToolkit::on_btn_setpasswords_clicked() {
string result = this->execute_query(db_selector.get_active_text(),
"UPDATE res_users SET password='admin'",
true, false);
this->set_output_to(result);
}
void DbToolkit::on_btn_disablecron_clicked() {
string result = this->execute_query(
db_selector.get_active_text(),
"UPDATE ir_cron SET active=false WHERE active=true", true, false);
this->set_output_to(result);
}
void DbToolkit::on_btn_getextid_clicked() {
string db = db_selector.get_active_text();
string model = extid_model_entry.get_text();
string id = extid_id_entry.get_text();
string result = this->execute_query(
db,
"SELECT CONCAT(module, '.', name) AS external_id FROM "
"ir_model_data WHERE res_id=" +
id + " AND model='" + model + "'",
false, false);
this->set_output_to(result);
}
void DbToolkit::on_btn_getfields_clicked() {
string db = db_selector.get_active_text();
string model = fields_model_entry.get_text();
// ROADMAP: Make this show more useful info
string result = this->execute_query(
db,
"SELECT name, ttype, field_description FROM ir_model_fields WHERE "
"model_id IN (SELECT id FROM ir_model WHERE model='" +
model + "')",
false, false);
this->set_output_to(result);
}
void DbToolkit::on_btn_bak_db_clicked() {
// Use postgres database, put db_selector db in query
string db = db_selector.get_active_text();
this->kick_sessions(db);
int active = db_selector.get_active_row_number();
string result = this->execute_query(
"postgres", "CREATE DATABASE " + db + "_bak WITH TEMPLATE " + db, false,
false);
this->on_btn_updatedblist_clicked();
this->set_output_to(result);
// Set the db selector back to the db we initially had
// NB: Potentially could lead to mis-setting due to the list changing,
// but it seems to work fine as the _bak suffixed always ends up later
// in the list
db_selector.set_active(active);
}
void DbToolkit::on_btn_drop_db_clicked() {
// Use postgres database, put db_selector db in query
string db = db_selector.get_active_text();
this->kick_sessions(db);
string result =
this->execute_query("postgres", "DROP DATABASE " + db, false, false);
this->on_btn_updatedblist_clicked();
this->set_output_to(result);
}
void DbToolkit::on_btn_restoredb_clicked() {
string db = db_selector.get_active_text();
string suffix = "_bak";
int active = db_selector.get_active_row_number();
string target_db_to_drop;
string target_db_to_restore;
bool has_suffix_bak =
db.size() >= suffix.size() &&
db.compare(db.size() - suffix.size(), suffix.size(), suffix) == 0;
if (has_suffix_bak == true) {
// Find if we have a non-_bak suffixed db in db_array
target_db_to_restore = db;
string stripped = db.erase(db.length() - 4);
for (auto i : db_array) {
if (i == stripped) {
target_db_to_drop = i;
}
}
} else {
// Find if we have a _bak suffixed db in db_array
target_db_to_drop = db;
for (auto i : db_array) {
if (i == db + "_bak") {
target_db_to_restore = i;
}
}
}
if (!target_db_to_drop.empty() && !target_db_to_restore.empty()) {
// Execute the drop & restore.
this->set_output_to("Dropping " + target_db_to_drop +
"\r\n& Restoring " + target_db_to_restore + "...");
this->kick_sessions(target_db_to_drop);
this->execute_query("postgres", "DROP DATABASE " + target_db_to_drop,
false, false);
this->execute_query("postgres",
"CREATE DATABASE " + target_db_to_drop +
" WITH TEMPLATE " + target_db_to_restore,
false, false);
this->on_btn_updatedblist_clicked();
this->set_output_to("Finished dropping " + target_db_to_drop +
" and restoring " + target_db_to_restore);
db_selector.set_active(active);
} else {
this->set_output_to(
"Could not determine both a _bak suffixed database, and a"
"non -_bak\r\nsuffixed database. Doing nothing");
}
}