Skip to content

Commit

Permalink
cats: Retire sql glueing layer.
Browse files Browse the repository at this point in the history
When I refactored the catalog backends in 2010 I was talked in to use
some glueing layer so the calling functions didn't need to much
changing. That has served it purpose but now after 6 years its time to
take the next step and move B_DB functions into proper class methods.
This patch removes the old glue layer and extends the B_DB class with
all methods needed to be a proper abstraction of the different database
functions needed. Also all old database functions are now promoted to
class methods. As part of the rewrite we also refactored quite a bit of
code and dropped the db_ prefix where it made sense as its now kind of
obvious its a method doing something with the database when its part of
a B_DB class that handles database abstraction.

I also made all the B_DB members now protected which is a lot easier now
most methods handle the inner content of the class and we don't have to
many external functions poking into our internal structures.

All low level methods previously protected via the bdb_priv.h header
are now proper private methods in the B_DB class and are only visible
to methods in source files that set _BDB_PRIV_INTERFACE_. As such we
control the access to these low level methods to either the methods in
the catalog shared libs or to the actual database backend drivers.

Lets call this catalog refactoring phase 3 after phase 1 and 2 which was
done in 2010.
  • Loading branch information
Marco van Wieringen committed Sep 1, 2016
1 parent dcfec26 commit bbf4d09
Show file tree
Hide file tree
Showing 77 changed files with 3,395 additions and 3,619 deletions.
3 changes: 1 addition & 2 deletions src/cats/Makefile.in
Expand Up @@ -58,8 +58,7 @@ SQLITE_LOBJS = $(SQLITE_SRCS:.c=.lo)
DB_LIBS=@DB_LIBS@

