Skip to content

Commit

Permalink
cats: add a generic interface for the sql copy command
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Feb 27, 2020
1 parent 6766a68 commit d2d5f9c
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 7 deletions.
4 changes: 4 additions & 0 deletions core/src/cats/bdb_dbi.h
Expand Up @@ -73,6 +73,10 @@ class BareosDbDBI : public BareosDbPrivateInterface {
bool SqlBatchStartFileTable(JobControlRecord* jcr);
bool SqlBatchEndFileTable(JobControlRecord* jcr, const char* error);
bool SqlBatchInsertFileTable(JobControlRecord* jcr, AttributesDbRecord* ar);
bool SqlCopyStart(const std::string& table_name,
const std::vector<std::string>& field_names);
bool SqlCopyInsert(const std::vector<ColumnData>& columns);
bool SqlCopyEnd();

public:
/*
Expand Down
7 changes: 6 additions & 1 deletion core/src/cats/bdb_mysql.h
Expand Up @@ -77,7 +77,12 @@ class BareosDbMysql : public BareosDbPrivateInterface {
bool SqlFieldIsNumeric(int field_type) override;
bool SqlBatchStartFileTable(JobControlRecord* jcr) override;
bool SqlBatchEndFileTable(JobControlRecord* jcr, const char* error) override;
bool SqlBatchInsertFileTable(JobControlRecord* jcr, AttributesDbRecord* ar) override;
bool SqlBatchInsertFileTable(JobControlRecord* jcr,
AttributesDbRecord* ar) override;
bool SqlCopyStart(const std::string& table_name,
const std::vector<std::string>& field_names) override;
bool SqlCopyInsert(const std::vector<ColumnData>& columns) override;
bool SqlCopyEnd() override;

public:
/*
Expand Down
4 changes: 4 additions & 0 deletions core/src/cats/bdb_postgresql.h
Expand Up @@ -78,6 +78,10 @@ class BareosDbPostgresql : public BareosDbPrivateInterface {
bool SqlBatchEndFileTable(JobControlRecord* jcr, const char* error) override;
bool SqlBatchInsertFileTable(JobControlRecord* jcr,
AttributesDbRecord* ar) override;
bool SqlCopyStart(const std::string& table_name,
const std::vector<std::string>& field_names) override;
bool SqlCopyInsert(const std::vector<ColumnData>& columns) override;
bool SqlCopyEnd() override;

bool CheckDatabaseEncoding(JobControlRecord* jcr);

Expand Down
6 changes: 6 additions & 0 deletions core/src/cats/cats.h
Expand Up @@ -36,6 +36,7 @@
#define BAREOS_CATS_CATS_H_ 1

#include "include/bareos.h"
#include "cats/column_data.h"
#include "lib/output_formatter.h"

class dlist;
Expand Down Expand Up @@ -1060,6 +1061,11 @@ class BareosDb : public BareosDbQueryEnum {
return SqlQuery(query, ResultHandler, ctx);
}

virtual bool SqlCopyStart(const std::string& table_name,
const std::vector<std::string>& field_names) = 0;
virtual bool SqlCopyInsert(const std::vector<ColumnData>& columns) = 0;
virtual bool SqlCopyEnd() = 0;

#ifdef _BDB_PRIV_INTERFACE_
/*
* Backend methods
Expand Down
35 changes: 35 additions & 0 deletions core/src/cats/column_data.h
@@ -0,0 +1,35 @@
/*
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.
*/

#ifndef BAREOS_CATS_COLUMN_DATA_H_
#define BAREOS_CATS_COLUMN_DATA_H_

#include "include/bareos.h"

#include <vector>

struct ColumnData {
std::size_t length_of_restore_object{};
const char* data_pointer{};
std::vector<char> converted_data;
};

#endif // BAREOS_CATS_COLUMN_DATA_H_
14 changes: 14 additions & 0 deletions core/src/cats/mysql_batch.cc
Expand Up @@ -139,4 +139,18 @@ bool BareosDbMysql::SqlBatchInsertFileTable(JobControlRecord* jcr,
return true;
}

bool BareosDbMysql::SqlCopyStart(const std::string& table_name,
const std::vector<std::string>& field_names)
{
return false;
}

bool BareosDbMysql::SqlCopyInsert(const std::vector<ColumnData>& columns)
{
return false;
}

bool BareosDbMysql::SqlCopyEnd() { return false; }


#endif // HAVE_MYSQL
61 changes: 61 additions & 0 deletions core/src/cats/postgresql_batch.cc
Expand Up @@ -238,4 +238,65 @@ bool BareosDbPostgresql::SqlBatchInsertFileTable(JobControlRecord* jcr,
return true;
}


bool BareosDbPostgresql::SqlCopyStart(
const std::string& table_name,
const std::vector<std::string>& column_names)
{
Dmsg0(500, "SqlCopyStart started\n");

/*
* We are starting a new query. reset everything.
*/
num_rows_ = -1;
row_number_ = -1;
field_number_ = -1;

SqlFreeResult();

std::string query_copy{"COPY " + table_name + " FROM STDIN"};

for (int i = 0; i < 10; i++) {
result_ = PQexec(db_handle_, query_copy.c_str());
if (result_) { break; }
Bmicrosleep(5, 0);
}
if (!result_) {
Dmsg1(50, "Query failed: %s\n", query);
goto bail_out;
}

status_ = PQresultStatus(result_);
if (status_ == PGRES_COPY_IN) {
/*
* How many fields in the set?
*/
num_fields_ = (int)PQnfields(result_);
num_rows_ = 0;
status_ = 1;
} else {
Dmsg1(50, "Result status failed: %s\n", query);
goto bail_out;
}

Dmsg0(500, "SqlBatchStartFileTable finishing\n");

return true;

bail_out:
Mmsg1(errmsg, _("error starting batch mode: %s"), PQerrorMessage(db_handle_));
status_ = 0;
PQclear(result_);
result_ = NULL;
return false;
}

bool BareosDbPostgresql::SqlCopyInsert(const std::vector<ColumnData>& columns)
{
return false;
}

bool BareosDbPostgresql::SqlCopyEnd() { return false; }


#endif // HAVE_POSTGRESQL
7 changes: 1 addition & 6 deletions core/src/dird/dbcopy/row_data.h
Expand Up @@ -22,18 +22,13 @@
#define BAREOS_SRC_DIRD_DBCOPY_ROW_DATA_H_

#include "include/bareos.h"
#include "cats/column_data.h"
#include "dird/dbcopy/database_column_descriptions.h"

#include <vector>

class BareosDb;

struct ColumnData {
std::size_t size{}; // length_of_restore_object
const char* data_pointer{};
std::vector<char> converted_data;
};

struct RowData {
RowData(const DatabaseColumnDescriptions::VectorOfColumnDescriptions&
column_descriptions_in,
Expand Down
10 changes: 10 additions & 0 deletions core/src/tests/run_on_incoming_connect_interval.cc
Expand Up @@ -205,6 +205,16 @@ class MockDatabase : public BareosDb {
bool ValidateConnection() override { return false; }
void StartTransaction(JobControlRecord* /*jcr*/) override {}
void EndTransaction(JobControlRecord* /*jcr*/) override {}
bool SqlCopyStart(const std::string& table_name,
const std::vector<std::string>& column_names) override
{
return false;
}
bool SqlCopyInsert(const std::vector<ColumnData>& columns) override
{
return false;
}
bool SqlCopyEnd() override { return false; }

private:
Mode mode_{};
Expand Down

0 comments on commit d2d5f9c

Please sign in to comment.