Remove need for gnc_dbi_safe_sync_all to be a friend of GncDbiSqlConnection.

Adds GncDbiSqlConnection::drop_indexes, changes GncDbiSqlConnection::table_operation
to use a regex-filtered table list from the DbiConn, and converts TableOpType
to a C++ enum.
This commit is contained in:
John Ralls 2016-11-28 10:24:35 -08:00
parent 46ce3f3745
commit 06af7d794f
4 changed files with 51 additions and 33 deletions

View File

@ -907,25 +907,20 @@ gnc_dbi_safe_sync_all (QofBackend* qof_be, QofBook* book)
g_return_if_fail (dbi_be != nullptr);
g_return_if_fail (book != nullptr);
ENTER ("book=%p, primary=%p", book, dbi_be->m_book);
auto table_list = conn->m_provider->get_table_list (conn->conn(), "");
if (!conn->table_operation (table_list, backup))
ENTER ("book=%p, primary=%p", book, m_book);
if (!conn->table_operation (TableOpType::backup))
{
qof_backend_set_error (qof_be, ERR_BACKEND_SERVER_ERR);
conn->table_operation (table_list, rollback);
set_error(ERR_BACKEND_SERVER_ERR);
conn->table_operation (TableOpType::rollback);
LEAVE ("Failed to rename tables");
return;
}
auto index_list = conn->m_provider->get_index_list (conn->m_conn);
for (auto index : index_list)
if (!conn->drop_indexes())
{
const char* errmsg;
conn->m_provider->drop_index (conn->m_conn, index);
if (DBI_ERROR_NONE != dbi_conn_error (conn->m_conn, &errmsg))
{
qof_backend_set_error (qof_be, ERR_BACKEND_SERVER_ERR);
conn->table_operation (table_list, rollback);
LEAVE ("Failed to drop indexes %s", errmsg);
conn->table_operation (TableOpType::rollback);
set_error (ERR_BACKEND_SERVER_ERR);
set_message("Failed to drop indexes");
LEAVE ("Failed to drop indexes");
return;
}
}
@ -933,12 +928,12 @@ gnc_dbi_safe_sync_all (QofBackend* qof_be, QofBook* book)
dbi_be->sync_all(book);
if (qof_backend_check_error (qof_be))
{
conn->table_operation (table_list, rollback);
conn->table_operation (TableOpType::rollback);
LEAVE ("Failed to create new database tables");
return;
}
conn->table_operation (table_list, drop_backup);
LEAVE ("book=%p", book);
conn->table_operation (TableOpType::drop_backup);
LEAVE ("book=%p", m_book);
}
/* ================================================================= */
static void

View File

@ -49,14 +49,14 @@ class GncSqlRow;
* @var rollback drop the name table if it exists and rename name_back to name
* @var drop_backup Drop the backup table
*/
typedef enum
enum TableOpType
{
drop = 0,
empty,
backup,
rollback,
drop_backup
} TableOpType;
};
/**
* Return values from conn_test_dbi_library

View File

@ -28,6 +28,10 @@ extern "C"
#include <platform.h>
#include <gnc-locale-utils.h>
}
#include <string>
#include <regex>
#include "gnc-dbisqlconnection.hpp"
static QofLogModule log_module = G_LOG_DOMAIN;
@ -586,16 +590,16 @@ GncDbiSqlConnection::table_manage_backup (const std::string& table_name,
dbi_result result = nullptr;
switch (op)
{
case backup:
case TableOpType::backup:
result = dbi_conn_queryf (m_conn, "ALTER TABLE %s RENAME TO %s",
table_name.c_str(), new_name.c_str());
break;
case rollback:
case TableOpType::rollback:
result = dbi_conn_queryf (m_conn,
"ALTER TABLE %s RENAME TO %s",
new_name.c_str(), table_name.c_str());
break;
case drop_backup:
case TableOpType::drop_backup:
result = dbi_conn_queryf (m_conn, "DROP TABLE %s",
new_name.c_str());
break;
@ -630,16 +634,20 @@ GncDbiSqlConnection::table_manage_backup (const std::string& table_name,
*/
bool
GncDbiSqlConnection::table_operation(const StrVec& table_names,
TableOpType op) noexcept
GncDbiSqlConnection::table_operation(TableOpType op) noexcept
{
g_return_val_if_fail (!table_names.empty(), FALSE);
static const std::regex backupre (".*_back");
bool retval{true};
for (auto table : table_names)
for (auto table : m_provider->get_table_list(m_conn, ""))
{
dbi_result result;
/* Ignore the lock table */
if (table == lock_table)
/* Skip the lock table and existing backup tables; the former we don't
* want to touch, the latter are handled by table_manage_backup. It
* would be nicer to handle this with the get_table_list query, but that
* can accept only SQL LIKE patterns (not even regexps) and there's no
* way to have a negative one.
*/
if (table == lock_table || std::regex_match(table, backupre))
{
continue;
}
@ -660,7 +668,7 @@ GncDbiSqlConnection::table_operation(const StrVec& table_names,
break;
}
}
/* Fall through */
/* Fall through to rename the _back tables back.*/
case backup:
case drop_backup:
result = table_manage_backup (table, op);
@ -689,6 +697,23 @@ GncDbiSqlConnection::table_operation(const StrVec& table_names,
return retval;
}
bool
GncDbiSqlConnection::drop_indexes() noexcept
{
auto index_list = m_provider->get_index_list (m_conn);
for (auto index : index_list)
{
const char* errmsg;
m_provider->drop_index (m_conn, index);
if (DBI_ERROR_NONE != dbi_conn_error (m_conn, &errmsg))
{
PERR("Failed to drop indexes %s", errmsg);
return false;
}
}
return true;
}
std::string
GncDbiSqlConnection::add_columns_ddl(const std::string& table_name,
const ColVec& info_vec) const noexcept

View File

@ -81,12 +81,10 @@ public:
bool verify() noexcept override;
bool retry_connection(const char* msg) noexcept override;
dbi_result table_manage_backup(const std::string& table_name, TableOpType op);
bool table_operation (const StrVec& table_name_list,
TableOpType op) noexcept;
bool table_operation (TableOpType op) noexcept;
std::string add_columns_ddl(const std::string& table_name,
const ColVec& info_vec) const noexcept;
friend void gnc_dbi_safe_sync_all (QofBackend* qbe, QofBook* book);
bool drop_indexes() noexcept;
private:
QofBackend* m_qbe;
dbi_conn m_conn;