/
database_export_postgresql.cc
311 lines (261 loc) · 9.06 KB
/
database_export_postgresql.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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2020-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "include/bareos.h"
#include "dird/dbcopy/database_export.h"
#include "dird/dbcopy/database_export_postgresql.h"
#include "dird/dbcopy/database_table_descriptions.h"
#include "dird/dbcopy/row_data.h"
#include "lib/bstringlist.h"
#include <iostream>
#include <memory>
DatabaseExportPostgresql::DatabaseExportPostgresql(
const DatabaseConnection& db_connection,
bool clear_tables)
: DatabaseExport(db_connection, clear_tables)
{
if (clear_tables) {
for (const auto& t : table_descriptions_->tables) {
if (t.table_name != "version") {
std::cout << "Deleting contents of table: " << t.table_name
<< std::endl;
std::string query("DELETE ");
query += " FROM ";
query += t.table_name;
if (!db_->SqlQuery(query.c_str())) {
std::string err{"Could not delete table: "};
err += t.table_name;
err += " ";
err += db_->get_errmsg();
throw std::runtime_error(err);
}
}
}
}
}
DatabaseExportPostgresql::~DatabaseExportPostgresql()
{
if (transaction_) { db_->SqlQuery("ROLLBACK"); }
}
void DatabaseExportPostgresql::CopyRow(RowData& source_data_row)
{
std::string query_into{"INSERT INTO "};
query_into += source_data_row.table_name;
query_into += " (";
std::string query_values = " VALUES (";
for (std::size_t i = 0; i < source_data_row.column_descriptions.size(); i++) {
const ColumnDescription* column_description =
table_descriptions_->GetColumnDescription(
source_data_row.table_name,
source_data_row.column_descriptions[i]->column_name);
if (!column_description) {
std::string err{"Could not get column description for: "};
err += source_data_row.column_descriptions[i]->column_name;
throw std::runtime_error(err);
}
query_into += column_description->column_name;
query_into += ", ";
if (i < source_data_row.columns.size()) {
query_values += "'";
column_description->db_export_converter(db_, source_data_row.columns[i]);
query_values += source_data_row.columns[i].data_pointer;
query_values += "',";
} else {
throw std::runtime_error("Row number does not match column description");
}
}
query_values.resize(query_values.size() - 1);
query_values += ")";
query_into.resize(query_into.size() - 2);
query_into += ")";
query_into += query_values;
if (!db_->SqlQuery(query_into.c_str())) {
std::string err{"DatabaseExportPostgresql: Could not execute query: "};
err += db_->get_errmsg();
throw std::runtime_error(err);
}
}
void DatabaseExportPostgresql::CopyStart()
{
// runs before first table
SelectSequenceSchema();
}
static void UpdateSequences(
BareosDb* db,
const DatabaseExportPostgresql::SequenceSchemaVector&
sequence_schema_vector,
const std::string& table_name)
{
for (const auto& s : sequence_schema_vector) {
std::string table_name_lower_case;
std::transform(table_name.cbegin(), table_name.cend(),
std::back_inserter(table_name_lower_case), ::tolower);
if (s.table_name == table_name_lower_case) {
std::string sequence_schema_query{"select setval(' "};
sequence_schema_query += s.sequence_name;
sequence_schema_query += "', (select max(";
sequence_schema_query += s.column_name;
sequence_schema_query += ") from ";
sequence_schema_query += table_name_lower_case;
sequence_schema_query += "))";
std::cout << "--> updating sequence" << std::endl;
if (!db->SqlQuery(sequence_schema_query.c_str())) {
throw std::runtime_error(
"DatabaseExportPostgresql: Could not set sequence");
}
}
}
}
void DatabaseExportPostgresql::CopyEnd() {}
void DatabaseExportPostgresql::CursorStartTable(const std::string& table_name)
{
const DatabaseTableDescriptions::TableDescription* table{
table_descriptions_->GetTableDescription(table_name)};
if (table == nullptr) {
std::string err{
"DatabaseExportPostgresql: Could not get table description for: "};
err += table_name;
throw std::runtime_error(err);
}
std::string query{"DECLARE curs1 NO SCROLL CURSOR FOR SELECT "};
for (const auto& c : table->column_descriptions) {
query += c->column_name;
query += ", ";
}
query.resize(query.size() - 2);
query += " FROM ";
query += table_name;
if (!db_->SqlQuery(query.c_str())) {
std::string err{
"DatabaseExportPostgresql (cursor): Could not execute query: "};
err += db_->get_errmsg();
throw std::runtime_error(err);
}
}
static bool TableIsEmtpy(BareosDb* db, const std::string& table_name)
{
std::string query{"SELECT * FROM "};
query += table_name;
query += " LIMIT 1";
if (!db->SqlQuery(query.c_str())) {
std::string err{"DatabaseExportPostgresql: Could not select table: "};
err += table_name;
throw std::runtime_error(err);
}
if (db->SqlNumRows() > 0) { return false; }
return true;
}
bool DatabaseExportPostgresql::TableExists(const std::string& table_name)
{
auto found = table_descriptions_->GetTableDescription(table_name);
if (found == nullptr) { return false; }
return true;
}
bool DatabaseExportPostgresql::StartTable(const std::string& table_name)
{
std::cout << "====== table " << table_name << " ======" << std::endl;
std::cout << "--> checking destination table..." << std::endl;
if (!TableExists(table_name)) {
std::cout << "--> destination table does not exist" << std::endl;
return false;
}
if (!TableIsEmtpy(db_, table_name)) {
std::cout << "--> destination table is not empty" << std::endl;
return false;
}
if (db_->SqlQuery("BEGIN")) {
transaction_ = true;
start_new_table = true;
}
return true;
}
void DatabaseExportPostgresql::EndTable(const std::string& table_name)
{
if (table_name == "RestoreObject") {
std::string query{
"UPDATE restoreobject SET objectlength=length(restoreobject)"};
if (!db_->SqlQuery(query.c_str())) {
std::string err{"Could not update RestoreObjects: "};
err += db_->get_errmsg();
throw std::runtime_error(err);
}
}
UpdateSequences(db_, sequence_schema_vector_, table_name);
if (transaction_) {
db_->SqlQuery("COMMIT");
transaction_ = false;
}
}
void DatabaseExportPostgresql::CompareRow(const RowData& data)
{
if (start_new_table) {
CursorStartTable(data.table_name);
start_new_table = false;
}
std::string query{"FETCH NEXT FROM curs1"};
RowData& rd(const_cast<RowData&>(data));
if (!db_->SqlQuery(query.c_str(), ResultHandlerCompare, &rd)) {
std::string err{
"DatabaseExportPostgresql (compare): Could not execute query: "};
err += db_->get_errmsg();
throw std::runtime_error(err);
}
}
int DatabaseExportPostgresql::ResultHandlerCompare(void* ctx,
int fields,
char** row)
{
const RowData* rd{static_cast<RowData*>(ctx)};
for (int i = 0; i < fields; i++) {
std::string r1{row[i]};
std::string r2{rd->columns[i].data_pointer};
if (r1 != r2) { throw std::runtime_error("Data is not same"); }
}
return 0;
}
void DatabaseExportPostgresql::SelectSequenceSchema()
{
const std::string query{
"select table_name, column_name,"
" column_default from information_schema.columns where table_schema ="
" 'public' and column_default like 'nextval(%';"};
if (!db_->SqlQuery(query.c_str(), ResultHandlerSequenceSchema,
&sequence_schema_vector_)) {
std::string err{"DatabaseExportPostgresql: Could not change sequence: "};
err += db_->get_errmsg();
throw std::runtime_error(err);
}
}
int DatabaseExportPostgresql::ResultHandlerSequenceSchema(void* ctx,
int fields,
char** row)
{
if (fields != 3) {
throw std::runtime_error("DatabaseExportPostgresql: Wrong number of rows");
}
SequenceSchema s;
s.table_name = row[0];
s.column_name = row[1];
BStringList l(row[2], "'");
if (l.size() != 3) {
throw std::runtime_error(
"DatabaseExportPostgresql: Wrong column_default syntax");
}
s.sequence_name = l[1];
SequenceSchemaVector* v = static_cast<SequenceSchemaVector*>(ctx);
v->push_back(s);
return 0;
}