LIBBAREOSSQL_SRCS = bvfs.c cats.c sql.c sql_cmds.c sql_create.c sql_delete.c \
sql_find.c sql_get.c sql_glue.c sql_list.c sql_pooling.c \
sql_update.c
sql_find.c sql_get.c sql_list.c sql_pooling.c sql_update.c
LIBBAREOSSQL_OBJS = $(LIBBAREOSSQL_SRCS:.c=.o)
LIBBAREOSSQL_LOBJS = $(LIBBAREOSSQL_SRCS:.c=.lo)
LIBBAREOSSQL_LT_RELEASE = @LIBBAREOSSQL_LT_RELEASE@
Expand Down
65 changes: 36 additions & 29 deletions src/cats/bdb_dbi.h
Expand Up @@ -2,6 +2,8 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2009-2011 Free Software Foundation Europe e.V.
Copyright (C) 2016-2016 Planets Communications B.V.
Copyright (C) 2016-2016 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
Expand All @@ -28,12 +30,46 @@ struct DBI_FIELD_GET {

class B_DB_DBI: public B_DB_PRIV {
private:
/*
* Members.
*/
dbi_inst m_instance;
dbi_conn *m_db_handle;
dbi_result *m_result;
DBI_FIELD_GET *m_field_get;

private:
/*
* Methods.
*/
bool open_database(JCR *jcr);
void close_database(JCR *jcr);
bool validate_connection(void);
void escape_string(JCR *jcr, char *snew, char *old, int len);
char *escape_object(JCR *jcr, char *old, int len);
void unescape_object(JCR *jcr, char *from, int32_t expected_len,
POOLMEM *&dest, int32_t *len);
void start_transaction(JCR *jcr);
void end_transaction(JCR *jcr);
bool sql_query_with_handler(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
bool sql_query_without_handler(const char *query, int flags = 0);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
const char *sql_strerror(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
SQL_FIELD *sql_fetch_field(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);

public:
/*
* Methods.
*/
B_DB_DBI(JCR *jcr,
const char *db_driver,
const char *db_name,
Expand All @@ -48,34 +84,5 @@ class B_DB_DBI: public B_DB_PRIV {
bool exit_on_fatal,
bool need_private);
~B_DB_DBI();

/* low level operations */
bool db_open_database(JCR *jcr);
void db_close_database(JCR *jcr);
bool db_validate_connection(void);
void db_escape_string(JCR *jcr, char *snew, char *old, int len);
char *db_escape_object(JCR *jcr, char *old, int len);
void db_unescape_object(JCR *jcr, char *from, int32_t expected_len,
POOLMEM *&dest, int32_t *len);
void db_start_transaction(JCR *jcr);
void db_end_transaction(JCR *jcr);
bool db_sql_query(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
bool sql_query(const char *query, int flags=0);
const char *sql_strerror(void);
int sql_num_rows(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
void sql_field_seek(int field);
SQL_FIELD *sql_fetch_field(void);
int sql_num_fields(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);
};

#endif /* __BDB_DBI_H_ */
57 changes: 32 additions & 25 deletions src/cats/bdb_ingres.h
Expand Up @@ -2,6 +2,8 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2009-2011 Free Software Foundation Europe e.V.
Copyright (C) 2016-2016 Planets Communications B.V.
Copyright (C) 2016-2016 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
Expand All @@ -23,13 +25,43 @@

class B_DB_INGRES: public B_DB_PRIV {
private:
/*
* Members.
*/
INGconn *m_db_handle;
INGresult *m_result;
bool m_explicit_commit;
int m_session_id;
alist *m_query_filters;

private:
/*
* Methods.
*/
bool open_database(JCR *jcr);
void close_database(JCR *jcr);
bool validate_connection(void);
void start_transaction(JCR *jcr);
void end_transaction(JCR *jcr);
bool sql_query_with_handler(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
bool sql_query_without_handler(const char *query, int flags = 0);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
const char *sql_strerror(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
SQL_FIELD *sql_fetch_field(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);

public:
/*
* Methods.
*/
B_DB_INGRES(JCR *jcr,
const char *db_driver,
const char *db_name,
Expand All @@ -44,30 +76,5 @@ class B_DB_INGRES: public B_DB_PRIV {
bool exit_on_fatal,
bool need_private);
~B_DB_INGRES();

/* low level operations */
bool db_open_database(JCR *jcr);
void db_close_database(JCR *jcr);
bool db_validate_connection(void);
void db_start_transaction(JCR *jcr);
void db_end_transaction(JCR *jcr);
bool db_sql_query(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
bool sql_query(const char *query, int flags=0);
const char *sql_strerror(void);
int sql_num_rows(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
void sql_field_seek(int field);
SQL_FIELD *sql_fetch_field(void);
int sql_num_fields(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);
};

#endif /* __BDB_INGRES_H_ */
66 changes: 37 additions & 29 deletions src/cats/bdb_mysql.h
Expand Up @@ -2,6 +2,8 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2009-2011 Free Software Foundation Europe e.V.
Copyright (C) 2016-2016 Planets Communications B.V.
Copyright (C) 2016-2016 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
Expand Down Expand Up @@ -30,11 +32,46 @@

class B_DB_MYSQL: public B_DB_PRIV {
private:
/*
* Members.
*/
MYSQL *m_db_handle;
MYSQL m_instance;
MYSQL_RES *m_result;

private:
/*
* Methods.
*/
bool open_database(JCR *jcr);
void close_database(JCR *jcr);
bool validate_connection(void);
void thread_cleanup(void);
void escape_string(JCR *jcr, char *snew, char *old, int len);
char *escape_object(JCR *jcr, char *old, int len);
void unescape_object(JCR *jcr, char *from, int32_t expected_len,
POOLMEM *&dest, int32_t *len);
void start_transaction(JCR *jcr);
void end_transaction(JCR *jcr);
bool sql_query_with_handler(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
bool sql_query_without_handler(const char *query, int flags = 0);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
const char *sql_strerror(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
SQL_FIELD *sql_fetch_field(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);

public:
/*
* Methods.
*/
B_DB_MYSQL(JCR *jcr,
const char *db_driver,
const char *db_name,
Expand All @@ -49,35 +86,6 @@ class B_DB_MYSQL: public B_DB_PRIV {
bool exit_on_fatal,
bool need_private);
~B_DB_MYSQL();

/* low level operations */
bool db_open_database(JCR *jcr);
void db_close_database(JCR *jcr);
bool db_validate_connection(void);
void db_thread_cleanup(void);
void db_escape_string(JCR *jcr, char *snew, char *old, int len);
char *db_escape_object(JCR *jcr, char *old, int len);
void db_unescape_object(JCR *jcr, char *from, int32_t expected_len,
POOLMEM *&dest, int32_t *len);
void db_start_transaction(JCR *jcr);
void db_end_transaction(JCR *jcr);
bool db_sql_query(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
bool sql_query(const char *query, int flags=0);
const char *sql_strerror(void);
int sql_num_rows(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
void sql_field_seek(int field);
SQL_FIELD *sql_fetch_field(void);
int sql_num_fields(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);
};

#endif /* __BDB_MYSQL_H_ */
68 changes: 38 additions & 30 deletions src/cats/bdb_postgresql.h
Expand Up @@ -2,6 +2,8 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2009-2011 Free Software Foundation Europe e.V.
Copyright (C) 2016-2016 Planets Communications B.V.
Copyright (C) 2016-2016 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
Expand All @@ -23,11 +25,47 @@

class B_DB_POSTGRESQL: public B_DB_PRIV {
private:
/*
* Members.
*/
PGconn *m_db_handle;
PGresult *m_result;
POOLMEM *m_buf; /* Buffer to manipulate queries */

private:
/*
* Methods.
*/
bool open_database(JCR *jcr);
void close_database(JCR *jcr);
bool validate_connection(void);
void escape_string(JCR *jcr, char *snew, char *old, int len);
char *escape_object(JCR *jcr, char *old, int len);
void unescape_object(JCR *jcr, char *from, int32_t expected_len, POOLMEM *&dest, int32_t *len);
void start_transaction(JCR *jcr);
void end_transaction(JCR *jcr);
bool big_sql_query(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
bool sql_query_with_handler(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
bool sql_query_without_handler(const char *query, int flags = 0);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
const char *sql_strerror(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
SQL_FIELD *sql_fetch_field(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);

bool check_database_encoding(JCR *jcr);

public:
/*
* Methods.
*/
B_DB_POSTGRESQL(JCR *jcr,
const char *db_driver,
const char *db_name,
Expand All @@ -42,35 +80,5 @@ class B_DB_POSTGRESQL: public B_DB_PRIV {
bool exit_on_fatal,
bool need_private);
~B_DB_POSTGRESQL();

/* low level operations */
bool db_open_database(JCR *jcr);
void db_close_database(JCR *jcr);
bool db_validate_connection(void);
void db_escape_string(JCR *jcr, char *snew, char *old, int len);
char *db_escape_object(JCR *jcr, char *old, int len);
void db_unescape_object(JCR *jcr, char *from, int32_t expected_len,
POOLMEM *&dest, int32_t *len);
void db_start_transaction(JCR *jcr);
void db_end_transaction(JCR *jcr);
bool db_sql_query(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
bool db_big_sql_query(const char *query, DB_RESULT_HANDLER *result_handler, void *ctx);
void sql_free_result(void);
SQL_ROW sql_fetch_row(void);
bool sql_query(const char *query, int flags=0);
const char *sql_strerror(void);
int sql_num_rows(void);
void sql_data_seek(int row);
int sql_affected_rows(void);
uint64_t sql_insert_autokey_record(const char *query, const char *table_name);
void sql_field_seek(int field);
SQL_FIELD *sql_fetch_field(void);
int sql_num_fields(void);
bool sql_field_is_not_null(int field_type);
bool sql_field_is_numeric(int field_type);
bool sql_batch_start(JCR *jcr);
bool sql_batch_end(JCR *jcr, const char *error);
bool sql_batch_insert(JCR *jcr, ATTR_DBR *ar);
};

#endif /* __BDB_POSTGRESQL_H_ */

0 comments on commit bbf4d09

Please sign in to comment.